Compare commits

..

15 Commits

6 changed files with 117 additions and 77 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,4 @@
/build /build
/dist /dist
OutGaugeInterpreter.spec OutGaugeInterpreter.spec
*.csv

View File

@@ -6,9 +6,11 @@ import serial
import time import time
import platform import platform
import sys import sys
import threading
from enum import Enum from enum import Enum
import tkinter import tkinter
from GAME_METHODS import * from GAME_METHODS import *
import functools #used for passing variables into callbacks
##check if python 3.11 is running code and exit if not ##check if python 3.11 is running code and exit if not
@@ -20,26 +22,43 @@ if not(sys.version_info[0] == 3 and sys.version_info[1] == 11):
##system init ##system init
runningOs = platform.system() runningOs = platform.system()
UDP_IP = "127.0.0.1" UDP_IP = "0.0.0.0"
BEAMNG_UDP_PORT = 4444 BEAMNG_UDP_PORT = 4444
FORZA_UDP_PORT= 4843 FORZA_UDP_PORT= 4843
SERIAL_PORT = "" SERIAL_PORT = ""
firstRun= True #used to do the headers for csv files firstRun= True #used to do the headers for csv files
pushingToArduino = False
stopThread = False
class GameType(Enum): class GameType(Enum):
NONE=0 NONE=0
BEAMNG = "BEAMNG" BEAMNG = "BEAMNG"
FORZA = "FORZA" FORZA = "FORZA"
class ProgramState(Enum):
PUSHING_DATA = 1
WAITNG = 2
ERROR = 3
appState =ProgramState.WAITNG
connectedArduino = False connectedArduino = False
connectedWebSocket = False connectedWebSocket = False
gameSelected = False gameSelected = False
portToConnect= 0 portToConnect= 0
gameType= GameType.NONE
carData = 0 carData = 0
csvOut = False csvOut = False
#Functions <------------------------------------------------------------------------------------------- #Functions <-------------------------------------------------------------------------------------------
def checkBoxChange():
if tkLoggingEnabled.get()==True:
loggingLocationEntry.config(state='normal')
loggingLocationText.set(value= "./"+ tkGametype.get()+"_"+str(int(time.time()))+".csv")
loggingLocationEntry.insert(0,loggingLocationText.get())
else:
loggingLocationText.set(value="")
loggingLocationEntry.delete(0,tkinter.END)
loggingLocationEntry.config(state='disabled')
def csvWriteOut(firstRun,carData,csvFile): def csvWriteOut(firstRun,carData,csvFile):
if firstRun == True: if firstRun == True:
out="" out=""
@@ -51,6 +70,59 @@ def csvWriteOut(firstRun,carData,csvFile):
outString = outString + str(num) +"," outString = outString + str(num) +","
csvFile.write(outString+"\n") csvFile.write(outString+"\n")
def runningThread():
global stopThread
gameType= tkGametype.get()
#check everything is connected
if gameType==GameType.BEAMNG.value:
portToConnect=BEAMNG_UDP_PORT
elif gameType==GameType.FORZA.value:
portToConnect=FORZA_UDP_PORT
try:
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((UDP_IP,portToConnect))
except:
print("please check you are able to open the socket on this system\ntired to open port: "+portToConnect)
exit()
try:
toPi=serial.Serial(SERIAL_PORT,115200,timeout=2) #connect to arduino
except:
print("please check connection to arduino and verify the correct serial port")
exit()
csvOut = tkLoggingEnabled.get()
if csvOut == True:
csvFile = open(loggingLocationText.get(),"a")
while not stopThread:
data, addr = sock.recvfrom(1024)
if gameType == GameType.BEAMNG.value:
unpackedData = struct.unpack(BEAMNG_METHODS.BEAMNG_DATA_FORMAT,data)
carData = BEAMNG_METHODS.unpackData(unpackedData)
kmh=carData["speed"]*3.6
if csvOut == True:
csvWriteOut(firstRun,carData,csvFile)
firstRun=False
elif gameType == GameType.FORZA.value:
unpackedData = struct.unpack(FORZA_METHODS.FORZA_DATA_FORMAT,data)
carData = FORZA_METHODS.unpackData(unpackedData)
kmh = carData["Speed"]*3.6
if csvOut == True:
csvWriteOut(firstRun,carData,csvFile)
firstRun=False
try:
toPi.write(str(kmh).encode()+":".encode())
except:
print("arduino disconnected please check connection\n")
#code runs from here<----------------------------------------------------------------------------------------------------------- #code runs from here<-----------------------------------------------------------------------------------------------------------
if runningOs == 'Windows': if runningOs == 'Windows':
@@ -63,11 +135,29 @@ else:
print("OS detection failed setting serial port to 'COM5'\n") print("OS detection failed setting serial port to 'COM5'\n")
SERIAL_PORT = 'COM5' SERIAL_PORT = 'COM5'
def startStopButtonFunc():
global appState
if appState == ProgramState.WAITNG:
startStopButton.config(text="stop")
appState = ProgramState.PUSHING_DATA
stopThread=False
workThread = threading.Thread(target=runningThread)
workThread.start()
statusTextVar.set("Running...")
elif appState == ProgramState.PUSHING_DATA:
startStopButton.config(text="start")
appState = ProgramState.WAITNG
statusTextVar.set("Waiting...")
stopThread = True
##GUI init ##GUI init
rootFrameGui= tkinter.Tk() rootFrameGui= tkinter.Tk()
rootFrameGui.title("Car speed to arduino") rootFrameGui.title("Car speed to arduino")
rootFrameGui.geometry("400x200") ##rootFrameGui.geometry("400x200")
bottomBarFrameGui = tkinter.ttk.Frame(rootFrameGui,padding=0,relief="groove",borderwidth=2,)
bottomBarFrameGui.pack(anchor="se",side="bottom",fill="x")
firstFrameGui=tkinter.ttk.Frame(rootFrameGui,padding=2,relief="groove",borderwidth=2) firstFrameGui=tkinter.ttk.Frame(rootFrameGui,padding=2,relief="groove",borderwidth=2)
firstFrameGui.pack(anchor="ne",side="left",expand=True) firstFrameGui.pack(anchor="ne",side="left",expand=True)
secondFrameGui=tkinter.ttk.Frame(rootFrameGui,padding=2,relief="groove",borderwidth=2) secondFrameGui=tkinter.ttk.Frame(rootFrameGui,padding=2,relief="groove",borderwidth=2)
@@ -93,81 +183,28 @@ tkinter.Label(gameSelectFrame,textvariable=gameSelectText)
gameSelectLOptions= [GameType.BEAMNG,GameType.FORZA] gameSelectLOptions= [GameType.BEAMNG,GameType.FORZA]
##gameSelectRadioButtons ##gameSelectRadioButtons
##space after text is so the buttons are the same size ##space after text is so the buttons are the same size
tkinter.Radiobutton(gameSelectFrame,text="BeamNG ",variable=tkGametype,value=GameType.BEAMNG.value).pack(anchor="w") tkinter.Radiobutton(gameSelectFrame,text="BeamNG ",variable=tkGametype,value=GameType.BEAMNG.value,command=checkBoxChange).pack(anchor="w")
tkinter.Radiobutton(gameSelectFrame,text="Forza ",variable=tkGametype,value=GameType.FORZA.value).pack(anchor="w") tkinter.Radiobutton(gameSelectFrame,text="Forza ",variable=tkGametype,value=GameType.FORZA.value,command=checkBoxChange).pack(anchor="w")
loggingFrameGui= tkinter.ttk.Frame(firstFrameGui,padding=5,relief="groove",borderwidth=2) loggingFrameGui= tkinter.ttk.Frame(firstFrameGui,padding=5,relief="groove",borderwidth=2)
loggingFrameGui.pack(anchor="nw") loggingFrameGui.pack(anchor="nw")
tkLoggingEnabled=tkinter.BooleanVar() tkLoggingEnabled=tkinter.BooleanVar()
tkLoggingEnabled.set(False) tkLoggingEnabled.set(False)
tkinter.Checkbutton(loggingFrameGui,text="Enable Logging",variable=tkLoggingEnabled,onvalue=True,offvalue=False).pack(side="bottom") tkinter.Checkbutton(loggingFrameGui,text="Enable Logging",variable=tkLoggingEnabled,onvalue=True,offvalue=False,command=checkBoxChange).pack(side="top")
loggingLocationText = tkinter.StringVar(value="")
loggingLocationEntry = tkinter.Entry(loggingFrameGui)
loggingLocationEntry.insert(0,loggingLocationText.get())
loggingLocationEntry.config(state='disabled')
loggingLocationEntry.pack(side="bottom")
startStopButton= tkinter.Button(bottomBarFrameGui,text= "start",command= startStopButtonFunc)
startStopButton.pack(side="right",fill="x")
statusTextVar = tkinter.StringVar(value="waiting...")
statusLable = tkinter.Label(bottomBarFrameGui,textvariable= statusTextVar)
statusLable.pack(anchor="w")
arduinoConnectedFrameGui = tkinter.ttk.Frame(secondFrameGui,padding=5,relief="groove",borderwidth=2)
arduinoConnectedFrameGui.pack()
arduinoConnectedStatusText=tkinter.StringVar(value="Connected OK")
arduinoConnectedLable = tkinter.Label(arduinoConnectedFrameGui,textvariable=arduinoConnectedStatusText)
arduinoConnectedLable.pack()
rootFrameGui.mainloop() rootFrameGui.mainloop()
##exit() #### exit for testing
#select game
while gameSelected == False:
gameNo = input("1:BEAMNG\n2:FORZA\n\n7:Toggle CSV out ("+str(csvOut)+")\n9:SET SERIAL PORT ("+SERIAL_PORT+")\n")
if gameNo == "1":
portToConnect = BEAMNG_UDP_PORT
gameSelected = True
gameType=GameType.BEAMNG
print("BeamNG Selected")
elif gameNo == "2":
portToConnect = FORZA_UDP_PORT
gameType=GameType.FORZA
gameSelected=True
print("Forza Selected")
elif gameNo == "7":
if csvOut == True:
csvOut=False
elif csvOut ==False:
csvOut =True
elif gameNo == "9":
SERIAL_PORT = input("set serial port: ")
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)
sock.bind((UDP_IP,portToConnect))
connectedWebSocket = True
except:
print("please check you are able to open the socket on this system\ntired to open port: "+portToConnect)
exit()
while connectedArduino == False:
try:
toPi=serial.Serial(SERIAL_PORT,115200,timeout=2) #connect to arduino
connectedArduino = True
except:
print("please check connection to arduino and verify the correct serial port")
time.sleep(1)
if csvOut == True:
csvFile = open(gameType.value+"_"+str(int(time.time()))+'.csv',"a")
print("ready:\n")
while True:
data, addr = sock.recvfrom(1024)
if gameType == GameType.BEAMNG:
unpackedData = struct.unpack(BEAMNG_METHODS.BEAMNG_DATA_FORMAT,data)
carData = BEAMNG_METHODS.unpackData(unpackedData)
kmh=carData["speed"]*3.6
if csvOut == True:
csvWriteOut(firstRun,carData,csvFile)
firstRun=False
elif gameType == GameType.FORZA:
unpackedData = struct.unpack(FORZA_METHODS.FORZA_DATA_FORMAT,data)
carData = FORZA_METHODS.unpackData(unpackedData)
kmh = carData["Speed"]*3.6
if csvOut == True:
csvWriteOut(firstRun,carData,csvFile)
firstRun=False
try:
toPi.write(str(kmh).encode()+":".encode())
except:
print("arduino disconnected please check connection\n")

View File

@@ -1 +1,3 @@
pyinstaller.exe --add-data .\GAME_METHODS\*:.\GAME_METHODS\ --onefile .\OutGaugeInterpreter.py pyinstaller.exe --add-data .\GAME_METHODS\*:.\GAME_METHODS\ --onefile .\OutGaugeInterpreter.py
pyinstaller.exe --add-data .\GAME_METHODS\*:.\GAME_METHODS\ --onefile --noconsole --optimize 2 .\OutGaugeInterpreter.py
pyinstaller --add-data ./GAME_METHODS/*:./GAME_METHODS/ --onefile ./OutGaugeInterpreter.py