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.

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