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.

197 lines
6.7 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. #include <DebounceEvent.h>
  9. #include <vector>
  10. typedef struct {
  11. DebounceEvent * button;
  12. unsigned long actions;
  13. unsigned int relayID;
  14. } button_t;
  15. std::vector<button_t> _buttons;
  16. #ifdef MQTT_BUTTON_TOPIC
  17. void buttonMQTT(unsigned char id, uint8_t event) {
  18. if (id >= _buttons.size()) return;
  19. String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
  20. char buffer[strlen(MQTT_BUTTON_TOPIC) + mqttGetter.length() + 3];
  21. sprintf(buffer, "%s/%d%s", MQTT_BUTTON_TOPIC, id, mqttGetter.c_str());
  22. char payload[2];
  23. sprintf(payload, "%d", event);
  24. mqttSend(buffer, payload);
  25. }
  26. #endif
  27. unsigned char buttonAction(unsigned char id, unsigned char event) {
  28. if (id >= _buttons.size()) return BUTTON_MODE_NONE;
  29. unsigned long actions = _buttons[id].actions;
  30. if (event == BUTTON_EVENT_PRESSED) return (actions) & 0x0F;
  31. if (event == BUTTON_EVENT_CLICK) return (actions >> 4) & 0x0F;
  32. if (event == BUTTON_EVENT_DBLCLICK) return (actions >> 8) & 0x0F;
  33. if (event == BUTTON_EVENT_LNGCLICK) return (actions >> 12) & 0x0F;
  34. if (event == BUTTON_EVENT_LNGLNGCLICK) return (actions >> 16) & 0x0F;
  35. return BUTTON_MODE_NONE;
  36. }
  37. unsigned long buttonStore(unsigned char pressed, unsigned char click, unsigned char dblclick, unsigned char lngclick, unsigned char lnglngclick) {
  38. unsigned int value;
  39. value = pressed;
  40. value += click << 4;
  41. value += dblclick << 8;
  42. value += lngclick << 12;
  43. value += lnglngclick << 16;
  44. return value;
  45. }
  46. uint8_t mapEvent(uint8_t event, uint8_t count, uint16_t length) {
  47. if (event == EVENT_PRESSED) return BUTTON_EVENT_PRESSED;
  48. if (event == EVENT_CHANGED) return BUTTON_EVENT_CLICK;
  49. if (event == EVENT_RELEASED) {
  50. if (count == 1) {
  51. if (length > BUTTON_LNGLNGCLICK_LENGTH) return BUTTON_EVENT_LNGLNGCLICK;
  52. if (length > BUTTON_LNGCLICK_LENGTH) return BUTTON_EVENT_LNGCLICK;
  53. return BUTTON_EVENT_CLICK;
  54. }
  55. if (count == 2) return BUTTON_EVENT_DBLCLICK;
  56. }
  57. }
  58. void buttonEvent(unsigned int id, unsigned char event) {
  59. DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", id, event);
  60. if (event == 0) return;
  61. #ifdef MQTT_BUTTON_TOPIC
  62. buttonMQTT(id, event);
  63. #endif
  64. unsigned char action = buttonAction(id, event);
  65. if (action == BUTTON_MODE_TOGGLE) {
  66. if (_buttons[id].relayID > 0) {
  67. relayToggle(_buttons[id].relayID - 1);
  68. }
  69. }
  70. if (action == BUTTON_MODE_AP) createAP();
  71. if (action == BUTTON_MODE_RESET) ESP.restart();
  72. if (action == BUTTON_MODE_PULSE) relayPulseToggle();
  73. if (action == BUTTON_MODE_FACTORY) {
  74. DEBUG_MSG("\n\nFACTORY RESET\n\n");
  75. settingsFactoryReset();
  76. ESP.restart();
  77. }
  78. }
  79. void buttonSetup() {
  80. #ifdef SONOFF_DUAL
  81. unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE);
  82. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), 0, 1});
  83. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), 0, 2});
  84. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY});
  85. #else
  86. #ifdef BUTTON1_PIN
  87. {
  88. unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK);
  89. _buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE), actions, BUTTON1_RELAY});
  90. }
  91. #endif
  92. #ifdef BUTTON2_PIN
  93. {
  94. unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK);
  95. _buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE), actions, BUTTON2_RELAY});
  96. }
  97. #endif
  98. #ifdef BUTTON3_PIN
  99. {
  100. unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK);
  101. _buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE), actions, BUTTON3_RELAY});
  102. }
  103. #endif
  104. #ifdef BUTTON4_PIN
  105. {
  106. unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK);
  107. _buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE), actions, BUTTON4_RELAY});
  108. }
  109. #endif
  110. #endif
  111. DEBUG_MSG("[BUTTON] Number of buttons: %d\n", _buttons.size());
  112. }
  113. void buttonLoop() {
  114. #ifdef SONOFF_DUAL
  115. if (Serial.available() >= 4) {
  116. unsigned char value;
  117. if (Serial.read() == 0xA0) {
  118. if (Serial.read() == 0x04) {
  119. value = Serial.read();
  120. if (Serial.read() == 0xA1) {
  121. // RELAYs and BUTTONs are synchonized in the SIL F330
  122. // The on-board BUTTON2 should toggle RELAY0 value
  123. // Since we are not passing back RELAY2 value
  124. // (in the relayStatus method) it will only be present
  125. // here if it has actually been pressed
  126. if ((value & 4) == 4) {
  127. buttonEvent(2, BUTTON_EVENT_CLICK);
  128. return;
  129. }
  130. // Otherwise check if any of the other two BUTTONs
  131. // (in the header) has been pressent, but we should
  132. // ensure that we only toggle one of them to avoid
  133. // the synchronization going mad
  134. // This loop is generic for any PSB-04 module
  135. for (unsigned int i=0; i<relayCount(); i++) {
  136. bool status = (value & (1 << i)) > 0;
  137. // Cjeck if the status for that relay has changed
  138. if (relayStatus(i) != status) {
  139. buttonEvent(i, BUTTON_EVENT_CLICK);
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. #else
  148. for (unsigned int i=0; i < _buttons.size(); i++) {
  149. if (unsigned char event = _buttons[i].button->loop()) {
  150. unsigned char count = _buttons[i].button->getEventCount();
  151. unsigned long length = _buttons[i].button->getEventLength();
  152. unsigned char mapped = mapEvent(event, count, length);
  153. buttonEvent(i, mapped);
  154. }
  155. }
  156. #endif
  157. }