-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathled.py
111 lines (91 loc) · 2.99 KB
/
led.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
import neopixel
import board
import re
import time
import threading
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation.animation.colorcycle import ColorCycle
from adafruit_led_animation.animation.blink import Blink
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.color import (
PURPLE,
WHITE,
AMBER,
JADE,
TEAL,
PINK,
MAGENTA,
ORANGE,
)
pixel_pin = board.D21
num_pixels = 300
dimmer = 100
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.RGB
pixels = neopixel.NeoPixel(
pixel_pin, num_pixels, brightness=1, auto_write=False, pixel_order=ORDER
)
solid = Solid(pixels, color=PINK)
blink = Blink(pixels, speed=0.5, color=JADE)
colorcycle = ColorCycle(pixels, speed=0.4, colors=[MAGENTA, ORANGE, TEAL])
chase = Chase(pixels, speed=0.1, color=WHITE, size=3, spacing=6)
comet = Comet(pixels, speed=0.01, color=PURPLE, tail_length=10, bounce=True)
pulse = Pulse(pixels, speed=0.1, color=AMBER, period=3)
last_color = [255, 255, 255]
loopEffect = False
class LED(threading.Thread):
def __init__(self, parent=None):
self.parent = parent
super(LED, self).__init__()
def target_with_callback(self):
self.method()
if self.callback is not None:
self.callback(*self.callback_args)
def terminate(self):
self._running = False
def hex_to_rgb(self, hex):
rgb = []
for i in (0, 2, 4):
decimal = int(hex[i:i+2], 16)
rgb.append(decimal)
return tuple(rgb)
def setLightColor(self, color_hex):
global last_color
global loopEffect
loopEffect = False
last_color = self.hex_to_rgb(color_hex)
pixels.fill(last_color)
# Uncomment this line if you have RGBW/GRBW NeoPixels
# pixels.fill((255, 0, 0, 0))
pixels.show()
def setLightDimmer(self, dimmer):
global last_color
global loopEffect
loopEffect = False
new_value = int(re.findall(r"'(.*?)'", dimmer)[0])/100
dimmed_color = [int(last_color[0]*new_value), int(last_color[1]*new_value), int(last_color[2]*new_value)]
pixels.fill(dimmed_color)
pixels.show()
print("set dimmer " + str(new_value) + " " + str(dimmed_color))
def toggleAnimation(self, id, switch):
print("toggle Animation: " + str(id) + " " + str(switch))
new_value = re.findall(r"'(.*?)'", str(switch))[0]
if new_value == "on":
animate = True
else:
animate = False
animations = AnimationSequence(
solid,
blink,
colorcycle,
chase,
comet,
pulse,
advance_interval=5,
auto_clear=True,
)
while animate:
animations.animate()