moved BeamNG methods to its own File
This commit is contained in:
parent
73d7cf6d0b
commit
cec09d6bcb
5 changed files with 54 additions and 46 deletions
47
GAME_METHODS/BEAMNG_METHODS.py
Normal file
47
GAME_METHODS/BEAMNG_METHODS.py
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
BEAMNG_DATA_FORMAT = "I4sHBBfffffffIIfff16s16sxxxx"
|
||||||
|
|
||||||
|
def decodeFlag(flag):
|
||||||
|
flagBin = str(bin(flag))
|
||||||
|
flags = {"showTurbo":newBool(flagBin[2]),"showKM":not newBool(flagBin[1]),"showBAR":not newBool(flagBin[0])}
|
||||||
|
return flags
|
||||||
|
|
||||||
|
def newBool(string):
|
||||||
|
if(string == "0"):
|
||||||
|
return False
|
||||||
|
if(string == "1"):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def decodeLights(lightsAvailable,lightsActive):
|
||||||
|
lightsAvBin = str(bin(lightsAvailable))[2:][::-1]
|
||||||
|
lightsActBin = str(bin(lightsActive))[2:][::-1]
|
||||||
|
lights = {}
|
||||||
|
totalLights = ["shift_light","full_beam","handbrake","pit_limiter","tc","left_turn","right_turn","both_turns","oil_warn","battery_warn","abs","spare_light"]
|
||||||
|
for i in range(0,12):
|
||||||
|
try:
|
||||||
|
lights[totalLights[i]] = newBool(lightsActBin[i])
|
||||||
|
except Exception:
|
||||||
|
lights[totalLights[i]] = False
|
||||||
|
return lights
|
||||||
|
|
||||||
|
def unpackData(unpackedData):
|
||||||
|
carData = {"time":unpackedData[0],
|
||||||
|
"carName":unpackedData[1].decode("utf-8"),
|
||||||
|
"flags": decodeFlag(unpackedData[2]),
|
||||||
|
"gear": unpackedData[3],
|
||||||
|
"PLID": unpackedData[4],
|
||||||
|
"speed": unpackedData[5],
|
||||||
|
"rpm": unpackedData[6],
|
||||||
|
"turboPressure":unpackedData[7],
|
||||||
|
"engTemp":unpackedData[8],
|
||||||
|
"fuel":unpackedData[9],
|
||||||
|
"oilPressure":unpackedData[10],
|
||||||
|
"oilTemp":unpackedData[11],
|
||||||
|
"lights":decodeLights(unpackedData[12],unpackedData[13]),
|
||||||
|
"throttle": unpackedData[14],
|
||||||
|
"brake": unpackedData[15],
|
||||||
|
"clutch": unpackedData[16],
|
||||||
|
"misc1": unpackedData[17],
|
||||||
|
"misc2": unpackedData[18]
|
||||||
|
}
|
||||||
|
return carData
|
||||||
1
GAME_METHODS/__init__.py
Normal file
1
GAME_METHODS/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
__all__= ["BEAMNG_METHODS"]
|
||||||
BIN
GAME_METHODS/__pycache__/BEAMNG_METHODS.cpython-311.pyc
Normal file
BIN
GAME_METHODS/__pycache__/BEAMNG_METHODS.cpython-311.pyc
Normal file
Binary file not shown.
BIN
GAME_METHODS/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
GAME_METHODS/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
|
|
@ -1,33 +1,11 @@
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
import serial
|
import serial
|
||||||
|
from GAME_METHODS import *
|
||||||
toPi=serial.Serial('COM5',115200)
|
##import BEAMNG_FILES as BEAM
|
||||||
|
|
||||||
def decodeFlag(flag):
|
|
||||||
flagBin = str(bin(flag))
|
|
||||||
flags = {"showTurbo":newBool(flagBin[2]),"showKM":not newBool(flagBin[1]),"showBAR":not newBool(flagBin[0])}
|
|
||||||
return flags
|
|
||||||
|
|
||||||
def newBool(string):
|
|
||||||
if(string == "0"):
|
|
||||||
return False
|
|
||||||
if(string == "1"):
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def decodeLights(lightsAvailable,lightsActive):
|
toPi=serial.Serial('COM5',115200) #connect to arduino
|
||||||
lightsAvBin = str(bin(lightsAvailable))[2:][::-1]
|
|
||||||
lightsActBin = str(bin(lightsActive))[2:][::-1]
|
|
||||||
lights = {}
|
|
||||||
totalLights = ["shift_light","full_beam","handbrake","pit_limiter","tc","left_turn","right_turn","both_turns","oil_warn","battery_warn","abs","spare_light"]
|
|
||||||
for i in range(0,12):
|
|
||||||
try:
|
|
||||||
lights[totalLights[i]] = newBool(lightsActBin[i])
|
|
||||||
except Exception:
|
|
||||||
lights[totalLights[i]] = False
|
|
||||||
return lights
|
|
||||||
|
|
||||||
UDP_IP = "127.0.0.1"
|
UDP_IP = "127.0.0.1"
|
||||||
UDP_PORT = 4444
|
UDP_PORT = 4444
|
||||||
|
|
||||||
|
|
@ -35,29 +13,11 @@ sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
||||||
sock.bind((UDP_IP,UDP_PORT))
|
sock.bind((UDP_IP,UDP_PORT))
|
||||||
print("waiting for data:\n")
|
print("waiting for data:\n")
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
data, addr = sock.recvfrom(1024)
|
data, addr = sock.recvfrom(1024)
|
||||||
unpackedData = struct.unpack("I4sHBBfffffffIIfff16s16sxxxx",data)
|
unpackedData = struct.unpack(BEAMNG_METHODS.BEAMNG_DATA_FORMAT,data)
|
||||||
carData = {"time":unpackedData[0],
|
carData = BEAMNG_METHODS.unpackData(unpackedData)
|
||||||
"carName":unpackedData[1].decode("utf-8"),
|
|
||||||
"flags": decodeFlag(unpackedData[2]),
|
|
||||||
"gear": unpackedData[3],
|
|
||||||
"PLID": unpackedData[4],
|
|
||||||
"speed": unpackedData[5],
|
|
||||||
"rpm": unpackedData[6],
|
|
||||||
"turboPressure":unpackedData[7],
|
|
||||||
"engTemp":unpackedData[8],
|
|
||||||
"fuel":unpackedData[9],
|
|
||||||
"oilPressure":unpackedData[10],
|
|
||||||
"oilTemp":unpackedData[11],
|
|
||||||
"lights":decodeLights(unpackedData[12],unpackedData[13]),
|
|
||||||
"throttle": unpackedData[14],
|
|
||||||
"brake": unpackedData[15],
|
|
||||||
"clutch": unpackedData[16],
|
|
||||||
"misc1": unpackedData[17],
|
|
||||||
"misc2": unpackedData[18]
|
|
||||||
}
|
|
||||||
#print("speed: %s" % (carData["speed"]*3.6))
|
|
||||||
kmh=carData["speed"]*3.6
|
kmh=carData["speed"]*3.6
|
||||||
toPi.write(str(kmh).encode()+":".encode())
|
toPi.write(str(kmh).encode()+":".encode())
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue