AwesomeStudioPedal
A programmable, multi-profile foot controller for DAWs, score readers, and studio automation
Loading...
Searching...
No Matches
esp32/src/led_controller.cpp
Go to the documentation of this file.
1#include "led_controller.h"
2
3LEDController::LEDController(uint8_t pin) : pin(pin) {}
4
5void LEDController::setup(uint32_t initialState)
6{
7 gpio_pad_select_gpio(static_cast<gpio_num_t>(pin));
8 gpio_set_direction(static_cast<gpio_num_t>(pin), GPIO_MODE_OUTPUT);
9 setState(initialState != 0);
10}
11
13{
14 if (currentState != state)
15 {
16 gpio_set_level(static_cast<gpio_num_t>(pin), state ? 1 : 0);
17 currentState = state;
18 }
19}
20
21void LEDController::toggle() { setState(! currentState); }
22
23void LEDController::startBlink(uint32_t intervalMs, int16_t count)
24{
25 if (blinking)
26 return; // already blinking — caller must stopBlink() first
27 stateBeforeBlink = currentState;
28 blinkInterval = intervalMs;
29 // count is full on/off cycles; we track half-cycles internally
30 blinkRemaining = (count < 0) ? -1 : static_cast<int16_t>(count * 2);
31 blinking = true;
32 lastToggleTime = 0; // will be set on first update() call
33}
34
36{
37 if (! blinking)
38 return;
39 blinking = false;
40 setState(stateBeforeBlink);
41}
42
43void LEDController::update(uint32_t now)
44{
45 if (! blinking)
46 return;
47
48 // Initialise lastToggleTime on the first update after startBlink
49 if (lastToggleTime == 0)
50 {
51 lastToggleTime = now;
52 setState(true); // start with LED on
53 return;
54 }
55
56 if (now - lastToggleTime < blinkInterval)
57 return;
58
59 lastToggleTime = now;
60 toggle();
61
62 if (blinkRemaining > 0)
63 {
64 blinkRemaining--;
65 if (blinkRemaining == 0)
66 {
67 stopBlink();
68 }
69 }
70}
void setup(uint32_t initialState=0) override
void update(uint32_t now) override
Drive timed behaviour — must be called every loop iteration.
void setState(bool state) override
void startBlink(uint32_t intervalMs, int16_t count=-1) override
Start a blink sequence.
void stopBlink() override
Stop any running blink and restore the pre-blink state.