Skip to content
Snippets Groups Projects

fix broken buttons support / debounce

Merged Thomas Maier requested to merge dev into main
4 files
+ 56
48
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 53
26
#include "buttons.hpp"
#include "hardware/gpio.h"
#include "pico/stdlib.h"
#include <map>
#define BTN_DEBOUNCE_MS 100
#define BTN_DEBOUNCE_MS 250
DebouncedButton::DebouncedButton(char name) {
_name = name;
last = to_ms_since_boot(get_absolute_time());
}
class DebouncedButton {
public:
DebouncedButton() {}
char DebouncedButton::name() {
return _name;
}
DebouncedButton(char name, uint8_t gpio) {
_name = name;
_gpio = gpio;
last = to_ms_since_boot(get_absolute_time());
}
bool DebouncedButton::debounced() {
unsigned long now = to_ms_since_boot(get_absolute_time());
if (last + BTN_DEBOUNCE_MS > now) {
last = now;
return true;
char name() {
return _name;
}
uint8_t gpio() {
return _gpio;
}
return false;
}
bool debounced() {
uint32_t now = to_ms_since_boot(get_absolute_time());
if (now - last > BTN_DEBOUNCE_MS) {
last = now;
return true;
}
return false;
}
private:
char _name;
uint8_t _gpio;
uint32_t last;
};
DebouncedButton _buttons[] = {
DebouncedButton('A', 12),
DebouncedButton('B', 13),
DebouncedButton('X', 14),
DebouncedButton('Y', 15),
};
void IoTeeButtons::setup(gpio_irq_callback_t cb) {
buttons[12] = DebouncedButton('A');
buttons[13] = DebouncedButton('B');
buttons[14] = DebouncedButton('X');
buttons[15] = DebouncedButton('Y');
for (auto const &b: buttons) {
uint8_t gpio = static_cast<uint8_t>(b.first);
gpio_pull_up(gpio);
gpio_set_irq_enabled_with_callback(gpio, GPIO_IRQ_EDGE_FALL, true, cb);
for (DebouncedButton button: _buttons) {
gpio_pull_up(button.gpio());
gpio_set_irq_enabled_with_callback(button.gpio(), GPIO_IRQ_EDGE_FALL, true, cb);
}
}
bool IoTeeButtons::debounced(uint8_t gpio) {
return buttons[gpio].debounced();
for (DebouncedButton &button: _buttons) {
if (button.gpio() == gpio) {
return button.debounced();
}
}
return false;
}
char IoTeeButtons::name(uint8_t gpio) {
return buttons[gpio].name();
for (DebouncedButton button: _buttons) {
if (button.gpio() == gpio) {
return button.name();
}
}
return ' ';
}
Loading