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.

268 lines
8.7 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. #if MQTT_SUPPORT
  79. buttonMQTT(id, event);
  80. #endif
  81. unsigned char action = _buttonGetAction(id, event);
  82. if (action == BUTTON_MODE_TOGGLE) {
  83. if (RELAY_NONE != _buttons[id].relayID) {
  84. relayToggle(_buttons[id].relayID);
  85. }
  86. }
  87. if (action == BUTTON_MODE_ON) {
  88. if (RELAY_NONE != _buttons[id].relayID) {
  89. relayStatus(_buttons[id].relayID, true);
  90. }
  91. }
  92. if (action == BUTTON_MODE_OFF) {
  93. if (RELAY_NONE != _buttons[id].relayID) {
  94. relayStatus(_buttons[id].relayID, false);
  95. }
  96. }
  97. if (action == BUTTON_MODE_AP) wifiStartAP();
  98. #if defined(JUSTWIFI_ENABLE_WPS)
  99. if (action == BUTTON_MODE_WPS) wifiStartWPS();
  100. #endif // defined(JUSTWIFI_ENABLE_WPS)
  101. #if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  102. if (action == BUTTON_MODE_SMART_CONFIG) wifiStartSmartConfig();
  103. #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  104. if (action == BUTTON_MODE_RESET) {
  105. deferredReset(100, CUSTOM_RESET_HARDWARE);
  106. }
  107. if (action == BUTTON_MODE_FACTORY) {
  108. DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n"));
  109. resetSettings();
  110. deferredReset(100, CUSTOM_RESET_FACTORY);
  111. }
  112. }
  113. void _buttonClear() {
  114. for (unsigned char i = 0; i < _buttons.size(); i++) {
  115. button_t element = _buttons[i];
  116. delete(element.button);
  117. }
  118. _buttons.clear();
  119. }
  120. void _buttonConfigure() {
  121. _buttonClear();
  122. #ifdef ITEAD_SONOFF_DUAL
  123. unsigned char relayId = getSetting("btnRelay", 2, RELAY_NONE).toInt();
  124. unsigned long actions = BUTTON_MODE_TOGGLE << 4;
  125. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1});
  126. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2});
  127. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, relayId});
  128. #else
  129. // TODO: maybe this setting should be changed, btnDelay => btnClickDelay?
  130. unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
  131. unsigned char index = 0;
  132. while (index < MAX_COMPONENTS) {
  133. unsigned char pin = getSetting("btnGPIO", index, GPIO_NONE).toInt();
  134. if (GPIO_NONE == pin) break;
  135. unsigned char relayId = getSetting("btnRelay", index, RELAY_NONE).toInt();
  136. unsigned char mode = getSetting("btnMode", index, BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH).toInt();
  137. unsigned long actions = _buttonGetActionMask(index);
  138. // DebounceEvent takes 4 parameters
  139. // * GPIO
  140. // * Button mode
  141. // * Debounce delay
  142. // * Wait delay for more clicks
  143. _buttons.push_back({new DebounceEvent(pin, mode, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, relayId});
  144. ++index;
  145. }
  146. #endif
  147. DEBUG_MSG_P(PSTR("[BUTTON] Buttons: %u\n"), _buttons.size());
  148. }
  149. void _buttonLoop() {
  150. #ifdef ITEAD_SONOFF_DUAL
  151. if (Serial.available() >= 4) {
  152. if (Serial.read() == 0xA0) {
  153. if (Serial.read() == 0x04) {
  154. unsigned char value = Serial.read();
  155. if (Serial.read() == 0xA1) {
  156. // RELAYs and BUTTONs are synchonized in the SIL F330
  157. // The on-board BUTTON2 should toggle RELAY0 value
  158. // Since we are not passing back RELAY2 value
  159. // (in the relayStatus method) it will only be present
  160. // here if it has actually been pressed
  161. if ((value & 4) == 4) {
  162. _buttonExecuteEvent(2, BUTTON_EVENT_CLICK);
  163. return;
  164. }
  165. // Otherwise check if any of the other two BUTTONs
  166. // (in the header) has been pressed, but we should
  167. // ensure that we only toggle one of them to avoid
  168. // the synchronization going mad
  169. // This loop is generic for any PSB-04 module
  170. for (unsigned int i=0; i<relayCount(); i++) {
  171. bool status = (value & (1 << i)) > 0;
  172. // Check if the status for that relay has changed
  173. if (relayStatus(i) != status) {
  174. _buttonExecuteEvent(i, BUTTON_EVENT_CLICK);
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. #else
  183. for (unsigned int i=0; i < _buttons.size(); i++) {
  184. if (unsigned char event = _buttons[i].button->loop()) {
  185. unsigned char count = _buttons[i].button->getEventCount();
  186. unsigned long length = _buttons[i].button->getEventLength();
  187. unsigned char mapped = _buttonGetEvent(event, count, length);
  188. _buttonExecuteEvent(i, mapped);
  189. }
  190. }
  191. #endif
  192. }
  193. // -----------------------------------------------------------------------------
  194. void buttonSetup() {
  195. _buttonConfigure();
  196. // Websocket Callbacks
  197. #if WEB_SUPPORT
  198. wsOnSendRegister(_buttonWebSocketOnSend);
  199. wsOnAfterParseRegister(_buttonConfigure);
  200. #endif
  201. settingsRegisterKeyCheck(_buttonKeyCheck);
  202. // Register loop
  203. espurnaRegisterLoop(_buttonLoop);
  204. }
  205. #endif // BUTTON_SUPPORT