-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaybulb.py
More file actions
91 lines (74 loc) · 2.41 KB
/
playbulb.py
File metadata and controls
91 lines (74 loc) · 2.41 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
import sys, re, subprocess, getopt
from time import sleep
def write(colour, bulb_list):
# Set Playbulb colour
for e in bulb_list:
print('gatttool -b ' + e + ' --char-write -a 0x0016 -n ' + colour)
subprocess.call(('gatttool -b ' + e + ' --char-write -a 0x0016 -n ' + colour).split())
# sleep(1)
def read(bulb_list):
for e in bulb_list:
proc = subprocess.Popen(('gatttool -b ' + e + ' --char-read -a 0x0016').split(), stdout = subprocess.PIPE)
# sleep(1)
for line in iter(proc.stdout.readline,''):
print 'output: ' + line
def main(argv):
PLAYBULB_ADDRESS_1 = "AC:E6:4B:07:70:E8"
PLAYBULB_ADDRESS_2 = "AC:E6:4B:07:A9:8E"
PLAYBULB_ADDRESS_3 = "AC:E6:4B:06:C9:12"
color = 'white'
mode = 'write'
COLOUR_MAP = { 'red': '00ff0000',
'blue': '000000ff',
'green': '0000ff00',
'white': 'FFFF6000',
'orange': '00FF0F00',
'yellow': '00FF7700',
'purple': '00FF00F0',
'off': '00000000'}
bulb_list = []
bulb_list.append(PLAYBULB_ADDRESS_1)
try:
opts, args = getopt.getopt(argv,"hc:t:m:",["colour", "target","mode"])
except getopt.GetoptError:
print 'playbulb.py -c <color> -t <targetbulb> -m <mode>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'playbulb.py -c <color> -t <targetbulb> -m <mode>'
sys.exit()
elif opt in ("-c", "--color"):
color = arg
elif opt in ("-m", "--mode"):
mode = arg
elif opt in ("-t", "--target"):
if arg== '1':
# todo: Find a better way to do this list in python
bulb_list = []
bulb_list.append(PLAYBULB_ADDRESS_1)
elif arg == '2':
bulb_list = []
bulb_list.append(PLAYBULB_ADDRESS_2)
elif arg == '3':
bulb_list = []
bulb_list.append(PLAYBULB_ADDRESS_3)
elif arg == 'all':
bulb_list = []
bulb_list.append(PLAYBULB_ADDRESS_1)
bulb_list.append(PLAYBULB_ADDRESS_2)
bulb_list.append(PLAYBULB_ADDRESS_3)
colour = COLOUR_MAP[color]
print 'colour is '+ color+' code is '+colour
# Your Playbulb address (obtained with 'sudo hcitool lescan')
# Show the name of the playbulb
for e in bulb_list:
proc = subprocess.Popen(('gatttool -b ' + e + ' --char-read -a 0x0003').split(), stdout = subprocess.PIPE)
# sleep(1)
for line in iter(proc.stdout.readline,''):
name = ''.join(x.strip() for x in re.findall(r'[0-9a-f]{2}\s', line)).decode("hex")
print 'Playbulb name: ' + name
read(bulb_list)
write(colour, bulb_list)
read(bulb_list)
if __name__ == "__main__":
main(sys.argv[1:])