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.

106 lines
2.7 KiB

  1. /*
  2. ESPurna
  3. BUTTON MODULE
  4. Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. // -----------------------------------------------------------------------------
  7. // BUTTON
  8. // -----------------------------------------------------------------------------
  9. #ifdef SONOFF_DUAL
  10. void buttonSetup() {}
  11. void buttonLoop() {
  12. if (Serial.available() >= 4) {
  13. unsigned char value;
  14. if (Serial.read() == 0xA0) {
  15. if (Serial.read() == 0x04) {
  16. value = Serial.read();
  17. if (Serial.read() == 0xA1) {
  18. // RELAYs and BUTTONs are synchonized in the SIL F330
  19. // The on-board BUTTON2 should toggle RELAY0 value
  20. // Since we are not passing back RELAY2 value
  21. // (in the relayStatus method) it will only be present
  22. // here if it has actually been pressed
  23. if ((value & 4) == 4) value = value ^ 1;
  24. // Otherwise check if any of the other two BUTTONs
  25. // (in the header) has been pressent, but we should
  26. // ensure that we only toggle one of them to avoid
  27. // the synchronization going mad
  28. // This loop is generic for any PSB-04 module
  29. for (unsigned int i=0; i<relayCount(); i++) {
  30. bool status = (value & (1 << i)) > 0;
  31. // relayStatus returns true if the status has changed
  32. if (relayStatus(i, status)) break;
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. #else
  40. #ifdef BUTTON1_PIN
  41. #include <DebounceEvent.h>
  42. #include <vector>
  43. std::vector<DebounceEvent *> _buttons;
  44. void buttonSetup() {
  45. #ifdef BUTTON1_PIN
  46. _buttons.push_back(new DebounceEvent(BUTTON1_PIN));
  47. #endif
  48. #ifdef BUTTON2_PIN
  49. _buttons.push_back(new DebounceEvent(BUTTON2_PIN));
  50. #endif
  51. #ifdef BUTTON3_PIN
  52. _buttons.push_back(new DebounceEvent(BUTTON3_PIN));
  53. #endif
  54. #ifdef BUTTON4_PIN
  55. _buttons.push_back(new DebounceEvent(BUTTON4_PIN));
  56. #endif
  57. DEBUG_MSG("[BUTTON] Number of buttons: %d\n", _buttons.size());
  58. }
  59. void buttonLoop() {
  60. for (unsigned int i=0; i < _buttons.size(); i++) {
  61. if (_buttons[i]->loop()) {
  62. uint8_t event = _buttons[i]->getEvent();
  63. DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event);
  64. if (i == 0) {
  65. if (event == EVENT_DOUBLE_CLICK) createAP();
  66. if (event == EVENT_LONG_CLICK) ESP.reset();
  67. }
  68. if (event == EVENT_SINGLE_CLICK) relayToggle(i);
  69. }
  70. }
  71. }
  72. #else
  73. void buttonSetup() {}
  74. void buttonLoop() {}
  75. #endif
  76. #endif