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.

160 lines
4.3 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. #ifdef LED_PULSE
  84. pinMode(LED_PULSE, OUTPUT);
  85. byte relayPulseMode = getSetting("relayPulseMode", String(RELAY_PULSE_MODE)).toInt();
  86. digitalWrite(LED_PULSE, relayPulseMode != RELAY_PULSE_NONE);
  87. #endif
  88. DEBUG_MSG("[BUTTON] Number of buttons: %d\n", _buttons.size());
  89. }
  90. void buttonLoop() {
  91. for (unsigned int i=0; i < _buttons.size(); i++) {
  92. if (_buttons[i].button->loop()) {
  93. uint8_t event = _buttons[i].button->getEvent();
  94. DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event);
  95. #ifdef MQTT_BUTTON_TOPIC
  96. buttonMQTT(i);
  97. #endif
  98. if (i == 0) {
  99. if (event == EVENT_DOUBLE_CLICK) createAP();
  100. if (event == EVENT_LONG_CLICK) ESP.reset();
  101. }
  102. #ifdef ITEAD_1CH_INCHING
  103. if (i == 1) {
  104. relayPulseToggle();
  105. continue;
  106. }
  107. #endif
  108. if (event == EVENT_SINGLE_CLICK) {
  109. if (_buttons[i].relayID > 0) {
  110. relayToggle(_buttons[i].relayID - 1);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. #else
  117. void buttonSetup() {}
  118. void buttonLoop() {}
  119. #endif
  120. #endif