-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth-expect.py
executable file
·87 lines (66 loc) · 2.22 KB
/
bluetooth-expect.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
#!/usr/bin/env python
import pexpect
import sys
import time
def clamp(val, minval, maxval):
return min(max(val, minval), maxval)
def pwd2hex(pwd):
hexkeystring = ""
for char in pwd:
hexkeystring += hex(ord(char)) + " "
return hexkeystring.strip()
# 0 and 1 are off, 100 is max
def brightness2hex(brightness):
return hex(clamp(brightness, 2, 100))
# the box says 2700 -- 6000 but internally it's 2700 -- 6500
def colour2hex(brightness):
clamped_colour = clamp(brightness, 2700, 6500)
high_byte = clamped_colour // 256
low_byte = clamped_colour - 256 * high_byte
return str(hex(low_byte)) + ' ' + str(hex(high_byte))
def set_pwd(child, pwd):
child.sendline("select-attribute 0000ffba-0000-1000-8000-00805f9b34fb")
child.expect('service')
child.sendline("write '" + pwd2hex(pwd) + "'")
time.sleep(2.0)
def set_colour(child, colour):
child.sendline("select-attribute 0000ffb6-0000-1000-8000-00805f9b34fb")
child.expect('service')
child.sendline("write '" + colour2hex(colour)+ "'")
time.sleep(2.0)
def set_brightness(child, brightness):
child.sendline("select-attribute 0000ffb8-0000-1000-8000-00805f9b34fb")
child.expect('service')
child.sendline("write '" + brightness2hex(brightness) + "'")
time.sleep(2.0)
def main():
prompt = '#'
address = '48:70:1E:4E:44:0C'
child = pexpect.spawn('sudo bluetoothctl')
child.logfile = sys.stdout.buffer
child.expect(prompt)
child.sendline('scan on')
child.expect('Device.*' + address + '.*')
child.sendline('scan off')
child.sendline('connect ' + address)
child.expect('Connection successful')
child.expect('Characteristic')
child.sendline('menu gatt')
child.expect('Menu gatt:')
pwd = str(1436)
set_pwd(child, pwd)
# brightness = 100
brightness = int(sys.argv[1])
set_brightness(child, brightness)
# colour = 6500
colour = int(sys.argv[2])
set_colour(child, colour)
child.sendline("back")
child.expect(prompt)
child.sendline('disconnect')
child.expect('Successful disconnected')
child.expect(prompt)
child.sendline('quit')
child.expect(pexpect.EOF)
if __name__ == "__main__":
main()