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.

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