-
Notifications
You must be signed in to change notification settings - Fork 1
/
neopixel_basics.py
178 lines (139 loc) · 5.34 KB
/
neopixel_basics.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
172
173
174
175
176
177
178
from micropython import const
from machine import Pin
from neopixel import NeoPixel
from urandom import randint
from utime import sleep, sleep_ms
NEOPIXEL_NUMBER = const(16)
LED_GPIO_PIN = const(23)
DELAY = const(5)
def value_verification(value: int, minimum: int = 0, maximum: int = 255) -> bool:
"""
verify if specific value is between specific minimum and maximum values
:param value: int of value
:param minimum: int of minimum value (default = 0)
:param maximum: int of maximum value (default = 255)
:return: bool
"""
if int(minimum) <= int(value) <= int(maximum):
return True
else:
return False
def random_rgb() -> tuple:
"""
return random rgb tuple with 3 items (between 0 and 255)
:return: tuple
"""
rgb = (randint(0, 255), randint(0, 255), randint(0, 255))
return rgb
def clear_all() -> None:
"""
turn off all LED's by rgb value (0, 0, 0)
:return: None
"""
global nps
nps.fill((0, 0, 0))
nps.write()
def set_color(red: int, green: int, blue: int) -> None:
"""
set all LED's (on) in specific rgb color
:param red: int between 0 and 255
:param green: int between 0 and 255
:param blue: int between 0 and 255
:return: None
"""
global nps
arg_verify = True
if not value_verification(value=red):
print(f'[ERROR] wrong argument {red} for parameter red')
arg_verify = False
if not value_verification(value=green):
print(f'[ERROR] wrong argument {green} for parameter green')
arg_verify = False
if not value_verification(value=blue):
print(f'[ERROR] wrong argument {blue} for parameter blue')
arg_verify = False
if arg_verify:
for item in range(NEOPIXEL_NUMBER):
nps[item] = (red, green, blue)
nps.write()
def cycle_color(foreground: tuple, background: tuple, rounds: int = 1, wait: int = 10) -> None:
"""
cycle a single LED in specific rgb color
:param foreground: tuple of foreground rgb colors eq. (255, 0, 0)
:param background: tuple of background rgb colors eq. (0, 0, 0)
:param rounds: int (minimum is 1, default = 1)
:param wait: int (minimum is 1, default = 10) in milliseconds
:return: None
"""
global nps
arg_verify = True
count = 1
if not value_verification(value=foreground[0], minimum=0, maximum=255):
print(f'[ERROR] wrong argument {foreground[0]} for parameter foreground red')
arg_verify = False
if not value_verification(value=foreground[1], minimum=0, maximum=255):
print(f'[ERROR] wrong argument {foreground[1]} for parameter foreground green')
arg_verify = False
if not value_verification(value=foreground[2], minimum=0, maximum=255):
print(f'[ERROR] wrong argument {foreground[2]} for parameter foreground blue')
arg_verify = False
if not value_verification(minimum=0, maximum=255, value=background[0]):
print(f'[ERROR] wrong argument {background[0]} for parameter background red')
arg_verify = False
if not value_verification(minimum=0, maximum=255, value=background[1]):
print(f'[ERROR] wrong argument {background[1]} for parameter background green')
arg_verify = False
if not value_verification(minimum=0, maximum=255, value=background[2]):
print(f'[ERROR] wrong argument {background[2]} for parameter background blue')
arg_verify = False
if not int(rounds) >= 1:
print(f'[ERROR] wrong argument {rounds} for parameter rounds')
arg_verify = False
if not int(wait) >= 1:
print(f'[ERROR] wrong argument {wait} for parameter wait')
arg_verify = False
if arg_verify:
set_color(background[0], background[1], background[2])
while count <= rounds:
count += 1
for item in range(NEOPIXEL_NUMBER):
nps[item] = (foreground[0], foreground[1], foreground[2])
nps[item - 1] = background
nps.write()
sleep_ms(wait)
def random_colors(value: str = 'single') -> None:
"""
set LED's to random rgb color value specified by parameter argument
:param value: string of either 'all' or 'single' (default = 'single')
:return: None
"""
global nps
valid = {'all', 'single'}
if value in valid and value == 'single':
for item in range(NEOPIXEL_NUMBER):
nps[item] = random_rgb()
nps.write()
if value in valid and value == 'all':
color = random_rgb()
for item in range(NEOPIXEL_NUMBER):
nps[item] = color
nps.write()
if __name__ == '__main__':
nps = NeoPixel(Pin(LED_GPIO_PIN), NEOPIXEL_NUMBER)
while True:
print('[INFO] Set each LED to random color')
random_colors()
sleep(DELAY)
print("[INFO] Set all LED's to random color")
random_colors('all')
sleep(DELAY)
print("[INFO] Turn off all LED's")
clear_all()
sleep(DELAY)
print("[INFO] Set all LED's to same color")
set_color(150, 150, 150)
sleep(DELAY)
print("[INFO] Cycle LED's with specific colors")
cycle_color(foreground=(255, 0, 0), background=(0, 0, 0), rounds=3, wait=50)
print("[INFO] Cycle LED's with specific colors")
cycle_color(foreground=(0, 0, 0), background=(0, 255, 0), rounds=3, wait=25)