Compare commits

..

No commits in common. "0c0413cb2c216cea40f678460d96bb2fde165133" and "423511bffc95e28a82ebd44cbb5e172c8d82afee" have entirely different histories.

8 changed files with 12 additions and 40 deletions

View File

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

View File

@ -26,8 +26,4 @@
<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.
</p>

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,10 +2,9 @@
#include "mgos_gpio.h"
#include "mgos_timers.h"
#include "buttonHandler.h"
#include <math.h>
#define ON_BOARD_BUTTON_PIN 0
#define MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS 0.3 /* seconds */
#define ON_BOARD_BUTTON_PIN 5
#define MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS 200
static double lastButtonPressTime = 0;
static uint8_t buttonPressCounter = 0;
@ -32,7 +31,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, uptime=%f", buttonPressCounter, mgos_uptime()));
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) {
@ -40,7 +39,6 @@ static void multiPressButtonHandler(void *arg) {
callbacks[i].callback(buttonPressCounter);
}
}
buttonPressCounter = 0;
}
static void buttonHandler(int pin, void *arg) {
@ -50,13 +48,12 @@ static void buttonHandler(int pin, void *arg) {
if (mgos_uptime() - lastButtonPressTime < MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS) {
++buttonPressCounter;
} else {
buttonPressCounter = 1;
buttonPressCounter = 0;
}
// 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(floor(1000 * MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS), 0, multiPressButtonHandler, NULL);
multiPressTimerId = mgos_set_timer(MAX_TIME_BETWEEN_MULTIPLE_BUTTON_PRESS_EVENTS + 1, 0, multiPressButtonHandler, NULL);
}
void init_button_handler() {

View File

@ -43,15 +43,6 @@ 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,8 +10,7 @@ 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_ONCE = 4 // on for 2 seconds, then off again
LED_BLINK_FAST = 3 // on 2x / second (250ms on, 250ms off)
};
extern void set_led_status(enum LEDStatus newStatus);

View File

@ -11,7 +11,6 @@
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) {
@ -22,10 +21,6 @@ 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);
@ -35,6 +30,5 @@ 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), true);
mgos_mqtt_pub(pubTopic, msg, strlen(msg), MG_MQTT_QOS(0));
//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));
}