-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
254 lines (206 loc) · 6.27 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
// 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
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 initialized = false; // switch to true when startup tasks are finished
uint8_t fadeActive = 0;
byte runMode = 0;
byte repCount = 0;
CRGB fadeBaseColor = CRGB::Black;
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);
}
}
void fadeTo(CRGB basecolor, byte fadeIncr) {
for (byte i = 0; i < NUM_LEDS; i++) {
leds[i] = leds[i].fadeToBlackBy(fadeIncr);
leds[i] |= basecolor;
}
}
// 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 selectNormalPalette() {
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;
}
}
void selectValentinePalette() {
switch(random8(3)) {
case 0:
currentPalette = CRGBPalette16(CRGB::Black, CRGB(255,64,54), CRGB::Red, CRGB::Red);
break;
case 1:
currentPalette = CRGBPalette16(CRGB::Black, CRGB::Crimson);
break;
case 2:
currentPalette = CRGBPalette16(CRGB::Black, CRGB::Red, CRGB::Black, CRGB::Red);
break;
}
}
void selectRandomPalette() {
if (runMode > 0) selectValentinePalette();
else selectNormalPalette();
}
#define NORMAL 0
#define RAINBOW 1
#define PALETTEWORDS 2
#define HOLLY 3
#define CANDYCANE 4
#define HOLLY2 5
const CRGB candycane[2] = {CRGB::Red, CRGB::Gray};
const CRGB holly[2] = {CRGB::Red, CRGB::Green};
CRGB colorCycle(byte colorGroup) {
static byte colorIndex = 0;
switch(colorGroup) {
case CANDYCANE:
{
colorIndex++;
if (colorIndex > 1) colorIndex = 0;
return candycane[colorIndex];
}
break;
case HOLLY:
case HOLLY2:
{
colorIndex++;
if (colorIndex > 1) colorIndex = 0;
return holly[colorIndex];
}
break;
default:
return CRGB::Black;
break;
}
}
void cyclePattern() {
cycleMillis = currentMillis;
if (++currentEffect >= numEffects) currentEffect = 0; // loop to start of effect list
effectInit = false; // trigger effect initialization when new effect is selected
fadeActive = 0;
repCount = 0;
}
// Interrupt normal operation to indicate that auto cycle mode has changed
void confirmBlink() {
if (autoCycle) { // one blue blink, auto mode active
fillAll(CRGB::DarkBlue);
FastLED.show();
FastLED.delay(200);
fillAll(CRGB::Black);
FastLED.delay(200);
} else { // two red blinks, manual mode active
fillAll(CRGB::DarkRed);
FastLED.show();
FastLED.delay(200);
fillAll(CRGB::Black);
FastLED.delay(200);
fillAll(CRGB::DarkRed);
FastLED.show();
FastLED.delay(200);
fillAll(CRGB::Black);
FastLED.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);
}
void saveEEPROMvals() {
updateEEPROM(0, 99);
updateEEPROM(1, currentEffect);
updateEEPROM(2, autoCycle);
updateEEPROM(3, currentBrightness);
updateEEPROM(4, runMode);
}
// Write settings to EEPROM if necessary
void checkEEPROM() {
if (eepromOutdated) {
if (currentMillis - eepromMillis > EEPROMDELAY) {
saveEEPROMvals();
eepromOutdated = false;
}
}
}