|
@ -15,7 +15,7 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com> |
|
|
|
|
|
|
|
|
typedef struct { |
|
|
typedef struct { |
|
|
DebounceEvent * button; |
|
|
DebounceEvent * button; |
|
|
unsigned int actions; |
|
|
|
|
|
|
|
|
unsigned long actions; |
|
|
unsigned int relayID; |
|
|
unsigned int relayID; |
|
|
} button_t; |
|
|
} button_t; |
|
|
|
|
|
|
|
@ -35,30 +35,36 @@ void buttonMQTT(unsigned char id, uint8_t event) { |
|
|
|
|
|
|
|
|
unsigned char buttonAction(unsigned char id, unsigned char event) { |
|
|
unsigned char buttonAction(unsigned char id, unsigned char event) { |
|
|
if (id >= _buttons.size()) return BUTTON_MODE_NONE; |
|
|
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; |
|
|
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; |
|
|
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; |
|
|
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_PRESSED) return BUTTON_EVENT_PRESSED; |
|
|
if (event == EVENT_CHANGED) return BUTTON_EVENT_CLICK; |
|
|
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) { |
|
|
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_AP) createAP(); |
|
|
if (action == BUTTON_MODE_RESET) ESP.restart(); |
|
|
if (action == BUTTON_MODE_RESET) ESP.restart(); |
|
|
if (action == BUTTON_MODE_PULSE) relayPulseToggle(); |
|
|
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() { |
|
|
void buttonSetup() { |
|
|
|
|
|
|
|
|
#ifdef SONOFF_DUAL
|
|
|
#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, 1}); |
|
|
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), 0, 2}); |
|
|
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), 0, 2}); |
|
|
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY}); |
|
|
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY}); |
|
@ -96,26 +114,26 @@ void buttonSetup() { |
|
|
|
|
|
|
|
|
#ifdef BUTTON1_PIN
|
|
|
#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
|
|
|
#endif
|
|
|
#ifdef BUTTON2_PIN
|
|
|
#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
|
|
|
#endif
|
|
|
#ifdef BUTTON3_PIN
|
|
|
#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
|
|
|
#endif
|
|
|
#ifdef BUTTON4_PIN
|
|
|
#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
|
|
|
#endif
|
|
|
|
|
|
|
|
@ -173,10 +191,7 @@ void buttonLoop() { |
|
|
#else
|
|
|
#else
|
|
|
|
|
|
|
|
|
for (unsigned int i=0; i < _buttons.size(); i++) { |
|
|
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
|
|
|
#endif
|
|
|