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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_SOUND
  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. TERN_(HAS_BEEPER, if (ui.sound_on) buzzer.click(10));
  44. }
  45. // Encoder initialization
  46. void Encoder_Configuration() {
  47. #if BUTTON_EXISTS(EN1)
  48. SET_INPUT_PULLUP(BTN_EN1);
  49. #endif
  50. #if BUTTON_EXISTS(EN2)
  51. SET_INPUT_PULLUP(BTN_EN2);
  52. #endif
  53. #if BUTTON_EXISTS(ENC)
  54. SET_INPUT_PULLUP(BTN_ENC);
  55. #endif
  56. #if HAS_BEEPER
  57. SET_OUTPUT(BEEPER_PIN); // TODO: Use buzzer.h which already inits this
  58. #endif
  59. }
  60. // Analyze encoder value and return state
  61. EncoderState Encoder_ReceiveAnalyze() {
  62. const millis_t now = millis();
  63. static uint8_t lastEncoderBits;
  64. uint8_t newbutton = 0;
  65. static signed char temp_diff = 0;
  66. EncoderState temp_diffState = ENCODER_DIFF_NO;
  67. if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
  68. if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
  69. if (BUTTON_PRESSED(ENC)) {
  70. static millis_t next_click_update_ms;
  71. if (ELAPSED(now, next_click_update_ms)) {
  72. next_click_update_ms = millis() + 300;
  73. Encoder_tick();
  74. #if PIN_EXISTS(LCD_LED)
  75. //LED_Action();
  76. #endif
  77. if (!ui.backlight) ui.refresh_brightness();
  78. const bool was_waiting = wait_for_user;
  79. wait_for_user = false;
  80. return was_waiting ? ENCODER_DIFF_NO : ENCODER_DIFF_ENTER;
  81. }
  82. else return ENCODER_DIFF_NO;
  83. }
  84. if (newbutton != lastEncoderBits) {
  85. switch (newbutton) {
  86. case 0:
  87. if (lastEncoderBits == 1) temp_diff++;
  88. else if (lastEncoderBits == 2) temp_diff--;
  89. break;
  90. case 2:
  91. if (lastEncoderBits == 0) temp_diff++;
  92. else if (lastEncoderBits == 3) temp_diff--;
  93. break;
  94. case 3:
  95. if (lastEncoderBits == 2) temp_diff++;
  96. else if (lastEncoderBits == 1) temp_diff--;
  97. break;
  98. case 1:
  99. if (lastEncoderBits == 3) temp_diff++;
  100. else if (lastEncoderBits == 0) temp_diff--;
  101. break;
  102. }
  103. lastEncoderBits = newbutton;
  104. }
  105. if (ABS(temp_diff) >= ENCODER_PULSES_PER_STEP) {
  106. if (temp_diff > 0) temp_diffState = TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CCW, ENCODER_DIFF_CW);
  107. else temp_diffState = TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CW, ENCODER_DIFF_CCW);
  108. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  109. millis_t ms = millis();
  110. int32_t encoderMultiplier = 1;
  111. // if must encoder rati multiplier
  112. if (EncoderRate.enabled) {
  113. const float abs_diff = ABS(temp_diff),
  114. encoderMovementSteps = abs_diff / (ENCODER_PULSES_PER_STEP);
  115. if (EncoderRate.lastEncoderTime) {
  116. // Note that the rate is always calculated between two passes through the
  117. // loop and that the abs of the temp_diff value is tracked.
  118. const float encoderStepRate = encoderMovementSteps / float(ms - EncoderRate.lastEncoderTime) * 1000;
  119. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  120. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  121. #if ENCODER_5X_STEPS_PER_SEC
  122. else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoderMultiplier = 5;
  123. #endif
  124. }
  125. EncoderRate.lastEncoderTime = ms;
  126. }
  127. #else
  128. constexpr int32_t encoderMultiplier = 1;
  129. #endif
  130. // EncoderRate.encoderMoveValue += (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  131. EncoderRate.encoderMoveValue = (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  132. if (EncoderRate.encoderMoveValue < 0) EncoderRate.encoderMoveValue = -EncoderRate.encoderMoveValue;
  133. temp_diff = 0;
  134. }
  135. return temp_diffState;
  136. }
  137. #if PIN_EXISTS(LCD_LED)
  138. // 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
  139. uint16_t LED_DataArray[LED_NUM];
  140. // LED light operation
  141. void LED_Action() {
  142. LED_Control(RGB_SCALE_WARM_WHITE,0x0F);
  143. delay(30);
  144. LED_Control(RGB_SCALE_WARM_WHITE,0x00);
  145. }
  146. // LED initialization
  147. void LED_Configuration() {
  148. SET_OUTPUT(LCD_LED_PIN);
  149. }
  150. // LED write data
  151. void LED_WriteData() {
  152. uint8_t tempCounter_LED, tempCounter_Bit;
  153. for (tempCounter_LED = 0; tempCounter_LED < LED_NUM; tempCounter_LED++) {
  154. for (tempCounter_Bit = 0; tempCounter_Bit < 24; tempCounter_Bit++) {
  155. if (LED_DataArray[tempCounter_LED] & (0x800000 >> tempCounter_Bit)) {
  156. LED_DATA_HIGH;
  157. DELAY_NS(300);
  158. LED_DATA_LOW;
  159. DELAY_NS(200);
  160. }
  161. else {
  162. LED_DATA_HIGH;
  163. LED_DATA_LOW;
  164. DELAY_NS(200);
  165. }
  166. }
  167. }
  168. }
  169. // LED control
  170. // RGB_Scale: RGB color ratio
  171. // luminance: brightness (0~0xFF)
  172. void LED_Control(const uint8_t RGB_Scale, const uint8_t luminance) {
  173. for (uint8_t i = 0; i < LED_NUM; i++) {
  174. LED_DataArray[i] = 0;
  175. switch (RGB_Scale) {
  176. case RGB_SCALE_R10_G7_B5: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 7/10) << 16 | luminance * 5/10; break;
  177. case RGB_SCALE_R10_G7_B4: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 7/10) << 16 | luminance * 4/10; break;
  178. case RGB_SCALE_R10_G8_B7: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 8/10) << 16 | luminance * 7/10; break;
  179. }
  180. }
  181. LED_WriteData();
  182. }
  183. // LED gradient control
  184. // RGB_Scale: RGB color ratio
  185. // luminance: brightness (0~0xFF)
  186. // change_Time: gradient time (ms)
  187. void LED_GraduallyControl(const uint8_t RGB_Scale, const uint8_t luminance, const uint16_t change_Interval) {
  188. struct { uint8_t g, r, b; } led_data[LED_NUM];
  189. for (uint8_t i = 0; i < LED_NUM; i++) {
  190. switch (RGB_Scale) {
  191. case RGB_SCALE_R10_G7_B5:
  192. led_data[i] = { luminance * 7/10, luminance * 10/10, luminance * 5/10 };
  193. break;
  194. case RGB_SCALE_R10_G7_B4:
  195. led_data[i] = { luminance * 7/10, luminance * 10/10, luminance * 4/10 };
  196. break;
  197. case RGB_SCALE_R10_G8_B7:
  198. led_data[i] = { luminance * 8/10, luminance * 10/10, luminance * 7/10 };
  199. break;
  200. }
  201. }
  202. struct { bool g, r, b; } led_flag = { false, false, false };
  203. for (uint8_t i = 0; i < LED_NUM; i++) {
  204. while (1) {
  205. const uint8_t g = uint8_t(LED_DataArray[i] >> 16),
  206. r = uint8_t(LED_DataArray[i] >> 8),
  207. b = uint8_t(LED_DataArray[i]);
  208. if (g == led_data[i].g) led_flag.g = true;
  209. else LED_DataArray[i] += (g > led_data[i].g) ? -0x010000 : 0x010000;
  210. if (r == led_data[i].r) led_flag.r = true;
  211. else LED_DataArray[i] += (r > led_data[i].r) ? -0x000100 : 0x000100;
  212. if (b == led_data[i].b) led_flag.b = true;
  213. else LED_DataArray[i] += (b > led_data[i].b) ? -0x000001 : 0x000001;
  214. LED_WriteData();
  215. if (led_flag.r && led_flag.g && led_flag.b) break;
  216. delay(change_Interval);
  217. }
  218. }
  219. }
  220. #endif // LCD_LED
  221. #endif // HAS_DWIN_E3V2