-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash
executable file
·229 lines (201 loc) · 7.92 KB
/
dash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/python3
# System modules
import tkinter as tk
import serial
import socket
import time
import obd
from tkinter import ttk
from dashSpecs import *
from queue import Queue
from threading import Thread
# Set up some global variables
num_fetch_threads = 1
queue = Queue()
# Gets data from dataMaker app
def readFromDataMaker(app, q):
print("*** Read thread running")
hostMACAddress = 'b8:27:eb:2d:17:5a'
#hostMACAddress = 'b8:27:eb:7b:60:24'
port = 3
backlog = 1
size = 128
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress,port))
s.listen(backlog)
client, address = s.accept()
print('Connected by', address)
while 1:
data = client.recv(size)
if data:
data = (data.decode("utf-8")).split()
q.put(data)
q.task_done()
#client.send(data)
# This gets the data from the ELM237 BT module
def readFromELM(command):
connection = obd.OBD()
fromELM = connection.query(command)
#todo: figure out how to differ commands and convert speed to mph
#print(command)
print(fromELM.value.magnitude)
magnitude = str(fromELM.value.magnitude)
return magnitude;
# This is the thread that reads from the Ardino
def readData(app, q):
print("*** Read thread running")
connection = obd.OBD()
speed = obd.commands.SPEED
rpm = obd.commands.RPM
eng_temp = obd.commands.COOLANT_TEMP
fuel_level = obd.commands.FUEL_LEVEL
intake_temp = obd.commands.INTAKE_TEMP
while True:
time.sleep(.05)
if(connection.query(speed)):
data = [
readFromELM(speed), #data[0]
readFromELM(rpm), #data[1]
readFromELM(eng_temp), #data[2]
#readFromELM(fuel_level), #etc.
readFromELM(intake_temp)
]
q.put(data)
q.task_done()
# Update Gauge Values
def updateGauges(app, q):
print("*** Update thread running")
rpmBlink = 0
fuelBlink = 0
while True:
data = q.get()
# SET MPH
app.MPH["value"] = float(data[0])
app.labelMPH["text"] = data[0] + " MPH"
# SET RPM
app.RPM["value"] = float(data[1])
app.labelTemp["text"] = data[1] + "RPM"
if int(data[1]) > RPM_RED_LINE:
if rpmBlink % 2:
app.style.configure("rpm.Horizontal.TProgressbar", background=RPM_RED_LINE_COLOR1,
forground=RPM_RED_LINE_COLOR1, thickness=100)
rpmBlink += 1
else:
app.style.configure("rpm.Horizontal.TProgressbar", background=RPM_RED_LINE_COLOR2,
forground=RPM_RED_LINE_COLOR2, thickness=100)
rpmBlink -= 1
elif int(data[1]) > RPM_WARNING:
app.style.configure("rpm.Horizontal.TProgressbar", background=RPM_WARNING_COLOR,
forground=RPM_WARNING_COLOR, thickness=100)
else:
app.style.configure("rpm.Horizontal.TProgressbar", background=RPM_COLOR,
forground=RPM_COLOR, thickness=100)
# SET ENGINE TEMP
app.Temp["value"] = float(data[2])
app.labelTemp["text"] = "Engine Temp " + data[2] + "C"
if int(data[2]) > ENGINE_TEMP_CRITICAL:
if fuelBlink % 2:
app.style.configure("engineTemp.Horizontal.TProgressbar",
background=ENGINE_TEMP_CRITICAL_COLOR1,
forground=ENGINE_TEMP_CRITICAL_COLOR1, thickness=100)
fuelBlink += 1
else:
app.style.configure("engineTemp.Horizontal.TProgressbar",
background=ENGINE_TEMP_CRITICAL_COLOR2,
forground=ENGINE_TEMP_CRITICAL_COLOR2, thickness=100)
fuelBlink -= 1
elif int(data[2]) > ENGINE_TEMP_WARNING:
app.style.configure("engineTemp.Horizontal.TProgressbar",
background=ENGINE_TEMP_WARNING_COLOR,
forground=ENGINE_TEMP_WARNING_COLOR, thickness=100)
else:
app.style.configure("engineTemp.Horizontal.TProgressbar", background=ENGINE_TEMP_COLOR,
forground=ENGINE_TEMP_COLOR, thickness=100)
# SET FUEL LEVEL
app.fuelLevel["value"] = float(data[3])
app.labelFuelLevel["text"] = "Fuel Level " + data[3] + "%"
if int(data[3]) <= FUEL_CRITICAL:
if fuelBlink % 2:
app.style.configure("fuel.Horizontal.TProgressbar", background='red',
forground='red', thickness=100)
fuelBlink += 1
else:
app.style.configure("fuel.Horizontal.TProgressbar", background='white',
forground='white', thickness=100)
fuelBlink -= 1
elif int(data[3]) < FUEL_WARNING:
app.style.configure("fuel.Horizontal.TProgressbar", background='yellow',
forground='yellow', thickness=100)
else:
app.style.configure("fuel.Horizontal.TProgressbar", background='white',
forground='white', thickness=100)
# SET INTAKE TEMP
app.intakeTemp["value"] = float(data[4])
app.labelIntakeTemp["text"] = "Intake Temp " + data[4] + "C"
# Declare a class for the GUI
class Dash(tk.Tk):
def __init__(self):
# Init
tk.Tk.__init__(self)
self.style = ttk.Style()
self.geometry(str(WIDTH)+'x'+str(HIGHT))
# Set up Styles
self.style.configure("BW.TLabel", foreground="black")
self.style.configure("rpm.Horizontal.TProgressbar", background='white',
forground='white', thickness=100)
self.style.configure("fuel.Horizontal.TProgressbar", background='white',
forground='white', thickness=100)
self.style.configure("engineTemp.Horizontal.TProgressbar", background='white',
forground='white', thickness=100)
# Set up the different screens
notebook = ttk.Notebook(self)
screen1 = ttk.Frame(notebook)
screen2 = ttk.Frame(notebook)
notebook.add(screen1, text='Screen 1')
notebook.add(screen2, text='Screen 2')
notebook.pack()
# TEMP
self.labelTemp = ttk.Label(screen1, text="Engine Temp C", style="BW.TLabel")
self.labelTemp.grid(row=2, column=0)
self.Temp = ttk.Progressbar(screen1, orient="horizontal", length=WIDTH,
mode="determinate", value=0, maximum=250,
style="engineTemp.Horizontal.TProgressbar")
self.Temp.grid(row=3, column=0, sticky="W")
# RPM
self.labelRPM = ttk.Label(screen1, text="RPM", style="BW.TLabel")
self.labelRPM.grid(row=0, column=0)
self.RPM = ttk.Progressbar(screen1, orient="horizontal", maximum=RPM_MAX,
length=WIDTH, mode="determinate", value=0,
style="rpm.Horizontal.TProgressbar")
self.RPM.grid(row=1, column=0)
# MPH
self.labelMPH = ttk.Label(screen2, text="MPH", style="BW.TLabel")
self.labelMPH.grid(row=0, column=0)
self.MPH = ttk.Progressbar(screen2, orient="horizontal", length=WIDTH,
mode="determinate", value=0, maximum=120)
self.MPH.grid(row=1, column=0)
# Fuel Level
self.labelFuelLevel = ttk.Label(screen1, text="Fuel Level", style="BW.TLabel")
self.labelFuelLevel.grid(row=4, column=0)
self.fuelLevel = ttk.Progressbar(screen1, orient="horizontal", length=WIDTH,
mode="determinate", value=0, maximum=100,
style="fuel.Horizontal.TProgressbar")
self.fuelLevel.grid(row=5, column=0, sticky="W")
# Intake Temp
self.labelIntakeTemp = ttk.Label(screen2, text="Intake Temp", style="BW.TLabel")
self.labelIntakeTemp.grid(row=0, column=3)
self.intakeTemp = ttk.Progressbar(screen2, orient="horizontal", length=400,
mode="determinate", value=0, maximum=200)
self.intakeTemp.grid(row=1, column=3)
print('*** Main thread running')
app = Dash()
# Set up some threads
read = Thread(target=readFromDataMaker, args=(app, queue,))
read.setDaemon(True)
read.start()
update = Thread(target=updateGauges, args=(app, queue,))
update.setDaemon(True)
update.start()
app.mainloop()
queue.join()
print('*** Done')