-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsMode.h
More file actions
116 lines (77 loc) · 2.65 KB
/
SettingsMode.h
File metadata and controls
116 lines (77 loc) · 2.65 KB
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
#include <FastLed.h>
// These should be static members but that would require a cpp file
static uint8_t _userLevel = 1;
static bool _exitingSettings = false;
class SettingsMode
{
public:
SettingsMode(Button* button) :
_button(button) {
}
void showSettings() {
PRINTX("Settings Mode - Current brightness ", FastLED.getBrightness());
init();
const uint8_t incrementLeds = NUM_LEDS / MAX_LEVEL;
const uint8_t incrementBrightness = MAX_BRIGHTNESS / MAX_LEVEL;
// Default to minimum brightness
_userLevel = 1;
_exitingSettings = false;
while (!_exitingSettings) {
_button->tick();
fadeToBlackBy(leds, STRIP_SIZE, 255);
// For each level until the selected one
for(uint8_t i = 1; i <= _userLevel; i++) {
// Draw incrementLeds pixels with its relevant brightness
uint8_t incrementStart = (i-1) * incrementLeds;
for (uint8_t x = 0; x < incrementLeds; ++x) {
uint8_t index = incrementStart + x;
leds[(STRIP_SIZE - 1) - index] = CHSV(43*x, 255, i*incrementBrightness);
}
}
delay_at_max_brightness_for_power(100);
show_at_max_brightness_for_power();
}
PRINT("Exiting settings");
exit();
}
static uint8_t getUserBrightness() {
return _userLevel * (MAX_BRIGHTNESS/MAX_LEVEL);
}
static void onSettingsClick() {
// Increase userLevel
_userLevel = ++_userLevel % (MAX_LEVEL + 1);
// Bring up userLevel if under the minimum
while ((_userLevel * (MAX_BRIGHTNESS / MAX_LEVEL) ) < MIN_BRIGHTNESS) {
_userLevel += 1;
}
PRINTX("New user level:", _userLevel);
PRINTX("Which sets a brightness of:", SettingsMode::getUserBrightness());
}
static void onSettingsLongPressStop() {
// Exit settings
PRINT("Exit clicked");
_exitingSettings = true;
}
protected:
void init() {
// TODO Would be cleaner to remove/disable all other button handlers
// to prevent unexpected behavior
_previousClickHandler = _button->_clickFunc;
_previousLongPressStopHandler = _button->_longPressStopFunc;
_button->attachClick(onSettingsClick);
_button->attachLongPressStop(onSettingsLongPressStop);
_button->flush();
}
void exit() {
_button->attachClick(_previousClickHandler);
_button->attachLongPressStop(_previousLongPressStopHandler);
_button->flush();
}
public:
Button* _button;
callbackFunction _previousClickHandler;
callbackFunction _previousLongPressStopHandler;
static const uint8_t MAX_LEVEL = 10;
static const uint8_t MAX_BRIGHTNESS = 255;
static const uint8_t MIN_BRIGHTNESS = 25;
};