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.

194 lines
5.2 KiB

7 years ago
  1. /*
  2. RF MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. Module key prefix: rfb (shared with RFBridge)
  5. */
  6. #if RF_SUPPORT
  7. #include <RCSwitch.h>
  8. RCSwitch * _rfModem;
  9. unsigned long _rf_learn_start = 0;
  10. unsigned char _rf_learn_id = 0;
  11. bool _rf_learn_status = true;
  12. bool _rf_learn_active = false;
  13. // -----------------------------------------------------------------------------
  14. // RF
  15. // -----------------------------------------------------------------------------
  16. unsigned long _rfRetrieve(unsigned char id, bool status) {
  17. String code = getSetting(status ? "rfbON" : "rfbOFF", id, "0");
  18. return strtoul(code.c_str(), 0, 16);
  19. }
  20. void _rfStore(unsigned char id, bool status, unsigned long code) {
  21. DEBUG_MSG_P(PSTR("[RF] Storing %d-%s => %X\n"), id, status ? "ON" : "OFF", code);
  22. char buffer[20];
  23. snprintf_P(buffer, sizeof(buffer), PSTR("%X"), code);
  24. setSetting(status ? "rfbON" : "rfbOFF", id, buffer);
  25. }
  26. void _rfLearn(unsigned char id, bool status) {
  27. _rf_learn_start = millis();
  28. _rf_learn_id = id;
  29. _rf_learn_status = status;
  30. _rf_learn_active = true;
  31. }
  32. void _rfForget(unsigned char id, bool status) {
  33. delSetting(status ? "rfbON" : "rfbOFF", id);
  34. // Websocket update
  35. #if WEB_SUPPORT
  36. char wsb[100];
  37. snprintf_P(wsb, sizeof(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"\"}]}"), id, status ? 1 : 0);
  38. wsSend(wsb);
  39. #endif
  40. }
  41. bool _rfMatch(unsigned long code, unsigned char& relayID, unsigned char& value) {
  42. bool found = false;
  43. DEBUG_MSG_P(PSTR("[RF] Trying to match code %X\n"), code);
  44. for (unsigned char i=0; i<relayCount(); i++) {
  45. unsigned long code_on = _rfRetrieve(i, true);
  46. unsigned long code_off = _rfRetrieve(i, false);
  47. if (code == code_on) {
  48. DEBUG_MSG_P(PSTR("[RF] Match ON code for relay %d\n"), i);
  49. value = 1;
  50. found = true;
  51. }
  52. if (code == code_off) {
  53. DEBUG_MSG_P(PSTR("[RF] Match OFF code for relay %d\n"), i);
  54. if (found) value = 2;
  55. found = true;
  56. }
  57. if (found) {
  58. relayID = i;
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. // -----------------------------------------------------------------------------
  65. // WEB
  66. // -----------------------------------------------------------------------------
  67. void _rfWebSocketOnSend(JsonObject& root) {
  68. char buffer[20];
  69. root["rfbVisible"] = 1;
  70. root["rfbCount"] = relayCount();
  71. JsonArray& rfb = root.createNestedArray("rfb");
  72. for (byte id=0; id<relayCount(); id++) {
  73. for (byte status=0; status<2; status++) {
  74. JsonObject& node = rfb.createNestedObject();
  75. snprintf_P(buffer, sizeof(buffer), PSTR("%X"), _rfRetrieve(id, status == 1));
  76. node["id"] = id;
  77. node["status"] = status;
  78. node["data"] = String(buffer);
  79. }
  80. }
  81. }
  82. void _rfWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  83. if (strcmp(action, "rfblearn") == 0) _rfLearn(data["id"], data["status"]);
  84. if (strcmp(action, "rfbforget") == 0) _rfForget(data["id"], data["status"]);
  85. if (strcmp(action, "rfbsend") == 0) _rfStore(data["id"], data["status"], data["data"].as<long>());
  86. }
  87. // -----------------------------------------------------------------------------
  88. void rfLoop() {
  89. if (_rfModem->available()) {
  90. static unsigned long last = 0;
  91. if (millis() - last > RF_DEBOUNCE) {
  92. last = millis();
  93. if (_rfModem->getReceivedValue() > 0) {
  94. unsigned long rf_code = _rfModem->getReceivedValue();
  95. DEBUG_MSG_P(PSTR("[RF] Received code: %X\n"), rf_code);
  96. if (_rf_learn_active) {
  97. _rf_learn_active = false;
  98. _rfStore(_rf_learn_id, _rf_learn_status, rf_code);
  99. // Websocket update
  100. #if WEB_SUPPORT
  101. char wsb[100];
  102. snprintf_P(
  103. wsb, sizeof(wsb),
  104. PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"%X\"}]}"),
  105. _rf_learn_id, _rf_learn_status ? 1 : 0, rf_code);
  106. wsSend(wsb);
  107. #endif
  108. } else {
  109. unsigned char id;
  110. unsigned char value;
  111. if (_rfMatch(rf_code, id, value)) {
  112. if (2 == value) {
  113. relayToggle(id);
  114. } else {
  115. relayStatus(id, 1 == value);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. _rfModem->resetAvailable();
  122. }
  123. if (_rf_learn_active && (millis() - _rf_learn_start > RF_LEARN_TIMEOUT)) {
  124. _rf_learn_active = false;
  125. }
  126. }
  127. void rfSetup() {
  128. _rfModem = new RCSwitch();
  129. _rfModem->enableReceive(RF_PIN);
  130. DEBUG_MSG_P(PSTR("[RF] RF receiver on GPIO %u\n"), RF_PIN);
  131. #if WEB_SUPPORT
  132. wsOnSendRegister(_rfWebSocketOnSend);
  133. wsOnActionRegister(_rfWebSocketOnAction);
  134. #endif
  135. // Register loop
  136. espurnaRegisterLoop(rfLoop);
  137. }
  138. #endif