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.

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