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.

280 lines
10 KiB

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