AwesomeStudioPedal
A programmable, multi-profile foot controller for DAWs, score readers, and studio automation
Loading...
Searching...
No Matches
esp32/src/button.cpp
Go to the documentation of this file.
1#include "button.h"
2#include <Arduino.h>
3#ifndef HOST_TEST_BUILD
4#include <driver/gpio.h>
5#endif
6
7Button::Button(uint8_t PIN) : PIN(PIN) {}
8
10{
11 gpio_pad_select_gpio(static_cast<gpio_num_t>(PIN));
12 pinMode(PIN, INPUT_PULLUP);
13}
14
15bool Button::isDebounced(unsigned long now) const
16{
17 return (now - lastDebounceTime) > debounceDelay;
18}
19
21{
22 unsigned long now = millis();
23 if (digitalRead(PIN) == HIGH)
24 {
25 if (awaitingRelease && isDebounced(now))
26 {
27 awaitingRelease = false;
28 lastDebounceTime = now;
29 }
30 return;
31 }
32 if (! awaitingRelease && isDebounced(now))
33 {
34 pressCount++;
35 awaitingRelease = true;
36 lastDebounceTime = now;
37 }
38}
39
41{
42 if (pressCount > 0)
43 {
44 pressCount--;
45 return true;
46 }
47 return false;
48}
49
51{
52 pressCount = 0;
53 awaitingRelease = false;
55}
void setup() override
Configures the GPIO pin as input with pull-up.
volatile bool awaitingRelease
True after press, until pin goes HIGH.
volatile uint8_t pressCount
Incremented by ISR, decremented by event()
Button(uint8_t PIN)
Constructs a Button for the given GPIO pin.
unsigned long lastDebounceTime
Timestamp of last accepted press.
void isr()
ISR entry point — call from an IRAM_ATTR interrupt handler.
unsigned long debounceDelay
Debounce window in milliseconds.
void reset() override
Clears the pressed flag.
bool event() override
Checks for a press event and clears the flag.