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.

245 lines
8.8 KiB

6 years ago
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. void buttonMQTT(unsigned char id, uint8_t event) {
  18. if (id >= _buttons.size()) return;
  19. char payload[2];
  20. itoa(event, payload, 10);
  21. mqttSend(MQTT_TOPIC_BUTTON, id, payload, false, false); // 1st bool = force, 2nd = retain
  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 long pressed, unsigned long click, unsigned long dblclick, unsigned long lngclick, unsigned long 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] Button #%u event %u\n"), id, event);
  67. if (event == 0) return;
  68. #if MQTT_SUPPORT
  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_ON) {
  78. if (_buttons[id].relayID > 0) {
  79. relayStatus(_buttons[id].relayID - 1, true);
  80. }
  81. }
  82. if (action == BUTTON_MODE_OFF) {
  83. if (_buttons[id].relayID > 0) {
  84. relayStatus(_buttons[id].relayID - 1, false);
  85. }
  86. }
  87. if (action == BUTTON_MODE_AP) createAP();
  88. if (action == BUTTON_MODE_RESET) {
  89. deferredReset(100, CUSTOM_RESET_HARDWARE);
  90. }
  91. if (action == BUTTON_MODE_FACTORY) {
  92. DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n"));
  93. resetSettings();
  94. deferredReset(100, CUSTOM_RESET_FACTORY);
  95. }
  96. }
  97. void buttonSetup() {
  98. #ifdef ITEAD_SONOFF_DUAL
  99. unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE);
  100. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1});
  101. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2});
  102. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY});
  103. #else
  104. unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
  105. #if BUTTON1_PIN != GPIO_NONE
  106. {
  107. unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK);
  108. _buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON1_RELAY});
  109. }
  110. #endif
  111. #if BUTTON2_PIN != GPIO_NONE
  112. {
  113. unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK);
  114. _buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON2_RELAY});
  115. }
  116. #endif
  117. #if BUTTON3_PIN != GPIO_NONE
  118. {
  119. unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK);
  120. _buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON3_RELAY});
  121. }
  122. #endif
  123. #if BUTTON4_PIN != GPIO_NONE
  124. {
  125. unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK);
  126. _buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON4_RELAY});
  127. }
  128. #endif
  129. #if BUTTON5_PIN != GPIO_NONE
  130. {
  131. unsigned int actions = buttonStore(BUTTON5_PRESS, BUTTON5_CLICK, BUTTON5_DBLCLICK, BUTTON5_LNGCLICK, BUTTON5_LNGLNGCLICK);
  132. _buttons.push_back({new DebounceEvent(BUTTON5_PIN, BUTTON5_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON5_RELAY});
  133. }
  134. #endif
  135. #if BUTTON6_PIN != GPIO_NONE
  136. {
  137. unsigned int actions = buttonStore(BUTTON6_PRESS, BUTTON6_CLICK, BUTTON6_DBLCLICK, BUTTON6_LNGCLICK, BUTTON6_LNGLNGCLICK);
  138. _buttons.push_back({new DebounceEvent(BUTTON6_PIN, BUTTON6_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON6_RELAY});
  139. }
  140. #endif
  141. #if BUTTON7_PIN != GPIO_NONE
  142. {
  143. unsigned int actions = buttonStore(BUTTON7_PRESS, BUTTON7_CLICK, BUTTON7_DBLCLICK, BUTTON7_LNGCLICK, BUTTON7_LNGLNGCLICK);
  144. _buttons.push_back({new DebounceEvent(BUTTON7_PIN, BUTTON7_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON7_RELAY});
  145. }
  146. #endif
  147. #if BUTTON8_PIN != GPIO_NONE
  148. {
  149. unsigned int actions = buttonStore(BUTTON8_PRESS, BUTTON8_CLICK, BUTTON8_DBLCLICK, BUTTON8_LNGCLICK, BUTTON8_LNGLNGCLICK);
  150. _buttons.push_back({new DebounceEvent(BUTTON8_PIN, BUTTON8_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON8_RELAY});
  151. }
  152. #endif
  153. #endif
  154. DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());
  155. // Register loop
  156. espurnaRegisterLoop(buttonLoop);
  157. }
  158. void buttonLoop() {
  159. #ifdef ITEAD_SONOFF_DUAL
  160. if (Serial.available() >= 4) {
  161. if (Serial.read() == 0xA0) {
  162. if (Serial.read() == 0x04) {
  163. unsigned char value = Serial.read();
  164. if (Serial.read() == 0xA1) {
  165. // RELAYs and BUTTONs are synchonized in the SIL F330
  166. // The on-board BUTTON2 should toggle RELAY0 value
  167. // Since we are not passing back RELAY2 value
  168. // (in the relayStatus method) it will only be present
  169. // here if it has actually been pressed
  170. if ((value & 4) == 4) {
  171. buttonEvent(2, BUTTON_EVENT_CLICK);
  172. return;
  173. }
  174. // Otherwise check if any of the other two BUTTONs
  175. // (in the header) has been pressed, but we should
  176. // ensure that we only toggle one of them to avoid
  177. // the synchronization going mad
  178. // This loop is generic for any PSB-04 module
  179. for (unsigned int i=0; i<relayCount(); i++) {
  180. bool status = (value & (1 << i)) > 0;
  181. // Check if the status for that relay has changed
  182. if (relayStatus(i) != status) {
  183. buttonEvent(i, BUTTON_EVENT_CLICK);
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. #else
  192. for (unsigned int i=0; i < _buttons.size(); i++) {
  193. if (unsigned char event = _buttons[i].button->loop()) {
  194. unsigned char count = _buttons[i].button->getEventCount();
  195. unsigned long length = _buttons[i].button->getEventLength();
  196. unsigned char mapped = mapEvent(event, count, length);
  197. buttonEvent(i, mapped);
  198. }
  199. }
  200. #endif
  201. }