forked from ph4r05/ph4-walkingpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpad.py
384 lines (301 loc) · 12.6 KB
/
pad.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import binascii
import logging
import time
from bleak import discover
from bleak import BleakClient
# typing
if False:
from bleak.backends.device import BLEDevice
logger = logging.getLogger(__name__)
class Scanner:
def __init__(self):
self.devices_dict = {}
self.devices_list = []
self.receive_data = []
self.walking_belt_candidates = [] # type: list[BLEDevice]
async def scan(self):
logger.info("Scanning for peripherals...")
dev = await discover()
for i in range(len(dev)):
# Print the devices discovered
info_str = ', '.join(["[%2d]" % i, str(dev[i].address), str(dev[i].name), str(dev[i].metadata["uuids"])])
logger.info("Device: %s" % info_str)
# Put devices information into list
self.devices_dict[dev[i].address] = []
self.devices_dict[dev[i].address].append(dev[i].name)
self.devices_dict[dev[i].address].append(dev[i].metadata["uuids"])
self.devices_list.append(dev[i].address)
if 'walkingpad' in dev[i].name.lower():
self.walking_belt_candidates.append(dev[i])
class WalkingPad:
MODE_STANDBY = 2
MODE_MANUAL = 1
MODE_AUTOMAT = 0
PREFS_MAX_SPEED = 3
PREFS_START_SPEED = 4
PREFS_START_INTEL = 5
PREFS_SENSITIVITY = 6
PREFS_DISPLAY = 7
PREFS_CHILD_LOCK = 9
PREFS_UNITS = 8
PREFS_TARGET = 1
TARGET_NONE = 0
TARGET_DIST = 1
TARGET_CAL = 2
TARGET_TIME = 3
BUTTON_None = 0
BUTTON_Down = 4
BUTTON_Stop = 3
BUTTON_Up = 2
BUTTON_long_mode = -6
BUTTON_up = -4
BUTTON_mode = -6
PAYLOADS_255 = [
[247, 165, 96, 74, 77, 147, 113, 41, 201, 253],
[247, 165, 96, 74, 58, 60, 113, 41, 95, 253],
[247, 165, 96, 74, 15, 165, 113, 41, 157, 253],
[247, 165, 96, 74, 21, 129, 113, 41, 127, 253],
[247, 165, 96, 74, 45, 189, 115, 171, 87, 253],
[247, 165, 96, 74, 49, 42, 113, 41, 68, 253],
[247, 165, 96, 74, 58, 60, 113, 41, 95, 253],
[247, 165, 96, 74, 77, 147, 113, 41, 201, 253],
]
@staticmethod
def int2byte(val, width=3):
return [(val >> (8 * (width - 1 - i)) & 0xff) for i in range(width)]
@staticmethod
def byte2int(val, width=3):
return sum([(val[i] << (8 * (width - 1 - i))) for i in range(width)])
@staticmethod
def fix_crc(cmd):
cmd[-2] = sum(cmd[1:-2]) % 256
return cmd
class WalkingPadCurStatus:
def __init__(self):
self.raw = None
self.dist = 0
self.time = 0
self.steps = 0
self.speed = 0
self.controller_button = 0
self.app_speed = 0
self.belt_state = 0
self.manual_mode = 0
self.rtime = 0
def load_from(self, cmd):
self.raw = bytearray(cmd)
self.belt_state = cmd[2]
self.speed = cmd[3]
self.manual_mode = cmd[4]
self.time = WalkingPad.byte2int(cmd[5:])
self.dist = WalkingPad.byte2int(cmd[8:])
self.steps = WalkingPad.byte2int(cmd[11:])
self.app_speed = cmd[14] # / 30
self.controller_button = cmd[16]
self.rtime = time.time()
@staticmethod
def check_type(cmd):
return bytes(cmd[0:2]) == bytes([248, 162])
@staticmethod
def from_data(cmd):
if not WalkingPadCurStatus.check_type(cmd):
raise ValueError('Incorrect message type, could not parse')
m = WalkingPadCurStatus()
m.load_from(cmd)
return m
def __str__(self):
return 'WalkingPadCurStatus(dist=%s, time=%s, steps=%s, speed=%s, state=%s, ' \
'mode=%s, app_speed=%s, button=%s, rest=%s)' \
% (self.dist / 100, self.time, self.steps, self.speed / 10, self.belt_state,
self.manual_mode, self.app_speed / 30 if self.app_speed > 0 else 0, self.manual_mode,
binascii.hexlify(bytearray([self.raw[15], self.raw[17]])).decode('utf8'))
class WalkingPadLastStatus:
def __init__(self):
self.raw = None
self.dist = 0
self.time = 0
self.steps = 0
self.rtime = 0
def load_from(self, cmd):
self.raw = bytearray(cmd)
self.time = WalkingPad.byte2int(cmd[8:])
self.dist = WalkingPad.byte2int(cmd[11:])
self.steps = WalkingPad.byte2int(cmd[14:])
self.rtime = time.time()
@staticmethod
def check_type(cmd):
return bytes(cmd[0:2]) == bytes([248, 167])
@staticmethod
def from_data(cmd):
if not WalkingPadLastStatus.check_type(cmd):
raise ValueError('Incorrect message type, could not parse')
m = WalkingPadLastStatus()
m.load_from(cmd)
return m
def __str__(self):
return 'WalkingPadLastStatus(dist=%s, time=%s, steps=%s, rest=%s)' \
% (self.dist / 100, self.time, self.steps, binascii.hexlify(self.raw[2:8]).decode('utf8'))
class Controller:
def __init__(self, address=None, do_read_chars=True):
self.address = address
self.do_read_chars = do_read_chars
self.log_messages_info = True
self.char_fe01 = None
self.char_fe02 = None
self.client = None
self.last_raw_cmd = None
self.last_cmd_time = None
self.last_status = None
self.last_record = None
self.minimal_cmd_space = 0.69
self.handler_cur_status = None
self.handler_last_status = None
self.handler_message = None
async def __aenter__(self):
await self.run()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.disconnect()
def notif_handler(self, sender, data):
logger_fnc = logger.info if self.log_messages_info else logger.debug
logger_fnc('Msg: %s' % (', '.join('{:02x}'.format(x) for x in data)))
already_notified = False
if WalkingPadCurStatus.check_type(data):
m = WalkingPadCurStatus.from_data(data)
self.last_status = m
already_notified = True
self.on_cur_status_received(sender, m)
if self.handler_cur_status:
self.handler_cur_status(sender, m)
logger_fnc('Status: %s' % (m,))
elif WalkingPadLastStatus.check_type(data):
m = WalkingPadLastStatus.from_data(data)
self.last_record = None
already_notified = True
self.on_last_status_received(sender, m)
if self.handler_last_status:
self.handler_last_status(sender, m)
logger_fnc('Record: %s' % (m,))
self.on_message_received(sender, data, already_notified)
if self.handler_message:
self.handler_message(sender, data, already_notified)
def on_message_received(self, sender, data, already_notified=False):
"""Override to use as message callback"""
def on_cur_status_received(self, sender, status: WalkingPadCurStatus):
"""Override to receive current status"""
def on_last_status_received(self, sender, status: WalkingPadLastStatus):
"""Override to receive last status"""
def fix_crc(self, cmd):
return WalkingPad.fix_crc(cmd)
async def disconnect(self):
logger.info("Disconnecting")
await self.client.disconnect()
async def connect(self, address=None):
address = address or self.address
if not address:
raise ValueError('No address given to connect to')
self.client = BleakClient(address)
return await self.client.connect()
async def send_cmd(self, cmd):
self.fix_crc(cmd)
if self.last_cmd_time and time.time() - self.last_cmd_time < self.minimal_cmd_space:
to_sleep = max(0, min(time.time() - self.last_cmd_time, self.minimal_cmd_space))
await asyncio.sleep(to_sleep)
return await self.send_cmd_raw(cmd)
async def send_cmd_raw(self, cmd):
self.last_raw_cmd = cmd
self.last_cmd_time = time.time()
r = await self.client.write_gatt_char(self.char_fe02, cmd)
return r
async def switch_mode(self, mode: int):
cmd = bytearray([247, 162, 2, mode, 0xff, 253])
return await self.send_cmd(cmd)
async def change_speed(self, speed: int):
cmd = bytearray([247, 162, 1, speed, 0xff, 253])
return await self.send_cmd(cmd)
async def stop_belt(self):
return await self.change_speed(0)
async def start_belt(self):
cmd = bytearray([247, 162, 4, 1, 0xff, 253])
return await self.send_cmd(cmd)
async def ask_profile(self, profile_idx=0):
cmd = bytearray(WalkingPad.PAYLOADS_255[profile_idx])
return await self.send_cmd(cmd)
async def ask_stats(self):
cmd = bytearray([247, 162, 0, 0, 162, 253])
return await self.send_cmd(cmd)
async def ask_hist(self, mode=0):
cmd = bytearray([247, 167, 170, 255, 80, 253] if mode == 0 else [247, 167, 170, 0, 81, 253])
return await self.send_cmd(cmd)
async def cmd_162_3_7(self, mode=0):
cmd = bytearray([247, 162, 3, 7, 172, 253])
return await self.send_cmd(cmd)
async def set_pref_arr(self, key: int, arr):
cmd = bytearray([247, 166, key, *arr, 172, 253])
return await self.send_cmd(cmd)
async def set_pref_int(self, key: int, val: int, stype: int = 0):
arr = [stype, *WalkingPad.int2byte(val)]
return await self.set_pref_arr(key, arr)
async def set_pref_max_speed(self, speed):
return await self.set_pref_int(WalkingPad.PREFS_MAX_SPEED, speed)
async def set_pref_start_speed(self, speed):
return await self.set_pref_int(WalkingPad.PREFS_START_SPEED, speed)
async def set_pref_inteli(self, enabled=False):
return await self.set_pref_int(WalkingPad.PREFS_START_INTEL, int(enabled))
async def set_pref_sensitivity(self, sensitivity=3): # 1 = high, 2 = medium, 3 = low
return await self.set_pref_int(WalkingPad.PREFS_SENSITIVITY, sensitivity)
async def set_pref_display(self, bit_mask: int): # 7bits
return await self.set_pref_int(WalkingPad.PREFS_DISPLAY, bit_mask)
async def set_pref_child_lock(self, enabled=False):
return await self.set_pref_int(WalkingPad.PREFS_CHILD_LOCK, int(enabled))
async def set_pref_units_miles(self, enabled=False):
return await self.set_pref_int(WalkingPad.PREFS_UNITS, int(enabled))
async def set_pref_target(self, target_type: int = 0, value: int = 0):
return await self.set_pref_int(WalkingPad.PREFS_TARGET, value, target_type)
async def run(self, address=None):
await self.connect(address)
client = self.client
x = await client.is_connected()
logger.info("Connected: {0}".format(x))
self.char_fe01 = None
self.char_fe02 = None
for service in client.services:
logger.info("[Service] {0}: {1}".format(service.uuid, service.description))
for char in service.characteristics:
value = None
if "read" in char.properties:
try:
if self.do_read_chars and char.uuid != '0000fe01-0000-1000-8000-00805f9b34fb':
value = bytes(await client.read_gatt_char(char.uuid))
except Exception as e:
logger.info('read failed for %s' % (char.uuid,))
value = str(e).encode()
logger.info(
"\t[Characteristic] {0}: (Handle: {1}) ({2}) | Name: {3}, Value: {4} ".format(
char.uuid,
char.handle,
",".join(char.properties),
char.description,
value,
)
)
if char.uuid.startswith('0000fe01'):
self.char_fe01 = char
if char.uuid.startswith('0000fe02'):
self.char_fe02 = char
for descriptor in char.descriptors:
value = await client.read_gatt_descriptor(descriptor.handle)
logger.info(
"\t\t[Descriptor] {0}: (Handle: {1}) | Value: {2} ".format(
descriptor.uuid, descriptor.handle, bytes(value)
)
)
try:
logger.info('Enabling notification for %s' % (self.char_fe01.uuid,))
await client.start_notify(self.char_fe01.uuid, self.notif_handler)
except Exception as e:
logger.warning("Notify failed: %s" % (e,))
logger.info('Service enumeration done')