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.

333 lines
12 KiB

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