MultiPurposeButton now uses a timer to time the long and short press
This commit is contained in:
parent
097814ea25
commit
06a90d4b04
|
@ -8,15 +8,15 @@ const char* connection_state_to_str(esp_a2d_connection_state_t state);
|
||||||
|
|
||||||
class MultiPurposeButton {
|
class MultiPurposeButton {
|
||||||
public:
|
public:
|
||||||
MultiPurposeButton(void(*short_press)(), void(*long_press)(), uint8_t threshold = 5);
|
MultiPurposeButton(void(*short_press)(), void(*long_press)(), uint16_t threshold = 500);
|
||||||
|
|
||||||
void tick(bool current);
|
void tick(bool current);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void(*short_press)();
|
void(* const short_press)();
|
||||||
void(*long_press)();
|
void(* const long_press)();
|
||||||
uint8_t threshold = 5;
|
const int16_t threshold = 500;
|
||||||
|
uint64_t start = 0;
|
||||||
bool previous = false;
|
bool previous = false;
|
||||||
uint8_t counter = 0;
|
|
||||||
bool acted = false;
|
bool acted = false;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_timer.h"
|
||||||
|
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
|
||||||
|
@ -13,34 +14,36 @@ const char* connection_state_to_str(esp_a2d_connection_state_t state) {
|
||||||
return states[state];
|
return states[state];
|
||||||
}
|
}
|
||||||
|
|
||||||
MultiPurposeButton::MultiPurposeButton(void(*short_press)(), void(*long_press)(), uint8_t threshold) : short_press(short_press), long_press(long_press), threshold(threshold) {}
|
MultiPurposeButton::MultiPurposeButton(void(*short_press)(), void(*long_press)(), uint16_t threshold) : short_press(short_press), long_press(long_press), threshold(threshold) {}
|
||||||
|
|
||||||
// @TOOD Use a timer instead of amount of updates as this can be inconsistent (e.g. when in eco mode)
|
// @TOOD Use a timer instead of amount of updates as this can be inconsistent (e.g. when in eco mode)
|
||||||
void MultiPurposeButton::tick(bool current) {
|
void MultiPurposeButton::tick(bool current) {
|
||||||
if (current && !acted) {
|
if (current != previous && current) {
|
||||||
if (counter >= threshold) {
|
// Button just got presset
|
||||||
// Long press
|
start = esp_timer_get_time();
|
||||||
if (long_press) {
|
|
||||||
ESP_LOGI("MPB", "Long press!");
|
|
||||||
long_press();
|
|
||||||
}
|
|
||||||
acted = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
counter++;
|
|
||||||
} else if (previous != current && !current) {
|
} else if (previous != current && !current) {
|
||||||
// The button just got released
|
// The button just got released
|
||||||
|
// Check for short press
|
||||||
// Short press
|
if (esp_timer_get_time() - start < threshold) {
|
||||||
if (counter < threshold) {
|
|
||||||
if (short_press) {
|
if (short_press) {
|
||||||
ESP_LOGI("MPB", "Short press!");
|
ESP_LOGI("MPB", "Short press!");
|
||||||
short_press();
|
short_press();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
counter = 0;
|
|
||||||
acted = false;
|
acted = false;
|
||||||
|
} else if (current && !acted) {
|
||||||
|
// Button is still being pressed
|
||||||
|
// Check if we exceed the timer for a long press
|
||||||
|
if (esp_timer_get_time() >= threshold) {
|
||||||
|
if (long_press) {
|
||||||
|
ESP_LOGI("MPB", "Long press!");
|
||||||
|
long_press();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure we only execute the long press once
|
||||||
|
acted = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
previous = current;
|
previous = current;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user