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.

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