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.

352 lines
12 KiB

6 years ago
7 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. if (wifiState() & WIFI_STATE_AP) {
  110. wifiStartSTA();
  111. } else {
  112. wifiStartAP();
  113. }
  114. }
  115. if (BUTTON_MODE_RESET == action) {
  116. deferredReset(100, CUSTOM_RESET_HARDWARE);
  117. }
  118. if (BUTTON_MODE_FACTORY == action) {
  119. DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n"));
  120. resetSettings();
  121. deferredReset(100, CUSTOM_RESET_FACTORY);
  122. }
  123. #if defined(JUSTWIFI_ENABLE_WPS)
  124. if (BUTTON_MODE_WPS == action) {
  125. wifiStartWPS();
  126. }
  127. #endif // defined(JUSTWIFI_ENABLE_WPS)
  128. #if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  129. if (BUTTON_MODE_SMART_CONFIG == action) {
  130. wifiStartSmartConfig();
  131. }
  132. #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  133. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  134. if (BUTTON_MODE_DIM_UP == action) {
  135. lightBrightnessStep(1);
  136. lightUpdate(true, true);
  137. }
  138. if (BUTTON_MODE_DIM_DOWN == action) {
  139. lightBrightnessStep(-1);
  140. lightUpdate(true, true);
  141. }
  142. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  143. }
  144. void buttonSetup() {
  145. #if defined(ITEAD_SONOFF_DUAL)
  146. unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE);
  147. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1});
  148. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2});
  149. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY});
  150. #elif defined(FOXEL_LIGHTFOX_DUAL)
  151. unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE);
  152. unsigned int btn1Relay = getSetting("btnRelay", 0, BUTTON1_RELAY - 1).toInt() + 1;
  153. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn1Relay});
  154. unsigned int btn2Relay = getSetting("btnRelay", 1, BUTTON2_RELAY - 1).toInt() + 1;
  155. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn2Relay});
  156. unsigned int btn3Relay = getSetting("btnRelay", 2, BUTTON3_RELAY - 1).toInt() + 1;
  157. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn3Relay});
  158. unsigned int btn4Relay = getSetting("btnRelay", 3, BUTTON4_RELAY - 1).toInt() + 1;
  159. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn4Relay});
  160. #else
  161. unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
  162. UNUSED(btnDelay);
  163. #if BUTTON1_PIN != GPIO_NONE
  164. {
  165. unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK, BUTTON1_TRIPLECLICK);
  166. _buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON1_RELAY});
  167. }
  168. #endif
  169. #if BUTTON2_PIN != GPIO_NONE
  170. {
  171. unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK, BUTTON2_TRIPLECLICK);
  172. _buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON2_RELAY});
  173. }
  174. #endif
  175. #if BUTTON3_PIN != GPIO_NONE
  176. {
  177. unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK, BUTTON3_TRIPLECLICK);
  178. _buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON3_RELAY});
  179. }
  180. #endif
  181. #if BUTTON4_PIN != GPIO_NONE
  182. {
  183. unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK, BUTTON4_TRIPLECLICK);
  184. _buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON4_RELAY});
  185. }
  186. #endif
  187. #if BUTTON5_PIN != GPIO_NONE
  188. {
  189. unsigned int actions = buttonStore(BUTTON5_PRESS, BUTTON5_CLICK, BUTTON5_DBLCLICK, BUTTON5_LNGCLICK, BUTTON5_LNGLNGCLICK, BUTTON5_TRIPLECLICK);
  190. _buttons.push_back({new DebounceEvent(BUTTON5_PIN, BUTTON5_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON5_RELAY});
  191. }
  192. #endif
  193. #if BUTTON6_PIN != GPIO_NONE
  194. {
  195. unsigned int actions = buttonStore(BUTTON6_PRESS, BUTTON6_CLICK, BUTTON6_DBLCLICK, BUTTON6_LNGCLICK, BUTTON6_LNGLNGCLICK, BUTTON6_TRIPLECLICK);
  196. _buttons.push_back({new DebounceEvent(BUTTON6_PIN, BUTTON6_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON6_RELAY});
  197. }
  198. #endif
  199. #if BUTTON7_PIN != GPIO_NONE
  200. {
  201. unsigned int actions = buttonStore(BUTTON7_PRESS, BUTTON7_CLICK, BUTTON7_DBLCLICK, BUTTON7_LNGCLICK, BUTTON7_LNGLNGCLICK, BUTTON7_TRIPLECLICK);
  202. _buttons.push_back({new DebounceEvent(BUTTON7_PIN, BUTTON7_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON7_RELAY});
  203. }
  204. #endif
  205. #if BUTTON8_PIN != GPIO_NONE
  206. {
  207. unsigned int actions = buttonStore(BUTTON8_PRESS, BUTTON8_CLICK, BUTTON8_DBLCLICK, BUTTON8_LNGCLICK, BUTTON8_LNGLNGCLICK, BUTTON8_TRIPLECLICK);
  208. _buttons.push_back({new DebounceEvent(BUTTON8_PIN, BUTTON8_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON8_RELAY});
  209. }
  210. #endif
  211. #endif
  212. DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());
  213. // Websocket Callbacks
  214. #if WEB_SUPPORT
  215. wsRegister()
  216. .onVisible(_buttonWebSocketOnVisible)
  217. .onKeyCheck(_buttonWebSocketOnKeyCheck);
  218. #endif
  219. // Register loop
  220. espurnaRegisterLoop(buttonLoop);
  221. }
  222. void buttonLoop() {
  223. #if defined(ITEAD_SONOFF_DUAL)
  224. if (Serial.available() >= 4) {
  225. if (Serial.read() == 0xA0) {
  226. if (Serial.read() == 0x04) {
  227. unsigned char value = Serial.read();
  228. if (Serial.read() == 0xA1) {
  229. // RELAYs and BUTTONs are synchonized in the SIL F330
  230. // The on-board BUTTON2 should toggle RELAY0 value
  231. // Since we are not passing back RELAY2 value
  232. // (in the relayStatus method) it will only be present
  233. // here if it has actually been pressed
  234. if ((value & 4) == 4) {
  235. buttonEvent(2, BUTTON_EVENT_CLICK);
  236. return;
  237. }
  238. // Otherwise check if any of the other two BUTTONs
  239. // (in the header) has been pressed, but we should
  240. // ensure that we only toggle one of them to avoid
  241. // the synchronization going mad
  242. // This loop is generic for any PSB-04 module
  243. for (unsigned int i=0; i<relayCount(); i++) {
  244. bool status = (value & (1 << i)) > 0;
  245. // Check if the status for that relay has changed
  246. if (relayStatus(i) != status) {
  247. buttonEvent(i, BUTTON_EVENT_CLICK);
  248. break;
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. #elif defined(FOXEL_LIGHTFOX_DUAL)
  256. if (Serial.available() >= 4) {
  257. if (Serial.read() == 0xA0) {
  258. if (Serial.read() == 0x04) {
  259. unsigned char value = Serial.read();
  260. if (Serial.read() == 0xA1) {
  261. DEBUG_MSG_P(PSTR("[BUTTON] [LIGHTFOX] Received buttons mask: %d\n"), value);
  262. for (unsigned int i=0; i<_buttons.size(); i++) {
  263. bool clicked = (value & (1 << i)) > 0;
  264. if (clicked) {
  265. buttonEvent(i, BUTTON_EVENT_CLICK);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. }
  272. #else
  273. for (unsigned int i=0; i < _buttons.size(); i++) {
  274. if (unsigned char event = _buttons[i].button->loop()) {
  275. unsigned char count = _buttons[i].button->getEventCount();
  276. unsigned long length = _buttons[i].button->getEventLength();
  277. unsigned char mapped = mapEvent(event, count, length);
  278. buttonEvent(i, mapped);
  279. }
  280. }
  281. #endif
  282. }
  283. #endif // BUTTON_SUPPORT