Compare commits
3 Commits
423511bffc
...
0c0413cb2c
Author | SHA1 | Date |
---|---|---|
Dirk Jahnke | 0c0413cb2c | |
Dirk Jahnke | e03ea33bcd | |
Dirk Jahnke | 542ee83c04 |
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"buttonhandler.h": "c"
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,4 +26,8 @@
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<img src="https://mongoose-os.com/images/app1.gif" width="75%">
|
<img src="https://mongoose-os.com/images/app1.gif" width="75%">
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
## Functionality
|
||||||
|
|
||||||
|
- Button on device: Push toggles the relay. LED is on for about 2 seconds to signal, push button has been recognized.
|
||||||
|
|
8
mos.yml
8
mos.yml
|
@ -45,9 +45,9 @@ config_schema:
|
||||||
# - ["http.document_root", "/"]
|
# - ["http.document_root", "/"]
|
||||||
# - ["http.hidden_files", "s_*"]
|
# - ["http.hidden_files", "s_*"]
|
||||||
# - ["http.auth_file", "s_pass"]
|
# - ["http.auth_file", "s_pass"]
|
||||||
# - ["wifi.ap.enable", true]
|
- ["wifi.ap.enable", true]
|
||||||
# - ["wifi.ap.ssid", "SONOFF_??????"]
|
- ["wifi.ap.ssid", "SONOFF_??????"]
|
||||||
# - ["wifi.ap.pass", "MySonoff"]
|
- ["wifi.ap.pass", "MySonoff"]
|
||||||
- ["wifi.sta.enable", true]
|
- ["wifi.sta.enable", true]
|
||||||
- ["wifi.sta.ssid", "Pinguin"]
|
- ["wifi.sta.ssid", "Pinguin"]
|
||||||
- ["wifi.sta.pass", "PaulchenAufmKlo34"]
|
- ["wifi.sta.pass", "PaulchenAufmKlo34"]
|
||||||
|
@ -60,7 +60,7 @@ config_schema:
|
||||||
|
|
||||||
build_vars:
|
build_vars:
|
||||||
# sonoff basic has 1MBytes flash only
|
# sonoff basic has 1MBytes flash only
|
||||||
# FLASH_SIZE: 1048576
|
FLASH_SIZE: 1048576
|
||||||
|
|
||||||
tags:
|
tags:
|
||||||
- c
|
- c
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
#include "mgos_gpio.h"
|
#include "mgos_gpio.h"
|
||||||
#include "mgos_timers.h"
|
#include "mgos_timers.h"
|
||||||
#include "buttonHandler.h"
|
#include "buttonHandler.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#define ON_BOARD_BUTTON_PIN 5
|
#define ON_BOARD_BUTTON_PIN 0
|
||||||
#define MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS 200
|
#define MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS 0.3 /* seconds */
|
||||||
|
|
||||||
static double lastButtonPressTime = 0;
|
static double lastButtonPressTime = 0;
|
||||||
static uint8_t buttonPressCounter = 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) {
|
static void multiPressButtonHandler(void *arg) {
|
||||||
(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
|
multiPressTimerId = 0; // timer used only once, thus we clear it
|
||||||
for (int i=0; i<callbacksRegistered; ++i) {
|
for (int i=0; i<callbacksRegistered; ++i) {
|
||||||
if (callbacks[i].numberPressed == buttonPressCounter) {
|
if (callbacks[i].numberPressed == buttonPressCounter) {
|
||||||
|
@ -39,6 +40,7 @@ static void multiPressButtonHandler(void *arg) {
|
||||||
callbacks[i].callback(buttonPressCounter);
|
callbacks[i].callback(buttonPressCounter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
buttonPressCounter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void buttonHandler(int pin, void *arg) {
|
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) {
|
if (mgos_uptime() - lastButtonPressTime < MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS) {
|
||||||
++buttonPressCounter;
|
++buttonPressCounter;
|
||||||
} else {
|
} else {
|
||||||
buttonPressCounter = 0;
|
buttonPressCounter = 1;
|
||||||
}
|
}
|
||||||
|
// LOG(LL_DEBUG, ("buttonHandler, lastButtonPressTime=%f, uptime=%f", lastButtonPressTime, mgos_uptime()));
|
||||||
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
|
// 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);
|
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() {
|
void init_button_handler() {
|
||||||
|
|
|
@ -43,6 +43,15 @@ static void blink_on_board_led_cb(void *arg) {
|
||||||
// toggle LED each tick
|
// toggle LED each tick
|
||||||
mgos_gpio_toggle(ON_BOARD_LED);
|
mgos_gpio_toggle(ON_BOARD_LED);
|
||||||
break;
|
break;
|
||||||
|
case LED_ONCE:
|
||||||
|
++numTicksLedHasThisState;
|
||||||
|
if (numTicksLedHasThisState == 1) {
|
||||||
|
mgos_gpio_write(ON_BOARD_LED, 0); // on
|
||||||
|
} else
|
||||||
|
if (numTicksLedHasThisState > 8) {
|
||||||
|
set_led_status(LED_OFF);
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
LOG(LL_ERROR, ("Invalid current LED status: %d -- ignored", currentStatus));
|
LOG(LL_ERROR, ("Invalid current LED status: %d -- ignored", currentStatus));
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -10,7 +10,8 @@ enum LEDStatus {
|
||||||
LED_OFF = 0, // permanently off
|
LED_OFF = 0, // permanently off
|
||||||
LED_ON = 1, // permanently on
|
LED_ON = 1, // permanently on
|
||||||
LED_BLINK_SLOW = 2, // on 0,5x / second (1s on, 1s off)
|
LED_BLINK_SLOW = 2, // on 0,5x / second (1s on, 1s off)
|
||||||
LED_BLINK_FAST = 3 // on 2x / second (250ms on, 250ms off)
|
LED_BLINK_FAST = 3, // on 2x / second (250ms on, 250ms off)
|
||||||
|
LED_ONCE = 4 // on for 2 seconds, then off again
|
||||||
};
|
};
|
||||||
|
|
||||||
extern void set_led_status(enum LEDStatus newStatus);
|
extern void set_led_status(enum LEDStatus newStatus);
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
static void buttonPressOne(int pressCount) {
|
static void buttonPressOne(int pressCount) {
|
||||||
LOG(LL_DEBUG, ("buttonPressOne called with pressCount=%d", pressCount));
|
LOG(LL_DEBUG, ("buttonPressOne called with pressCount=%d", pressCount));
|
||||||
set_relay(RELAY_TOGGLE);
|
set_relay(RELAY_TOGGLE);
|
||||||
|
set_led_status(LED_ONCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void buttonPressTwo(int pressCount) {
|
static void buttonPressTwo(int pressCount) {
|
||||||
|
@ -21,6 +22,10 @@ static void buttonPressThree(int pressCount) {
|
||||||
LOG(LL_DEBUG, ("buttonPressThree called with pressCount=%d", pressCount));
|
LOG(LL_DEBUG, ("buttonPressThree called with pressCount=%d", pressCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void on_board_led_off_cb(void *arg) {
|
||||||
|
(void) arg;
|
||||||
|
set_led_status(LED_OFF);
|
||||||
|
}
|
||||||
enum mgos_app_init_result mgos_app_init(void) {
|
enum mgos_app_init_result mgos_app_init(void) {
|
||||||
init_led_handler();
|
init_led_handler();
|
||||||
set_led_status(LED_BLINK_FAST);
|
set_led_status(LED_BLINK_FAST);
|
||||||
|
@ -30,5 +35,6 @@ enum mgos_app_init_result mgos_app_init(void) {
|
||||||
add_button_press_callback(3, buttonPressThree);
|
add_button_press_callback(3, buttonPressThree);
|
||||||
init_mqtt_handler();
|
init_mqtt_handler();
|
||||||
LOG(LL_INFO, ("SONOFF app initialized"));
|
LOG(LL_INFO, ("SONOFF app initialized"));
|
||||||
|
mgos_set_timer(5000, 0, on_board_led_off_cb, NULL);
|
||||||
return MGOS_APP_INIT_SUCCESS;
|
return MGOS_APP_INIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ static void pubMqttStatus(char *event) {
|
||||||
event,
|
event,
|
||||||
mgos_get_free_heap_size(),
|
mgos_get_free_heap_size(),
|
||||||
relay_is_on() ? "true" : "false");
|
relay_is_on() ? "true" : "false");
|
||||||
mgos_mqtt_pub(pubTopic, msg, strlen(msg), MG_MQTT_QOS(0));
|
mgos_mqtt_pub(pubTopic, msg, strlen(msg), MG_MQTT_QOS(0), true);
|
||||||
//mg_mqtt_publish(c, get_cfg()->mqtt.pub, 0, MG_MQTT_QOS(0), msg, n);
|
//mg_mqtt_publish(c, get_cfg()->mqtt.pub, 0, MG_MQTT_QOS(0), msg, n);
|
||||||
LOG(LL_DEBUG, ("MQTT pub topic=%s -> %s", pubTopic, msg));
|
LOG(LL_DEBUG, ("MQTT pub topic=%s -> %s", pubTopic, msg));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue