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.

93 lines
2.3 KiB

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