-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_strip.py
141 lines (117 loc) · 3.73 KB
/
led_strip.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
import time
import random
from concurrent.futures import ThreadPoolExecutor, as_completed
try:
from rpi_ws281x import Color, Adafruit_NeoPixel
except ImportError:
print("Error found importing rpi_ws281x library")
# LED strip configuration:
LED_COUNT = 50 # Number of LED pixels.
LED_FREQ_HZ = 800000 # LED signal frequency in hertz
LED_DMA = 10 # DMA channel to use for generating signal
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal
LED_PIN_1 = 18 # GPIO pin 18
LED_CHANNEL_1 = 0 # set to '0' for GPIOs 18, etc.
LED_PIN_2 = 19 # GPIO pin 19
LED_CHANNEL_2 = 1 # set to '1' for GPIOs 19, etc.
def initialize():
print("Initializing led strip 1")
strip1 = Adafruit_NeoPixel(
LED_COUNT,
LED_PIN_1,
LED_FREQ_HZ,
LED_DMA,
LED_INVERT,
LED_BRIGHTNESS,
LED_CHANNEL_1)
strip1.begin()
print("Led strip 1 intialized")
time.sleep(1) # Needed to ensure both leds are initialized properly
print("Initializing led strip 2")
strip2 = Adafruit_NeoPixel(
LED_COUNT,
LED_PIN_2,
LED_FREQ_HZ,
LED_DMA,
LED_INVERT,
LED_BRIGHTNESS,
LED_CHANNEL_2)
strip2.begin()
print("Led strip 2 intialized")
return [strip1, strip2]
def trigger_lightning(lightnings):
with ThreadPoolExecutor(len(lightnings)) as pool:
futures = []
print("Let's make some noise!")
for lightning in lightnings:
future = pool.submit(
show_lightning,
lightning.strip,
lightning.color)
futures.append(future)
print("Waitining for lightnings to be done")
for waiting_future in as_completed(futures):
print(waiting_future.result())
print("Lightning request accomplished")
def switch_lamp_on(lightnings):
print("Turning lamp on")
for lightning in lightnings:
color = lightning.color
fill(lightning.strip, Color(color.red, color.green, color.blue))
def show_lightning(strip, color):
adafruit_color = Color(color.red, color.green, color.blue)
# Wait a random amount of time before showing the lightning
time.sleep(random.uniform(0.0, 1.5))
lightning_type = random.randint(0, 2)
if lightning_type == 0:
show_lightning_model_1(strip, adafruit_color)
elif lightning_type == 1:
show_lightning_model_2(strip, adafruit_color)
else:
show_lightning_model_3(strip, adafruit_color)
return "Lightning shown!"
def show_lightning_model_1(strip, color):
color_thunder(strip, color)
blink(strip, color)
turn_off(strip)
def show_lightning_model_2(strip, color):
color_thunder(strip, color)
time.sleep(0.25)
turn_off(strip)
def show_lightning_model_3(strip, color):
blink(strip, color)
time.sleep(0.25)
turn_off(strip)
def color_thunder(strip, color):
print("Starting color thunder")
wait = 1
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
time.sleep(wait / 10000.0)
wait = wait + 2
time.sleep(0.4)
print("Color thunder done")
def blink(strip, color):
print("Starting blink")
turn_off(strip)
fill(strip, color)
time.sleep(0.2)
turn_off(strip)
time.sleep(0.4)
fill(strip, color)
time.sleep(0.2)
turn_off(strip)
time.sleep(0.1)
fill(strip, color)
print("Blink done")
def turn_off(strip):
print("Turning off")
fill(strip, Color(0, 0, 0))
def fill(strip, color):
print("Starting fill")
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
print("Fill done")