forked from wled-dev/WLED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathryan_usermod.h
426 lines (353 loc) · 15.2 KB
/
ryan_usermod.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#pragma once
#include "wled.h"
#include <Arduino.h>
#include <U8x8lib.h>
#include <string>
#include <sstream>
// Main loop macros
#define LOOP_POLL_DELAY_MS 2
// OLED macros
#define OLED_PIN_SCL 5 // D1
#define OLED_PIN_SDA 4 // D2
#define OLED_SLEEP_AFTER_MS 5 * 60 * 1000
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(U8X8_PIN_NONE, OLED_PIN_SCL, OLED_PIN_SDA);
// Rotary encoder macros
#define CLK_PIN 14 // D6
#define DT_PIN 12 // D5
#define SW_PIN 13 //D7
#define BUTTON_PRESS_DEBOUNCE_MS 1000 / 4
// Effect macros
#define NUM_EFFECTS 118
#define EFFECT_INDEX_START 2
#define MAX_EFFECT_NAME_PARTS 3 // The max potential number of space-separated words in a given effect name.
// Palette macros
#define NUM_PALETTES 71
#define PALETTE_INDEX_START 6
// Font macros
#define STARTUP_FONT u8x8_font_victoriabold8_u
#define GENERAL_FONT u8x8_font_victoriabold8_r
// Screen macros
#define NUM_SCREENS 3
#define TOTAL_NUM_OPTIONS 6
#define INTENSITY_SCROLL_STEP 5
class RyanUsermod : public Usermod
{
private:
unsigned long lastLoopTime;
unsigned long loopTime;
int effectIndex = EFFECT_INDEX_START;
const int8_t bannedEffects[12] = { 0, 1, 32, 50, 58, 62, 82, 83, 84, 96, 98, 116 };
int paletteIndex = PALETTE_INDEX_START;
const int8_t bannedPalettes[6] = { 0, 1, 2, 3, 4, 5 };
unsigned char encoderAPrev = 0;
unsigned char buttonState = HIGH;
unsigned long lastButtonPressTime = 0;
long lastOledUpdate = 0;
bool oledTurnedOff = true;
const int8_t numOptionsPerScreen[3] = { 2, 3, 1 };
bool optionSelected = false;
int optionIndex = 0;
int brightnessIndex = 5;
int speedIndex = 5;
int intensityPercent = 50;
public:
/**
* @brief Called once at boot.
*/
void setup()
{
pinMode(CLK_PIN, INPUT_PULLUP);
pinMode(DT_PIN, INPUT_PULLUP);
pinMode(SW_PIN, INPUT_PULLUP);
lastLoopTime = millis();
loopTime = lastLoopTime;
// Start with the matrix on black (off).
CRGB fastled_col;
col[0] = fastled_col.Black;
col[1] = fastled_col.Black;
col[2] = fastled_col.Black;
effectCurrent = 0;
effectPalette = paletteIndex;
colorUpdated(CALL_MODE_NO_NOTIFY);
updateInterfaces(CALL_MODE_NO_NOTIFY);
u8x8.begin();
u8x8.setPowerSave(0);
u8x8.setContrast(255);
u8x8.setFont(STARTUP_FONT);
std::string startupHeader = "LED MATRIX";
u8x8.drawString(0, 0, startupHeader.c_str());
uint8_t tiles[8] = { 1, 1, 1, 1, 1, 1, 1, 1 };
int startupHeaderLength = startupHeader.length();
for (int i = 0; i < startupHeaderLength; ++i) // Draws a thin underline to the header.
{
u8x8.drawTile(i, 2, 1, tiles);
}
u8x8.drawString(0, 3, "CLICK TO");
u8x8.drawString(0, 5, "BEGIN.");
u8x8.setFont(GENERAL_FONT);
}
/**
* @brief Called continuously. For reading events, reading sensors, etc.
*/
void loop()
{
if (millis() - lastLoopTime > LOOP_POLL_DELAY_MS)
{
int encoderA = digitalRead(DT_PIN);
int encoderB = digitalRead(CLK_PIN);
buttonState = digitalRead(SW_PIN);
if (buttonState == LOW && oledTurnedOff) // Allow for button press down to wake OLED when asleep.
{
lastButtonPressTime = millis();
u8x8.setPowerSave(0);
oledTurnedOff = false;
u8x8.clear();
updateOled();
return;
}
else if (buttonState == LOW && millis() - lastButtonPressTime > BUTTON_PRESS_DEBOUNCE_MS) // Button pressed down to select an option.
{
lastButtonPressTime = millis();
optionSelected = !optionSelected;
updateOled();
}
else if (!encoderA && encoderAPrev) // If knob rotated.
{
if (oledTurnedOff) // Allow for rotation to wake OLED when asleep.
{
u8x8.setPowerSave(0);
oledTurnedOff = false;
u8x8.clear();
updateOled();
return;
}
bool clockwise = (encoderB == HIGH);
if (optionSelected)
{
if (optionIndex == 0) // Effect
{
clockwise ? ++effectIndex : --effectIndex;
int numBanned = sizeof(bannedEffects) / sizeof(*bannedEffects);
while (std::find(bannedEffects, bannedEffects + numBanned, effectIndex) != bannedEffects + numBanned)
clockwise ? ++effectIndex : --effectIndex;
if (clockwise && effectIndex >= NUM_EFFECTS)
effectIndex = EFFECT_INDEX_START;
else if (!clockwise && effectIndex < 0)
effectIndex = NUM_EFFECTS - 1;
effectCurrent = effectIndex;
strip.restartRuntime();
colorUpdated(CALL_MODE_NO_NOTIFY);
}
else if (optionIndex == 1) // Palette
{
clockwise ? ++paletteIndex : --paletteIndex;
int numBanned = sizeof(bannedPalettes) / sizeof(*bannedPalettes);
while (std::find(bannedPalettes, bannedPalettes + numBanned, paletteIndex) != bannedPalettes + numBanned)
clockwise ? ++paletteIndex : --paletteIndex;
if (clockwise && paletteIndex >= NUM_EFFECTS)
paletteIndex = EFFECT_INDEX_START;
else if (!clockwise && paletteIndex < 0)
paletteIndex = NUM_EFFECTS - 1;
effectPalette = paletteIndex;
colorUpdated(CALL_MODE_NO_NOTIFY);
}
else if (optionIndex == 2) // Brightness
{
clockwise ? ++brightnessIndex : --brightnessIndex;
if (brightnessIndex > 10)
brightnessIndex = 10;
else if (brightnessIndex < 0)
brightnessIndex = 0;
bri = quantitativeIndexToRange(brightnessIndex);
stateUpdated(CALL_MODE_NO_NOTIFY);
}
else if (optionIndex == 3) // Speed
{
clockwise ? ++speedIndex : --speedIndex;
if (speedIndex > 10)
speedIndex = 10;
else if (speedIndex < 0)
speedIndex = 0;
effectSpeed = quantitativeIndexToRange(speedIndex);
strip.restartRuntime();
colorUpdated(CALL_MODE_NO_NOTIFY);
}
else if (optionIndex == 4) // Intensity
{
intensityPercent += clockwise ? INTENSITY_SCROLL_STEP : -INTENSITY_SCROLL_STEP;
if (intensityPercent > 100)
intensityPercent = 100;
else if (intensityPercent < 0)
intensityPercent = 0;
effectIntensity = percentToValue(intensityPercent);
colorUpdated(CALL_MODE_NO_NOTIFY);
}
updateInterfaces(CALL_MODE_NO_NOTIFY);
}
else
{
(clockwise) ? ++optionIndex : --optionIndex;
if (clockwise && optionIndex >= TOTAL_NUM_OPTIONS)
optionIndex = 0;
if (!clockwise && optionIndex < 0)
optionIndex = TOTAL_NUM_OPTIONS - 1;
}
updateOled();
}
if (!oledTurnedOff && millis() - lastOledUpdate > OLED_SLEEP_AFTER_MS) // Sleep OLED if inactive.
{
u8x8.setPowerSave(1);
oledTurnedOff = true;
}
encoderAPrev = encoderA;
lastLoopTime = millis();
}
}
/**
* Updates the OLED to show the updated mode or value
* when the rotary encoder is turned.
*/
void updateOled()
{
lastOledUpdate = millis();
u8x8.clear();
if (effectCurrent != effectIndex) // Handle resuming after startup on mode 0 (1/2).
{
effectCurrent = effectIndex;
colorUpdated(CALL_MODE_NO_NOTIFY);
updateInterfaces(CALL_MODE_NO_NOTIFY);
}
int screenIndex = 0;
int optionCount = 0;
for (int i = 0; i < NUM_SCREENS; ++i) // Calculate screenIndex from optionIndex.
{
int numOptions = numOptionsPerScreen[i];
if (optionCount + numOptions >= optionIndex + 1)
{
screenIndex = i;
break;
}
else
{
optionCount += numOptions;
}
}
if (screenIndex == 0) // Effect and palette screen.
{
std::string effectLabel = " EFFECT: ";
if (optionIndex == 0)
effectLabel = ">EFFECT: ";
if (optionIndex == 0 && optionSelected)
u8x8.setInverseFont(1);
u8x8.drawString(0, 0, effectLabel.c_str());
u8x8.setInverseFont(0);
u8x8.drawString(0, 2, typeConcat(" ", getCurrentEffectOrPaletteName(true).c_str()).c_str());
std::string paletteLabel = " PALETTE: ";
if (optionIndex == 1)
paletteLabel = ">PALETTE: ";
if (optionIndex == 1 && optionSelected)
u8x8.setInverseFont(1);
u8x8.drawString(0, 5, paletteLabel.c_str());
u8x8.setInverseFont(0);
u8x8.drawString(0, 7, typeConcat(" ", getCurrentEffectOrPaletteName(false).c_str()).c_str());
}
else if (screenIndex == 1) // Brightness, speed, and intensity screen.
{
std::string brightnessLabel = " BRI: ";
if (optionIndex == 2)
brightnessLabel = ">BRI: ";
if (optionIndex == 2 && optionSelected)
u8x8.setInverseFont(1);
u8x8.drawString(0, 1, typeConcat(brightnessLabel.c_str(), typeConcat(brightnessIndex, "/10").c_str()).c_str());
u8x8.setInverseFont(0);
std::string speedLabel = " SPEED: ";
if (optionIndex == 3)
speedLabel = ">SPEED: ";
if (optionIndex == 3 && optionSelected)
u8x8.setInverseFont(1);
u8x8.drawString(0, 3, typeConcat(speedLabel.c_str(), typeConcat(speedIndex, "/10").c_str()).c_str());
u8x8.setInverseFont(0);
std::string intensityLabel = " OPTION: ";
if (optionIndex == 4)
intensityLabel = ">OPTION: ";
if (optionIndex == 4 && optionSelected)
u8x8.setInverseFont(1);
u8x8.drawString(0, 5, typeConcat(intensityLabel.c_str(), typeConcat(intensityPercent, "%").c_str()).c_str());
u8x8.setInverseFont(0);
}
else if (screenIndex == 2) // Save default screen.
{
// TODO:
u8x8.drawString(0, 0, "save TODO uwu");
}
}
/**
* @brief Get the Current Effect Or Palette Name object
*
* @param type True when effect, false when palette.
* @return String The current effect or palette name.
*/
std::string getCurrentEffectOrPaletteName(bool type) // effect when true, palette when false.
{
char lineBuffer[21];
const char* relatedJSON = type ? JSON_mode_names : JSON_palette_names;
const uint8_t currentEffectOrMode = type ? effectCurrent : effectPalette;
uint8_t printedChars = extractModeName(currentEffectOrMode, relatedJSON, lineBuffer, 19);
if (lineBuffer[0] == '*' && lineBuffer[1] == ' ')
{
for (byte i = 2; i <= printedChars; i++)
lineBuffer[i - 2] = lineBuffer[i]; //include '\0'
printedChars -= 2;
}
return lineBuffer;
}
/**
* Concat 2 datatypes (mostly "int/const char*" and "std::string/const char*" pairs)
* into a single std::string.
*
* @tparam T The datatype of the second parameter.
* @tparam U The datatype of the second parameter.
* @param a The value of the first argument.
* @param b The value of the second argument.
* @return std::string The concatinated result.
*/
template<class T, class U>
std::string typeConcat(T a, U b)
{
std::ostringstream oss;
oss << a;
oss << b;
return oss.str();
}
/**
* @brief Convert a value in [0, 10] to be a value in the range of [0, 255].
*
* @param index The brightness, speed, or intensity index.
* @return int The index value scaled to be in [0, 255].
*/
int quantitativeIndexToRange(int index)
{
return (index * 255) / (10) + 0;
}
/**
* @brief Convert a percent to a value in the range of [0, 255].
*
* @param percent The percent to convert into a value.
* @return int The converted value.
*/
int percentToValue(int percent)
{
return floor(255 * (percent / 100.0));
}
// /**
// * @brief Convert a value in the range of [0, 255] to a percentage.
// *
// * @param value The value to convert to a percentage.
// * @return std::string The calculated value formatted to X% notation.
// */
// std::string quantitativeToPercentString(int value)
// {
// int calculated = (value * 100) / 255;
// return typeConcat(calculated, "%");
// }
};