Fixed button handler

This commit is contained in:
Dirk Jahnke 2018-02-02 14:16:34 +01:00
parent 423511bffc
commit 542ee83c04
1 changed files with 8 additions and 5 deletions

View File

@ -2,9 +2,10 @@
#include "mgos_gpio.h"
#include "mgos_timers.h"
#include "buttonHandler.h"
#include <math.h>
#define ON_BOARD_BUTTON_PIN 5
#define MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS 200
#define ON_BOARD_BUTTON_PIN 0
#define MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS 0.3 /* seconds */
static double lastButtonPressTime = 0;
static uint8_t buttonPressCounter = 0;
@ -31,7 +32,7 @@ void add_button_press_callback(int numberPressed, button_press_callback cb) {
static void multiPressButtonHandler(void *arg) {
(void) arg;
LOG(LL_DEBUG, ("multiPressButtonHandler called after %d presses", buttonPressCounter));
LOG(LL_DEBUG, ("multiPressButtonHandler called after %d presses, uptime=%f", buttonPressCounter, mgos_uptime()));
multiPressTimerId = 0; // timer used only once, thus we clear it
for (int i=0; i<callbacksRegistered; ++i) {
if (callbacks[i].numberPressed == buttonPressCounter) {
@ -39,6 +40,7 @@ static void multiPressButtonHandler(void *arg) {
callbacks[i].callback(buttonPressCounter);
}
}
buttonPressCounter = 0;
}
static void buttonHandler(int pin, void *arg) {
@ -48,12 +50,13 @@ static void buttonHandler(int pin, void *arg) {
if (mgos_uptime() - lastButtonPressTime < MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS) {
++buttonPressCounter;
} else {
buttonPressCounter = 0;
buttonPressCounter = 1;
}
// LOG(LL_DEBUG, ("buttonHandler, lastButtonPressTime=%f, uptime=%f", lastButtonPressTime, mgos_uptime()));
lastButtonPressTime = mgos_uptime();
// at this point we do not know if more press events will come that we need to count, therefore we wait before we act
if (multiPressTimerId != 0) mgos_clear_timer(multiPressTimerId);
multiPressTimerId = mgos_set_timer(MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS + 1, 0, multiPressButtonHandler, NULL);
multiPressTimerId = mgos_set_timer(floor(1000 * MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS), 0, multiPressButtonHandler, NULL);
}
void init_button_handler() {