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.

223 lines
7.5 KiB

7 years ago
  1. /*
  2. BUTTON MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. // -----------------------------------------------------------------------------
  6. // BUTTON
  7. // -----------------------------------------------------------------------------
  8. #include <DebounceEvent.h>
  9. #include <vector>
  10. typedef struct {
  11. DebounceEvent * button;
  12. unsigned long actions;
  13. unsigned int relayID;
  14. } button_t;
  15. std::vector<button_t> _buttons;
  16. #if MQTT_SUPPORT
  17. #ifdef MQTT_TOPIC_BUTTON
  18. void buttonMQTT(unsigned char id, uint8_t event) {
  19. if (id >= _buttons.size()) return;
  20. char payload[2];
  21. snprintf_P(payload, sizeof(payload), PSTR("%d"), event);
  22. mqttSend(MQTT_TOPIC_BUTTON, id, payload);
  23. }
  24. #endif
  25. #endif
  26. int buttonFromRelay(unsigned int relayID) {
  27. for (unsigned int i=0; i < _buttons.size(); i++) {
  28. if (_buttons[i].relayID == relayID) return i;
  29. }
  30. return -1;
  31. }
  32. bool buttonState(unsigned char id) {
  33. if (id >= _buttons.size()) return false;
  34. return _buttons[id].button->pressed();
  35. }
  36. unsigned char buttonAction(unsigned char id, unsigned char event) {
  37. if (id >= _buttons.size()) return BUTTON_MODE_NONE;
  38. unsigned long actions = _buttons[id].actions;
  39. if (event == BUTTON_EVENT_PRESSED) return (actions) & 0x0F;
  40. if (event == BUTTON_EVENT_CLICK) return (actions >> 4) & 0x0F;
  41. if (event == BUTTON_EVENT_DBLCLICK) return (actions >> 8) & 0x0F;
  42. if (event == BUTTON_EVENT_LNGCLICK) return (actions >> 12) & 0x0F;
  43. if (event == BUTTON_EVENT_LNGLNGCLICK) return (actions >> 16) & 0x0F;
  44. return BUTTON_MODE_NONE;
  45. }
  46. unsigned long buttonStore(unsigned char pressed, unsigned char click, unsigned char dblclick, unsigned char lngclick, unsigned char lnglngclick) {
  47. unsigned int value;
  48. value = pressed;
  49. value += click << 4;
  50. value += dblclick << 8;
  51. value += lngclick << 12;
  52. value += lnglngclick << 16;
  53. return value;
  54. }
  55. uint8_t mapEvent(uint8_t event, uint8_t count, uint16_t length) {
  56. if (event == EVENT_PRESSED) return BUTTON_EVENT_PRESSED;
  57. if (event == EVENT_CHANGED) return BUTTON_EVENT_CLICK;
  58. if (event == EVENT_RELEASED) {
  59. if (count == 1) {
  60. if (length > BUTTON_LNGLNGCLICK_DELAY) return BUTTON_EVENT_LNGLNGCLICK;
  61. if (length > BUTTON_LNGCLICK_DELAY) return BUTTON_EVENT_LNGCLICK;
  62. return BUTTON_EVENT_CLICK;
  63. }
  64. if (count == 2) return BUTTON_EVENT_DBLCLICK;
  65. }
  66. }
  67. void buttonEvent(unsigned int id, unsigned char event) {
  68. DEBUG_MSG_P(PSTR("[BUTTON] Pressed #%d, event: %d\n"), id, event);
  69. if (event == 0) return;
  70. #if MQTT_SUPPORT
  71. #ifdef MQTT_TOPIC_BUTTON
  72. buttonMQTT(id, event);
  73. #endif
  74. #endif
  75. unsigned char action = buttonAction(id, event);
  76. if (action == BUTTON_MODE_TOGGLE) {
  77. if (_buttons[id].relayID > 0) {
  78. relayToggle(_buttons[id].relayID - 1);
  79. }
  80. }
  81. if (action == BUTTON_MODE_ON) {
  82. if (_buttons[id].relayID > 0) {
  83. relayStatus(_buttons[id].relayID - 1, true);
  84. }
  85. }
  86. if (action == BUTTON_MODE_OFF) {
  87. if (_buttons[id].relayID > 0) {
  88. relayStatus(_buttons[id].relayID - 1, false);
  89. }
  90. }
  91. if (action == BUTTON_MODE_AP) createAP();
  92. if (action == BUTTON_MODE_RESET) {
  93. deferredReset(100, CUSTOM_RESET_HARDWARE);
  94. }
  95. if (action == BUTTON_MODE_FACTORY) {
  96. DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n"));
  97. settingsFactoryReset();
  98. deferredReset(100, CUSTOM_RESET_FACTORY);
  99. }
  100. }
  101. void buttonSetup() {
  102. #ifdef ITEAD_SONOFF_DUAL
  103. unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE);
  104. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1});
  105. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2});
  106. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY});
  107. #else
  108. unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
  109. #ifdef BUTTON1_PIN
  110. {
  111. unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK);
  112. _buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON1_RELAY});
  113. }
  114. #endif
  115. #ifdef BUTTON2_PIN
  116. {
  117. unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK);
  118. _buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON2_RELAY});
  119. }
  120. #endif
  121. #ifdef BUTTON3_PIN
  122. {
  123. unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK);
  124. _buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON3_RELAY});
  125. }
  126. #endif
  127. #ifdef BUTTON4_PIN
  128. {
  129. unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK);
  130. _buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON4_RELAY});
  131. }
  132. #endif
  133. #endif
  134. DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %d\n"), _buttons.size());
  135. }
  136. void buttonLoop() {
  137. #ifdef ITEAD_SONOFF_DUAL
  138. if (Serial.available() >= 4) {
  139. unsigned char value;
  140. if (Serial.read() == 0xA0) {
  141. if (Serial.read() == 0x04) {
  142. value = Serial.read();
  143. if (Serial.read() == 0xA1) {
  144. // RELAYs and BUTTONs are synchonized in the SIL F330
  145. // The on-board BUTTON2 should toggle RELAY0 value
  146. // Since we are not passing back RELAY2 value
  147. // (in the relayStatus method) it will only be present
  148. // here if it has actually been pressed
  149. if ((value & 4) == 4) {
  150. buttonEvent(2, BUTTON_EVENT_CLICK);
  151. return;
  152. }
  153. // Otherwise check if any of the other two BUTTONs
  154. // (in the header) has been pressed, but we should
  155. // ensure that we only toggle one of them to avoid
  156. // the synchronization going mad
  157. // This loop is generic for any PSB-04 module
  158. for (unsigned int i=0; i<relayCount(); i++) {
  159. bool status = (value & (1 << i)) > 0;
  160. // Check if the status for that relay has changed
  161. if (relayStatus(i) != status) {
  162. buttonEvent(i, BUTTON_EVENT_CLICK);
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. #else
  171. for (unsigned int i=0; i < _buttons.size(); i++) {
  172. if (unsigned char event = _buttons[i].button->loop()) {
  173. unsigned char count = _buttons[i].button->getEventCount();
  174. unsigned long length = _buttons[i].button->getEventLength();
  175. unsigned char mapped = mapEvent(event, count, length);
  176. buttonEvent(i, mapped);
  177. }
  178. }
  179. #endif
  180. }