-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathble.py
307 lines (254 loc) · 10.7 KB
/
ble.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#import sys
# ruff: noqa: E402
#sys.path.append("")
#from micropython import const
# main.py -- put your code here!
from micropython import const
import gc
import asyncio
import bluetooth
import aioble
import random
import struct
#MPY: soft reboot
#Connecting to Device(ADDR_PUBLIC, b4:10:7b:36:2b:c5)
#device_name = 'KS-16S0130'
device_names = ['KSN-16X-120093','KS-16S0130']
KS_CHAR_UUID = bluetooth.UUID(0xffe1) #bluetooth.UUID("0000ffe1-0000-1000-8000-00805f9b34fb")
KS_SERV_UUID = bluetooth.UUID(0xffe0) # bluetooth.UUID('0000ffe0-0000-1000-8000-00805f9b34fb')
from wheeldata import g_wheeldata
def decode4byte(data: bytearray, position):
val_h, val_l = struct.unpack_from('<HH', data, position)
return (val_h << 16) + val_l
BATT_16S = ['16S']
BATT_20S = ['16X', 'S18', '18L', '18X'] #include only first 3 symbols of the model!
def decode_ks_packet(data: bytearray):
#packet = struct.unpack("<B", data)
# check header and format
if len(data) >= 20:
a1 = data[0] & 255
a2 = data[1] & 255
if a1 != 170 and a2 != 85:
return f"Packet Is not Recognized: {data}"
else:
return "packet is too short: data:{}".format(data)
if (data[16] & 255) == 0xA9: # Live data
voltage = struct.unpack_from("<H", data, 2)[0] / 100.0
speed = struct.unpack_from("<H", data, 4)[0] / 100.0
odo = decode4byte(data, 6) / 1000.0 # KS18L scaler is skipped!
current = struct.unpack_from('<h', data, 10)[0] / 100.0 # current can be negative!!
#wd.setCurrent((data[10] & 0xFF) + (data[11] <<
temperature = struct.unpack_from('<H', data, 12)[0] / 100.0
mode : byte = 0x0
if (data[15] & 255) == 224:
mode = struct.unpack_from("<B", data, 14)[0]
g_wheeldata.live_pkt.update(voltage, speed, odo, current, temperature, mode)
return # str(g_wheeldata.live_pkt)
elif (data[16] & 255) == 0xF6: # { //speed limit (PWM?)
speedlimit = struct.unpack_from("<H", data, 2)[0] / 100.0
g_wheeldata.speedlimit_pkt.update(speedlimit)
return #str(g_wheeldata.speedlimit_pkt)
elif (data[16] & 255) == 0xB9:
trip = decode4byte(data, 2) / 1000.0
uptime = struct.unpack_from('<H', data, 6)[0] / 100.0
topspeed = struct.unpack_from('<H', data, 8 )[0] / 100.0
fanstate = struct.unpack_from('<B', data, 12)[0] # 1 if fan is running
g_wheeldata.trip_pkt.update(trip, uptime, topspeed, fanstate)
return #str(g_wheeldata.trip_pkt)
elif (data[16] & 255) == 0xf5: #cpu load
cpuload = struct.unpack_from('<B', data, 14)[0]
output = struct.unpack_from('<B', data, 15)[0]
g_wheeldata.cpuload_pkt.update(cpuload, output)
return #str(g_wheeldata.cpuload_pkt)
elif (data[16] & 255 ) == 0xbb: # name and model
modelstr = str(data[2:14].decode()) #, encoding='iso-8859-1')
ss = modelstr.split('-')
print('Name', ss[0], 'Model', ss[1], 'Version', ss[2])
g_wheeldata.name = ss[0]
g_wheeldata.model = ss[1]
g_wheeldata.version = ss[2]
if ss[1][0:3] in BATT_16S:
g_wheeldata.cells = 16
if ss[1][0:3] in BATT_20S:
g_wheeldata.cells = 20
g_wheeldata.got_name_and_model = True
print('Detected number of cells:', g_wheeldata.cells)
return #Name and model", data
else:
return f"Not a Live Data Packet: {data}"
async def discover_chars_for_service(ks_service):
"""an example, not used in the ode currently"""
chars = []
async for characteristic in ks_service.characteristics():
chars.append(characteristic)
print('found ch:', characteristic)
for c in chars:
print('char:', c)
async for descriptor in c.descriptors():
print('\tdescriptor:',descriptor)
async def discover_devices():
# Scan for 5 seconds, in active mode, with very low interval/window (to
# maximise detection rate).
res_name = set()
res_services = {}
async with aioble.scan(duration_ms=10000, interval_us=30000, window_us=30000, active=True) as scanner:
async for device in scanner:
if device.addr() not in res_name:
res_name.add(result.name())
serv = []
for s in result.services():
serv.append(s)
print('Service: - ', s)
res_services[result.name()] = serv
print('Scan complete. Devices:')
for d in res_name:
print('Device:', d , 'Services:', res_services.get(d, 'No Services'))
return None
async def find_ks_device():
async with aioble.scan(duration_ms=5000, interval_us=30000, window_us=30000, active=True) as scanner: # , active=True) as scanner:
async for result in scanner:
#if KS_SERV_UUID in result.services():
# print('nectfound device:', result.device)
# return result.device
if result.name() in device_names:
print("Device: ", result)
# for s in result.services():
# print('se:', s)
#if KS_SERV_UUID in result.services():
# return result.device
#else:
# print("Device is present but the service is not found")
return result.device
return None
from tft_display import gui
class Ble():
def __init__(self):
self.connection = None
self.device = None
self.service = None
self.characteristic = None
async def connect_and_process(self):
#print('Scan for devices:')
#await discover_devices()
gui.jump_to_connect()
gc.collect()
try:
self.device = await find_ks_device()
if not self.device:
print("KS Device is not found")
return
print("Connecting to", self.device)
self.connection = await self.device.connect()
if self.connection:
print('Connected')
else:
print('could not connect to the device')
except asyncio.TimeoutError:
print("Timeout during connection")
return
async with self.connection:
try:
self.service = await self.connection.service(KS_SERV_UUID)
if self.service:
print('Connected to service ', KS_SERV_UUID)
else:
print('Could not connect to service' , KS_SERV_UUID)
return
print('connect ot characteristic', KS_CHAR_UUID)
self.characteristic = await self.service.characteristic(KS_CHAR_UUID)
if self.characteristic:
await self.characteristic.subscribe(notify=True)
print('Subscribed to:', self.characteristic)
else:
print('failed to get characteristic', KS_CHAR_UUID)
return
except asyncio.TimeoutError:
print("Timeout discovering services/characteristics")
return
gui.back_from_connect()
try:
#immediately send a request!
if not g_wheeldata.got_name_and_model: # do it untill get result (:
await asyncio.sleep_ms(100)
await self.request(0x9b) # request manufacturer and model
#await asyncio.sleep_ms(100)
#if self.connection.is_connected():
# pass
#await self.request(0x9B) # request manufacturer and model
#await asyncio.sleep_ms(100)
#await self.request(0x63) # request serial data
#else:
# print('oops disconnected')
while self.connection.is_connected():
#print('wait for notification')
data = await self.characteristic.notified() # yes! this method returns data that cause the notification!!!
res = decode_ks_packet(data)
if res:
print(res)
await asyncio.sleep_ms(10)
#cnt -= 1
#if cnt < 0:
# return
except Exception as e:
print(f'BLE server disconnected: {e}')
import sys
sys.print_exception(e)
finally: # set object to disconnected state
await self.disconnect()
def is_connected(self):
if self.connection:
return self.connection.is_connected()
return False
async def disconnect(self):
if self.is_connected():
await self.connection.disconnect()
self.connection = None
self.device = None
self.service = None
self.characteristic = None
g_wheeldata.got_name_and_model = False
async def request(self, reqtype: byte, value_2 = 0x0, value_3 = 0x0): # value_2 and value_3 - write parameter value
if self.characteristic:
ba = bytearray(20)
ba[0] = 0xAA
ba[1] = 0x55
ba[2] = value_2
ba[3] = value_3
ba[16] = reqtype
ba[17] = 0x14
ba[18] = 0x5A
ba[19] = 0x5A
#print('request: ba:' , ba)
await self.characteristic.write(ba)
else:
print('request: characteristic is broken')
ble = Ble()
#asyncio.run(ble_process())
async def scan():
async with aioble.scan(duration_ms=2000, interval_us=30000, window_us=30000, active=True) as devices: # Scan for 5 seconds
#print('Devices:', devices)
#print('\n'.join(dir(devices)))
async for result in devices:
#print('Device:', '\n'.join(dir(device.adv_data)))
print(f"Device: {result.name()}, Address: {result.device.addr_hex()}")
for adv in result.services():
print(f"Advertised Service: {adv}")
if result.device.addr_hex() == 'b4:10:7b:36:2b:c5':
return result.device
return None
#asyncio.run(scan())
async def connect_and_discover(addr):
device = await scan()
#if not device:
connection = await device.connect()
print('Connection ', "\n".join(dir(connection)), '\n device: ', "\n".join(dir(device)))
serv = []
async for service in connection.services():
print(f"Service UUID: {service.uuid}")
serv.append(service)
#characteristics = await service.discover_characteristics()
for s in serv:
print('For service', s)
async for char in s.characteristics():
print(f"Characteristic UUID: {char.uuid}")
#asyncio.run(connect_and_discover("b4:10:7b:36:2b:c5"))