forked from diodeface/ToasterBlaster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
334 lines (279 loc) · 11.6 KB
/
main.cpp
File metadata and controls
334 lines (279 loc) · 11.6 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
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
/**
* Adeon's Toaster Blaster
* ---------------------------------------------------------------------------
* This is a LED control program for protogen helmets to be used with MAX7219
* LED matrixes and ESP32 dev boards. Keyframe animation system allows you
* to create animations with ease. Facial expressions are controlled with an
* inexpensive bluetooth controller (MOCUTE-052F), with more gamepads to be
* supported in the future.
*
* This is a pre-release with a some bugs, unfinished code and
* lacking documentation. Use at your own risk.
*/
#include <Arduino.h>
#include "main.h"
DisplayManager* displayManager;
SequencePlayer* sequencePlayer;
Controller* controller;
TaskHandle_t asyncLoopTask;
Timestamp lastTime;
#ifdef ENABLE_ANIMATED_MOUTH
AnimatedMouth* animatedMouth;
#endif
SettingsManager* settingsManager;
HeadsUpDisplay* hud;
TweenManager* tweenManager;
AnalogBoopSensor* boopSensor;
EyeBlink* eyeBlink;
LEDStrip* ledStrip;
UptimeCounter* uptimeCounter;
PWMFan* fanControl;
void setup() {
TOASTER_LOG("\n\n\n\nStarting up...\n");
randomSeed(analogRead(0));
EEPROM.begin(16);
hud = new U8G2HUDWrapper(Config::Pins::HUD_SCL, Config::Pins::HUD_SDA);
displayManager = new DisplayManager(
new Max7219(Config::Pins::MATRIX_DIN, Config::Pins::MATRIX_CS, Config::Pins::MATRIX_CLK), hud, EEPROM.read(Config::EEPROM::MATRIX)
);
tweenManager = new TweenManager(displayManager);
// Register displays (matrix position, number of rows (size), bitmask, hud preview position)
// Simply comment out any displays that are not in use.
using namespace Config::Displays;
displayManager->addDisplays({
new Display(POS_EYE_L, 16, EYE_L, {49, 0}, tweenManager),
new Display(POS_EYE_R, 16, EYE_R, {1, 0}, tweenManager),
new Display(POS_MOUTH_L, 32, MOUTH_L, {34, 8}, tweenManager),
new Display(POS_MOUTH_R, 32, MOUTH_R, {0, 8}, tweenManager),
new Display(POS_NOSE_L, 8, NOSE_L, {35, 0}, tweenManager),
new Display(POS_NOSE_R, 8, NOSE_R, {23, 0}, tweenManager),
});
// To add persistent rotation or other effects to your displays, add a global effect:
//displayManager->addGlobalEffect(new Rotate180(ALL));
ledStrip = new LEDStrip(EEPROM.read(Config::EEPROM::LEDSTRIP));
#ifdef BT_CONTROLLER
BLEGamepad::init();
createButtonMapping();
#endif
eyeBlink = new EyeBlink(
displayManager,
&Overlays::EyeBlink::closing,
&Overlays::EyeBlink::closed,
&Overlays::EyeBlink::opening,
5 SECONDS,
20 SECONDS,
EEPROM.read(Config::EEPROM::AUTO_BLINK)
);
sequencePlayer = new SequencePlayer(displayManager, tweenManager, ledStrip, eyeBlink, &Sequences::startup, EEPROM.read(Config::EEPROM::RARE_TRANSITION_CHANCE), true);
sequencePlayer->addCommonTransitions({
&Transitions::blink,
&Transitions::crossfade
});
sequencePlayer->addRareTransitions({
&Transitions::drop,
&Transitions::slide,
&Transitions::losePower,
&Transitions::glitch,
//&Transitions::expand, // having all the LEDs on may cause a voltage drop and crash the ESP32
&Transitions::explode,
//&Transitions::shuffle,
&Transitions::fizz,
&Transitions::doomMelt,
});
#ifdef ENABLE_ANIMATED_MOUTH
animatedMouth = new AnimatedMouth(displayManager, 33 MILLIS, 500, 500, EEPROM.read(Config::EEPROM::ANIMATED_MOUTH));
#endif
/*
boopSensor = new DigitalBoopSensor(
displayManager, new OverlayPlayer(displayManager, &Overlays::boop, false, false), &Overlays::boop,
&Transitions::glitch, PIN_BOOP_DIGITAL, BOOP_ACTIVATION_THRESHOLD, BOOP_ACTIVATION_MAX,
EEPROM.read(EEPROM_BOOP_SENSOR)
);
*/
boopSensor = new AnalogBoopSensor(
displayManager, new OverlayPlayer(displayManager, &Overlays::boop, false, false),
&Overlays::boop, // enable overlay
&Transitions::glitch, // disable overlay
Config::Pins::BOOP_ANALOG, Config::BOOP_TRIGGER_COUNT, Config::BOOP_TRIGGERS_MAX,
(f32)EEPROM.read(Config::EEPROM::BOOP_TRIGGER_MULTIPLIER) * 0.01f, // trigger multiplier
200, // calibration readings
false, // invert
EEPROM.read(Config::EEPROM::BOOP_SENSOR) // enable
);
fanControl = new PWMFan(Config::Pins::FAN_PWM, Config::FAN_PWM_CHANNEL, EEPROM.read(Config::EEPROM::FAN_PWM_SPEED));
createSettingsMenu();
controller = new StartupController(displayManager, 4 SECONDS, [](){changeController(new SettingsController(displayManager, settingsManager));});
uptimeCounter = new UptimeCounter();
xTaskCreatePinnedToCore(loopAsync, "loopAsync", 2048, NULL, 1, &asyncLoopTask, 1);
}
// Main program loop
void loop(){
// calculate delta time
deltaTime = (Timestamp)(micros() - lastTime);
if(deltaTime < Config::FRAMETIME) return;
lastTime = micros();
#ifdef BT_CONTROLLER
BLEGamepad::update();
#endif
sequencePlayer->update();
controller->update();
tweenManager->update();
boopSensor->update();
eyeBlink->update();
ledStrip->update();
uptimeCounter->update();
#ifdef ENABLE_ANIMATED_MOUTH
animatedMouth->update();
#endif
displayManager->update();
}
// Components to be updated independently on core 1
void loopAsync(void* pvParameters) {
while(true) {
hud->update();
}
}
// Controller related functions
void changeController(Controller* newController) {
delete controller;
switch(newController->getSequencePlayerState()){
case SP_PLAY: sequencePlayer->start(); break;
case SP_STOP: sequencePlayer->stop(); break;
}
controller = newController;
}
void handleJoystickInput(u8 x, u8 y) {
BT_LOG("Joystick input (%d, %d)\n", x, y);
controller->handleInput(x, y);
}
void createButtonMapping() { // Change all mapped key inputs here
BLEGamepad::inputHandler.mapButtonsToJoystick(BTN_UP, BTN_DOWN, BTN_LEFT, BTN_RIGHT, handleJoystickInput);
BLEGamepad::inputHandler.mapButtonsToJoystick(BTN_ALT_UP, BTN_ALT_DOWN, BTN_ALT_LEFT, BTN_ALT_RIGHT, handleJoystickInput);
BLEGamepad::inputHandler.mapButtonsToJoystick(BTN_ALT_X, BTN_ALT_B, BTN_ALT_A, BTN_ALT_Y, handleJoystickInput);
BLEGamepad::inputHandler.mapButtons({
{BTN_TRIG1, BTN_STATE_PRESSED, [](){
eyeBlink->handleInput(true);
}},
{BTN_TRIG1, BTN_STATE_RELEASED, [](){
eyeBlink->handleInput(false);
}},
{BTN_SELECT, BTN_STATE_PRESSED, [](){
changeController( new FaceSwitcherController( displayManager, sequencePlayer, &Sequences::heartEyes, &Sequences::dead, &Sequences::dizzy, &Sequences::questioning ));
}},
{BTN_START, BTN_STATE_PRESSED, [](){
changeController( new FaceSwitcherController( displayManager, sequencePlayer, &Sequences::angry, &Sequences::annoyed, &Sequences::spooked, &Sequences::blushing ));
}},
{BTN_X, BTN_STATE_PRESSED, [](){
changeController( new FaceSwitcherController( displayManager, sequencePlayer, &Sequences::neutral, &Sequences::joy, &Sequences::squinting, &Sequences::angryHappy ));
}},
{BTN_A, BTN_STATE_PRESSED, [](){
boopSensor->calibrate();
}},
{BTN_B, BTN_STATE_PRESSED, [](){
changeController(new SettingsController(displayManager, settingsManager));
}},
{BTN_Y, BTN_STATE_PRESSED, [](){
changeController( new AutomaticFaceSwitcherController(
displayManager, sequencePlayer, 5 SECONDS, 20 SECONDS, {
&Sequences::neutral,
&Sequences::joy,
&Sequences::squinting,
&Sequences::angryHappy,
&Sequences::spooked,
&Sequences::angry,
&Sequences::blushing
})
);
}},
});
}
void createSettingsMenu() {
settingsManager = new SettingsManager();
settingsManager->addSettings({
new LambdaSetting(
"Matrix", "Brightness", 9, // brightness was originally 15
[]() -> u8 { return displayManager->getMatrixBrightness(); },
[](i8 value) { displayManager->addMatrixBrightness(value); },
Config::EEPROM::MATRIX_BRIGHTNESS
),
new ComponentToggleSetting("Matrix", "Enable", displayManager, Config::EEPROM::MATRIX),
new ComponentToggleSetting("LED Strip", "Enable", ledStrip, Config::EEPROM::LEDSTRIP),
new ComponentToggleSetting("Auto blink", "Enable", eyeBlink, Config::EEPROM::AUTO_BLINK),
new ComponentToggleSetting("Boop sensor", "Enable", boopSensor, Config::EEPROM::BOOP_SENSOR),
new LambdaSetting(
"Boop sensor", "Sensitivity", 100,
[]() -> u8 { return boopSensor->getTriggerMultiplier(); },
[](i8 value) { boopSensor->addTriggerMultiplier(value); },
Config::EEPROM::BOOP_TRIGGER_MULTIPLIER
),
new LambdaSetting(
"Boop sensor", "Calibrate", 0, // add function only
[]() -> u8 { return 0; },
[](i8 value) { boopSensor->calibrate(); }
),
#ifdef ENABLE_ANIMATED_MOUTH
new ComponentToggleSetting("Mouth anim.", "Enable", animatedMouth, Config::EEPROM::ANIMATED_MOUTH),
new LambdaSetting(
"Mouth anim.", "Noise floor", 250,
[]() -> u8 { return animatedMouth->getNoiseFloor(); },
[](i8 value) { animatedMouth->addNoiseFloor(value * 10); },
Config::EEPROM::ANIMATED_MOUTH_NOISE_FLOOR
),
new LambdaSetting(
"Mouth anim.", "Peak minimum", 250,
[]() -> u8 { return animatedMouth->getPeakMinimum(); },
[](i8 value) { animatedMouth->addPeakMinimum(value * 10); },
Config::EEPROM::ANIMATED_MOUTH_PEAK_MIN
),
new LambdaSetting(
"Mouth anim.", "Calibrate mic.", 0,
[]() -> u8 { return 0; },
[](i8 value) { animatedMouth->reset(); }
),
#endif
new LambdaSetting(
"Transitions", "Rare chance %", 100,
[]() -> u8 { return sequencePlayer->getRareTransitionChance(); },
[](i8 value) { sequencePlayer->addRareTransitionChance(value * 10); },
Config::EEPROM::RARE_TRANSITION_CHANCE
),
new LambdaSetting(
"Fan control", "Speed", 255,
[]() -> u8 { return fanControl->getSpeed(); },
[](i8 value) { fanControl->addSpeed(value * 10); },
Config::EEPROM::FAN_PWM_SPEED
),
new LambdaSetting(
"Fun", "Snake Game", 0,
[]() -> u8 { return 0; },
[](i8 value) {
#ifdef ENABLE_ANIMATED_MOUTH
animatedMouth->stop();
#endif
changeController(new SnakeGameController(displayManager, MOUTH, EYES, 200 MILLIS));
}
),
new LambdaSetting(
"Fun", "Analyzer", 0,
[]() -> u8 { return 0; },
[](i8 value) {
#ifdef ENABLE_ANIMATED_MOUTH
animatedMouth->stop();
#endif
changeController(new SpectrumAnalyzerController(displayManager));
}
),
new LambdaSetting(
"Misc.", "Restart", 0,
[]() -> u8 { return 0; },
[](i8 value) {
ESP.restart();
}
),
new LambdaSetting(
"Misc.", "Save settings", 0,
[]() -> u8 { return 0; },
[](i8 value) { EEPROM.commit(); }
),
});
}