Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.9 KiB

5 years ago
  1. /*
  2. LightFox module
  3. Copyright (C) 2019 by Andrey F. Kupreychik <foxle@quickfox.ru>
  4. */
  5. #ifdef FOXEL_LIGHTFOX_DUAL
  6. // -----------------------------------------------------------------------------
  7. // DEFINITIONS
  8. // -----------------------------------------------------------------------------
  9. #define LIGHTFOX_CODE_START 0xA0
  10. #define LIGHTFOX_CODE_LEARN 0xF1
  11. #define LIGHTFOX_CODE_CLEAR 0xF2
  12. #define LIGHTFOX_CODE_STOP 0xA1
  13. // -----------------------------------------------------------------------------
  14. // PUBLIC
  15. // -----------------------------------------------------------------------------
  16. void lightfoxLearn() {
  17. Serial.write(LIGHTFOX_CODE_START);
  18. Serial.write(LIGHTFOX_CODE_LEARN);
  19. Serial.write(0x00);
  20. Serial.write(LIGHTFOX_CODE_STOP);
  21. Serial.println();
  22. Serial.flush();
  23. DEBUG_MSG_P(PSTR("[LIGHTFOX] Learn comman sent\n"));
  24. }
  25. void lightfoxClear() {
  26. Serial.write(LIGHTFOX_CODE_START);
  27. Serial.write(LIGHTFOX_CODE_CLEAR);
  28. Serial.write(0x00);
  29. Serial.write(LIGHTFOX_CODE_STOP);
  30. Serial.println();
  31. Serial.flush();
  32. DEBUG_MSG_P(PSTR("[LIGHTFOX] Clear comman sent\n"));
  33. }
  34. // -----------------------------------------------------------------------------
  35. // WEB
  36. // -----------------------------------------------------------------------------
  37. #if WEB_SUPPORT
  38. void _lightfoxWebSocketOnSend(JsonObject& root) {
  39. root["lightfoxVisible"] = 1;
  40. uint8_t buttonsCount = _buttons.size();
  41. root["lightfoxRelayCount"] = relayCount();
  42. JsonArray& rfb = root.createNestedArray("lightfoxButtons");
  43. for (byte id=0; id<buttonsCount; id++) {
  44. JsonObject& node = rfb.createNestedObject();
  45. node["id"] = id;
  46. node["relay"] = getSetting("btnRelay", id, "0");
  47. }
  48. }
  49. void _lightfoxWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  50. if (strcmp(action, "lightfoxLearn") == 0) lightfoxLearn();
  51. if (strcmp(action, "lightfoxClear") == 0) lightfoxClear();
  52. }
  53. #endif
  54. // -----------------------------------------------------------------------------
  55. // TERMINAL
  56. // -----------------------------------------------------------------------------
  57. #if TERMINAL_SUPPORT
  58. void _lightfoxInitCommands() {
  59. settingsRegisterCommand(F("LIGHTFOX.LEARN"), [](Embedis* e) {
  60. lightfoxLearn();
  61. DEBUG_MSG_P(PSTR("+OK\n"));
  62. });
  63. settingsRegisterCommand(F("LIGHTFOX.CLEAR"), [](Embedis* e) {
  64. lightfoxClear();
  65. DEBUG_MSG_P(PSTR("+OK\n"));
  66. });
  67. }
  68. #endif
  69. // -----------------------------------------------------------------------------
  70. // SETUP & LOOP
  71. // -----------------------------------------------------------------------------
  72. void lightfoxSetup() {
  73. #if WEB_SUPPORT
  74. wsOnSendRegister(_lightfoxWebSocketOnSend);
  75. wsOnActionRegister(_lightfoxWebSocketOnAction);
  76. #endif
  77. #if TERMINAL_SUPPORT
  78. _lightfoxInitCommands();
  79. #endif
  80. }
  81. #endif