-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledstripCLI.py
executable file
·171 lines (151 loc) · 2.97 KB
/
ledstripCLI.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
#!/usr/bin/python
import time
import RPi.GPIO as GPIO
import sys, getopt
CLK=11
DAT=12
debug=False
delay=0
red=0
green=0
blue=0
state="Off"
#debug=True
def init():
GPIO.setwarnings(debug)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(CLK, GPIO.OUT)
GPIO.setup(DAT, GPIO.OUT)
def cleanup():
GPIO.cleanup()
def Clock():
GPIO.output(CLK,False)
time.sleep(delay)
GPIO.output(CLK,True)
time.sleep(delay)
def Send32Zero():
for x in range(32):
GPIO.output(DAT,False)
if (debug):
print "0"
Clock()
def getCode(dat):
tmp=0
if ((dat & 0x80) == 0):
tmp |= 0x02
if ((dat & 0x40) == 0):
tmp|= 0x01
# print "Get Code [%d] %s : %s" % (dat, tmp, bin(dat))
return tmp
def SetColor( Red, Green, Blue):
dx =0
# print bin(dx)
dx |= 0x03 << 30
# print bin(dx)
dx |= getCode(Blue)
# print bin(dx)
dx |= getCode(Green)
# print bin(dx)
dx |= getCode(Red)
# print bin(dx)
dx |= Blue <<16
# print bin(dx)
dx |= Green <<8
# print bin(dx)
dx |= Red
if (debug):
print bin(dx)
Send(dx)
def Send(dx):
if (debug):
print "Sending [%s]" % (bin (dx))
Send32Zero()
for x in range(32):
if ((dx & 0x80000000) != 0 ):
GPIO.output(DAT,True)
if (debug):
print "1",
else:
GPIO.output(DAT,False)
if( debug):
print "0",
dx <<= 1
Clock()
if (debug):
print ""
Send32Zero()
def AllOff():
print "All 0"
SetColor(0,0,0)
#time.sleep(1)
def AllOn():
print "All 255"
SetColor(255,255,255)
def Red():
setColor(255,0,0)
def Green():
setColor(0,255,0)
def Blue():
setColor(0,0,255)
def setColor(red,green,blue):
SetColor(red,green,blue)
def done():
cleanup()
# for z in range (10):
# for x in range(255):
# #print "X:%d" % (x)try:
# #y=x*10
# SetColor(x,x,x)
# for x in range(255):
# SetColor(255-x,255-x,255-x)
# for x in range(20):
# SetColor(255,0,0)
# time.sleep(0.1)
# SetColor(0,0,255)
# time.sleep(0.1)
try:
opts, args = getopt.getopt(sys.argv[1:],'hs:r:g:b:')
# print "got Opts: "
# print sys.argv
except getopt.GetoptError:
print 'ledstrip.py -h -s [On|Off] -r [val] -g [val] -b [val] '
sys.exit(2)
#print "-"
#print opts
#print args
#print "="
for opt, arg in opts:
# print "Opt: %s" % opt
if opt == '-h':
print 'ledstrip.py -h -s [On|Off] -r [val] -g [val] -b [val] '
sys.exit()
elif opt in ("-w"):
red=255
green=255
blue=255
elif opt in ("-s"):
state = arg
if state == "On":
red=255
green=255
blue=255
if state == "Off":
red=0
green=0
blue=0
elif opt in ("-r"):
red = int(arg)
elif opt in ("-g"):
green = int(arg)
elif opt in ("-b"):
blue = int(arg)
#print 'red:', red
#print 'green:', green
#print 'blue:', blue
#print 'state:',state
#if __name__ == "__main__":
# main(sys.argv[1:])
#print "Init:"
init()
SetColor(red, green, blue)
done()