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.

206 lines
6.9 KiB

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