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.

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