Skip to content
Snippets Groups Projects
Commit 033d1127 authored by Thomas Maier's avatar Thomas Maier
Browse files

Merge branch 'dev' into 'main'

fix broken buttons support / debounce

See merge request !5
parents f65930dd 28b0bb66
No related branches found
No related tags found
1 merge request!5fix broken buttons support / debounce
#include "buttons.hpp" #include "buttons.hpp"
#include "hardware/gpio.h" #include "hardware/gpio.h"
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include <map>
#define BTN_DEBOUNCE_MS 100 #define BTN_DEBOUNCE_MS 250
DebouncedButton::DebouncedButton(char name) { class DebouncedButton {
_name = name; public:
last = to_ms_since_boot(get_absolute_time()); DebouncedButton() {}
}
char DebouncedButton::name() { DebouncedButton(char name, uint8_t gpio) {
return _name; _name = name;
} _gpio = gpio;
last = to_ms_since_boot(get_absolute_time());
}
bool DebouncedButton::debounced() { char name() {
unsigned long now = to_ms_since_boot(get_absolute_time()); return _name;
if (last + BTN_DEBOUNCE_MS > now) { }
last = now;
return true; 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) { void IoTeeButtons::setup(gpio_irq_callback_t cb) {
buttons[12] = DebouncedButton('A'); for (DebouncedButton button: _buttons) {
buttons[13] = DebouncedButton('B'); gpio_pull_up(button.gpio());
buttons[14] = DebouncedButton('X'); gpio_set_irq_enabled_with_callback(button.gpio(), GPIO_IRQ_EDGE_FALL, true, cb);
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);
} }
} }
bool IoTeeButtons::debounced(uint8_t gpio) { 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) { char IoTeeButtons::name(uint8_t gpio) {
return buttons[gpio].name(); for (DebouncedButton button: _buttons) {
if (button.gpio() == gpio) {
return button.name();
}
}
return ' ';
} }
...@@ -3,22 +3,6 @@ ...@@ -3,22 +3,6 @@
#include "hardware/gpio.h" #include "hardware/gpio.h"
#include <stdio.h> #include <stdio.h>
#include <map>
class DebouncedButton {
public:
DebouncedButton();
DebouncedButton(char name);
char name();
bool debounced();
private:
char _name;
unsigned long last;
};
class IoTeeButtons { class IoTeeButtons {
public: public:
...@@ -27,9 +11,6 @@ public: ...@@ -27,9 +11,6 @@ public:
static bool debounced(uint8_t gpio); static bool debounced(uint8_t gpio);
static char name(uint8_t gpio); static char name(uint8_t gpio);
private:
static std::map<int8_t, DebouncedButton> buttons;
}; };
#endif #endif
#include "messaging.hpp" #include "messaging.hpp"
#include <stdio.h> #include <stdio.h>
#include <queue>
Message::Message() {} Message::Message() {}
...@@ -56,7 +57,6 @@ std::queue<Message> _tx_q; ...@@ -56,7 +57,6 @@ std::queue<Message> _tx_q;
struct repeating_timer _tx_pump_rt; struct repeating_timer _tx_pump_rt;
bool _tx_pump_isr(struct repeating_timer *_1) { bool _tx_pump_isr(struct repeating_timer *_1) {
// TODO: move out of isr in some sexy way...
while (!_tx_q.empty()) { while (!_tx_q.empty()) {
_tx_q.front().transmit(); _tx_q.front().transmit();
_tx_q.pop(); _tx_q.pop();
......
...@@ -2,10 +2,9 @@ ...@@ -2,10 +2,9 @@
#define IOTEE_MESSAGING_HPP #define IOTEE_MESSAGING_HPP
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include <queue>
#define MESSAGE_START_BYTE '|' #define MESSAGE_START_BYTE '|'
#define MESSAGE_MAX_DATA_LEN 8 #define MESSAGE_MAX_DATA_LEN 128
#define SERIAL_TIMEOUT_US 1000 #define SERIAL_TIMEOUT_US 1000
#define TRIGGER_TX_PUMP_DELAY_MS 10 #define TRIGGER_TX_PUMP_DELAY_MS 10
...@@ -34,6 +33,7 @@ public: ...@@ -34,6 +33,7 @@ public:
class TxPump { class TxPump {
public: public:
static void start(); static void start();
static void enqueue(Message m); static void enqueue(Message m);
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment