diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 0f545d75..3a017afc 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -187,6 +187,9 @@ void setup() { #if UART_MQTT_SUPPORT uartmqttSetup(); #endif + #ifdef FOXEL_LIGHTFOX_DUAL + lightfoxSetup(); + #endif // 3rd party code hook diff --git a/code/espurna/lightfox.ino b/code/espurna/lightfox.ino new file mode 100644 index 00000000..e13220ec --- /dev/null +++ b/code/espurna/lightfox.ino @@ -0,0 +1,99 @@ +/* + +LightFox module + +Copyright (C) 2019 by Andrey F. Kupreychik + +*/ + +#ifdef FOXEL_LIGHTFOX_DUAL + +// ----------------------------------------------------------------------------- +// DEFINITIONS +// ----------------------------------------------------------------------------- + +#define LIGHTFOX_CODE_START 0xA0 +#define LIGHTFOX_CODE_LEARN 0xF1 +#define LIGHTFOX_CODE_CLEAR 0xF2 +#define LIGHTFOX_CODE_STOP 0xA1 + +// ----------------------------------------------------------------------------- +// PUBLIC +// ----------------------------------------------------------------------------- + +void lightfoxLearn() { + Serial.write(LIGHTFOX_CODE_START); + Serial.write(LIGHTFOX_CODE_LEARN); + Serial.write(0x00); + Serial.write(LIGHTFOX_CODE_STOP); + Serial.println(); + Serial.flush(); + DEBUG_MSG_P(PSTR("[LIGHTFOX] Learn comman sent\n")); +} + +void lightfoxClear() { + Serial.write(LIGHTFOX_CODE_START); + Serial.write(LIGHTFOX_CODE_CLEAR); + Serial.write(0x00); + Serial.write(LIGHTFOX_CODE_STOP); + Serial.println(); + Serial.flush(); + DEBUG_MSG_P(PSTR("[LIGHTFOX] Clear comman sent\n")); +} + +// ----------------------------------------------------------------------------- +// WEB +// ----------------------------------------------------------------------------- + +#if WEB_SUPPORT + +void _lightfoxWebSocketOnSend(JsonObject& root) { + root["lightfoxVisible"] = 1; +} + +void _lightfoxWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) { + if (strcmp(action, "lightfoxLearn") == 0) lightfoxLearn(); + if (strcmp(action, "lightfoxClear") == 0) lightfoxClear(); +} + +#endif + +// ----------------------------------------------------------------------------- +// TERMINAL +// ----------------------------------------------------------------------------- + +#if TERMINAL_SUPPORT + +void _lightfoxInitCommands() { + + settingsRegisterCommand(F("LIGHTFOX.LEARN"), [](Embedis* e) { + lightfoxLearn(); + DEBUG_MSG_P(PSTR("+OK\n")); + }); + + settingsRegisterCommand(F("LIGHTFOX.CLEAR"), [](Embedis* e) { + lightfoxClear(); + DEBUG_MSG_P(PSTR("+OK\n")); + }); +} + +#endif + +// ----------------------------------------------------------------------------- +// SETUP & LOOP +// ----------------------------------------------------------------------------- + +void lightfoxSetup() { + + #if WEB_SUPPORT + wsOnSendRegister(_lightfoxWebSocketOnSend); + wsOnActionRegister(_lightfoxWebSocketOnAction); + #endif + + #if TERMINAL_SUPPORT + _lightfoxInitCommands(); + #endif + +} + +#endif