From 4202d3769cbb1350c552f3d5741049b42d3cc5ee Mon Sep 17 00:00:00 2001 From: "Andrey F. Kupreychik" Date: Thu, 20 Dec 2018 01:09:08 +0700 Subject: [PATCH] First try for LightFox dual --- code/espurna/button.ino | 39 ++++++++++++++++++++++++++++++++-- code/espurna/config/arduino.h | 1 + code/espurna/config/hardware.h | 21 ++++++++++++++++++ code/espurna/migrate.ino | 10 +++++++++ code/espurna/relay.ino | 2 ++ code/espurna/system.ino | 2 +- code/platformio.ini | 23 ++++++++++++++++++++ 7 files changed, 95 insertions(+), 3 deletions(-) diff --git a/code/espurna/button.ino b/code/espurna/button.ino index 89e048e4..fe4de9f9 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -140,13 +140,25 @@ void buttonEvent(unsigned int id, unsigned char event) { void buttonSetup() { - #ifdef ITEAD_SONOFF_DUAL + #if defined(ITEAD_SONOFF_DUAL) 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}); + #elif defined(FOXEL_LIGHTFOX_DUAL) + + unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE); + unsigned int btn1Relay = getSetting("btnRelay", 0, BUTTON1_RELAY - 1).toInt() + 1; + _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn1Relay}); + unsigned int btn2Relay = getSetting("btnRelay", 1, BUTTON2_RELAY - 1).toInt() + 1; + _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn2Relay}); + unsigned int btn3Relay = getSetting("btnRelay", 2, BUTTON3_RELAY - 1).toInt() + 1; + _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn3Relay}); + unsigned int btn4Relay = getSetting("btnRelay", 3, BUTTON4_RELAY - 1).toInt() + 1; + _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn4Relay}); + #else unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt(); @@ -216,7 +228,7 @@ void buttonSetup() { void buttonLoop() { - #ifdef ITEAD_SONOFF_DUAL + #if defined(ITEAD_SONOFF_DUAL) if (Serial.available() >= 4) { if (Serial.read() == 0xA0) { @@ -256,6 +268,29 @@ void buttonLoop() { } } + #elif defined(FOXEL_LIGHTFOX_DUAL) + + if (Serial.available() >= 4) { + if (Serial.read() == 0xA0) { + if (Serial.read() == 0x04) { + unsigned char value = Serial.read(); + if (Serial.read() == 0xA1) { + + DEBUG_MSG_P(PSTR("[BUTTON] [LIGHTFOX] Received buttons mask: %d\n"), value); + + for (unsigned int i=0; i<_buttons.size(); i++) { + + bool clicked = (value & (1 << i)) > 0; + + if (clicked) { + buttonEvent(i, BUTTON_EVENT_CLICK); + } + } + } + } + } + } + #else for (unsigned int i=0; i < _buttons.size(); i++) { diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index fe4b081a..e30cc24a 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -100,6 +100,7 @@ //#define PHYX_ESP12_RGB //#define IWOOLE_LED_TABLE_LAMP //#define EXS_WIFI_RELAY_V50 +//#define FOXEL_LIGHTFOX_DUAL //-------------------------------------------------------------------------------- // Features (values below are non-default values) diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 6068a274..0a644bce 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -2852,6 +2852,27 @@ #define LED1_PIN 4 #define LED1_PIN_INVERSE 1 +// ----------------------------------------------------------------------------- +// Foxel's LightFox dual +// https://github.com/foxel/esp-dual-rf-switch +// ----------------------------------------------------------------------------- + +#elif defined(FOXEL_LIGHTFOX_DUAL) + + // Info + #define MANUFACTURER "Foxel" + #define DEVICE "LightFox dual" + #define SERIAL_BAUDRATE 19200 + #define RELAY_PROVIDER RELAY_PROVIDER_DUAL + #define DUMMY_RELAY_COUNT 2 + #define DEBUG_SERIAL_SUPPORT 0 + + // Buttons + #define BUTTON1_RELAY 1 + #define BUTTON2_RELAY 2 + #define BUTTON3_RELAY 2 + #define BUTTON4_RELAY 1 + // ----------------------------------------------------------------------------- // TEST boards (do not use!!) // ----------------------------------------------------------------------------- diff --git a/code/espurna/migrate.ino b/code/espurna/migrate.ino index 4e45cfec..1664f808 100644 --- a/code/espurna/migrate.ino +++ b/code/espurna/migrate.ino @@ -1223,6 +1223,16 @@ void migrate() { setSetting("ledGPIO", 1, 15); setSetting("ledLogic", 1, 0); + #elif defined(FOXEL_LIGHTFOX_DUAL) + + setSetting("board", 92); + setSetting("btnRelay", 0, 0); + setSetting("btnRelay", 1, 1); + setSetting("btnRelay", 2, 1); + setSetting("btnRelay", 3, 0); + setSetting("relayProvider", RELAY_PROVIDER_DUAL); + setSetting("relays", 2); + #else // Allow users to define new settings without migration config diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index df01fbc4..84409754 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -67,6 +67,8 @@ void _relayProviderStatus(unsigned char id, bool status) { if (_relays[i].current_status) mask = mask + (1 << i); } + DEBUG_MSG_P(PSTR("[RELAY] [DUAL] Sending relay mask: %d\n"), mask); + // Send it to F330 Serial.flush(); Serial.write(0xA0); diff --git a/code/espurna/system.ino b/code/espurna/system.ino index c988c6cd..b7e402c0 100644 --- a/code/espurna/system.ino +++ b/code/espurna/system.ino @@ -144,7 +144,7 @@ void _systemSetupSpecificHardware() { // These devices use the hardware UART // to communicate to secondary microcontrollers - #if defined(ITEAD_SONOFF_RFBRIDGE) || defined(ITEAD_SONOFF_DUAL) || (RELAY_PROVIDER == RELAY_PROVIDER_STM) + #if defined(ITEAD_SONOFF_RFBRIDGE) || (RELAY_PROVIDER == RELAY_PROVIDER_DUAL) || (RELAY_PROVIDER == RELAY_PROVIDER_STM) Serial.begin(SERIAL_BAUDRATE); #endif diff --git a/code/platformio.ini b/code/platformio.ini index 9d81cf13..ffe3580b 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -394,6 +394,29 @@ extra_scripts = ${common.extra_scripts} # ------------------------------------------------------------------------------ +[env:foxel-lightfox-dual] +platform = ${common.platform} +framework = ${common.framework} +board = ${common.board_1m} +board_build.flash_mode = ${common.flash_mode} +lib_deps = ${common.lib_deps} +lib_ignore = ${common.lib_ignore} +build_flags = ${common.build_flags_1m0m} -DFOXEL_LIGHTFOX_DUAL +extra_scripts = ${common.extra_scripts} + +[env:foxel-lightfox-dual-ota] +platform = ${common.platform} +framework = ${common.framework} +board = ${common.board_1m} +board_build.flash_mode = ${common.flash_mode} +lib_deps = ${common.lib_deps} +lib_ignore = ${common.lib_ignore} +build_flags = ${common.build_flags_1m0m} -DFOXEL_LIGHTFOX_DUAL +upload_speed = ${common.upload_speed} +upload_port = ${common.upload_port} +upload_flags = ${common.upload_flags} +extra_scripts = ${common.extra_scripts} + [env:itead-sonoff-basic] platform = ${common.platform} framework = ${common.framework}