added ability to read speed from forza
This commit is contained in:
parent
9ec76323d7
commit
237708e155
5 changed files with 289 additions and 11 deletions
|
|
@ -2,36 +2,47 @@ import socket
|
|||
import struct
|
||||
import serial
|
||||
import time
|
||||
from enum import Enum
|
||||
from GAME_METHODS import *
|
||||
##import BEAMNG_FILES as BEAM
|
||||
|
||||
|
||||
COM_PORT = 'COM5'
|
||||
UDP_IP = "127.0.0.1"
|
||||
BEAMNG_UDP_PORT = 4444
|
||||
FORZA_UDP_PORT= 4843
|
||||
|
||||
class GameType(Enum):
|
||||
NONE=0,
|
||||
BEAMNG = 1,
|
||||
FORZA = 2
|
||||
|
||||
connectedArduino = False
|
||||
connectedWebSocket = False
|
||||
gameSelected = False
|
||||
portToConnect= 0
|
||||
dataFormat = ""
|
||||
gameType= GameType.NONE
|
||||
carData = 0
|
||||
|
||||
#code runs from here<-----------------------------------------------------------------------------------------------------------
|
||||
|
||||
#select game
|
||||
while gameSelected == False:
|
||||
gameNo = input("1:BEAMNG\n2:FORZA\n")
|
||||
if gameNo == "1":
|
||||
portToConnect = BEAMNG_UDP_PORT
|
||||
dataFormat =BEAMNG_METHODS.BEAMNG_DATA_FORMAT
|
||||
gameSelected = True
|
||||
gameType=GameType.BEAMNG
|
||||
print("BeamNG Selected")
|
||||
elif gameNo == "2":
|
||||
portToConnect = FORZA_UDP_PORT
|
||||
dataFormat = FORZA_METHODS.FROZA_DATA_FORMAT
|
||||
print("Forza Selected")
|
||||
gameType=GameType.FORZA
|
||||
gameSelected=True
|
||||
print("Forza Selected")
|
||||
|
||||
else:
|
||||
print("please select a number from the list")
|
||||
|
||||
|
||||
#check everything is connected
|
||||
while connectedWebSocket == False:
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
||||
|
|
@ -49,12 +60,18 @@ while connectedArduino == False:
|
|||
print("please check connection to arduino and verify the correct COM port")
|
||||
time.sleep(1)
|
||||
|
||||
print("ready:\n")
|
||||
|
||||
print(struct.calcsize(FORZA_METHODS.FORZA_DATA_FORMAT))
|
||||
print("ready:\n")
|
||||
while True:
|
||||
data, addr = sock.recvfrom(1024)
|
||||
unpackedData = struct.unpack(dataFormat,data)
|
||||
carData = BEAMNG_METHODS.unpackData(unpackedData)
|
||||
kmh=carData["speed"]*3.6
|
||||
if gameType == GameType.BEAMNG:
|
||||
unpackedData = struct.unpack(BEAMNG_METHODS.BEAMNG_DATA_FORMAT,data)
|
||||
carData = BEAMNG_METHODS.unpackData(unpackedData)
|
||||
kmh=carData["speed"]*3.6
|
||||
elif gameType == GameType.FORZA:
|
||||
# unpackedData = struct.unpack(FORZA_METHODS.FORZA_DATA_FORMAT,data)
|
||||
carData = FORZA_METHODS.get_data(data)
|
||||
kmh = carData["Speed"]*3.6
|
||||
toPi.write(str(kmh).encode()+":".encode())
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue