-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
202 lines (163 loc) · 6.43 KB
/
code.py
File metadata and controls
202 lines (163 loc) · 6.43 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from adafruit_circuitplayground.express import cpx
import time
import random
class NeopixelPattern:
def __init__(self):
#basic colors
self.red = (255,0,0)
self.orange = (255,127,0)
self.yellow = (255,255,0)
self.yellow_green = (127,255,0)
self.green = (0,255,0)
self.green_blue = (0,127,127)
self.blue = (0,0,255)
self.blue_purple = (127,0,255)
self.purple = (225,0,255)
self.red_purple = (255,0,127)
#pattern color range
self.colors = [self.red,self.orange,self.yellow,self.yellow_green,
self.green,self.green_blue,self.blue,self.blue_purple,self.purple,self.red_purple]
#current frame number
self.frame = 0
#wait times for animation
self.smooth_time = 0.01
#self.pulse_time_start = 0.4
#self.pulse_time_end = 0.4
self.transition_time = 1
#pixel brightness
self.brightness = 0.3
cpx.pixels.brightness = self.brightness
#automatic pattern type
self.auto_patt = "fism" #spin smooth
#solid pattern
self.color_count = 0
self.num_colors = 10
self.num_steps = self.transition_time/ self.smooth_time
self.cur_step = 0
self.cur_brightness = self.brightness
self.fade_in = False
"""def show_base_frame(self):
for i in range(10):
cpx.pixels[i] = self.colors[i]"""
"""def update_frame_spin(self):
add_value = self.frame
for i in range(10):
if i + add_value >= 10:
add_value = -i
cpx.pixels[i+add_value] = self.colors[i]
#print(add_value)
self.frame += 1
if self.frame >= 10:
self.frame = 0"""
def update_frame_random(self):
for i in range(10):
cpx.pixels[i] = self.colors[random.randint(0,9)]
"""def display_spin_smooth(self):
self.update_frame_spin()
time.sleep(self.smooth_time)"""
def display_spin_pulse(self):
self.update_frame_spin()
time.sleep(self.pulse_time)
cpx.pixels.fill((0,0,0))
time.sleep(self.pulse_time)
def display_rand_smooth(self):
self.update_frame_random()
time.sleep(self.smooth_time)
def display_rand_pulse(self):
self.update_frame_random()
time.sleep(self.pulse_time_start)
cpx.pixels.fill((0,0,0))
time.sleep(self.pulse_time_end)
def display_fill_smooth(self, random = False):
if self.color_count == (self.num_colors-1):
r_dif = (self.colors[self.color_count][0] - self.colors[0][0])\
/ self.num_steps * self.cur_step
g_dif = (self.colors[self.color_count][1] - self.colors[0][1])\
/ self.num_steps * self.cur_step
b_dif = (self.colors[self.color_count][2] - self.colors[0][2])\
/ self.num_steps * self.cur_step
else:
r_dif = (self.colors[self.color_count][0] - self.colors[self.color_count+1][0])\
/ self.num_steps * self.cur_step
g_dif = (self.colors[self.color_count][1] - self.colors[self.color_count+1][1])\
/ self.num_steps * self.cur_step
b_dif = (self.colors[self.color_count][2] - self.colors[self.color_count+1][2])\
/ self.num_steps * self.cur_step
r = int(self.colors[self.color_count][0] - r_dif)
g = int(self.colors[self.color_count][1] - g_dif)
b = int(self.colors[self.color_count][2] - b_dif)
color = (r,g,b)
#color = self.colors[self.color_count]
cpx.pixels.fill(color)
self.cur_step += 1
if self.cur_step > self.num_steps:
self.cur_step = 0
self.color_count += 1
if self.color_count >= (self.num_colors):
self.color_count = 0
time.sleep(self.smooth_time)
def display_fill_pulse(self, random = False):
color = self.colors[self.color_count]
cpx.pixels.brightness = self.cur_brightness
""" r = int(color[0]/(1-self.cur_brightness))
g= int(color[1]/(1-self.cur_brightness))
b = int(color[2]/(1-self.cur_brightness))
color =(r,g,b)"""
cpx.pixels.fill(color)
step = (self.brightness)/ self.num_steps
if self.fade_in:
self.cur_brightness += step
else:
self.cur_brightness -= step
self.cur_step += 1
if self.cur_step > self.num_steps:
print(self.cur_brightness)
self.cur_step = 0
self.fade_in = not self.fade_in
if self.fade_in:
self.color_count += 1
if self.color_count >= (self.num_colors):
self.color_count = 0
time.sleep(self.smooth_time)
def display_auto(self):
if self.auto_patt == "spsm":
self.display_spin_smooth()
elif self.auto_patt == "sppu":
self.display_spin_pulse()
elif self.auto_patt == "rasm":
self.display_rand_smooth()
elif self.auto_patt == "rapu":
self.display_rand_pulse()
elif self.auto_patt == "fism":
self.display_fill_smooth()
else:
self.display_fill_pulse()
fire = NeopixelPattern()
fire.colors= (fire.red, fire.red, fire.red,fire.orange,fire.orange,
fire.orange,fire.yellow,fire.yellow,fire.yellow,fire.yellow)
fire.smooth_time = 0.3
fire.auto_patt = "rasm"
rainbow = NeopixelPattern()
siren = NeopixelPattern()
siren.colors = (siren.blue,siren.red)
siren.num_colors = 2
siren.smooth_time = 0.0001
siren.num_steps = 0.0007 / siren.smooth_time
siren.auto_patt = "fipu"
cool = NeopixelPattern()
cool.colors = (cool.green_blue,cool.blue,cool.blue_purple, cool.purple,
cool.red_purple)
cool.num_colors = 5
party = NeopixelPattern()
party.auto_patt = "fipu"
party.smooth_time = 0.0001
party.num_steps = 0.0005 / party.smooth_time
pattern_list = [rainbow,fire,siren, cool,party]
pattern = 0
while True:
pattern_list[pattern].display_auto()
if cpx.tapped:
pattern += 1
cpx.pixels.brightness = 0.3
if pattern >= 5:
pattern = 0