Skip to content

Commit 463a898

Browse files
committed
use arcada for changing gradients n stuff
1 parent 4e82d4d commit 463a898

1 file changed

Lines changed: 298 additions & 0 deletions

File tree

M4_Eyes/user_touchneopixels.cpp

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
#if 0 // Change to 1 to enable this code (must enable ONE user*.cpp only!)
2+
3+
#include "globals.h"
4+
5+
static uint32_t lastBehaviorChange;
6+
static uint32_t lastWave;
7+
8+
static int currentBehavior = 0;
9+
10+
static int colorIndex = 0;
11+
12+
const uint32_t SAMPLE_TIME = 50000;
13+
static uint8_t currentStep = 100;
14+
static int direction = -3;
15+
static uint32_t lastColorShift;
16+
const int N_HEARTBEAT_FRAMES = 20;
17+
static uint8_t currentHeartBeatStep = 0;
18+
static uint8_t heartbeat[] = { 80, 80, 80, 80, 20,
19+
20, 120, 255, 255, 120,
20+
80, 40, 20, 0, 0,
21+
0, 0, 0, 0, 0 };
22+
23+
const int N_BREATH_IN_FRAMES = 45;
24+
static uint8_t currentBreathStep = 0;
25+
static uint16_t breathIn[] = { 7, 14, 21, 28, 35,
26+
42, 49, 56, 63, 70,
27+
77, 84, 91, 98, 105,
28+
112, 119, 126, 133, 140,
29+
147, 154, 161, 168, 175,
30+
182, 189, 196, 203, 210,
31+
217, 224, 231, 238, 255,
32+
255, 255, 255, 255, 255,
33+
255, 255, 255, 255, 255};
34+
35+
const int N_BREATH_OUT_FRAMES = 50;
36+
static uint16_t breathOut[] = { 100, 97, 94, 91, 88,
37+
85, 82, 79, 76, 73,
38+
70, 67, 64, 61, 58,
39+
55, 52, 49, 46, 43,
40+
40, 37, 34, 31, 28,
41+
25, 22, 19, 16, 13,
42+
10, 7, 4, 1, 0,
43+
0, 0, 0, 0, 0,
44+
0, 0, 0, 0, 0,
45+
0, 0, 0, 0, 0};
46+
47+
static int breathFrames = N_BREATH_IN_FRAMES;
48+
static uint16_t *breath = breathIn;
49+
50+
void setGradientPixelColor(uint16_t ledPosition, uint16_t step, float redRatio, float greenRatio, float blueRatio)
51+
{
52+
float green = step * greenRatio;
53+
float red = step * redRatio;
54+
float blue = step * blueRatio;
55+
arcada.pixels.setPixelColor(ledPosition, green, red, blue);
56+
}
57+
58+
void stepOrangeBlackGradient(uint16_t ledPosition, uint16_t step) {
59+
setGradientPixelColor(ledPosition, step, 1, 0.64453125, 0);
60+
}
61+
62+
void stepPurpleBlackGradient(uint16_t ledPosition, uint16_t step) {
63+
setGradientPixelColor(ledPosition, step, 0.8815, 0.4028, 1);
64+
}
65+
66+
void stepRedBlackGradient(uint16_t ledPosition, uint16_t step) {
67+
setGradientPixelColor(ledPosition, step, 1, 0, 0);
68+
}
69+
70+
void stepYellowBlackGradient(uint16_t ledPosition, uint16_t step) {
71+
setGradientPixelColor(ledPosition, step, 1, 1, 0);
72+
}
73+
74+
void stepWhiteBlackGradient(uint16_t ledPosition, uint16_t step) {
75+
setGradientPixelColor(ledPosition, step, 1, 1, 1);
76+
}
77+
78+
void stepRedOrangeGradient(uint16_t ledPosition, uint16_t step) {
79+
int red = 255;
80+
float green = step * .647;
81+
int blue = 0;
82+
arcada.pixels.setPixelColor(ledPosition, green, red, blue);
83+
}
84+
85+
void stepOrangeYellowGradient(uint16_t ledPosition, uint16_t step) {
86+
int red = 255;
87+
float green = 165 + (step % 90);
88+
int blue = 0;
89+
arcada.pixels.setPixelColor(ledPosition, green, red, blue);
90+
}
91+
92+
void stepYellowGreenGradient(uint16_t ledPosition, uint16_t step) {
93+
int red = 255 - step * 2;
94+
float green = 255 - (step * 1.5);
95+
int blue = 0;
96+
arcada.pixels.setPixelColor(ledPosition, green, red, blue);
97+
}
98+
99+
void stepGreenBlueGradient(uint16_t ledPosition, uint16_t step) {
100+
int red = 0;
101+
float green = 128 - step;
102+
int blue = step;
103+
arcada.pixels.setPixelColor(ledPosition, green, red, blue);
104+
}
105+
106+
void stepBlueIndigoGradient(uint16_t ledPosition, uint16_t step) {
107+
int red = step;
108+
float green = 0;
109+
int blue = 255 - step;
110+
arcada.pixels.setPixelColor(ledPosition, green, red, blue);
111+
}
112+
113+
void stepIndigoVioletGradient(uint16_t ledPosition, uint16_t step) {
114+
int red = 75+step;
115+
float green = step;
116+
int blue = 130+step;
117+
arcada.pixels.setPixelColor(ledPosition, green, red, blue);
118+
}
119+
120+
void stepVioletRedGradient(uint16_t ledPosition, uint16_t step) {
121+
int red = 238+(step*.17);
122+
float green = 130 - step;
123+
int blue = 238 - (step * 2);
124+
arcada.pixels.setPixelColor(ledPosition, green, red, blue);
125+
}
126+
127+
typedef void (*StepColorGradient)(uint16_t, uint16_t);
128+
129+
static uint16_t rainbowWalkLEDSteps[] = {5, 6, 1, 4};
130+
const int N_RAINBOW_GRADIENTS = 7;
131+
// ROY G BIV
132+
static StepColorGradient rainbowWalkGradients[] = { &stepRedOrangeGradient,
133+
&stepOrangeYellowGradient,
134+
&stepYellowGreenGradient,
135+
&stepGreenBlueGradient,
136+
&stepBlueIndigoGradient,
137+
&stepIndigoVioletGradient,
138+
&stepVioletRedGradient,
139+
};
140+
static uint32_t lastRainbowColorShift;
141+
142+
void advanceRainbowWalk(void) {
143+
uint16_t i;
144+
for(i = 0; i < arcada.pixels.numPixels(); i++) {
145+
rainbowWalkGradients[rainbowWalkLEDSteps[i]](i, currentStep);
146+
}
147+
currentStep = currentStep + 25;
148+
if(currentStep > 100) {
149+
currentStep = 0;
150+
for(i = 0; i < arcada.pixels.numPixels(); i++) {
151+
rainbowWalkLEDSteps[i] = rainbowWalkLEDSteps[i] + 1;
152+
if(rainbowWalkLEDSteps[i] >= N_RAINBOW_GRADIENTS) {
153+
rainbowWalkLEDSteps[i] = 0;
154+
}
155+
}
156+
}
157+
arcada.pixels.show();
158+
}
159+
160+
static StepColorGradient breathGradient = &stepRedBlackGradient;
161+
162+
void advanceBreath(void)
163+
{
164+
uint16_t i;
165+
for(i = 0; i < arcada.pixels.numPixels(); i++) {
166+
breathGradient(i, breath[currentBreathStep]);
167+
}
168+
arcada.pixels.show();
169+
170+
if(currentBreathStep == breathFrames - 1) {
171+
currentBreathStep = 0;
172+
if(breath == breathIn) {
173+
breath = breathOut;
174+
breathFrames = N_BREATH_OUT_FRAMES;
175+
breathGradient = &stepPurpleBlackGradient;
176+
}
177+
else {
178+
breath = breathIn;
179+
breathFrames = N_BREATH_IN_FRAMES;
180+
breathGradient = &stepRedBlackGradient;
181+
}
182+
}
183+
else {
184+
currentBreathStep = currentBreathStep + 1;
185+
}
186+
}
187+
188+
void advanceHalloweenGradients(void) {
189+
int i;
190+
for(i = 0; i < arcada.pixels.numPixels(); i++) {
191+
switch (colorIndex)
192+
{
193+
case 0:
194+
stepOrangeBlackGradient(i, currentStep);
195+
break;
196+
case 1:
197+
stepRedBlackGradient(i, currentStep);
198+
break;
199+
case 2:
200+
stepYellowBlackGradient(i, currentStep);
201+
break;
202+
default:
203+
stepWhiteBlackGradient(i, currentStep);
204+
break;
205+
}
206+
}
207+
arcada.pixels.show();
208+
209+
if(currentStep <= 0) {
210+
direction = 3;
211+
// shift to next color
212+
colorIndex = colorIndex + 1;
213+
if(colorIndex >= arcada.pixels.numPixels()) {
214+
colorIndex = 0;
215+
}
216+
}
217+
else if(currentStep >= 100) {
218+
direction = -3;
219+
}
220+
currentStep = currentStep + direction;
221+
}
222+
223+
void advanceHeartBeat(void) {
224+
int i;
225+
for(i = 0; i < arcada.pixels.numPixels(); i++) {
226+
stepRedBlackGradient(i, heartbeat[currentHeartBeatStep]);
227+
}
228+
arcada.pixels.show();
229+
230+
if(currentHeartBeatStep == N_HEARTBEAT_FRAMES - 1) {
231+
currentHeartBeatStep = 0;
232+
} else {
233+
currentHeartBeatStep = currentHeartBeatStep + 1;
234+
}
235+
}
236+
237+
void user_setup(void) {
238+
lastBehaviorChange, lastWave, lastColorShift, lastRainbowColorShift = micros();
239+
arcada.pixels.setBrightness(255);
240+
advanceHalloweenGradients();
241+
}
242+
243+
void changeBehavior(int newBehavior, uint32_t timeOfChange) {
244+
currentBehavior = newBehavior;
245+
lastBehaviorChange = timeOfChange;
246+
}
247+
248+
249+
void user_loop(void) {
250+
uint8_t pressed_buttons = arcada.readButtons();
251+
uint8_t justpressed_buttons = arcada.justPressedButtons();
252+
253+
uint32_t elapsedSince = micros();
254+
if (justpressed_buttons & ARCADA_BUTTONMASK_UP){
255+
changeBehavior(0, elapsedSince);
256+
}
257+
else if(justpressed_buttons & ARCADA_BUTTONMASK_DOWN) {
258+
changeBehavior(1, elapsedSince);
259+
}
260+
else if(justpressed_buttons & ARCADA_BUTTONMASK_LEFT) {
261+
changeBehavior(2, elapsedSince);
262+
}
263+
else if(justpressed_buttons & ARCADA_BUTTONMASK_RIGHT) {
264+
changeBehavior(3, elapsedSince);
265+
}
266+
267+
if (elapsedSince - lastWave > SAMPLE_TIME) {
268+
lastWave = elapsedSince;
269+
switch (currentBehavior) {
270+
case 0:
271+
advanceHalloweenGradients();
272+
break;
273+
case 1:
274+
advanceHeartBeat();
275+
break;
276+
case 2:
277+
advanceBreath();
278+
break;
279+
case 3:
280+
advanceRainbowWalk();
281+
break;
282+
case 20:
283+
changeBehavior(0, elapsedSince);
284+
break;
285+
default:
286+
arcada.pixels.fill(0, 0, 0);
287+
arcada.pixels.show();
288+
break;
289+
}
290+
291+
if(elapsedSince - lastBehaviorChange > SAMPLE_TIME * 500) {
292+
lastBehaviorChange = elapsedSince;
293+
currentBehavior++;
294+
}
295+
}
296+
}
297+
298+
#endif

0 commit comments

Comments
 (0)