From 883d3fe98a0e2648051b2da0d80e9b460a4e12b2 Mon Sep 17 00:00:00 2001 From: Seth Samuel Date: Fri, 8 Nov 2024 11:07:47 +1300 Subject: [PATCH] added send to arduino --- OutGaugeInterpreter.py | 54 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/OutGaugeInterpreter.py b/OutGaugeInterpreter.py index c5cad81..fa68957 100644 --- a/OutGaugeInterpreter.py +++ b/OutGaugeInterpreter.py @@ -1,11 +1,63 @@ import socket +import struct +import serial + +toPi=serial.Serial('COM5',115200) + +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 UDP_IP = "127.0.0.1" UDP_PORT = 4444 sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) sock.bind((UDP_IP,UDP_PORT)) +print("waiting for data:\n") while True: data, addr = sock.recvfrom(1024) - print("received mesage: %s" % data) \ No newline at end of file + unpackedData = struct.unpack("I4sHBBfffffffIIfff16s16sxxxx",data) + 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] + } + #print("speed: %s" % (carData["speed"]*3.6)) + kmh=carData["speed"]*3.6 + toPi.write(str(kmh).encode()) +