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.

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