-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetneocolor.py
executable file
·164 lines (156 loc) · 5.72 KB
/
setneocolor.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# sets color of first neo pixel
from __future__ import division
import time
import sys
from math import *
import redis
from neopixel import *
# LED strip configuration:
LED_COUNT = 61 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 100 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
def wheel(pos):
"""Generate rainbow colors across 0-255 positions."""
if pos < 85:
return Color(pos * 3, 255 - pos * 3, 0)
elif pos < 170:
pos -= 85
return Color(255 - pos * 3, 0, pos * 3)
else:
pos -= 170
return Color(0, pos * 3, 255 - pos * 3)
def clockcalc(value):
return 1+((30+int(value))%60)
def showit():
if ( valid == "yes" ):
strip.show()
# Main program logic follows:
if __name__ == '__main__':
# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
# Intialize the library (must be called once before other functions).
strip.begin()
myredis=redis.StrictRedis(host='localhost', port=6379, db=0)
while True:
time.sleep(50/1000.0)
try:
r=int(myredis.get('neored'))
except:
r=0
try:
g=int(myredis.get('neogreen'))
except:
g=0
try:
b=int(myredis.get('neoblue'))
except:
b=0
try:
action=myredis.get('neoaction')
except:
action="set"
try:
valid=myredis.get('valid')
except:
valid="yes"
if ( valid != "yes" ):
continue
myredis.set('valid','no')
if(action == "set"):
print "Color set to: ",r,g,b
for i in range(0,LED_COUNT):
strip.setPixelColor(i, Color(g,r,b))
showit()
if(action == "number"):
tmp=myredis.get('neovalue')
if(not tmp):
continue
i=clockcalc(int(tmp))
print "Digit ",i," set to: ",r,g,b
strip.setPixelColor(i, Color(r,g,b))
showit()
if(action == "morse"):
print "Morsing with: ",r,g,b
speed=10
for i in range(0,LED_COUNT):
strip.setPixelColor(i, Color(0,0,0))
morsecode=myredis.get('neovalue')
morsep=1
for c in morsecode:
if c==".":
strip.setPixelColor(morsep, Color(r,g,b))
morsep+=1
strip.setPixelColor(morsep, Color(0,0,0))
morsep+=1
if c=="-":
strip.setPixelColor(morsep, Color(r,g,b))
morsep+=1
strip.setPixelColor(morsep, Color(r,g,b))
morsep+=1
strip.setPixelColor(morsep, Color(0,0,0))
morsep+=1
strip.setPixelColor(morsep, Color(0,0,0))
morsep+=1
if c==" ":
strip.setPixelColor(morsep, Color(0,0,0))
morsep+=1
strip.setPixelColor(morsep, Color(0,0,0))
morsep+=1
strip.setPixelColor(morsep, Color(0,0,0))
morsep+=1
strip.setPixelColor(morsep, Color(0,0,0))
morsep+=1
showit()
time.sleep(1)
if(action == "pulse"):
print "Pulsing with: ",r,g,b
pulsewidth=50
while (action==myredis.get('neoaction')):
for i in range(pulsewidth):
if(action!=myredis.get('neoaction')):
break
# reset all pixels to black
color=Color(0,0,0)
for j in range(0,60):
strip.setPixelColor(clockcalc(j), color)
# put the hours hand as white, skew by 5 depending on minutes value
clockpos=int(int(time.strftime('%I',time.localtime()))*5+int(time.strftime('%M',time.localtime()))/60*5)
for j in range(-2,3):
strip.setPixelColor(clockcalc(clockpos+j),Color(30,30,30))
# set the hours marks as a plusing blue
trigo=sin(i/pulsewidth*pi)
color=Color(int(r*trigo),int(g*trigo),int(b*trigo))
for j in range(0,60,5):
strip.setPixelColor(clockcalc(j), color)
# put the current people count as magenta
if myredis.get('neocounter') >= 0:
strip.setPixelColor(clockcalc(myredis.get('neocounter')),Color(10,0,10))
# put the minute hand as red
strip.setPixelColor(clockcalc(time.strftime('%M',time.localtime())),Color(30,0,0))
# put the seconds hand as green
strip.setPixelColor(clockcalc(time.strftime('%S',time.localtime())),Color(0,30,0))
showit()
time.sleep(50/1000.0)
if(action == "flash"):
print "Flashing with: ",r,g,b
print myredis.get('neoaction')
wait_ms=50
"""Rainbow movie theater light style chaser animation."""
while (action==myredis.get('neoaction')):
print " Flashing with: ",r,g,b
for j in range(256):
print " Flashing with: ",r,g,b
if(action!=myredis.get('neoaction')):
break
for q in range(3):
for i in range(0, strip.numPixels(), 3):
strip.setPixelColor(i+q, wheel((i+j) % 255))
showit()
time.sleep(wait_ms/1000.0)
for i in range(0, strip.numPixels(), 3):
strip.setPixelColor(i+q, 0)