-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmabeee_ctrl.py
More file actions
executable file
·66 lines (53 loc) · 1.66 KB
/
Copy pathmabeee_ctrl.py
File metadata and controls
executable file
·66 lines (53 loc) · 1.66 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
#!/usr/bin/env python3
from bluepy import btle
import sys
import os
import cmd
def scan_for(print_list=False, name=None, scan_duration=3.0):
scanner = btle.Scanner(0)
devices = scanner.scan(scan_duration)
result = None
for device in devices:
for (code, description, val) in device.getScanData():
if code == 9 and val.startswith("MaBeee"):
if print_list:
print(f"- Device {val} addr {device.addr}")
if val == name:
result = device.addr
break
return result
def connect(mac_addr):
peripheral = btle.Peripheral()
peripheral.connect(mac_addr, btle.ADDR_TYPE_RANDOM)
return peripheral
def write_pwm(peripheral, value):
PWM_HANDLE = 17
PWM_INDEX = 1
data = bytearray(peripheral.readCharacteristic(PWM_HANDLE))
data[PWM_INDEX] = value
peripheral.writeCharacteristic(17, bytes(data), True)
if len(sys.argv) <= 1:
print("""Usage:
./mabeee_ctrl.py scan Device scan (requires root):
./mabeee_ctrl.py [MAC addr|name] Connect to device
""")
elif sys.argv[1] == "scan":
scan_for(print_list=True)
else:
target = sys.argv[1]
if target.startswith("MaBeee"):
target_addr = scan_for(name=target)
else:
target_addr = target
peripheral = connect(target_addr)
while True:
print("PWM 0-100: ", end="")
sys.stdout.flush()
line = sys.stdin.readline()
try:
value = int(line.strip())
write_pwm(peripheral, value)
except ValueError:
break
peripheral.disconnect()
print("End.")