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.

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