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.

251 lines
8.4 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 == 1) ? BUTTON_MODE_AP : BUTTON_MODE_NONE).toInt();
  49. unsigned long lngClickAction = getSetting("btnLngClick", id, (id == 1) ? BUTTON_MODE_RESET : BUTTON_MODE_NONE).toInt();
  50. unsigned long lnglngClickAction = getSetting("btnLngLngClick", id, (id == 1) ? BUTTON_MODE_FACTORY : BUTTON_MODE_NONE).toInt();
  51. unsigned long tripleClickAction = getSetting("btnTripleClick", id, (id == 1) ? 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 _buttonLoop() {
  114. #ifdef ITEAD_SONOFF_DUAL
  115. if (Serial.available() >= 4) {
  116. if (Serial.read() == 0xA0) {
  117. if (Serial.read() == 0x04) {
  118. unsigned char value = Serial.read();
  119. if (Serial.read() == 0xA1) {
  120. // RELAYs and BUTTONs are synchonized in the SIL F330
  121. // The on-board BUTTON2 should toggle RELAY0 value
  122. // Since we are not passing back RELAY2 value
  123. // (in the relayStatus method) it will only be present
  124. // here if it has actually been pressed
  125. if ((value & 4) == 4) {
  126. _buttonExecuteEvent(2, BUTTON_EVENT_CLICK);
  127. return;
  128. }
  129. // Otherwise check if any of the other two BUTTONs
  130. // (in the header) has been pressed, but we should
  131. // ensure that we only toggle one of them to avoid
  132. // the synchronization going mad
  133. // This loop is generic for any PSB-04 module
  134. for (unsigned int i=0; i<relayCount(); i++) {
  135. bool status = (value & (1 << i)) > 0;
  136. // Check if the status for that relay has changed
  137. if (relayStatus(i) != status) {
  138. _buttonExecuteEvent(i, BUTTON_EVENT_CLICK);
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. #else
  147. for (unsigned int i=0; i < _buttons.size(); i++) {
  148. if (unsigned char event = _buttons[i].button->loop()) {
  149. unsigned char count = _buttons[i].button->getEventCount();
  150. unsigned long length = _buttons[i].button->getEventLength();
  151. unsigned char mapped = _buttonGetEvent(event, count, length);
  152. _buttonExecuteEvent(i, mapped);
  153. }
  154. }
  155. #endif
  156. }
  157. // -----------------------------------------------------------------------------
  158. void buttonSetup() {
  159. #ifdef ITEAD_SONOFF_DUAL
  160. unsigned char relayId = getSetting("btnRelay", 2, RELAY_NONE).toInt();
  161. unsigned long actions = BUTTON_MODE_TOGGLE << 4;
  162. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1});
  163. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2});
  164. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, relayId});
  165. #else
  166. // TODO: maybe this setting should be changed, btnDelay => btnClickDelay?
  167. unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
  168. unsigned char index = 0;
  169. while (index < MAX_COMPONENTS) {
  170. unsigned char pin = getSetting("btnGPIO", index, GPIO_NONE).toInt();
  171. if (GPIO_NONE == pin) break;
  172. unsigned char relayId = getSetting("btnRelay", index, RELAY_NONE).toInt();
  173. unsigned char mode = getSetting("btnMode", index, BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH).toInt();
  174. unsigned long actions = _buttonGetActionMask(index);
  175. // DebounceEvent takes 4 parameters
  176. // * GPIO
  177. // * Button mode
  178. // * Debounce delay
  179. // * Wait delay for more clicks
  180. _buttons.push_back({new DebounceEvent(pin, mode, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, relayId});
  181. ++index;
  182. }
  183. #endif
  184. DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());
  185. // Websocket Callbacks
  186. #if WEB_SUPPORT
  187. wsOnSendRegister(_buttonWebSocketOnSend);
  188. #endif
  189. settingsRegisterKeyCheck(_buttonKeyCheck);
  190. // Register loop
  191. espurnaRegisterLoop(_buttonLoop);
  192. }
  193. #endif // BUTTON_SUPPORT