-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqt_brigde.py
More file actions
158 lines (123 loc) · 5.54 KB
/
qt_brigde.py
File metadata and controls
158 lines (123 loc) · 5.54 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
import logging
import threading
import time
from PyQt5 import QtCore
import ftms
from antsend import AntSend
from bluezero import adapter
from ble_peripheral import FtmsPeripheral
from ble_central import BleCentral
logging.basicConfig(level=logging.WARNING)
_LOGGER = logging.getLogger(__name__)
class MoveOnError(Exception):
pass
def get_adapters():
a = list(adapter.Adapter.available())
return a
class BleBridge(QtCore.QThread):
any_signal = QtCore.pyqtSignal(list)
def __init__(self, parent=None, adapter_lib=None):
super(BleBridge, self).__init__(parent)
self.a = adapter_lib
self.threads = {}
self.pill2kill = threading.Event()
self.pill2kill2 = threading.Event()
self.pill2kill3 = threading.Event()
if len(self.a) < 2:
self.ble_in = BleCentral(stop_event=self.pill2kill, adapter_address=self.a[0])
self.ble_out = FtmsPeripheral(stop_event=self.pill2kill3, adapter_address=self.a[0], have_to_work=False)
else:
self.ble_in = BleCentral(stop_event=self.pill2kill, adapter_address=self.a[0],
blacklist_address=self.a[1])
self.ble_out = FtmsPeripheral(stop_event=self.pill2kill3, adapter_address=self.a[1])
self.ant_send = AntSend(self.pill2kill2)
self.t = []
def start_pause(self, running):
if running is False:
self.ble_in.ftms_control_value = [True, False, bytearray([0x07])]
else:
self.ble_in.ftms_control_value = [True, False, bytearray([0x08, 0x02])]
def stop_running(self):
self.ble_in.ftms_control_value = [True, False, bytearray([0x08, 0x02])]
time.sleep(0.25)
self.ble_in.ftms_control_value = [True, False, bytearray([0x00])]
time.sleep(0.25)
self.ble_in.ftms_control_value = [True, False, bytearray([0x01])]
time.sleep(0.25)
self.ble_in.ftms_control_value = [True, False, bytearray([0x00])]
def set_speed(self, speed):
speed_bytes = bytearray([0x02]) + int(speed*100).to_bytes(2, byteorder='little')
self.ble_in.ftms_control_value = [True, False, speed_bytes]
def set_incline(self, incline):
incline_bytes = bytearray([0x03]) + int(incline*10).to_bytes(2, byteorder='little')
self.ble_in.ftms_control_value = [True, False, incline_bytes]
def increase_speed(self):
speed = self.ble_in.values[0] + 20
speed_bytes = bytearray([0x02]) + int(speed).to_bytes(2, byteorder='little')
self.ble_in.ftms_control_value = [True, False, speed_bytes]
def decrease_speed(self):
speed = self.ble_in.values[0] - 20
speed_bytes = bytearray([0x02]) + int(speed).to_bytes(2, byteorder='little')
self.ble_in.ftms_control_value = [True, False, speed_bytes]
def increase_incline(self):
incline = self.ble_in.values[3] + 5
incline_bytes = bytearray([0x03]) + int(incline).to_bytes(2, byteorder='little')
self.ble_in.ftms_control_value = [True, False, incline_bytes]
def decrease_incline(self):
incline = self.ble_in.values[3] - 5
incline_bytes = bytearray([0x03]) + int(incline).to_bytes(2, byteorder='little')
self.ble_in.ftms_control_value = [True, False, incline_bytes]
def update_ble_out(self, ftms_control_value):
if ftms_control_value[1] is True:
self.ble_out.treadmill_data_values = self.ble_in.value
self.ble_out.ftms_status_value = self.ble_in.ftms_status_value
self.ble_out.training_status_value = self.ble_in.training_status_value
elif ftms_control_value[0] is True:
self.ble_in.ftms_control_value = ftms_control_value
def update_ant(self):
self.ant_send.TreadmillSpeed = self.ble_in.values[0] / 360 # m/s
self.ant_send.TreadmillDistance = self.ble_in.values[9]
def run(self):
print("start bridge...")
if len(self.a) < 2:
print("Only one adapter available. No Peripheral!")
self.threads[1] = threading.Thread(target=self.ble_in.ble_central_start)
self.t += [1]
else:
self.threads[1] = threading.Thread(target=self.ble_in.ble_central_start)
self.t += [1]
self.threads[2] = threading.Thread(target=self.ble_out.ftms_peripheral_start)
self.t += [2]
self.threads[3] = threading.Thread(target=self.ant_send.openchanel)
self.t += [3]
try:
for i in self.t:
print("start thread", i)
self.threads[i].start()
while not self.pill2kill.wait(0.1):
update_ble_out_thread = threading.Thread(target=self.update_ble_out,
args=(ftms.ftms_control_value, ))
update_ble_out_thread.start()
update_ant_thread = threading.Thread(target=self.update_ant)
update_ant_thread.start()
self.any_signal.emit(self.ble_in.values)
except KeyboardInterrupt:
pass
finally:
print("loop ended")
self.pill2kill.set()
self.pill2kill2.set()
self.pill2kill3.set()
for i in self.t:
self.threads[i].join()
logging.shutdown()
print("Close bridge...")
def stop(self):
self.pill2kill.set()
self.pill2kill2.set()
self.pill2kill3.set()
for i in self.t:
self.threads[i].join()
self.any_signal = None
print("Bridge Thread stopped")
return