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.

268 lines
9.8 KiB

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