Skip to content

Commit 0e9e354

Browse files
authored
Create utils.h
1 parent ec1ce36 commit 0e9e354

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

  • GemmaM0_Band_Jacket/DiscoBandCamp
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Assorted useful functions and variables
2+
3+
// Global variables
4+
boolean effectInit = false; // indicates if a pattern has been recently switched
5+
uint16_t effectDelay = 0; // time between automatic effect changes
6+
unsigned long effectMillis = 0; // store the time of last effect function run
7+
unsigned long cycleMillis = 0; // store the time of last effect change
8+
unsigned long currentMillis; // store current loop's millis value
9+
unsigned long hueMillis; // store time of last hue change
10+
byte currentEffect = 0; // index to the currently running effect
11+
boolean autoCycle = true; // flag for automatic effect changes
12+
byte currentBrightness = STARTBRIGHTNESS; // 0-255 will be scaled to 0-MAXBRIGHTNESS
13+
14+
15+
// Setup information for the NoisePlusPalette noiseplay example
16+
#define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
17+
uint16_t speed = 5;
18+
uint16_t scale = 30;
19+
uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
20+
21+
// The 16 bit version of our coordinates
22+
static uint16_t x;
23+
static uint16_t y;
24+
static uint16_t z;
25+
uint8_t colorLoop = 1;
26+
27+
28+
CRGBPalette16 currentPalette(RainbowColors_p); // global palette storage
29+
30+
typedef void (*functionList)(); // definition for list of effect function pointers
31+
extern const byte numEffects;
32+
33+
// Increment the global hue value for functions that use it
34+
byte cycleHue = 0;
35+
byte cycleHueCount = 0;
36+
void hueCycle(byte incr) {
37+
cycleHueCount = 0;
38+
cycleHue+=incr;
39+
}
40+
41+
// Set every LED in the array to a specified color
42+
void fillAll(CRGB fillColor) {
43+
for (byte i = 0; i < NUM_LEDS; i++) {
44+
leds[i] = fillColor;
45+
}
46+
}
47+
48+
// Fade every LED in the array by a specified amount
49+
void fadeAll(byte fadeIncr) {
50+
for (byte i = 0; i < NUM_LEDS; i++) {
51+
leds[i] = leds[i].fadeToBlackBy(fadeIncr);
52+
}
53+
}
54+
55+
// Shift all pixels by one, right or left (0 or 1)
56+
void scrollArray(byte scrollDir) {
57+
58+
byte scrollX = 0;
59+
for (byte x = 1; x < kMatrixWidth; x++) {
60+
if (scrollDir == 0) {
61+
scrollX = kMatrixWidth - x;
62+
} else if (scrollDir == 1) {
63+
scrollX = x - 1;
64+
}
65+
66+
for (byte y = 0; y < kMatrixHeight; y++) {
67+
leds[XY(scrollX,y)] = leds[XY(scrollX + scrollDir*2 - 1,y)];
68+
}
69+
}
70+
71+
}
72+
73+
74+
// Pick a random palette from a list
75+
void selectRandomPalette() {
76+
77+
switch(random8(8)) {
78+
case 0:
79+
currentPalette = CloudColors_p;
80+
break;
81+
82+
case 1:
83+
currentPalette = LavaColors_p;
84+
break;
85+
86+
case 2:
87+
currentPalette = OceanColors_p;
88+
break;
89+
90+
case 4:
91+
currentPalette = ForestColors_p;
92+
break;
93+
94+
case 5:
95+
currentPalette = RainbowColors_p;
96+
break;
97+
98+
case 6:
99+
currentPalette = PartyColors_p;
100+
break;
101+
102+
case 7:
103+
currentPalette = HeatColors_p;
104+
break;
105+
}
106+
107+
}
108+
109+
// Fill the x/y array of 8-bit noise values using the inoise8 function.
110+
void fillnoise8() {
111+
// If we're runing at a low "speed", some 8-bit artifacts become visible
112+
// from frame-to-frame. In order to reduce this, we can do some fast data-smoothing.
113+
// The amount of data smoothing we're doing depends on "speed".
114+
uint8_t dataSmoothing = 0;
115+
if( speed < 50) {
116+
dataSmoothing = 200 - (speed * 4);
117+
}
118+
119+
for(int i = 0; i < MAX_DIMENSION; i++) {
120+
int ioffset = scale * i;
121+
for(int j = 0; j < MAX_DIMENSION; j++) {
122+
int joffset = scale * j;
123+
124+
uint8_t data = inoise8(x + ioffset,y + joffset,z);
125+
126+
// The range of the inoise8 function is roughly 16-238.
127+
// These two operations expand those values out to roughly 0..255
128+
// You can comment them out if you want the raw noise data.
129+
data = qsub8(data,16);
130+
data = qadd8(data,scale8(data,39));
131+
132+
if( dataSmoothing ) {
133+
uint8_t olddata = noise[i][j];
134+
uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
135+
data = newdata;
136+
}
137+
138+
noise[i][j] = data;
139+
}
140+
}
141+
142+
z += speed;
143+
144+
// apply slow drift to X and Y, just for visual variation.
145+
x += speed / 8;
146+
y -= speed / 16;
147+
}
148+
149+
void mapNoiseToLEDsUsingPalette()
150+
{
151+
static uint8_t ihue=0;
152+
153+
for(int i = 0; i < kMatrixWidth; i++) {
154+
for(int j = 0; j < kMatrixHeight; j++) {
155+
// We use the value at the (i,j) coordinate in the noise
156+
// array for our brightness, and the flipped value from (j,i)
157+
// for our pixel's index into the color palette.
158+
159+
uint8_t index = noise[j][i];
160+
uint8_t bri = noise[i][j];
161+
162+
// if this palette is a 'loop', add a slowly-changing base value
163+
if( colorLoop) {
164+
index += ihue;
165+
}
166+
167+
// brighten up, as the color palette itself often contains the
168+
// light/dark dynamic range desired
169+
if( bri > 127 ) {
170+
bri = 255;
171+
} else {
172+
bri = dim8_raw( bri * 2);
173+
}
174+
175+
CRGB color = ColorFromPalette( currentPalette, index, bri);
176+
leds[XY(i,j)] = color;
177+
}
178+
}
179+
180+
ihue+=1;
181+
}

0 commit comments

Comments
 (0)