Skip to content

Commit b41f69f

Browse files
authored
Merge pull request #877 from ladyada/master
adaptation of @jonsampson/
2 parents 4e82d4d + 0e41bf2 commit b41f69f

1 file changed

Lines changed: 289 additions & 0 deletions

File tree

M4_Eyes/user_touchneopixels.cpp

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

0 commit comments

Comments
 (0)