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.

191 lines
6.3 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.reset();
  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. #ifdef LED_PULSE
  100. pinMode(LED_PULSE, OUTPUT);
  101. byte relayPulseMode = getSetting("relayPulseMode", String(RELAY_PULSE_MODE)).toInt();
  102. digitalWrite(LED_PULSE, relayPulseMode != RELAY_PULSE_NONE);
  103. #endif
  104. #endif
  105. DEBUG_MSG("[BUTTON] Number of buttons: %d\n", _buttons.size());
  106. }
  107. void buttonLoop() {
  108. #ifdef SONOFF_DUAL
  109. if (Serial.available() >= 4) {
  110. unsigned char value;
  111. if (Serial.read() == 0xA0) {
  112. if (Serial.read() == 0x04) {
  113. value = Serial.read();
  114. if (Serial.read() == 0xA1) {
  115. // RELAYs and BUTTONs are synchonized in the SIL F330
  116. // The on-board BUTTON2 should toggle RELAY0 value
  117. // Since we are not passing back RELAY2 value
  118. // (in the relayStatus method) it will only be present
  119. // here if it has actually been pressed
  120. if ((value & 4) == 4) {
  121. buttonEvent(2, BUTTON_EVENT_CLICK);
  122. return;
  123. }
  124. // Otherwise check if any of the other two BUTTONs
  125. // (in the header) has been pressent, but we should
  126. // ensure that we only toggle one of them to avoid
  127. // the synchronization going mad
  128. // This loop is generic for any PSB-04 module
  129. for (unsigned int i=0; i<relayCount(); i++) {
  130. bool status = (value & (1 << i)) > 0;
  131. // Cjeck if the status for that relay has changed
  132. if (relayStatus(i) != status) {
  133. buttonEvent(i, BUTTON_EVENT_CLICK);
  134. break;
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. #else
  142. for (unsigned int i=0; i < _buttons.size(); i++) {
  143. if (_buttons[i].button->loop()) {
  144. uint8_t event = mapEvent(_buttons[i].button->getEvent());
  145. buttonEvent(i, event);
  146. }
  147. }
  148. #endif
  149. }