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.

142 lines
3.9 KiB

  1. /*
  2. BUTTON MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. // -----------------------------------------------------------------------------
  6. // BUTTON
  7. // -----------------------------------------------------------------------------
  8. #ifdef SONOFF_DUAL
  9. #ifdef MQTT_BUTTON_TOPIC
  10. void buttonMQTT(unsigned char id) {
  11. String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
  12. char buffer[strlen(MQTT_BUTTON_TOPIC) + mqttGetter.length() + 3];
  13. sprintf(buffer, "%s/%d%s", MQTT_BUTTON_TOPIC, id, mqttGetter.c_str());
  14. mqttSend(buffer, 1);
  15. mqttSend(buffer, 0);
  16. }
  17. #endif
  18. void buttonSetup() {}
  19. void buttonLoop() {
  20. if (Serial.available() >= 4) {
  21. unsigned char value;
  22. if (Serial.read() == 0xA0) {
  23. if (Serial.read() == 0x04) {
  24. value = Serial.read();
  25. if (Serial.read() == 0xA1) {
  26. // RELAYs and BUTTONs are synchonized in the SIL F330
  27. // The on-board BUTTON2 should toggle RELAY0 value
  28. // Since we are not passing back RELAY2 value
  29. // (in the relayStatus method) it will only be present
  30. // here if it has actually been pressed
  31. if ((value & 4) == 4) {
  32. value = value ^ 1;
  33. #ifdef MQTT_BUTTON_TOPIC
  34. buttonMQTT(0);
  35. #endif
  36. }
  37. // Otherwise check if any of the other two BUTTONs
  38. // (in the header) has been pressent, but we should
  39. // ensure that we only toggle one of them to avoid
  40. // the synchronization going mad
  41. // This loop is generic for any PSB-04 module
  42. for (unsigned int i=0; i<relayCount(); i++) {
  43. bool status = (value & (1 << i)) > 0;
  44. // relayStatus returns true if the status has changed
  45. if (relayStatus(i, status)) break;
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. #else
  53. #ifdef BUTTON1_PIN
  54. #include <DebounceEvent.h>
  55. #include <vector>
  56. typedef struct {
  57. DebounceEvent * button;
  58. unsigned int relayID;
  59. } button_t;
  60. std::vector<button_t> _buttons;
  61. #ifdef MQTT_BUTTON_TOPIC
  62. void buttonMQTT(unsigned char id) {
  63. if (id >= _buttons.size()) return;
  64. String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
  65. char buffer[strlen(MQTT_BUTTON_TOPIC) + mqttGetter.length() + 3];
  66. sprintf(buffer, "%s/%d%s", MQTT_BUTTON_TOPIC, id, mqttGetter.c_str());
  67. mqttSend(buffer, _buttons[id].button->pressed() ? "1" : "0");
  68. }
  69. #endif
  70. void buttonSetup() {
  71. #ifdef BUTTON1_PIN
  72. _buttons.push_back({new DebounceEvent(BUTTON1_PIN), BUTTON1_RELAY});
  73. #endif
  74. #ifdef BUTTON2_PIN
  75. _buttons.push_back({new DebounceEvent(BUTTON2_PIN), BUTTON2_RELAY});
  76. #endif
  77. #ifdef BUTTON3_PIN
  78. _buttons.push_back({new DebounceEvent(BUTTON3_PIN), BUTTON3_RELAY});
  79. #endif
  80. #ifdef BUTTON4_PIN
  81. _buttons.push_back({new DebounceEvent(BUTTON4_PIN), BUTTON4_RELAY});
  82. #endif
  83. DEBUG_MSG("[BUTTON] Number of buttons: %d\n", _buttons.size());
  84. }
  85. void buttonLoop() {
  86. for (unsigned int i=0; i < _buttons.size(); i++) {
  87. if (_buttons[i].button->loop()) {
  88. uint8_t event = _buttons[i].button->getEvent();
  89. DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event);
  90. #ifdef MQTT_BUTTON_TOPIC
  91. buttonMQTT(i);
  92. #endif
  93. if (i == 0) {
  94. if (event == EVENT_DOUBLE_CLICK) createAP();
  95. if (event == EVENT_LONG_CLICK) ESP.reset();
  96. }
  97. if (event == EVENT_SINGLE_CLICK) {
  98. if (_buttons[i].relayID > 0) {
  99. relayToggle(_buttons[i].relayID - 1);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. #else
  106. void buttonSetup() {}
  107. void buttonLoop() {}
  108. #endif
  109. #endif