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.

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