-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTPMS-Monitor.py
More file actions
executable file
·267 lines (245 loc) · 8.58 KB
/
Copy pathTPMS-Monitor.py
File metadata and controls
executable file
·267 lines (245 loc) · 8.58 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#########################################################################
# Simple Python3 TPMS scanner #
# Sept 2025 ride #
# Originally based on "tpms-bleak.py" at https://github.com/andi38/TPMS #
# Added QT window and other functionality #
# Lots of fixes and help from ChatGPT actually. #
# It was useful. https://chatgpt.com/g/g-cKrO5THiU-python-code-fixer #
#########################################################################
#basic system stuff and async modules
import sys, asyncio, qasync
#GUI stuff
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QDesktopWidget
from PyQt5.QtGui import QPainter, QBrush, QColor, QFont, QIcon
from PyQt5.QtCore import Qt, QRect
#Bluetooth BLE stuff
from bleak import BleakScanner
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData
#Get the sensor addresses from the other file
from sensorMacAddresses import sensors
#import pprint
#Parse the data RX'd from the sensors
def parse_sensor_data(advertisement_data):
mfdata = advertisement_data.manufacturer_data
for data1, list2 in mfdata.items():
list1 = [data1 % 256, data1 // 256]
ldata = list1 + list(list2)
batt = ldata[1] / 10
temp = ldata[2]
press = (((ldata[3] * 256) | (ldata[4])) - 145) / 10.0 # in psi
return {
"BATT": batt,
"TEMPc": temp,
"TEMPf": round((temp * (9 / 5)) + 32, 2),
"BAR": round(press / 14.504, 2),
"PSI": round(press, 1),
"KPA": round((press / 14.504) * 100, 1),
#"KPA": round(press * 6.895, 2),
}
return None
#BLE Scanner function
async def ble_device_scanner(window):
try:
# Mapping MAC addresses to tire positions
mac_to_tire = {
sensors.FL_SENSOR: "FL",
sensors.FR_SENSOR: "FR",
sensors.RL_SENSOR: "RL",
sensors.RR_SENSOR: "RR",
}
def found(device: BLEDevice, advertisement_data: AdvertisementData):
tire = mac_to_tire.get(device.address)
if tire:
parsed = parse_sensor_data(advertisement_data)
if parsed:
window.tire_info[tire].update(parsed)
window.activity = 1
window.activity_timer.start(750)
window.trigger_repaint()
# Start BT scanning
scanner = BleakScanner(detection_callback=found)
await scanner.start()
# Keep the scanner alive
while True:
await asyncio.sleep(10.0)
window.trigger_repaint()
except asyncio.CancelledError:
await scanner.stop()
raise
#This draws the window
class MainWindow(QMainWindow):
def __init__(self, loop):
super().__init__()
self.loop = loop
#Stick the Window in the bottom left corner
self.setGeometry(0, 800, 350, 400)
#window title
self.setWindowTitle("Truckputer TPMS")
#program icon
self.setWindowIcon(QIcon("./TPMSicon.png"))
#removes the window's top bar and borders
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
#window background set to black
self.setStyleSheet("background-color: black")
#window transparency
self.setWindowOpacity(0.9)
self.ble_task = None
#activity light toggle on/off
self.activity = 0
#activity light timer (ChatGPT help)
self.activity_timer = QtCore.QTimer()
self.activity_timer.setSingleShot(True)
self.activity_timer.timeout.connect(self.reset_activity)
#Array to store the tire info and which units we are looking at
self.tire_info = {
"UNITS":"PSI",
"FL":{"BATT":0, "TEMPf":0, "TEMPc":32, "PSI":0, "BAR":0.0, "KPA":0},
"FR":{"BATT":0, "TEMPf":0, "TEMPc":32, "PSI":0, "BAR":0.0, "KPA":0},
"RL":{"BATT":0, "TEMPf":0, "TEMPc":32, "PSI":0, "BAR":0.0, "KPA":0},
"RR":{"BATT":0, "TEMPf":0, "TEMPc":32, "PSI":0, "BAR":0.0, "KPA":0},
"SP":{"BATT":0, "TEMPf":0, "TEMPc":32, "PSI":0, "BAR":0.0, "KPA":0}
}
#the units button to cycle PSI, BAR, KPA values
self.unitsButton = QPushButton("PSI", self)
self.unitsButton.setFont(QFont("Noto Sans", 20, QFont.Bold))
self.unitsButton.setStyleSheet("background-color: #303030")
self.unitsButton.clicked.connect(self.cycleUnits)
self.unitsButton.setGeometry(145, 40, 60, 50)
#reset the activity dot
def reset_activity(self):
self.activity = 0
self.update()
#Draws the basic vehicle wheels and axles
def drawVehicleFrame(self, qp):
frtLeft = QRect(15,15,100,155)
frtRight = QRect(235,15,100,155)
rearLeft = QRect(15,215,100,155)
rearRight = QRect(235,215,100,155)
#Store the wheel rectangles so they are easier to use later
self.tireRects = {
"FL": frtLeft,
"FR": frtRight,
"RL": rearLeft,
"RR": rearRight,
}
#draw wheels
for rect in self.tireRects.values():
qp.drawRect(rect)
#draw front "axle"
qp.drawLine(102,95,250,95)
#draw rear "axle"
qp.drawLine(102,295,250,295)
#draw "driveline"
qp.drawLine(175,95,175,295)
#make a little circle for the rear diff
#(makes it easy to tell front and rear! ;-) )
qp.setBrush(QBrush(Qt.white, Qt.SolidPattern))
qp.drawEllipse(165,285,20,20) #x,y,width,height
#draw the little activity light at top center of window
def activityIndicator(self, qp):
if self.activity == 1:
qp.setBrush(QBrush(Qt.green, Qt.SolidPattern))
else:
qp.setBrush(QBrush(Qt.NoBrush))
qp.drawEllipse(170,10,10,10)
#draw the sensor info in each "wheel"
def drawTireInfo(self, qp, rect, tire, units):
upperText = (Qt.AlignTop | Qt.AlignHCenter)
centerText = Qt.AlignCenter
bottomText = (Qt.AlignBottom | Qt.AlignHCenter)
qp.setFont(QFont("Noto Sans", 30, QFont.Bold))
value = tire[units]
#Check pressure and change color based on range
#PSI
if units == "PSI":
if 30 <= value <= 40:
qp.setPen(QtCore.Qt.GlobalColor.green)
elif 20 <= value < 30:
qp.setPen(QtCore.Qt.GlobalColor.yellow)
else:
qp.setPen(QtCore.Qt.GlobalColor.red)
#BAR
if units == "BAR":
if 2.1 <= value <= 2.8:
qp.setPen(QtCore.Qt.GlobalColor.green)
elif 1.4 <= value < 2.1:
qp.setPen(QtCore.Qt.GlobalColor.yellow)
else:
qp.setPen(QtCore.Qt.GlobalColor.red)
#KPA
if units == "KPA":
qp.setFont(QFont("Noto Sans", 21, QFont.Bold))
if 210 <= value <= 280:
qp.setPen(QtCore.Qt.GlobalColor.green)
elif 140 <= value < 210:
qp.setPen(QtCore.Qt.GlobalColor.yellow)
else:
qp.setPen(QtCore.Qt.GlobalColor.red)
qp.drawText(rect, upperText, str(value))
qp.setFont(QFont("Noto Sans", 11, QFont.Bold))
qp.setPen(QtCore.Qt.GlobalColor.white)
qp.drawText(rect, bottomText, f"{tire['TEMPf']}°F\n{tire['TEMPc']}°C\n{tire['BATT']}v\n")
#Actually draw the window by calling all the other functions above.
def paintEvent(self, event):
qp = QPainter(self)
qp.setPen(QtCore.Qt.GlobalColor.white)
#Draw vehicle and wheels outline
self.drawVehicleFrame(qp)
#Activity indicator
self.activityIndicator(qp)
#Fill in Sensor Info
units = self.tire_info['UNITS']
for tire, rect in self.tireRects.items():
self.drawTireInfo(qp, rect, self.tire_info[tire], units)
#Stop drawing the Vehicle and other stuff.
qp.end()
#Repaint window?
def trigger_repaint(self):
self.update()
#Start Scanner when window is opened
def showEvent(self, event):
"""Start BLE scanner when window is shown"""
if self.ble_task is None or self.ble_task.done():
self.ble_task = self.loop.create_task(ble_device_scanner(self))
#print("scan disabled...")
super().showEvent(event)
#Stop scanner when window is closed
def closeEvent(self, event):
#Stop BLE scanner when window is closed
if self.ble_task and not self.ble_task.done():
self.ble_task.cancel()
#print("scan cancelled...")
super().closeEvent(event)
#pprint.pprint(self.tire_info) #FOR DEBUGGING
#keeps track of which unit of measure we are looking at
def cycleUnits(self):
current = self.tire_info['UNITS']
order = ["PSI", "BAR", "KPA"]
nextUnit = order[(order.index(current) + 1) % len(order)]
self.tire_info['UNITS'] = nextUnit
self.unitsButton.setText(f"{nextUnit}")
self.trigger_repaint()
#This ties it all together and runs everything
#(and adds the Quit button!)
async def main_window():
app = QApplication(sys.argv)
loop = qasync.QEventLoop(app)
asyncio.set_event_loop(loop)
window = MainWindow(loop)
#Quit button for when we remove title bar, etc.
quitButton = QPushButton("Quit", window)
quitButton.setFont(QFont("Noto Sans", 14, QFont.Bold))
quitButton.setStyleSheet("background-color: #303030")
quitButton.clicked.connect(window.close)
quitButton.setGeometry(145, 345, 60, 50)
window.show()
window.trigger_repaint()
# ensure coroutine is "awaited" before run_forever blocks
#no idea what that means, but thanks ChatGPT!
await asyncio.sleep(0)
with loop:
loop.run_forever()
if __name__ == "__main__":
asyncio.run(main_window())