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.

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