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.

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