Compare commits

..

3 Commits

Author SHA1 Message Date
Dirk Jahnke 0c0413cb2c Basic app running now 2018-02-02 14:18:09 +01:00
Dirk Jahnke e03ea33bcd Added LED_ONCE to led handler 2018-02-02 14:17:40 +01:00
Dirk Jahnke 542ee83c04 Fixed button handler 2018-02-02 14:16:34 +01:00
8 changed files with 40 additions and 12 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"buttonhandler.h": "c"
}
}

View File

@ -27,3 +27,7 @@
<p align="center">
<img src="https://mongoose-os.com/images/app1.gif" width="75%">
</p>
## Functionality
- Button on device: Push toggles the relay. LED is on for about 2 seconds to signal, push button has been recognized.

View File

@ -45,9 +45,9 @@ config_schema:
# - ["http.document_root", "/"]
# - ["http.hidden_files", "s_*"]
# - ["http.auth_file", "s_pass"]
# - ["wifi.ap.enable", true]
# - ["wifi.ap.ssid", "SONOFF_??????"]
# - ["wifi.ap.pass", "MySonoff"]
- ["wifi.ap.enable", true]
- ["wifi.ap.ssid", "SONOFF_??????"]
- ["wifi.ap.pass", "MySonoff"]
- ["wifi.sta.enable", true]
- ["wifi.sta.ssid", "Pinguin"]
- ["wifi.sta.pass", "PaulchenAufmKlo34"]
@ -60,7 +60,7 @@ config_schema:
build_vars:
# sonoff basic has 1MBytes flash only
# FLASH_SIZE: 1048576
FLASH_SIZE: 1048576
tags:
- c

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() {

View File

@ -43,6 +43,15 @@ static void blink_on_board_led_cb(void *arg) {
// toggle LED each tick
mgos_gpio_toggle(ON_BOARD_LED);
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:
LOG(LL_ERROR, ("Invalid current LED status: %d -- ignored", currentStatus));
break;

View File

@ -10,7 +10,8 @@ enum LEDStatus {
LED_OFF = 0, // permanently off
LED_ON = 1, // permanently on
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);

View File

@ -11,6 +11,7 @@
static void buttonPressOne(int pressCount) {
LOG(LL_DEBUG, ("buttonPressOne called with pressCount=%d", pressCount));
set_relay(RELAY_TOGGLE);
set_led_status(LED_ONCE);
}
static void buttonPressTwo(int pressCount) {
@ -21,6 +22,10 @@ static void buttonPressThree(int 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) {
init_led_handler();
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);
init_mqtt_handler();
LOG(LL_INFO, ("SONOFF app initialized"));
mgos_set_timer(5000, 0, on_board_led_off_cb, NULL);
return MGOS_APP_INIT_SUCCESS;
}

View File

@ -56,7 +56,7 @@ static void pubMqttStatus(char *event) {
event,
mgos_get_free_heap_size(),
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);
LOG(LL_DEBUG, ("MQTT pub topic=%s -> %s", pubTopic, msg));
}