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.

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