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.

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