Browse Source

Added factory reset on long-long click (>10 seconds)

fastled
Xose Pérez 7 years ago
parent
commit
c2beb4a44e
5 changed files with 99 additions and 59 deletions
  1. +44
    -29
      code/espurna/button.ino
  2. +17
    -12
      code/espurna/config/general.h
  3. +13
    -0
      code/espurna/config/hardware.h
  4. +24
    -17
      code/espurna/settings.ino
  5. +1
    -1
      code/platformio.ini

+ 44
- 29
code/espurna/button.ino View File

@ -15,7 +15,7 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
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


+ 17
- 12
code/espurna/config/general.h View File

@ -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
// -----------------------------------------------------------------------------


+ 13
- 0
code/espurna/config/hardware.h View File

@ -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


+ 24
- 17
code/espurna/settings.ino View File

@ -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; i<size; i++) {
@ -128,6 +126,15 @@ void settingsSetup() {
e->response(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());


+ 1
- 1
code/platformio.ini View File

@ -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 =


Loading…
Cancel
Save