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.

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