From c2beb4a44e191e6da85539d6172e07d67cd32eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 25 Feb 2017 01:32:37 +0100 Subject: [PATCH] Added factory reset on long-long click (>10 seconds) --- code/espurna/button.ino | 73 ++++++++++++++++++++-------------- code/espurna/config/general.h | 29 ++++++++------ code/espurna/config/hardware.h | 13 ++++++ code/espurna/settings.ino | 41 +++++++++++-------- code/platformio.ini | 2 +- 5 files changed, 99 insertions(+), 59 deletions(-) diff --git a/code/espurna/button.ino b/code/espurna/button.ino index 70245338..1238ec9e 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -15,7 +15,7 @@ Copyright (C) 2016-2017 by Xose PĂ©rez typedef struct { DebounceEvent * button; - unsigned int actions; + unsigned long actions; unsigned int relayID; } button_t; @@ -35,30 +35,36 @@ void buttonMQTT(unsigned char id, uint8_t event) { unsigned char buttonAction(unsigned char id, unsigned char event) { if (id >= _buttons.size()) return BUTTON_MODE_NONE; - unsigned int actions = _buttons[id].actions; - if (event == BUTTON_EVENT_PRESSED) return (actions >> 12) & 0x0F; - if (event == BUTTON_EVENT_CLICK) return (actions >> 8) & 0x0F; - if (event == BUTTON_EVENT_DBLCLICK) return (actions >> 4) & 0x0F; - if (event == BUTTON_EVENT_LNGCLICK) return (actions) & 0x0F; + unsigned long actions = _buttons[id].actions; + if (event == BUTTON_EVENT_PRESSED) return (actions) & 0x0F; + if (event == BUTTON_EVENT_CLICK) return (actions >> 4) & 0x0F; + if (event == BUTTON_EVENT_DBLCLICK) return (actions >> 8) & 0x0F; + if (event == BUTTON_EVENT_LNGCLICK) return (actions >> 12) & 0x0F; + if (event == BUTTON_EVENT_LNGLNGCLICK) return (actions >> 16) & 0x0F; return BUTTON_MODE_NONE; } -unsigned int buttonStore(unsigned char pressed, unsigned char click, unsigned char dblclick, unsigned char lngclick) { +unsigned long buttonStore(unsigned char pressed, unsigned char click, unsigned char dblclick, unsigned char lngclick, unsigned char lnglngclick) { unsigned int value; - value = pressed << 12; - value += click << 8; - value += dblclick << 4; - value += lngclick; + value = pressed; + value += click << 4; + value += dblclick << 8; + value += lngclick << 12; + value += lnglngclick << 16; return value; } -uint8_t mapEvent(uint8_t event) { +uint8_t mapEvent(uint8_t event, uint8_t count, uint16_t length) { if (event == EVENT_PRESSED) return BUTTON_EVENT_PRESSED; if (event == EVENT_CHANGED) return BUTTON_EVENT_CLICK; - if (event == EVENT_SINGLE_CLICK) return BUTTON_EVENT_CLICK; - if (event == EVENT_DOUBLE_CLICK) return BUTTON_EVENT_DBLCLICK; - if (event == EVENT_LONG_CLICK) return BUTTON_EVENT_LNGCLICK; - return BUTTON_EVENT_NONE; + if (event == EVENT_RELEASED) { + if (count == 1) { + if (length > BUTTON_LNGLNGCLICK_LENGTH) return BUTTON_EVENT_LNGLNGCLICK; + if (length > BUTTON_LNGCLICK_LENGTH) return BUTTON_EVENT_LNGCLICK; + return BUTTON_EVENT_CLICK; + } + if (count == 2) return BUTTON_EVENT_DBLCLICK; + } } void buttonEvent(unsigned int id, unsigned char event) { @@ -80,14 +86,26 @@ void buttonEvent(unsigned int id, unsigned char event) { if (action == BUTTON_MODE_AP) createAP(); if (action == BUTTON_MODE_RESET) ESP.restart(); if (action == BUTTON_MODE_PULSE) relayPulseToggle(); + if (action == BUTTON_MODE_FACTORY) { + DEBUG_MSG("\n\nFACTORY RESET\n\n"); + settingsFactoryReset(); + ESP.restart(); + } } +DebounceEvent::TDebounceEventCallback buttonCallbackProvider(unsigned int index) { + return [index](uint8_t pin, uint8_t event, uint8_t count, uint16_t length) { + uint8_t mapped = mapEvent(event, count, length); + buttonEvent(index, mapped); + }; +} + void buttonSetup() { #ifdef SONOFF_DUAL - unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE); + unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE); _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), 0, 1}); _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), 0, 2}); _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY}); @@ -96,26 +114,26 @@ void buttonSetup() { #ifdef BUTTON1_PIN { - unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK); - _buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE), actions, BUTTON1_RELAY}); + unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK); + _buttons.push_back({new DebounceEvent(BUTTON1_PIN, buttonCallbackProvider(_buttons.size()), BUTTON1_MODE), actions, BUTTON1_RELAY}); } #endif #ifdef BUTTON2_PIN { - unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK); - _buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE), actions, BUTTON2_RELAY}); + unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK); + _buttons.push_back({new DebounceEvent(BUTTON2_PIN, buttonCallbackProvider(_buttons.size()), BUTTON2_MODE), actions, BUTTON2_RELAY}); } #endif #ifdef BUTTON3_PIN { - unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK); - _buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE), actions, BUTTON3_RELAY}); + unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK); + _buttons.push_back({new DebounceEvent(BUTTON3_PIN, buttonCallbackProvider(_buttons.size()), BUTTON3_MODE), actions, BUTTON3_RELAY}); } #endif #ifdef BUTTON4_PIN { - unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK); - _buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE), actions, BUTTON4_RELAY}); + unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK); + _buttons.push_back({new DebounceEvent(BUTTON4_PIN, buttonCallbackProvider(_buttons.size()), BUTTON4_MODE), actions, BUTTON4_RELAY}); } #endif @@ -173,10 +191,7 @@ void buttonLoop() { #else for (unsigned int i=0; i < _buttons.size(); i++) { - if (_buttons[i].button->loop()) { - uint8_t event = mapEvent(_buttons[i].button->getEvent()); - buttonEvent(i, event); - } + _buttons[i].button->loop(); } #endif diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 192ec70e..eeaa3283 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -19,19 +19,24 @@ // BUTTON //-------------------------------------------------------------------------------- -#define BUTTON_EVENT_NONE 0 -#define BUTTON_EVENT_PRESSED 1 -#define BUTTON_EVENT_CLICK 2 -#define BUTTON_EVENT_DBLCLICK 3 -#define BUTTON_EVENT_LNGCLICK 4 +#define BUTTON_LNGCLICK_LENGTH 1000 +#define BUTTON_LNGLNGCLICK_LENGTH 10000 -#define BUTTON_MODE_NONE 0 -#define BUTTON_MODE_TOGGLE 1 -#define BUTTON_MODE_AP 2 -#define BUTTON_MODE_RESET 3 -#define BUTTON_MODE_PULSE 4 +#define BUTTON_EVENT_NONE 0 +#define BUTTON_EVENT_PRESSED 1 +#define BUTTON_EVENT_CLICK 2 +#define BUTTON_EVENT_DBLCLICK 3 +#define BUTTON_EVENT_LNGCLICK 4 +#define BUTTON_EVENT_LNGLNGCLICK 5 -#define BUTTON_DEFAULT_MODE BUTTON_MODE_TOGGLE +#define BUTTON_MODE_NONE 0 +#define BUTTON_MODE_TOGGLE 1 +#define BUTTON_MODE_AP 2 +#define BUTTON_MODE_RESET 3 +#define BUTTON_MODE_PULSE 4 +#define BUTTON_MODE_FACTORY 5 + +#define BUTTON_DEFAULT_MODE BUTTON_MODE_TOGGLE //-------------------------------------------------------------------------------- // RELAY @@ -105,7 +110,7 @@ // of the web interface by running "gulp buildfs_embed" #ifndef EMBEDDED_WEB -#define EMBEDDED_WEB 0 +#define EMBEDDED_WEB 1 #endif // ----------------------------------------------------------------------------- diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 523a379c..b2f790bc 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -369,6 +369,19 @@ #define BUTTON4_LNGCLICK BUTTON_MODE_NONE #endif +#ifndef BUTTON1_LNGLNGCLICK +#define BUTTON1_LNGLNGCLICK BUTTON_MODE_FACTORY +#endif +#ifndef BUTTON2_LNGLNGCLICK +#define BUTTON2_LNGLNGCLICK BUTTON_MODE_NONE +#endif +#ifndef BUTTON3_LNGLNGCLICK +#define BUTTON3_LNGLNGCLICK BUTTON_MODE_NONE +#endif +#ifndef BUTTON4_LNGLNGCLICK +#define BUTTON4_LNGLNGCLICK BUTTON_MODE_NONE +#endif + #ifndef BUTTON1_RELAY #define BUTTON1_RELAY 0 #endif diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index 5d99d6ab..3e5d4ffc 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -60,6 +60,13 @@ String settingsKeyName(unsigned int index) { } +void settingsFactoryReset() { + for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) { + EEPROM.write(i, 0xFF); + } + EEPROM.commit(); +} + void settingsSetup() { EEPROM.begin(SPI_FLASH_SEC_SIZE); @@ -92,32 +99,23 @@ void settingsSetup() { ESP.restart(); }); - Embedis::command( F("STATUS"), [](Embedis* e) { - e->stream->printf("Free heap: %d bytes\n", ESP.getFreeHeap()); + Embedis::command( F("FACTORY.RESET"), [](Embedis* e) { + settingsFactoryReset(); e->response(Embedis::OK); }); - Embedis::command( F("EEPROM.DUMP"), [](Embedis* e) { - for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) { - if (i % 16 == 0) e->stream->printf("\n[%04X] ", i); - e->stream->printf("%02X ", EEPROM.read(i)); - } - e->stream->printf("\n"); + Embedis::command( F("HEAP"), [](Embedis* e) { + e->stream->printf("Free HEAP: %d bytes\n", ESP.getFreeHeap()); e->response(Embedis::OK); }); - Embedis::command( F("EEPROM.ERASE"), [](Embedis* e) { - for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) { - EEPROM.write(i, 0xFF); - } - EEPROM.commit(); + Embedis::command( F("EEPROM"), [](Embedis* e) { + unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize(); + e->stream->printf("Number of keys: %d\n", settingsKeyCount()); + e->stream->printf("Free EEPROM: %d bytes (%d%%)\n", freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE); e->response(Embedis::OK); }); - Embedis::command( F("SETTINGS.SIZE"), [](Embedis* e) { - e->response(String(settingsSize())); - }); - Embedis::command( F("DUMP"), [](Embedis* e) { unsigned int size = settingsKeyCount(); for (unsigned int i=0; iresponse(Embedis::OK); }); + Embedis::command( F("DUMP.RAW"), [](Embedis* e) { + for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) { + if (i % 16 == 0) e->stream->printf("\n[%04X] ", i); + e->stream->printf("%02X ", EEPROM.read(i)); + } + e->stream->printf("\n"); + e->response(Embedis::OK); + }); + DEBUG_MSG("[SETTINGS] EEPROM size: %d bytes\n", SPI_FLASH_SEC_SIZE); DEBUG_MSG("[SETTINGS] Settings size: %d bytes\n", settingsSize()); diff --git a/code/platformio.ini b/code/platformio.ini index c69b7cdb..c8ce90d2 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -25,7 +25,7 @@ lib_deps = https://bitbucket.org/xoseperez/fauxmoesp.git https://bitbucket.org/xoseperez/nofuss.git https://bitbucket.org/xoseperez/emonliteesp.git - https://bitbucket.org/xoseperez/debounceevent.git + https://bitbucket.org/xoseperez/debounceevent.git#dev https://github.com/xoseperez/RemoteSwitch-arduino-library.git lib_ignore =