-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors_gui.py
executable file
·67 lines (56 loc) · 2.07 KB
/
sensors_gui.py
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
import bluepy.btle as btle
import struct
from MainWindow import Ui_MainWindow
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class SpeedDelegate(btle.DefaultDelegate):
def __init__(self):
btle.DefaultDelegate.__init__(self)
self.prevCRev = None
self.prevETime = None
def handleNotification(self, cHandle, data):
cRev, eTime = struct.unpack("IH", data[1:1+4+2])
eTime /= 1024.
if self.prevCRev and cRev > self.prevCRev and eTime > self.prevETime:
speed = ((cRev - self.prevCRev) * 72 * 0.09144) / float(eTime - self.prevETime)
print("Speed: " + str(speed) + " km/h")
ui.speed.display(speed)
self.prevCRev = cRev
self.prevETime = eTime
class CadenceDelegate(btle.DefaultDelegate):
def __init__(self):
btle.DefaultDelegate.__init__(self)
self.prevCRev = None
self.prevETime = None
def handleNotification(self, cHandle, data):
cRev, eTime = struct.unpack("HH", data[1:1+2*2])
eTime /= 1024.
if self.prevCRev and cRev > self.prevCRev and eTime > self.prevETime:
#print("cRev: " + str(self.prevCRev) + " -> " + str(cRev) + "\t" + "eTime: " + str(self.prevETime) + " -> " + str(eTime))
cadence = ((cRev - self.prevCRev) * 60.) / float(eTime - self.prevETime)
print("Cadence: " + str(cadence) + " rpm")
ui.cadence.display(cadence)
self.prevCRev = cRev
self.prevETime = eTime
print("Connecting to speed sensor...")
spdPeri = btle.Peripheral("<address>", btle.ADDR_TYPE_RANDOM)
spdPeri.setDelegate(SpeedDelegate())
spdPeri.writeCharacteristic(0x0024, b"\1\0", True)
print("Speed sensor connected")
print("Connecting to cadence sensor...")
cadPeri = btle.Peripheral("<address>", btle.ADDR_TYPE_RANDOM)
cadPeri.setDelegate(CadenceDelegate())
cadPeri.writeCharacteristic(0x0024, b"\1\0", True)
print("Cadence sensor connected")
def checkForUpdates():
spdPeri.waitForNotifications(0.1)
cadPeri.waitForNotifications(0.1)
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.showMaximized()
timer = QtCore.QTimer()
timer.timeout.connect(checkForUpdates)
timer.start(0)
sys.exit(app.exec_())