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.

118 lines
3.1 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. #ifdef ITEAD_1CH_INCHING
  58. pinMode(LED_INCHING, OUTPUT);
  59. byte relayInch = getSetting("relayInch", String(RELAY_INCHING)).toInt();
  60. digitalWrite(LED_INCHING, relayInch != RELAY_INCHING_NONE);
  61. #endif
  62. DEBUG_MSG("[BUTTON] Number of buttons: %d\n", _buttons.size());
  63. }
  64. void buttonLoop() {
  65. for (unsigned int i=0; i < _buttons.size(); i++) {
  66. if (_buttons[i]->loop()) {
  67. uint8_t event = _buttons[i]->getEvent();
  68. DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event);
  69. if (i == 0) {
  70. if (event == EVENT_DOUBLE_CLICK) createAP();
  71. if (event == EVENT_LONG_CLICK) ESP.reset();
  72. }
  73. #ifdef ITEAD_1CH_INCHING
  74. if (i == 1) {
  75. relayInchingToggle();
  76. continue;
  77. }
  78. #endif
  79. if (event == EVENT_SINGLE_CLICK) relayToggle(i);
  80. }
  81. }
  82. }
  83. #else
  84. void buttonSetup() {}
  85. void buttonLoop() {}
  86. #endif
  87. #endif