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.

222 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. sprintf_P(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. customReset(CUSTOM_RESET_HARDWARE);
  90. ESP.restart();
  91. }
  92. if (action == BUTTON_MODE_PULSE) relayPulseToggle();
  93. if (action == BUTTON_MODE_FACTORY) {
  94. DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n"));
  95. settingsFactoryReset();
  96. customReset(CUSTOM_RESET_FACTORY);
  97. ESP.restart();
  98. }
  99. }
  100. void buttonSetup() {
  101. #ifdef ITEAD_SONOFF_DUAL
  102. unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE);
  103. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1});
  104. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2});
  105. _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY});
  106. #else
  107. unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
  108. #ifdef BUTTON1_PIN
  109. {
  110. unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK);
  111. _buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON1_RELAY});
  112. }
  113. #endif
  114. #ifdef BUTTON2_PIN
  115. {
  116. unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK);
  117. _buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON2_RELAY});
  118. }
  119. #endif
  120. #ifdef BUTTON3_PIN
  121. {
  122. unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK);
  123. _buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON3_RELAY});
  124. }
  125. #endif
  126. #ifdef BUTTON4_PIN
  127. {
  128. unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK);
  129. _buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON4_RELAY});
  130. }
  131. #endif
  132. #endif
  133. DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %d\n"), _buttons.size());
  134. }
  135. void buttonLoop() {
  136. #ifdef ITEAD_SONOFF_DUAL
  137. if (Serial.available() >= 4) {
  138. unsigned char value;
  139. if (Serial.read() == 0xA0) {
  140. if (Serial.read() == 0x04) {
  141. value = Serial.read();
  142. if (Serial.read() == 0xA1) {
  143. // RELAYs and BUTTONs are synchonized in the SIL F330
  144. // The on-board BUTTON2 should toggle RELAY0 value
  145. // Since we are not passing back RELAY2 value
  146. // (in the relayStatus method) it will only be present
  147. // here if it has actually been pressed
  148. if ((value & 4) == 4) {
  149. buttonEvent(2, BUTTON_EVENT_CLICK);
  150. return;
  151. }
  152. // Otherwise check if any of the other two BUTTONs
  153. // (in the header) has been pressed, but we should
  154. // ensure that we only toggle one of them to avoid
  155. // the synchronization going mad
  156. // This loop is generic for any PSB-04 module
  157. for (unsigned int i=0; i<relayCount(); i++) {
  158. bool status = (value & (1 << i)) > 0;
  159. // Check if the status for that relay has changed
  160. if (relayStatus(i) != status) {
  161. buttonEvent(i, BUTTON_EVENT_CLICK);
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. #else
  170. for (unsigned int i=0; i < _buttons.size(); i++) {
  171. if (unsigned char event = _buttons[i].button->loop()) {
  172. unsigned char count = _buttons[i].button->getEventCount();
  173. unsigned long length = _buttons[i].button->getEventLength();
  174. unsigned char mapped = mapEvent(event, count, length);
  175. buttonEvent(i, mapped);
  176. }
  177. }
  178. #endif
  179. }