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.

94 lines
2.3 KiB

  1. /*
  2. ESPurna
  3. RF MODULE
  4. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. #if ENABLE_RF
  7. #include <RemoteReceiver.h>
  8. unsigned long rfCode = 0;
  9. unsigned long rfCodeON = 0;
  10. unsigned long rfCodeOFF = 0;
  11. // -----------------------------------------------------------------------------
  12. // RF
  13. // -----------------------------------------------------------------------------
  14. void rfLoop() {
  15. return;
  16. if (rfCode == 0) return;
  17. DEBUG_MSG("[RF] Received code: %lu\n", rfCode);
  18. if (rfCode == rfCodeON) relayStatus(0, true);
  19. if (rfCode == rfCodeOFF) relayStatus(0, false);
  20. rfCode = 0;
  21. }
  22. void rfBuildCodes() {
  23. unsigned long code = 0;
  24. // channel
  25. unsigned int channel = getSetting("rfChannel", RF_CHANNEL).toInt();
  26. for (byte i = 0; i < 5; i++) {
  27. code *= 3;
  28. if (channel & 1) code += 1;
  29. channel >>= 1;
  30. }
  31. // device
  32. unsigned int device = getSetting("rfDevice", RF_DEVICE).toInt();
  33. for (byte i = 0; i < 5; i++) {
  34. code *= 3;
  35. if (device != i) code += 2;
  36. }
  37. // status
  38. code *= 9;
  39. rfCodeOFF = code + 2;
  40. rfCodeON = code + 6;
  41. DEBUG_MSG("[RF] Code ON : %lu\n", rfCodeON);
  42. DEBUG_MSG("[RF] Code OFF: %lu\n", rfCodeOFF);
  43. }
  44. void rfCallback(unsigned long code, unsigned int period) {
  45. rfCode = code;
  46. }
  47. void rfSetup() {
  48. pinMode(RF_PIN, INPUT_PULLUP);
  49. rfBuildCodes();
  50. RemoteReceiver::init(RF_PIN, 3, rfCallback);
  51. RemoteReceiver::disable();
  52. DEBUG_MSG("[RF] Disabled\n");
  53. static WiFiEventHandler e1 = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) {
  54. RemoteReceiver::disable();
  55. DEBUG_MSG("[RF] Disabled\n");
  56. });
  57. static WiFiEventHandler e2 = WiFi.onSoftAPModeStationDisconnected([](const WiFiEventSoftAPModeStationDisconnected& event) {
  58. RemoteReceiver::disable();
  59. DEBUG_MSG("[RF] Disabled\n");
  60. });
  61. static WiFiEventHandler e3 = WiFi.onStationModeConnected([](const WiFiEventStationModeConnected& event) {
  62. RemoteReceiver::enable();
  63. DEBUG_MSG("[RF] Enabled\n");
  64. });
  65. static WiFiEventHandler e4 = WiFi.onSoftAPModeStationConnected([](const WiFiEventSoftAPModeStationConnected& event) {
  66. RemoteReceiver::enable();
  67. DEBUG_MSG("[RF] Enabled\n");
  68. });
  69. }
  70. #endif