-
Notifications
You must be signed in to change notification settings - Fork 46
/
utils.h
166 lines (136 loc) · 4.43 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
// 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
CRGBPalette16 currentPalette(RainbowColors_p); // global palette storage
typedef void (*functionList)(); // definition for list of effect function pointers
extern const 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;
}
}
// 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);
}
// 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);
eepromOutdated = false;
}
}
}