From e6570fe1bb580a24be23e215857372b1b392c108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Wed, 23 May 2018 22:15:38 +0200 Subject: [PATCH] Triple click to enter WPS mode, fast LED blinking --- code/espurna/button.ino | 30 ++++++++++-------- code/espurna/config/defaults.h | 25 +++++++++++++++ code/espurna/config/types.h | 11 +++++++ code/espurna/led.ino | 58 ++++++++++++++++++---------------- code/espurna/settings.ino | 2 +- code/espurna/telnet.ino | 2 +- code/espurna/wifi.ino | 37 ++++++++++++++++++---- 7 files changed, 115 insertions(+), 50 deletions(-) diff --git a/code/espurna/button.ino b/code/espurna/button.ino index e85fba43..5e5927ce 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -60,16 +60,18 @@ unsigned char buttonAction(unsigned char id, unsigned char event) { 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; + if (event == BUTTON_EVENT_TRIPLECLICK) return (actions >> 20) & 0x0F; return BUTTON_MODE_NONE; } -unsigned long buttonStore(unsigned long pressed, unsigned long click, unsigned long dblclick, unsigned long lngclick, unsigned long lnglngclick) { +unsigned long buttonStore(unsigned long pressed, unsigned long click, unsigned long dblclick, unsigned long lngclick, unsigned long lnglngclick, unsigned long tripleclick) { unsigned int value; value = pressed; value += click << 4; value += dblclick << 8; value += lngclick << 12; value += lnglngclick << 16; + value += tripleclick << 20; return value; } @@ -77,12 +79,13 @@ 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_RELEASED) { - if (count == 1) { + if (1 == count) { if (length > BUTTON_LNGLNGCLICK_DELAY) return BUTTON_EVENT_LNGLNGCLICK; if (length > BUTTON_LNGCLICK_DELAY) return BUTTON_EVENT_LNGCLICK; return BUTTON_EVENT_CLICK; } - if (count == 2) return BUTTON_EVENT_DBLCLICK; + if (2 == count) return BUTTON_EVENT_DBLCLICK; + if (3 == count) return BUTTON_EVENT_TRIPLECLICK; } return BUTTON_EVENT_NONE; } @@ -113,7 +116,8 @@ void buttonEvent(unsigned int id, unsigned char event) { relayStatus(_buttons[id].relayID - 1, false); } } - if (action == BUTTON_MODE_AP) createAP(); + if (action == BUTTON_MODE_AP) wifiStartAP(); + if (action == BUTTON_MODE_WPS) wifiStartWPS(); if (action == BUTTON_MODE_RESET) { deferredReset(100, CUSTOM_RESET_HARDWARE); } @@ -129,7 +133,7 @@ void buttonSetup() { #ifdef ITEAD_SONOFF_DUAL - unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, 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, BUTTON_MODE_NONE); _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1}); _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2}); _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY}); @@ -140,49 +144,49 @@ void buttonSetup() { #if BUTTON1_PIN != GPIO_NONE { - unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK); + unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK, BUTTON1_TRIPLECLICK); _buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON1_RELAY}); } #endif #if BUTTON2_PIN != GPIO_NONE { - unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK); + unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK, BUTTON2_TRIPLECLICK); _buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON2_RELAY}); } #endif #if BUTTON3_PIN != GPIO_NONE { - unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK); + unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK, BUTTON3_TRIPLECLICK); _buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON3_RELAY}); } #endif #if BUTTON4_PIN != GPIO_NONE { - unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK); + unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK, BUTTON4_TRIPLECLICK); _buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON4_RELAY}); } #endif #if BUTTON5_PIN != GPIO_NONE { - unsigned int actions = buttonStore(BUTTON5_PRESS, BUTTON5_CLICK, BUTTON5_DBLCLICK, BUTTON5_LNGCLICK, BUTTON5_LNGLNGCLICK); + unsigned int actions = buttonStore(BUTTON5_PRESS, BUTTON5_CLICK, BUTTON5_DBLCLICK, BUTTON5_LNGCLICK, BUTTON5_LNGLNGCLICK, BUTTON5_TRIPLECLICK); _buttons.push_back({new DebounceEvent(BUTTON5_PIN, BUTTON5_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON5_RELAY}); } #endif #if BUTTON6_PIN != GPIO_NONE { - unsigned int actions = buttonStore(BUTTON6_PRESS, BUTTON6_CLICK, BUTTON6_DBLCLICK, BUTTON6_LNGCLICK, BUTTON6_LNGLNGCLICK); + unsigned int actions = buttonStore(BUTTON6_PRESS, BUTTON6_CLICK, BUTTON6_DBLCLICK, BUTTON6_LNGCLICK, BUTTON6_LNGLNGCLICK, BUTTON6_TRIPLECLICK); _buttons.push_back({new DebounceEvent(BUTTON6_PIN, BUTTON6_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON6_RELAY}); } #endif #if BUTTON7_PIN != GPIO_NONE { - unsigned int actions = buttonStore(BUTTON7_PRESS, BUTTON7_CLICK, BUTTON7_DBLCLICK, BUTTON7_LNGCLICK, BUTTON7_LNGLNGCLICK); + unsigned int actions = buttonStore(BUTTON7_PRESS, BUTTON7_CLICK, BUTTON7_DBLCLICK, BUTTON7_LNGCLICK, BUTTON7_LNGLNGCLICK, BUTTON7_TRIPLECLICK); _buttons.push_back({new DebounceEvent(BUTTON7_PIN, BUTTON7_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON7_RELAY}); } #endif #if BUTTON8_PIN != GPIO_NONE { - unsigned int actions = buttonStore(BUTTON8_PRESS, BUTTON8_CLICK, BUTTON8_DBLCLICK, BUTTON8_LNGCLICK, BUTTON8_LNGLNGCLICK); + unsigned int actions = buttonStore(BUTTON8_PRESS, BUTTON8_CLICK, BUTTON8_DBLCLICK, BUTTON8_LNGCLICK, BUTTON8_LNGLNGCLICK, BUTTON8_TRIPLECLICK); _buttons.push_back({new DebounceEvent(BUTTON8_PIN, BUTTON8_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON8_RELAY}); } #endif diff --git a/code/espurna/config/defaults.h b/code/espurna/config/defaults.h index 8a5a37f2..5e4a85be 100644 --- a/code/espurna/config/defaults.h +++ b/code/espurna/config/defaults.h @@ -108,6 +108,31 @@ #define BUTTON8_DBLCLICK BUTTON_MODE_NONE #endif +#ifndef BUTTON1_TRIPLECLICK +#define BUTTON1_TRIPLECLICK BUTTON_MODE_WPS +#endif +#ifndef BUTTON2_TRIPLECLICK +#define BUTTON2_TRIPLECLICK BUTTON_MODE_NONE +#endif +#ifndef BUTTON3_TRIPLECLICK +#define BUTTON3_TRIPLECLICK BUTTON_MODE_NONE +#endif +#ifndef BUTTON4_TRIPLECLICK +#define BUTTON4_TRIPLECLICK BUTTON_MODE_NONE +#endif +#ifndef BUTTON5_TRIPLECLICK +#define BUTTON5_TRIPLECLICK BUTTON_MODE_NONE +#endif +#ifndef BUTTON6_TRIPLECLICK +#define BUTTON6_TRIPLECLICK BUTTON_MODE_NONE +#endif +#ifndef BUTTON7_TRIPLECLICK +#define BUTTON7_TRIPLECLICK BUTTON_MODE_NONE +#endif +#ifndef BUTTON8_TRIPLECLICK +#define BUTTON8_TRIPLECLICK BUTTON_MODE_NONE +#endif + #ifndef BUTTON1_LNGCLICK #define BUTTON1_LNGCLICK BUTTON_MODE_RESET #endif diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index 9d10c62a..981aa78d 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -3,6 +3,15 @@ // Do not touch this definitions //------------------------------------------------------------------------------ +// ----------------------------------------------------------------------------- +// WIFI +// ----------------------------------------------------------------------------- + +#define WIFI_STATE_AP 1 +#define WIFI_STATE_STA 2 +#define WIFI_STATE_AP_STA 3 +#define WIFI_STATE_WPS 4 + //------------------------------------------------------------------------------ // BUTTONS //------------------------------------------------------------------------------ @@ -14,6 +23,7 @@ #define BUTTON_EVENT_DBLCLICK 3 #define BUTTON_EVENT_LNGCLICK 4 #define BUTTON_EVENT_LNGLNGCLICK 5 +#define BUTTON_EVENT_TRIPLECLICK 6 #define BUTTON_MODE_NONE 0 #define BUTTON_MODE_TOGGLE 1 @@ -23,6 +33,7 @@ #define BUTTON_MODE_RESET 5 #define BUTTON_MODE_PULSE 6 #define BUTTON_MODE_FACTORY 7 +#define BUTTON_MODE_WPS 8 // Needed for ESP8285 boards under Windows using PlatformIO (?) #ifndef BUTTON_PUSHBUTTON diff --git a/code/espurna/led.ino b/code/espurna/led.ino index b93013f8..30b6aaa6 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -182,16 +182,18 @@ void ledSetup() { void ledLoop() { + uint8_t wifi_state = wifiState(); + for (unsigned char i=0; i<_leds.size(); i++) { if (_ledMode(i) == LED_MODE_WIFI) { - if (wifiConnected()) { - if (WiFi.getMode() == WIFI_AP) { - _ledBlink(i, 900, 100); - } else { - _ledBlink(i, 4900, 100); - } + if (wifi_state & WIFI_STATE_WPS) { + _ledBlink(i, 100, 100); + } else if (wifi_state & WIFI_STATE_STA) { + _ledBlink(i, 4900, 100); + } else if (wifi_state & WIFI_STATE_AP) { + _ledBlink(i, 900, 100); } else { _ledBlink(i, 500, 500); } @@ -200,19 +202,19 @@ void ledLoop() { if (_ledMode(i) == LED_MODE_FINDME_WIFI) { - if (wifiConnected()) { + if (wifi_state & WIFI_STATE_WPS) { + _ledBlink(i, 100, 100); + } else if (wifi_state & WIFI_STATE_STA) { if (relayStatus(_leds[i].relay-1)) { - if (WiFi.getMode() == WIFI_AP) { - _ledBlink(i, 900, 100); - } else { - _ledBlink(i, 4900, 100); - } + _ledBlink(i, 4900, 100); } else { - if (WiFi.getMode() == WIFI_AP) { - _ledBlink(i, 100, 900); - } else { - _ledBlink(i, 100, 4900); - } + _ledBlink(i, 100, 4900); + } + } else if (wifi_state & WIFI_STATE_AP) { + if (relayStatus(_leds[i].relay-1)) { + _ledBlink(i, 900, 100); + } else { + _ledBlink(i, 100, 900); } } else { _ledBlink(i, 500, 500); @@ -222,19 +224,19 @@ void ledLoop() { if (_ledMode(i) == LED_MODE_RELAY_WIFI) { - if (wifiConnected()) { + if (wifi_state & WIFI_STATE_WPS) { + _ledBlink(i, 100, 100); + } else if (wifi_state & WIFI_STATE_STA) { if (relayStatus(_leds[i].relay-1)) { - if (WiFi.getMode() == WIFI_AP) { - _ledBlink(i, 100, 900); - } else { - _ledBlink(i, 100, 4900); - } + _ledBlink(i, 100, 4900); } else { - if (WiFi.getMode() == WIFI_AP) { - _ledBlink(i, 900, 100); - } else { - _ledBlink(i, 4900, 100); - } + _ledBlink(i, 4900, 100); + } + } else if (wifi_state & WIFI_STATE_AP) { + if (relayStatus(_leds[i].relay-1)) { + _ledBlink(i, 100, 900); + } else { + _ledBlink(i, 900, 100); } } else { _ledBlink(i, 500, 500); diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index ce98370a..72654514 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -257,7 +257,7 @@ void _settingsInitCommands() { settingsRegisterCommand(F("INFO"), [](Embedis* e) { info(); - wifiStatus(); + wifiDebug(); //StreamString s; //WiFi.printDiag(s); //DEBUG_MSG(s.c_str()); diff --git a/code/espurna/telnet.ino b/code/espurna/telnet.ino index 006d5d21..4f858ab4 100644 --- a/code/espurna/telnet.ino +++ b/code/espurna/telnet.ino @@ -138,7 +138,7 @@ void _telnetNewClient(AsyncClient *client) { // If there is no terminal support automatically dump info and crash data #if TERMINAL_SUPPORT == 0 info(); - wifiStatus(); + wifiDebug(); debugDumpCrashInfo(); debugClearCrashInfo(); #endif diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index 4e8c00af..32c7e139 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -10,6 +10,7 @@ Copyright (C) 2016-2018 by Xose PĂ©rez #include uint32_t _wifi_scan_client_id = 0; +bool _wifi_wps_running = false; // ----------------------------------------------------------------------------- // PRIVATE @@ -198,6 +199,14 @@ void _wifiInject() { void _wifiWPS(justwifi_messages_t code, char * parameter) { + if (MESSAGE_WPS_START == code) { + _wifi_wps_running = true; + } + + if (MESSAGE_WPS_ERROR == code) { + _wifi_wps_running = false; + } + if (MESSAGE_WPS_SUCCESS == code) { String ssid = WiFi.SSID(); @@ -207,7 +216,7 @@ void _wifiWPS(justwifi_messages_t code, char * parameter) { uint8_t count = 0; while (count < WIFI_MAX_NETWORKS) { if (!hasSetting("ssid", count)) break; - if (getSetting("ssid", count).equals(ssid)) break; + if (ssid.equals(getSetting("ssid", count, ""))) break; count++; } @@ -217,6 +226,8 @@ void _wifiWPS(justwifi_messages_t code, char * parameter) { setSetting("ssid", count, ssid); setSetting("pass", count, pass); + _wifi_wps_running = false; + } } @@ -285,7 +296,7 @@ void _wifiDebug(justwifi_messages_t code, char * parameter) { } if (code == MESSAGE_CONNECTED) { - wifiStatus(); + wifiDebug(); } if (code == MESSAGE_DISCONNECTED) { @@ -299,7 +310,7 @@ void _wifiDebug(justwifi_messages_t code, char * parameter) { } if (code == MESSAGE_ACCESSPOINT_CREATED) { - wifiStatus(); + wifiDebug(); } if (code == MESSAGE_ACCESSPOINT_FAILED) { @@ -339,12 +350,12 @@ void _wifiInitCommands() { }); settingsRegisterCommand(F("WIFI.AP"), [](Embedis* e) { - createAP(); + wifiStartAP(); DEBUG_MSG_P(PSTR("+OK\n")); }); settingsRegisterCommand(F("WIFI.WPS"), [](Embedis* e) { - jw.startWPS(); + wifiStartWPS(); DEBUG_MSG_P(PSTR("+OK\n")); }); @@ -422,12 +433,16 @@ void wifiDisconnect() { jw.disconnect(); } -bool createAP() { +bool wifiStartAP() { jw.disconnect(); jw.resetReconnectTimeout(); return jw.createAP(); } +void wifiStartWPS() { + jw.startWPS(); +} + void wifiReconnectCheck() { bool connected = false; #if WEB_SUPPORT @@ -439,7 +454,15 @@ void wifiReconnectCheck() { jw.setReconnectTimeout(connected ? 0 : WIFI_RECONNECT_INTERVAL); } -void wifiStatus() { +uint8_t wifiState() { + uint8_t state = 0; + if (jw.connected()) state += WIFI_STATE_STA; + if (jw.connectable()) state += WIFI_STATE_AP; + if (_wifi_wps_running) state += WIFI_STATE_WPS; + return state; +} + +void wifiDebug() { if (WiFi.getMode() == WIFI_AP_STA) { DEBUG_MSG_P(PSTR("[WIFI] MODE AP + STA --------------------------------\n"));