-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetcolor.py
executable file
·94 lines (77 loc) · 2.92 KB
/
setcolor.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
#!/usr/bin/env python
#setcolor r g b -> sets for all devices
#setcolor -n 0 r g b -> sets only for first device
#setcolor -p center r g b -> sets for all devices in center
#setcolor -i 100 r g b -> sets intensity
from optparse import OptionParser
import sys
from pycyborg import get_all_cyborgs,POSITION
colormap={
'off':(0,0,0),
'red':(255,0,0),
'green':(0,255,0),
'blue':(0,0,255),
}
if __name__=='__main__':
optionparser=OptionParser()
optionparser.add_option("-n",type="int", dest="num",help="only change n-th device color (start at 0)")
optionparser.add_option("-p",dest="position",help="only search for devices at the specified position. possible values are: center,n,ne,e,se,s,sw,w,nw")
optionparser.add_option("-i",type="int", dest="intensity",help="set intensity (0-100)")
optionparser.add_option("-v",dest="verbose",action="store_true",default=False, help="be verbose")
(options,pargs) = optionparser.parse_args()
if len(pargs)==1 and pargs[0].lower() in colormap:
r,g,b=colormap[pargs[0].lower()]
elif len(pargs)==3:
try:
r,g,b=int(pargs[0]),int(pargs[1]),int(pargs[2])
except:
print("r g b must be integers 0-255")
optionparser.print_help()
sys.exit(1)
else:
print("missing r/g/b values")
optionparser.print_help()
sys.exit(1)
if r<0 or r>255 or g<0 or g>255 or b<0 or b>255:
print("r g b must be integers 0-255")
optionparser.print_help()
sys.exit(1)
if options.intensity!=None:
if options.intensity<0 or options.intensity>100:
print("intensity must be 0-100")
optionparser.print_help()
sys.exit(1)
pos=None
if options.position!=None:
pos=options.position.upper()
if pos not in POSITION:
print("position must be one of: center,n,ne,e,se,s,sw,w,nw")
optionparser.print_help()
sys.exit(1)
#end arg checking
cyborgs=get_all_cyborgs(lights_off=False)
if options.verbose:
print("Found %s cyborgs"%len(cyborgs))
#filter by position
if pos!=None:
lst=[]
for cyborg in cyborgs:
if cyborg.position==pos:
lst.append(cyborg)
cyborgs=lst
if options.num!=None:
try:
onlyone=cyborgs[options.num]
cyborgs=[onlyone,]
except:
print("can not select index %s from cyborgs. I have %s"%(options.num,len(cyborgs)))
sys.exit(1)
if options.verbose:
print("Setting color to %s,%s,%s on %s cyborg(s)"%(r,g,b,len(cyborgs)))
#cyborg candidate list complete, perform the update
for cy in cyborgs:
if options.verbose:
print("Changing : %s"%cy)
if options.intensity!=None:
cy.set_intensity(options.intensity)
cy.set_rgb_color(r,g,b,force=True)