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.

270 lines
8.8 KiB

6 years ago
7 years ago
  1. /*
  2. BUTTON MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. Module key prefix: btn
  5. */
  6. // -----------------------------------------------------------------------------
  7. // BUTTON
  8. // -----------------------------------------------------------------------------
  9. #if BUTTON_SUPPORT
  10. #include <DebounceEvent.h>
  11. #include <vector>
  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. void _buttonWebSocketOnSend(JsonObject& root) {
  28. root["btnDelay"] = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
  29. }
  30. #endif
  31. bool _buttonKeyCheck(const char * key) {
  32. return (strncmp(key, "btn", 3) == 0);
  33. }
  34. unsigned char _buttonGetAction(unsigned char id, unsigned char event) {
  35. if (id >= _buttons.size()) return BUTTON_MODE_NONE;
  36. unsigned long actions = _buttons[id].actions;
  37. if (event == BUTTON_EVENT_PRESSED) return (actions) & 0x0F;
  38. if (event == BUTTON_EVENT_CLICK) return (actions >> 4) & 0x0F;
  39. if (event == BUTTON_EVENT_DBLCLICK) return (actions >> 8) & 0x0F;
  40. if (event == BUTTON_EVENT_LNGCLICK) return (actions >> 12) & 0x0F;
  41. if (event == BUTTON_EVENT_LNGLNGCLICK) return (actions >> 16) & 0x0F;
  42. if (event == BUTTON_EVENT_TRIPLECLICK) return (actions >> 20) & 0x0F;
  43. return BUTTON_MODE_NONE;
  44. }
  45. unsigned long _buttonGetActionMask(unsigned char id) {
  46. unsigned long pressAction = getSetting("btnPress", id, BUTTON_MODE_NONE).toInt();
  47. unsigned long clickAction = getSetting("btnClick", id, BUTTON_MODE_TOGGLE).toInt();
  48. unsigned long dblClickAction = getSetting("btnDblClick", id, (id == 0) ? BUTTON_MODE_AP : BUTTON_MODE_NONE).toInt();
  49. unsigned long lngClickAction = getSetting("btnLngClick", id, (id == 0) ? BUTTON_MODE_RESET : BUTTON_MODE_NONE).toInt();
  50. unsigned long lnglngClickAction = getSetting("btnLngLngClick", id, (id == 0) ? BUTTON_MODE_FACTORY : BUTTON_MODE_NONE).toInt();
  51. unsigned long tripleClickAction = getSetting("btnTripleClick", id, (id == 0) ? BUTTON_MODE_NONE : BUTTON_MODE_NONE).toInt();
  52. unsigned long value;
  53. value = pressAction;
  54. value += clickAction << 4;
  55. value += dblClickAction << 8;
  56. value += lngClickAction << 12;
  57. value += lnglngClickAction << 16;
  58. value += tripleClickAction << 20;
  59. return value;
  60. }
  61. uint8_t _buttonGetEvent(uint8_t event, uint8_t count, uint16_t length) {
  62. if (event == EVENT_PRESSED) return BUTTON_EVENT_PRESSED;
  63. if (event == EVENT_CHANGED) return BUTTON_EVENT_CLICK;
  64. if (event == EVENT_RELEASED) {
  65. if (1 == count) {
  66. if (length > BUTTON_LNGLNGCLICK_DELAY) return BUTTON_EVENT_LNGLNGCLICK;
  67. if (length > BUTTON_LNGCLICK_DELAY) return BUTTON_EVENT_LNGCLICK;
  68. return BUTTON_EVENT_CLICK;
  69. }
  70. if (2 == count) return BUTTON_EVENT_DBLCLICK;
  71. if (3 == count) return BUTTON_EVENT_TRIPLECLICK;
  72. }
  73. return BUTTON_EVENT_NONE;
  74. }
  75. void _buttonExecuteEvent(unsigned int id, unsigned char event) {
  76. DEBUG_MSG_P(PSTR("[BUTTON] Button #%u event %u\n"), id, event);
  77. if (event == 0) return;
  78. unsigned char action = _buttonGetAction(id, event);
  79. #if MQTT_SUPPORT
  80. if (action != BUTTON_MODE_NONE || BUTTON_MQTT_SEND_ALL_EVENTS) {
  81. buttonMQTT(id, event);
  82. }
  83. #endif
  84. if (action == BUTTON_MODE_TOGGLE) {
  85. if (RELAY_NONE != _buttons[id].relayID) {
  86. relayToggle(_buttons[id].relayID);
  87. }
  88. }
  89. if (action == BUTTON_MODE_ON) {
  90. if (RELAY_NONE != _buttons[id].relayID) {
  91. relayStatus(_buttons[id].relayID, true);
  92. }
  93. }
  94. if (action == BUTTON_MODE_OFF) {
  95. if (RELAY_NONE != _buttons[id].relayID) {
  96. relayStatus(_buttons[id].relayID, false);
  97. }
  98. }
  99. if (action == BUTTON_MODE_AP) wifiStartAP();
  100. #if defined(JUSTWIFI_ENABLE_WPS)
  101. if (action == BUTTON_MODE_WPS) wifiStartWPS();
  102. #endif // defined(JUSTWIFI_ENABLE_WPS)
  103. #if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  104. if (action == BUTTON_MODE_SMART_CONFIG) wifiStartSmartConfig();
  105. #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  106. if (action == BUTTON_MODE_RESET) {
  107. deferredReset(100, CUSTOM_RESET_HARDWARE);
  108. }
  109. if (action == BUTTON_MODE_FACTORY) {
  110. DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n"));
  111. resetSettings();
  112. deferredReset(100, CUSTOM_RESET_FACTORY);
  113. }
  114. }
  115. void _buttonClear() {
  116. for (unsigned char i = 0; i < _buttons.size(); i++) {
  117. button_t element = _buttons[i];
  118. delete(element.button);
  119. }
  120. _buttons.clear();
  121. }
  122. void _buttonConfigure() {
  123. _buttonClear();
  124. #ifdef ITEAD_SONOFF_DUAL
  125. unsigned char relayId = getSetting("btnRelay", 2, RELAY_NONE).toInt();
  126. unsigned long actions = BUTTON_MODE_TOGGLE << 4;
  127. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1});
  128. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2});
  129. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, relayId});
  130. #else
  131. // TODO: maybe this setting should be changed, btnDelay => btnClickDelay?
  132. unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
  133. unsigned char index = 0;
  134. while (index < MAX_COMPONENTS) {
  135. unsigned char pin = getSetting("btnGPIO", index, GPIO_NONE).toInt();
  136. if (GPIO_NONE == pin) break;
  137. unsigned char relayId = getSetting("btnRelay", index, RELAY_NONE).toInt();
  138. unsigned char mode = getSetting("btnMode", index, BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH).toInt();
  139. unsigned long actions = _buttonGetActionMask(index);
  140. // DebounceEvent takes 4 parameters
  141. // * GPIO
  142. // * Button mode
  143. // * Debounce delay
  144. // * Wait delay for more clicks
  145. _buttons.push_back({new DebounceEvent(pin, mode, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, relayId});
  146. ++index;
  147. }
  148. #endif
  149. DEBUG_MSG_P(PSTR("[BUTTON] Buttons: %u\n"), _buttons.size());
  150. }
  151. void _buttonLoop() {
  152. #ifdef ITEAD_SONOFF_DUAL
  153. if (Serial.available() >= 4) {
  154. if (Serial.read() == 0xA0) {
  155. if (Serial.read() == 0x04) {
  156. unsigned char value = Serial.read();
  157. if (Serial.read() == 0xA1) {
  158. // RELAYs and BUTTONs are synchonized in the SIL F330
  159. // The on-board BUTTON2 should toggle RELAY0 value
  160. // Since we are not passing back RELAY2 value
  161. // (in the relayStatus method) it will only be present
  162. // here if it has actually been pressed
  163. if ((value & 4) == 4) {
  164. _buttonExecuteEvent(2, BUTTON_EVENT_CLICK);
  165. return;
  166. }
  167. // Otherwise check if any of the other two BUTTONs
  168. // (in the header) has been pressed, but we should
  169. // ensure that we only toggle one of them to avoid
  170. // the synchronization going mad
  171. // This loop is generic for any PSB-04 module
  172. for (unsigned int i=0; i<relayCount(); i++) {
  173. bool status = (value & (1 << i)) > 0;
  174. // Check if the status for that relay has changed
  175. if (relayStatus(i) != status) {
  176. _buttonExecuteEvent(i, BUTTON_EVENT_CLICK);
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. #else
  185. for (unsigned int i=0; i < _buttons.size(); i++) {
  186. if (unsigned char event = _buttons[i].button->loop()) {
  187. unsigned char count = _buttons[i].button->getEventCount();
  188. unsigned long length = _buttons[i].button->getEventLength();
  189. unsigned char mapped = _buttonGetEvent(event, count, length);
  190. _buttonExecuteEvent(i, mapped);
  191. }
  192. }
  193. #endif
  194. }
  195. // -----------------------------------------------------------------------------
  196. void buttonSetup() {
  197. _buttonConfigure();
  198. // Websocket Callbacks
  199. #if WEB_SUPPORT
  200. wsOnSendRegister(_buttonWebSocketOnSend);
  201. #endif
  202. settingsRegisterKeyCheck(_buttonKeyCheck);
  203. // Register loop
  204. espurnaRegisterReload(_buttonConfigure);
  205. espurnaRegisterLoop(_buttonLoop);
  206. }
  207. #endif // BUTTON_SUPPORT