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.

106 lines
2.7 KiB

  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. return;
  43. if (rfCode == 0) return;
  44. DEBUG_MSG_P(PSTR("[RF] Received code: %lu\n"), rfCode);
  45. if (rfCode == rfCodeON) relayStatus(0, true);
  46. if (rfCode == rfCodeOFF) relayStatus(0, false);
  47. rfCode = 0;
  48. }
  49. void rfCallback(unsigned long code, unsigned int period) {
  50. rfCode = code;
  51. }
  52. void rfSetup() {
  53. pinMode(RF_PIN, INPUT_PULLUP);
  54. _rfBuildCodes();
  55. RemoteReceiver::init(RF_PIN, 3, rfCallback);
  56. RemoteReceiver::disable();
  57. DEBUG_MSG_P(PSTR("[RF] Disabled\n"));
  58. static WiFiEventHandler e1 = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) {
  59. RemoteReceiver::disable();
  60. DEBUG_MSG_P(PSTR("[RF] Disabled\n"));
  61. });
  62. static WiFiEventHandler e2 = WiFi.onSoftAPModeStationDisconnected([](const WiFiEventSoftAPModeStationDisconnected& event) {
  63. RemoteReceiver::disable();
  64. DEBUG_MSG_P(PSTR("[RF] Disabled\n"));
  65. });
  66. static WiFiEventHandler e3 = WiFi.onStationModeConnected([](const WiFiEventStationModeConnected& event) {
  67. RemoteReceiver::enable();
  68. DEBUG_MSG_P(PSTR("[RF] Enabled\n"));
  69. });
  70. static WiFiEventHandler e4 = WiFi.onSoftAPModeStationConnected([](const WiFiEventSoftAPModeStationConnected& event) {
  71. RemoteReceiver::enable();
  72. DEBUG_MSG_P(PSTR("[RF] Enabled\n"));
  73. });
  74. #if WEB_SUPPORT
  75. wsOnSendRegister(_rfWebSocketOnSend);
  76. wsOnAfterParseRegister(_rfBuildCodes);
  77. #endif
  78. }
  79. #endif