Added button handler
This commit is contained in:
61
src/buttonHandler.c
Normal file
61
src/buttonHandler.c
Normal file
@@ -0,0 +1,61 @@
|
||||
#include "mgos.h"
|
||||
#include "mgos_gpio.h"
|
||||
#include "mgos_timers.h"
|
||||
#include "buttonHandler.h"
|
||||
|
||||
#define ON_BOARD_BUTTON_PIN 5
|
||||
#define MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS 200
|
||||
|
||||
static double lastButtonPressTime = 0;
|
||||
static uint8_t buttonPressCounter = 0;
|
||||
static mgos_timer_id multiPressTimerId = 0;
|
||||
static uint8_t callbacksRegistered = 0;
|
||||
#define MAX_CALLBACKS 10
|
||||
|
||||
struct callbackReg_s {
|
||||
int numberPressed;
|
||||
button_press_callback callback;
|
||||
};
|
||||
|
||||
static struct callbackReg_s callbacks[MAX_CALLBACKS];
|
||||
|
||||
void add_button_press_callback(int numberPressed, button_press_callback cb) {
|
||||
if (callbacksRegistered < MAX_CALLBACKS) {
|
||||
callbacks[callbacksRegistered].callback = cb;
|
||||
callbacks[callbacksRegistered].numberPressed = numberPressed;
|
||||
++callbacksRegistered;
|
||||
} else {
|
||||
LOG(LL_ERROR, ("ERROR: Too many callbacks for button press! -- ignored this on for %d presses", numberPressed));
|
||||
}
|
||||
}
|
||||
|
||||
static void multiPressButtonHandler(void *arg) {
|
||||
(void) arg;
|
||||
LOG(LL_DEBUG, ("multiPressButtonHandler called after %d presses", buttonPressCounter));
|
||||
multiPressTimerId = 0; // timer used only once, thus we clear it
|
||||
for (int i=0; i<callbacksRegistered; ++i) {
|
||||
if (callbacks[i].numberPressed == buttonPressCounter) {
|
||||
LOG(LL_DEBUG, ("Calling button press callback for %d presses, index %d", buttonPressCounter, i));
|
||||
callbacks[i].callback(buttonPressCounter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void buttonHandler(int pin, void *arg) {
|
||||
(void) pin;
|
||||
(void) arg;
|
||||
// button release event occured
|
||||
if (mgos_uptime() - lastButtonPressTime < MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS) {
|
||||
++buttonPressCounter;
|
||||
} else {
|
||||
buttonPressCounter = 0;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
void init_button_handler() {
|
||||
mgos_gpio_set_button_handler(ON_BOARD_BUTTON_PIN, MGOS_GPIO_PULL_UP, MGOS_GPIO_INT_EDGE_POS, 50, buttonHandler, NULL);
|
||||
}
|
9
src/buttonHandler.h
Normal file
9
src/buttonHandler.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef buttonHandler_h_included
|
||||
#define buttonHandler_h_included
|
||||
|
||||
typedef void (*button_press_callback)(int numberPressed);
|
||||
|
||||
extern void init_button_handler();
|
||||
extern void add_button_press_callback(int numberPressed, button_press_callback cb);
|
||||
|
||||
#endif
|
@@ -15,17 +15,15 @@
|
||||
|
||||
static int ON_BOARD_LED = 13; /* sonoff basic LED pin */
|
||||
|
||||
static LEDStatus currentStatus = LED_OFF;
|
||||
static enum LEDStatus currentStatus = LED_OFF;
|
||||
static int numTicksLedHasThisState = 0;
|
||||
|
||||
void set_led_status(LEDStatus newStatus) {
|
||||
void set_led_status(enum LEDStatus newStatus) {
|
||||
numTicksLedHasThisState = 0;
|
||||
currentStatus = newStatus;
|
||||
LOG(LL_DEBUG, ("set_led_status to %d", newStatus));
|
||||
LOG(LL_INFO, ("set_led_status to %d", newStatus));
|
||||
}
|
||||
|
||||
static uint8_t led_timer_ticks = 0; /* for led blinker use */
|
||||
|
||||
static void blink_on_board_led_cb(void *arg) {
|
||||
switch (currentStatus) {
|
||||
case LED_OFF:
|
||||
@@ -35,7 +33,7 @@ static void blink_on_board_led_cb(void *arg) {
|
||||
mgos_gpio_write(ON_BOARD_LED, 0); // on
|
||||
break;
|
||||
case LED_BLINK_SLOW:
|
||||
++numTicksLedHasThisState = 0;
|
||||
++numTicksLedHasThisState;
|
||||
if (numTicksLedHasThisState >= 4) {
|
||||
numTicksLedHasThisState = 0;
|
||||
mgos_gpio_toggle(ON_BOARD_LED);
|
||||
@@ -55,5 +53,5 @@ static void blink_on_board_led_cb(void *arg) {
|
||||
void init_led_handler() {
|
||||
mgos_gpio_set_mode(ON_BOARD_LED, MGOS_GPIO_MODE_OUTPUT);
|
||||
mgos_set_timer(250, MGOS_TIMER_REPEAT, blink_on_board_led_cb, NULL);
|
||||
LOG(LL_DEBUG, ("LED handler initialized"));
|
||||
LOG(LL_INFO, ("LED handler initialized"));
|
||||
}
|
||||
|
@@ -13,7 +13,7 @@ enum LEDStatus {
|
||||
LED_BLINK_FAST = 3 // on 2x / second (250ms on, 250ms off)
|
||||
};
|
||||
|
||||
extern void set_led_status(LEDStatus newStatus);
|
||||
extern void set_led_status(enum LEDStatus newStatus);
|
||||
extern void init_led_handler();
|
||||
|
||||
#endif
|
31
src/main.c
31
src/main.c
@@ -1,19 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "common/platform.h"
|
||||
#include "common/cs_file.h"
|
||||
#include "mgos.h"
|
||||
#include "mgos_app.h"
|
||||
#include "mgos_gpio.h"
|
||||
#include "mgos_sys_config.h"
|
||||
#include "mgos_timers.h"
|
||||
#include "mgos_hal.h"
|
||||
#include "mgos_dlsym.h"
|
||||
#include "mgos_mqtt.h"
|
||||
#include "mjs.h"
|
||||
#include "common/platform.h"
|
||||
#include "common/cs_file.h"
|
||||
|
||||
#include "buttonHandler.h"
|
||||
#include "ledHandler.h"
|
||||
|
||||
#define RELAY_PIN 12
|
||||
|
||||
bool mqtt_conn_flag = false;
|
||||
|
||||
@@ -36,7 +36,7 @@ int mqtt_connected(void) {
|
||||
static void mqtt_ev_handler(struct mg_connection *c, int ev, void *p, void *user_data) {
|
||||
struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
|
||||
if (ev == MG_EV_MQTT_CONNACK) {
|
||||
LOG(LL_INFO, ("MQTT connected: %d", msg->connack_ret_code));
|
||||
LOG(LL_INFO, ("SonoffApp: MQTT connected: %d", msg->connack_ret_code));
|
||||
mqtt_conn_flag = true;
|
||||
//if (get_cfg()->mqtt.pub == NULL) {
|
||||
//LOG(LL_ERROR, ("Run 'mos config-set mqtt.pub=... '"));
|
||||
@@ -50,9 +50,26 @@ static void mqtt_ev_handler(struct mg_connection *c, int ev, void *p, void *user
|
||||
(void) c;
|
||||
}
|
||||
|
||||
static void buttonPressOne(int pressCount) {
|
||||
LOG(LL_DEBUG, ("buttonPressOne called with pressCount=%d", pressCount));
|
||||
}
|
||||
|
||||
static void buttonPressTwo(int pressCount) {
|
||||
LOG(LL_DEBUG, ("buttonPressTwo called with pressCount=%d", pressCount));
|
||||
}
|
||||
|
||||
static void buttonPressThree(int pressCount) {
|
||||
LOG(LL_DEBUG, ("buttonPressThree called with pressCount=%d", pressCount));
|
||||
}
|
||||
|
||||
enum mgos_app_init_result mgos_app_init(void) {
|
||||
init_led_handler();
|
||||
set_led_status(LED_BLINK_FAST);
|
||||
init_button_handler();
|
||||
add_button_press_callback(1, buttonPressOne);
|
||||
add_button_press_callback(2, buttonPressTwo);
|
||||
add_button_press_callback(3, buttonPressThree);
|
||||
mgos_mqtt_add_global_handler(mqtt_ev_handler, NULL);
|
||||
LOG(LL_DEBUG, ("SONOFF app initialized"));
|
||||
LOG(LL_INFO, ("SONOFF app initialized"));
|
||||
return MGOS_APP_INIT_SUCCESS;
|
||||
}
|
||||
|
Reference in New Issue
Block a user