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.

156 lines
4.5 KiB

  1. /*
  2. ENCODER MODULE
  3. Copyright (C) 2018-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "encoder.h"
  6. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  7. struct encoder_t {
  8. Encoder * encoder;
  9. unsigned char button_pin;
  10. unsigned char button_logic;
  11. unsigned char button_mode;
  12. unsigned char mode;
  13. unsigned char channel1; // default
  14. unsigned char channel2; // only if button defined and pressed
  15. };
  16. std::vector<encoder_t> _encoders;
  17. unsigned long _encoder_min_delta = 1;
  18. void _encoderConfigure() {
  19. _encoder_min_delta = getSetting("encMinDelta", ENCODER_MINIMUM_DELTA);
  20. if (!_encoder_min_delta) _encoder_min_delta = 1;
  21. // no need to reload objects right now
  22. if (_encoders.size()) return;
  23. // Clean previous encoders and re-add them
  24. for (auto& encoder : _encoders) {
  25. delete encoder.encoder;
  26. }
  27. _encoders.clear();
  28. // TODO: encEnable
  29. // TODO: implement reloading without re-allocating objects
  30. #if (ENCODER1_PIN1 != GPIO_NONE) && (ENCODER1_PIN2 != GPIO_NONE)
  31. {
  32. _encoders.push_back({
  33. new Encoder(ENCODER1_PIN1, ENCODER1_PIN2),
  34. ENCODER1_BUTTON_PIN, ENCODER1_BUTTON_LOGIC, ENCODER1_BUTTON_MODE, ENCODER1_MODE,
  35. ENCODER1_CHANNEL1, ENCODER1_CHANNEL2
  36. });
  37. }
  38. #endif
  39. #if (ENCODER2_PIN1 != GPIO_NONE) && (ENCODER2_PIN2 != GPIO_NONE)
  40. {
  41. _encoders.push_back({
  42. new Encoder(ENCODER2_PIN1, ENCODER2_PIN2),
  43. ENCODER2_BUTTON_PIN, ENCODER2_BUTTON_LOGIC, ENCODER2_BUTTON_MODE, ENCODER2_MODE,
  44. ENCODER2_CHANNEL1, ENCODER2_CHANNEL2
  45. });
  46. }
  47. #endif
  48. #if (ENCODER3_PIN1 != GPIO_NONE) && (ENCODER3_PIN2 != GPIO_NONE)
  49. {
  50. _encoders.push_back({
  51. new Encoder(ENCODER3_PIN1, ENCODER3_PIN2),
  52. ENCODER3_BUTTON_PIN, ENCODER3_BUTTON_LOGIC, ENCODER3_BUTTON_MODE, ENCODER3_MODE,
  53. ENCODER3_CHANNEL1, ENCODER3_CHANNEL2
  54. });
  55. }
  56. #endif
  57. #if (ENCODER4_PIN1 != GPIO_NONE) && (ENCODER4_PIN2 != GPIO_NONE)
  58. {
  59. _encoders.push_back({
  60. new Encoder(ENCODER4_PIN1, ENCODER4_PIN2),
  61. ENCODER4_BUTTON_PIN, ENCODER4_BUTTON_LOGIC, ENCODER4_BUTTON_MODE, ENCODER4_MODE,
  62. ENCODER4_CHANNEL1, ENCODER4_CHANNEL2
  63. });
  64. }
  65. #endif
  66. #if (ENCODER5_PIN1 != GPIO_NONE) && (ENCODER5_PIN2 != GPIO_NONE)
  67. {
  68. _encoders.push_back({
  69. new Encoder(ENCODER5_PIN1, ENCODER5_PIN2),
  70. ENCODER5_BUTTON_PIN, ENCODER5_BUTTON_LOGIC, ENCODER5_BUTTON_MODE, ENCODER5_MODE,
  71. ENCODER5_CHANNEL1, ENCODER5_CHANNEL2
  72. });
  73. }
  74. #endif
  75. // TODO: manage buttons through debounceevent?
  76. for (auto& encoder : _encoders) {
  77. if (GPIO_NONE != encoder.button_pin) {
  78. pinMode(encoder.button_pin, encoder.button_mode);
  79. }
  80. }
  81. }
  82. void _encoderLoop() {
  83. // for each encoder, read delta (read()) and map button action
  84. for (auto& encoder : _encoders) {
  85. const auto delta = encoder.encoder->read();
  86. encoder.encoder->write(0);
  87. if ((0 == delta) || (_encoder_min_delta > abs(delta))) continue;
  88. if (encoder.button_pin == GPIO_NONE) {
  89. // if there is no button, the encoder drives CHANNEL1
  90. lightChannelStep(encoder.channel1, delta);
  91. } else {
  92. // otherwise, use button based on encoder mode
  93. bool pressed = (digitalRead(encoder.button_pin) != encoder.button_logic);
  94. if (ENCODER_MODE_CHANNEL == encoder.mode) {
  95. // the button controls what channel we are changing
  96. lightChannelStep(pressed ? encoder.channel2 : encoder.channel1, delta);
  97. } if (ENCODER_MODE_RATIO == encoder.mode) {
  98. // the button controls if we are changing the channel ratio or the overall brightness
  99. if (pressed) {
  100. lightChannelStep(encoder.channel1, delta);
  101. lightChannelStep(encoder.channel2, -delta);
  102. } else {
  103. lightBrightnessStep(delta);
  104. }
  105. }
  106. }
  107. lightUpdate(true, true);
  108. }
  109. }
  110. // -----------------------------------------------------------------------------
  111. void encoderSetup() {
  112. // Configure encoders
  113. _encoderConfigure();
  114. // Main callbacks
  115. espurnaRegisterLoop(_encoderLoop);
  116. espurnaRegisterReload(_encoderConfigure);
  117. DEBUG_MSG_P(PSTR("[ENCODER] Number of encoders: %u\n"), _encoders.size());
  118. }
  119. #endif // ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)