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.

247 lines
8.8 KiB

7 years ago
  1. /*
  2. BUTTON MODULE
  3. Copyright (C) 2016-2018 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. #if MQTT_SUPPORT
  17. #ifdef MQTT_TOPIC_BUTTON
  18. void buttonMQTT(unsigned char id, uint8_t event) {
  19. if (id >= _buttons.size()) return;
  20. char payload[2];
  21. snprintf_P(payload, sizeof(payload), PSTR("%d"), event);
  22. mqttSend(MQTT_TOPIC_BUTTON, id, payload);
  23. }
  24. #endif
  25. #endif
  26. int buttonFromRelay(unsigned int relayID) {
  27. for (unsigned int i=0; i < _buttons.size(); i++) {
  28. if (_buttons[i].relayID == relayID) return i;
  29. }
  30. return -1;
  31. }
  32. bool buttonState(unsigned char id) {
  33. if (id >= _buttons.size()) return false;
  34. return _buttons[id].button->pressed();
  35. }
  36. unsigned char buttonAction(unsigned char id, unsigned char event) {
  37. if (id >= _buttons.size()) return BUTTON_MODE_NONE;
  38. unsigned long actions = _buttons[id].actions;
  39. if (event == BUTTON_EVENT_PRESSED) return (actions) & 0x0F;
  40. if (event == BUTTON_EVENT_CLICK) return (actions >> 4) & 0x0F;
  41. if (event == BUTTON_EVENT_DBLCLICK) return (actions >> 8) & 0x0F;
  42. if (event == BUTTON_EVENT_LNGCLICK) return (actions >> 12) & 0x0F;
  43. if (event == BUTTON_EVENT_LNGLNGCLICK) return (actions >> 16) & 0x0F;
  44. return BUTTON_MODE_NONE;
  45. }
  46. unsigned long buttonStore(unsigned long pressed, unsigned long click, unsigned long dblclick, unsigned long lngclick, unsigned long lnglngclick) {
  47. unsigned int value;
  48. value = pressed;
  49. value += click << 4;
  50. value += dblclick << 8;
  51. value += lngclick << 12;
  52. value += lnglngclick << 16;
  53. return value;
  54. }
  55. uint8_t mapEvent(uint8_t event, uint8_t count, uint16_t length) {
  56. if (event == EVENT_PRESSED) return BUTTON_EVENT_PRESSED;
  57. if (event == EVENT_CHANGED) return BUTTON_EVENT_CLICK;
  58. if (event == EVENT_RELEASED) {
  59. if (count == 1) {
  60. if (length > BUTTON_LNGLNGCLICK_DELAY) return BUTTON_EVENT_LNGLNGCLICK;
  61. if (length > BUTTON_LNGCLICK_DELAY) return BUTTON_EVENT_LNGCLICK;
  62. return BUTTON_EVENT_CLICK;
  63. }
  64. if (count == 2) return BUTTON_EVENT_DBLCLICK;
  65. }
  66. }
  67. void buttonEvent(unsigned int id, unsigned char event) {
  68. DEBUG_MSG_P(PSTR("[BUTTON] Button #%d event %d\n"), id, event);
  69. if (event == 0) return;
  70. #if MQTT_SUPPORT
  71. #ifdef MQTT_TOPIC_BUTTON
  72. buttonMQTT(id, event);
  73. #endif
  74. #endif
  75. unsigned char action = buttonAction(id, event);
  76. if (action == BUTTON_MODE_TOGGLE) {
  77. if (_buttons[id].relayID > 0) {
  78. relayToggle(_buttons[id].relayID - 1);
  79. }
  80. }
  81. if (action == BUTTON_MODE_ON) {
  82. if (_buttons[id].relayID > 0) {
  83. relayStatus(_buttons[id].relayID - 1, true);
  84. }
  85. }
  86. if (action == BUTTON_MODE_OFF) {
  87. if (_buttons[id].relayID > 0) {
  88. relayStatus(_buttons[id].relayID - 1, false);
  89. }
  90. }
  91. if (action == BUTTON_MODE_AP) createAP();
  92. if (action == BUTTON_MODE_RESET) {
  93. deferredReset(100, CUSTOM_RESET_HARDWARE);
  94. }
  95. if (action == BUTTON_MODE_FACTORY) {
  96. DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n"));
  97. settingsFactoryReset();
  98. deferredReset(100, CUSTOM_RESET_FACTORY);
  99. }
  100. }
  101. void buttonSetup() {
  102. #ifdef ITEAD_SONOFF_DUAL
  103. unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE);
  104. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1});
  105. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2});
  106. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY});
  107. #else
  108. unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
  109. #ifdef BUTTON1_PIN
  110. {
  111. unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK);
  112. _buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON1_RELAY});
  113. }
  114. #endif
  115. #ifdef BUTTON2_PIN
  116. {
  117. unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK);
  118. _buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON2_RELAY});
  119. }
  120. #endif
  121. #ifdef BUTTON3_PIN
  122. {
  123. unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK);
  124. _buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON3_RELAY});
  125. }
  126. #endif
  127. #ifdef BUTTON4_PIN
  128. {
  129. unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK);
  130. _buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON4_RELAY});
  131. }
  132. #endif
  133. #ifdef BUTTON5_PIN
  134. {
  135. unsigned int actions = buttonStore(BUTTON5_PRESS, BUTTON5_CLICK, BUTTON5_DBLCLICK, BUTTON5_LNGCLICK, BUTTON5_LNGLNGCLICK);
  136. _buttons.push_back({new DebounceEvent(BUTTON5_PIN, BUTTON5_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON5_RELAY});
  137. }
  138. #endif
  139. #ifdef BUTTON6_PIN
  140. {
  141. unsigned int actions = buttonStore(BUTTON6_PRESS, BUTTON6_CLICK, BUTTON6_DBLCLICK, BUTTON6_LNGCLICK, BUTTON6_LNGLNGCLICK);
  142. _buttons.push_back({new DebounceEvent(BUTTON6_PIN, BUTTON6_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON6_RELAY});
  143. }
  144. #endif
  145. #ifdef BUTTON7_PIN
  146. {
  147. unsigned int actions = buttonStore(BUTTON7_PRESS, BUTTON7_CLICK, BUTTON7_DBLCLICK, BUTTON7_LNGCLICK, BUTTON7_LNGLNGCLICK);
  148. _buttons.push_back({new DebounceEvent(BUTTON7_PIN, BUTTON7_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON7_RELAY});
  149. }
  150. #endif
  151. #ifdef BUTTON8_PIN
  152. {
  153. unsigned int actions = buttonStore(BUTTON8_PRESS, BUTTON8_CLICK, BUTTON8_DBLCLICK, BUTTON8_LNGCLICK, BUTTON8_LNGLNGCLICK);
  154. _buttons.push_back({new DebounceEvent(BUTTON8_PIN, BUTTON8_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON8_RELAY});
  155. }
  156. #endif
  157. #endif
  158. DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %d\n"), _buttons.size());
  159. }
  160. void buttonLoop() {
  161. #ifdef ITEAD_SONOFF_DUAL
  162. if (Serial.available() >= 4) {
  163. unsigned char value;
  164. if (Serial.read() == 0xA0) {
  165. if (Serial.read() == 0x04) {
  166. value = Serial.read();
  167. if (Serial.read() == 0xA1) {
  168. // RELAYs and BUTTONs are synchonized in the SIL F330
  169. // The on-board BUTTON2 should toggle RELAY0 value
  170. // Since we are not passing back RELAY2 value
  171. // (in the relayStatus method) it will only be present
  172. // here if it has actually been pressed
  173. if ((value & 4) == 4) {
  174. buttonEvent(2, BUTTON_EVENT_CLICK);
  175. return;
  176. }
  177. // Otherwise check if any of the other two BUTTONs
  178. // (in the header) has been pressed, but we should
  179. // ensure that we only toggle one of them to avoid
  180. // the synchronization going mad
  181. // This loop is generic for any PSB-04 module
  182. for (unsigned int i=0; i<relayCount(); i++) {
  183. bool status = (value & (1 << i)) > 0;
  184. // Check if the status for that relay has changed
  185. if (relayStatus(i) != status) {
  186. buttonEvent(i, BUTTON_EVENT_CLICK);
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. #else
  195. for (unsigned int i=0; i < _buttons.size(); i++) {
  196. if (unsigned char event = _buttons[i].button->loop()) {
  197. unsigned char count = _buttons[i].button->getEventCount();
  198. unsigned long length = _buttons[i].button->getEventLength();
  199. unsigned char mapped = mapEvent(event, count, length);
  200. buttonEvent(i, mapped);
  201. }
  202. }
  203. #endif
  204. }