-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.h
266 lines (210 loc) · 6.61 KB
/
utils.h
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Assorted useful functions and variables
// Global variables
boolean effectInit = false; // indicates if a pattern has been recently switched
uint16_t effectDelay = 0; // time between automatic effect changes
unsigned long effectMillis = 0; // store the time of last effect function run
unsigned long cycleMillis = 0; // store the time of last effect change
unsigned long currentMillis; // store current loop's millis value
unsigned long hueMillis; // store time of last hue change
unsigned long eepromMillis; // store time of last setting change
unsigned long audioMillis; // store time of last audio update
byte currentEffect = 0; // index to the currently running effect
boolean autoCycle = true; // flag for automatic effect changes
boolean eepromOutdated = false; // flag for when EEPROM may need to be updated
byte currentBrightness = STARTBRIGHTNESS; // 0-255 will be scaled to 0-MAXBRIGHTNESS
boolean audioEnabled = true; // flag for running audio patterns
boolean audioActive = false;
uint8_t fadeActive = 0;
CRGBPalette16 currentPalette(RainbowColors_p); // global palette storage
typedef void (*functionList)(); // definition for list of effect function pointers
extern byte numEffects;
// Increment the global hue value for functions that use it
byte cycleHue = 0;
byte cycleHueCount = 0;
void hueCycle(byte incr) {
cycleHueCount = 0;
cycleHue+=incr;
}
// Set every LED in the array to a specified color
void fillAll(CRGB fillColor) {
for (byte i = 0; i < NUM_LEDS; i++) {
leds[i] = fillColor;
}
}
// Fade every LED in the array by a specified amount
void fadeAll(byte fadeIncr) {
for (byte i = 0; i < NUM_LEDS; i++) {
leds[i] = leds[i].fadeToBlackBy(fadeIncr);
}
}
// Shift all pixels by one, right or left (0 or 1)
void scrollArray(byte scrollDir) {
byte scrollX = 0;
for (byte x = 1; x < kMatrixWidth; x++) {
if (scrollDir == 0) {
scrollX = kMatrixWidth - x;
} else if (scrollDir == 1) {
scrollX = x - 1;
}
for (byte y = 0; y < kMatrixHeight; y++) {
leds[XY(scrollX,y)] = leds[XY(scrollX + scrollDir*2 - 1,y)];
}
}
}
// Pick a random palette from a list
void selectRandomPalette() {
switch(random8(8)) {
case 0:
currentPalette = CloudColors_p;
break;
case 1:
currentPalette = LavaColors_p;
break;
case 2:
currentPalette = OceanColors_p;
break;
case 4:
currentPalette = ForestColors_p;
break;
case 5:
currentPalette = RainbowColors_p;
break;
case 6:
currentPalette = PartyColors_p;
break;
case 7:
currentPalette = HeatColors_p;
break;
}
}
// Pick a random palette from a list
void selectRandomAudioPalette() {
switch(random8(8)) {
case 0:
currentPalette = CRGBPalette16(CRGB::Red, CRGB::Orange, CRGB::Gray);
break;
case 1:
currentPalette = CRGBPalette16(CRGB::Blue, CRGB::Red, CRGB::Red);
break;
case 2:
currentPalette = CRGBPalette16(CRGB::LightGrey, CRGB::MidnightBlue, CRGB::Black);
break;
case 4:
currentPalette = CRGBPalette16(CRGB::DarkGreen, CRGB::PaleGreen);
break;
case 5:
currentPalette = RainbowColors_p;
break;
case 6:
currentPalette = PartyColors_p;
break;
case 7:
currentPalette = HeatColors_p;
break;
}
}
void selectRandomNoisePalette() {
switch(random8(4)) {
case 0:
currentPalette = CRGBPalette16(CRGB::Black, CRGB::Red, CRGB::Black, CRGB::Blue);
break;
case 1:
currentPalette = CRGBPalette16(CRGB::DarkGreen, CRGB::Black, CRGB::Green);
break;
case 2:
currentPalette = CRGBPalette16(CRGB(0,0,8), CRGB(0,0,16), CRGB(0,0,32), CRGB::White);
break;
case 3:
currentPalette = CRGBPalette16(CRGB(255,0,127), CRGB::Black, CRGB::OrangeRed);
break;
case 5:
currentPalette = RainbowColors_p;
break;
case 6:
currentPalette = PartyColors_p;
break;
case 7:
currentPalette = HeatColors_p;
break;
}
}
// Interrupt normal operation to indicate that auto cycle mode has changed
void confirmBlink(CRGB blinkColor, byte count) {
for (byte i = 0; i < count; i++) {
fillAll(blinkColor);
FastLED.show();
delay(200);
fillAll(CRGB::Black);
FastLED.show();
delay(200);
}
}
// Determine flash address of text string
unsigned int currentStringAddress = 0;
void selectFlashString(byte string) {
currentStringAddress = pgm_read_word(&stringArray[string]);
}
// Fetch font character bitmap from flash
byte charBuffer[5] = {0};
void loadCharBuffer(byte character) {
byte mappedCharacter = character;
if (mappedCharacter >= 32 && mappedCharacter <= 95) {
mappedCharacter -= 32; // subtract font array offset
} else if (mappedCharacter >= 97 && mappedCharacter <= 122) {
mappedCharacter -= 64; // subtract font array offset and convert lowercase to uppercase
} else {
mappedCharacter = 96; // unknown character block
}
for (byte i = 0; i < 5; i++) {
charBuffer[i] = pgm_read_byte(Font[mappedCharacter]+i);
}
}
// Fetch a character value from a text string in flash
char loadStringChar(byte string, byte character) {
return (char) pgm_read_byte(currentStringAddress + character);
}
// write EEPROM value if it's different from stored value
void updateEEPROM(byte location, byte value) {
if (EEPROM.read(location) != value) EEPROM.write(location, value);
}
// Write settings to EEPROM if necessary
void checkEEPROM() {
if (eepromOutdated) {
if (currentMillis - eepromMillis > EEPROMDELAY) {
updateEEPROM(0, 99);
updateEEPROM(1, currentEffect);
updateEEPROM(2, autoCycle);
updateEEPROM(3, currentBrightness);
updateEEPROM(4, audioEnabled);
eepromOutdated = false;
}
}
}
#define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
uint16_t scale = 72;
static uint16_t nx;
static uint16_t ny;
static uint16_t nz;
uint16_t nspeed = 0;
// Fill the x/y array of 8-bit noise values using the inoise8 function.
void fillnoise8() {
for(int i = 0; i < MAX_DIMENSION; i++) {
int ioffset = scale * i;
for(int j = 0; j < MAX_DIMENSION; j++) {
int joffset = scale * j;
noise[i][j] = inoise8(nx + ioffset,ny + joffset,nz);
}
}
nz += nspeed;
}
byte nextBrightness(boolean resetVal) {
const byte brightVals[6] = {32,64,96,160,224,255};
if (resetVal) {
currentBrightness = STARTBRIGHTNESS;
} else {
currentBrightness++;
if (currentBrightness > sizeof(brightVals)/sizeof(brightVals[0])) currentBrightness = 0;
}
return brightVals[currentBrightness];
}