My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

encoder.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /*****************************************************************************
  23. * @file lcd/e3v2/common/encoder.cpp
  24. * @brief Rotary encoder functions
  25. *****************************************************************************/
  26. #include "../../../inc/MarlinConfigPre.h"
  27. #if HAS_DWIN_E3V2
  28. #include "encoder.h"
  29. #include "../../buttons.h"
  30. #include "../../../MarlinCore.h"
  31. #include "../../marlinui.h"
  32. #include "../../../HAL/shared/Delay.h"
  33. #if HAS_BUZZER
  34. #include "../../../libs/buzzer.h"
  35. #endif
  36. #include <stdlib.h>
  37. #ifndef ENCODER_PULSES_PER_STEP
  38. #define ENCODER_PULSES_PER_STEP 4
  39. #endif
  40. ENCODER_Rate EncoderRate;
  41. // TODO: Replace with ui.quick_feedback
  42. void Encoder_tick() {
  43. #if PIN_EXISTS(BEEPER)
  44. if (ui.sound_on) buzzer.click(10);
  45. #endif
  46. }
  47. // Encoder initialization
  48. void Encoder_Configuration() {
  49. #if BUTTON_EXISTS(EN1)
  50. SET_INPUT_PULLUP(BTN_EN1);
  51. #endif
  52. #if BUTTON_EXISTS(EN2)
  53. SET_INPUT_PULLUP(BTN_EN2);
  54. #endif
  55. #if BUTTON_EXISTS(ENC)
  56. SET_INPUT_PULLUP(BTN_ENC);
  57. #endif
  58. #if PIN_EXISTS(BEEPER)
  59. SET_OUTPUT(BEEPER_PIN); // TODO: Use buzzer.h which already inits this
  60. #endif
  61. }
  62. // Analyze encoder value and return state
  63. EncoderState Encoder_ReceiveAnalyze() {
  64. const millis_t now = millis();
  65. static uint8_t lastEncoderBits;
  66. uint8_t newbutton = 0;
  67. static signed char temp_diff = 0;
  68. EncoderState temp_diffState = ENCODER_DIFF_NO;
  69. if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
  70. if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
  71. if (BUTTON_PRESSED(ENC)) {
  72. static millis_t next_click_update_ms;
  73. if (ELAPSED(now, next_click_update_ms)) {
  74. next_click_update_ms = millis() + 300;
  75. Encoder_tick();
  76. #if PIN_EXISTS(LCD_LED)
  77. //LED_Action();
  78. #endif
  79. if (!ui.backlight) ui.refresh_brightness();
  80. const bool was_waiting = wait_for_user;
  81. wait_for_user = false;
  82. return was_waiting ? ENCODER_DIFF_NO : ENCODER_DIFF_ENTER;
  83. }
  84. else return ENCODER_DIFF_NO;
  85. }
  86. if (newbutton != lastEncoderBits) {
  87. switch (newbutton) {
  88. case ENCODER_PHASE_0:
  89. if (lastEncoderBits == ENCODER_PHASE_3) temp_diff++;
  90. else if (lastEncoderBits == ENCODER_PHASE_1) temp_diff--;
  91. break;
  92. case ENCODER_PHASE_1:
  93. if (lastEncoderBits == ENCODER_PHASE_0) temp_diff++;
  94. else if (lastEncoderBits == ENCODER_PHASE_2) temp_diff--;
  95. break;
  96. case ENCODER_PHASE_2:
  97. if (lastEncoderBits == ENCODER_PHASE_1) temp_diff++;
  98. else if (lastEncoderBits == ENCODER_PHASE_3) temp_diff--;
  99. break;
  100. case ENCODER_PHASE_3:
  101. if (lastEncoderBits == ENCODER_PHASE_2) temp_diff++;
  102. else if (lastEncoderBits == ENCODER_PHASE_0) temp_diff--;
  103. break;
  104. }
  105. lastEncoderBits = newbutton;
  106. }
  107. if (ABS(temp_diff) >= ENCODER_PULSES_PER_STEP) {
  108. if (temp_diff > 0) temp_diffState = TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CCW, ENCODER_DIFF_CW);
  109. else temp_diffState = TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CW, ENCODER_DIFF_CCW);
  110. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  111. millis_t ms = millis();
  112. int32_t encoderMultiplier = 1;
  113. // if must encoder rati multiplier
  114. if (EncoderRate.enabled) {
  115. const float abs_diff = ABS(temp_diff),
  116. encoderMovementSteps = abs_diff / (ENCODER_PULSES_PER_STEP);
  117. if (EncoderRate.lastEncoderTime) {
  118. // Note that the rate is always calculated between two passes through the
  119. // loop and that the abs of the temp_diff value is tracked.
  120. const float encoderStepRate = encoderMovementSteps / float(ms - EncoderRate.lastEncoderTime) * 1000;
  121. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  122. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  123. #if ENCODER_5X_STEPS_PER_SEC
  124. else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoderMultiplier = 5;
  125. #endif
  126. }
  127. EncoderRate.lastEncoderTime = ms;
  128. }
  129. #else
  130. constexpr int32_t encoderMultiplier = 1;
  131. #endif
  132. // EncoderRate.encoderMoveValue += (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  133. EncoderRate.encoderMoveValue = (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  134. if (EncoderRate.encoderMoveValue < 0) EncoderRate.encoderMoveValue = -EncoderRate.encoderMoveValue;
  135. temp_diff = 0;
  136. }
  137. return temp_diffState;
  138. }
  139. #if PIN_EXISTS(LCD_LED)
  140. // Take the low 24 valid bits 24Bit: G7 G6 G5 G4 G3 G2 G1 G0 R7 R6 R5 R4 R3 R2 R1 R0 B7 B6 B5 B4 B3 B2 B1 B0
  141. uint16_t LED_DataArray[LED_NUM];
  142. // LED light operation
  143. void LED_Action() {
  144. LED_Control(RGB_SCALE_WARM_WHITE,0x0F);
  145. delay(30);
  146. LED_Control(RGB_SCALE_WARM_WHITE,0x00);
  147. }
  148. // LED initialization
  149. void LED_Configuration() {
  150. SET_OUTPUT(LCD_LED_PIN);
  151. }
  152. // LED write data
  153. void LED_WriteData() {
  154. uint8_t tempCounter_LED, tempCounter_Bit;
  155. for (tempCounter_LED = 0; tempCounter_LED < LED_NUM; tempCounter_LED++) {
  156. for (tempCounter_Bit = 0; tempCounter_Bit < 24; tempCounter_Bit++) {
  157. if (LED_DataArray[tempCounter_LED] & (0x800000 >> tempCounter_Bit)) {
  158. LED_DATA_HIGH;
  159. DELAY_NS(300);
  160. LED_DATA_LOW;
  161. DELAY_NS(200);
  162. }
  163. else {
  164. LED_DATA_HIGH;
  165. LED_DATA_LOW;
  166. DELAY_NS(200);
  167. }
  168. }
  169. }
  170. }
  171. // LED control
  172. // RGB_Scale: RGB color ratio
  173. // luminance: brightness (0~0xFF)
  174. void LED_Control(const uint8_t RGB_Scale, const uint8_t luminance) {
  175. for (uint8_t i = 0; i < LED_NUM; i++) {
  176. LED_DataArray[i] = 0;
  177. switch (RGB_Scale) {
  178. case RGB_SCALE_R10_G7_B5: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 7/10) << 16 | luminance * 5/10; break;
  179. case RGB_SCALE_R10_G7_B4: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 7/10) << 16 | luminance * 4/10; break;
  180. case RGB_SCALE_R10_G8_B7: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 8/10) << 16 | luminance * 7/10; break;
  181. }
  182. }
  183. LED_WriteData();
  184. }
  185. // LED gradient control
  186. // RGB_Scale: RGB color ratio
  187. // luminance: brightness (0~0xFF)
  188. // change_Time: gradient time (ms)
  189. void LED_GraduallyControl(const uint8_t RGB_Scale, const uint8_t luminance, const uint16_t change_Interval) {
  190. struct { uint8_t g, r, b; } led_data[LED_NUM];
  191. for (uint8_t i = 0; i < LED_NUM; i++) {
  192. switch (RGB_Scale) {
  193. case RGB_SCALE_R10_G7_B5:
  194. led_data[i] = { luminance * 7/10, luminance * 10/10, luminance * 5/10 };
  195. break;
  196. case RGB_SCALE_R10_G7_B4:
  197. led_data[i] = { luminance * 7/10, luminance * 10/10, luminance * 4/10 };
  198. break;
  199. case RGB_SCALE_R10_G8_B7:
  200. led_data[i] = { luminance * 8/10, luminance * 10/10, luminance * 7/10 };
  201. break;
  202. }
  203. }
  204. struct { bool g, r, b; } led_flag = { false, false, false };
  205. for (uint8_t i = 0; i < LED_NUM; i++) {
  206. while (1) {
  207. const uint8_t g = uint8_t(LED_DataArray[i] >> 16),
  208. r = uint8_t(LED_DataArray[i] >> 8),
  209. b = uint8_t(LED_DataArray[i]);
  210. if (g == led_data[i].g) led_flag.g = true;
  211. else LED_DataArray[i] += (g > led_data[i].g) ? -0x010000 : 0x010000;
  212. if (r == led_data[i].r) led_flag.r = true;
  213. else LED_DataArray[i] += (r > led_data[i].r) ? -0x000100 : 0x000100;
  214. if (b == led_data[i].b) led_flag.b = true;
  215. else LED_DataArray[i] += (b > led_data[i].b) ? -0x000001 : 0x000001;
  216. LED_WriteData();
  217. if (led_flag.r && led_flag.g && led_flag.b) break;
  218. delay(change_Interval);
  219. }
  220. }
  221. }
  222. #endif // LCD_LED
  223. #endif // HAS_DWIN_E3V2