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.

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