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.

102 lines
2.3 KiB

6 years ago
  1. /*
  2. RF MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if RF_SUPPORT
  6. #include <RemoteReceiver.h>
  7. unsigned long rfCode = 0;
  8. unsigned long rfCodeON = 0;
  9. unsigned long rfCodeOFF = 0;
  10. // -----------------------------------------------------------------------------
  11. // RF
  12. // -----------------------------------------------------------------------------
  13. void _rfWebSocketOnSend(JsonObject& root) {
  14. root["rfVisible"] = 1;
  15. root["rfChannel"] = getSetting("rfChannel", RF_CHANNEL);
  16. root["rfDevice"] = getSetting("rfDevice", RF_DEVICE);
  17. }
  18. void _rfBuildCodes() {
  19. unsigned long code = 0;
  20. // channel
  21. unsigned int channel = getSetting("rfChannel", RF_CHANNEL).toInt();
  22. for (byte i = 0; i < 5; i++) {
  23. code *= 3;
  24. if (channel & 1) code += 1;
  25. channel >>= 1;
  26. }
  27. // device
  28. unsigned int device = getSetting("rfDevice", RF_DEVICE).toInt();
  29. for (byte i = 0; i < 5; i++) {
  30. code *= 3;
  31. if (device != i) code += 2;
  32. }
  33. // status
  34. code *= 9;
  35. rfCodeOFF = code + 2;
  36. rfCodeON = code + 6;
  37. DEBUG_MSG_P(PSTR("[RF] Code ON : %lu\n"), rfCodeON);
  38. DEBUG_MSG_P(PSTR("[RF] Code OFF: %lu\n"), rfCodeOFF);
  39. }
  40. // -----------------------------------------------------------------------------
  41. void rfLoop() {
  42. if (rfCode == 0) return;
  43. DEBUG_MSG_P(PSTR("[RF] Received code: %lu\n"), rfCode);
  44. if (rfCode == rfCodeON) relayStatus(0, true);
  45. if (rfCode == rfCodeOFF) relayStatus(0, false);
  46. rfCode = 0;
  47. }
  48. void rfCallback(unsigned long code, unsigned int period) {
  49. rfCode = code;
  50. }
  51. void rfSetup() {
  52. pinMode(RF_PIN, INPUT_PULLUP);
  53. _rfBuildCodes();
  54. RemoteReceiver::init(RF_PIN, 3, rfCallback);
  55. RemoteReceiver::disable();
  56. DEBUG_MSG_P(PSTR("[RF] Disabled\n"));
  57. wifiRegister([](justwifi_messages_t code, char * parameter) {
  58. if (code == MESSAGE_CONNECTED || code == MESSAGE_ACCESSPOINT_CREATED) {
  59. RemoteReceiver::enable();
  60. DEBUG_MSG_P(PSTR("[RF] Enabled\n"));
  61. }
  62. if (code == MESSAGE_DISCONNECTED)
  63. RemoteReceiver::disable();
  64. DEBUG_MSG_P(PSTR("[RF] Disabled\n"));
  65. }
  66. });
  67. #if WEB_SUPPORT
  68. wsOnSendRegister(_rfWebSocketOnSend);
  69. wsOnAfterParseRegister(_rfBuildCodes);
  70. #endif
  71. // Register loop
  72. espurnaRegisterLoop(rfLoop);
  73. }
  74. #endif