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.

rotary_encoder.cpp 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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/creality/rotary_encoder.cpp
  24. * @author LEO / Creality3D
  25. * @date 2019/07/06
  26. * @version 2.0.1
  27. * @brief Rotary encoder functions
  28. *****************************************************************************/
  29. #include "../../../inc/MarlinConfigPre.h"
  30. #if ENABLED(DWIN_CREALITY_LCD)
  31. #include "rotary_encoder.h"
  32. #include "../../buttons.h"
  33. #include "../../../MarlinCore.h"
  34. #include "../../../HAL/shared/Delay.h"
  35. #if HAS_BUZZER
  36. #include "../../../libs/buzzer.h"
  37. #endif
  38. #include <stdlib.h>
  39. #ifndef ENCODER_PULSES_PER_STEP
  40. #define ENCODER_PULSES_PER_STEP 4
  41. #endif
  42. ENCODER_Rate EncoderRate;
  43. // Buzzer
  44. void Encoder_tick() {
  45. #if PIN_EXISTS(BEEPER)
  46. WRITE(BEEPER_PIN, HIGH);
  47. delay(10);
  48. WRITE(BEEPER_PIN, LOW);
  49. #endif
  50. }
  51. // Encoder initialization
  52. void Encoder_Configuration() {
  53. #if BUTTON_EXISTS(EN1)
  54. SET_INPUT_PULLUP(BTN_EN1);
  55. #endif
  56. #if BUTTON_EXISTS(EN2)
  57. SET_INPUT_PULLUP(BTN_EN2);
  58. #endif
  59. #if BUTTON_EXISTS(ENC)
  60. SET_INPUT_PULLUP(BTN_ENC);
  61. #endif
  62. #if PIN_EXISTS(BEEPER)
  63. SET_OUTPUT(BEEPER_PIN);
  64. #endif
  65. }
  66. // Analyze encoder value and return state
  67. ENCODER_DiffState Encoder_ReceiveAnalyze() {
  68. const millis_t now = millis();
  69. static uint8_t lastEncoderBits;
  70. uint8_t newbutton = 0;
  71. static signed char temp_diff = 0;
  72. ENCODER_DiffState temp_diffState = ENCODER_DIFF_NO;
  73. if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
  74. if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
  75. if (BUTTON_PRESSED(ENC)) {
  76. static millis_t next_click_update_ms;
  77. if (ELAPSED(now, next_click_update_ms)) {
  78. next_click_update_ms = millis() + 300;
  79. Encoder_tick();
  80. #if PIN_EXISTS(LCD_LED)
  81. //LED_Action();
  82. #endif
  83. const bool was_waiting = wait_for_user;
  84. wait_for_user = false;
  85. return was_waiting ? ENCODER_DIFF_NO : ENCODER_DIFF_ENTER;
  86. }
  87. else return ENCODER_DIFF_NO;
  88. }
  89. if (newbutton != lastEncoderBits) {
  90. switch (newbutton) {
  91. case ENCODER_PHASE_0:
  92. if (lastEncoderBits == ENCODER_PHASE_3) temp_diff++;
  93. else if (lastEncoderBits == ENCODER_PHASE_1) temp_diff--;
  94. break;
  95. case ENCODER_PHASE_1:
  96. if (lastEncoderBits == ENCODER_PHASE_0) temp_diff++;
  97. else if (lastEncoderBits == ENCODER_PHASE_2) temp_diff--;
  98. break;
  99. case ENCODER_PHASE_2:
  100. if (lastEncoderBits == ENCODER_PHASE_1) temp_diff++;
  101. else if (lastEncoderBits == ENCODER_PHASE_3) temp_diff--;
  102. break;
  103. case ENCODER_PHASE_3:
  104. if (lastEncoderBits == ENCODER_PHASE_2) temp_diff++;
  105. else if (lastEncoderBits == ENCODER_PHASE_0) temp_diff--;
  106. break;
  107. }
  108. lastEncoderBits = newbutton;
  109. }
  110. if (ABS(temp_diff) >= ENCODER_PULSES_PER_STEP) {
  111. #if ENABLED(REVERSE_ENCODER_DIRECTION)
  112. if (temp_diff > 0) temp_diffState = ENCODER_DIFF_CCW;
  113. else temp_diffState = ENCODER_DIFF_CW;
  114. #else
  115. if (temp_diff > 0) temp_diffState = ENCODER_DIFF_CW;
  116. else temp_diffState = ENCODER_DIFF_CCW;
  117. #endif
  118. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  119. millis_t ms = millis();
  120. int32_t encoderMultiplier = 1;
  121. // if must encoder rati multiplier
  122. if (EncoderRate.enabled) {
  123. const float abs_diff = ABS(temp_diff),
  124. encoderMovementSteps = abs_diff / (ENCODER_PULSES_PER_STEP);
  125. if (EncoderRate.lastEncoderTime) {
  126. // Note that the rate is always calculated between two passes through the
  127. // loop and that the abs of the temp_diff value is tracked.
  128. const float encoderStepRate = encoderMovementSteps / float(ms - EncoderRate.lastEncoderTime) * 1000;
  129. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  130. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  131. #if ENCODER_5X_STEPS_PER_SEC
  132. else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoderMultiplier = 5;
  133. #endif
  134. }
  135. EncoderRate.lastEncoderTime = ms;
  136. }
  137. #else
  138. constexpr int32_t encoderMultiplier = 1;
  139. #endif
  140. // EncoderRate.encoderMoveValue += (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  141. EncoderRate.encoderMoveValue = (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  142. if (EncoderRate.encoderMoveValue < 0) EncoderRate.encoderMoveValue = -EncoderRate.encoderMoveValue;
  143. temp_diff = 0;
  144. }
  145. return temp_diffState;
  146. }
  147. #if PIN_EXISTS(LCD_LED)
  148. // 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
  149. uint16_t LED_DataArray[LED_NUM];
  150. // LED light operation
  151. void LED_Action() {
  152. LED_Control(RGB_SCALE_WARM_WHITE,0x0F);
  153. delay(30);
  154. LED_Control(RGB_SCALE_WARM_WHITE,0x00);
  155. }
  156. // LED initialization
  157. void LED_Configuration() {
  158. SET_OUTPUT(LCD_LED_PIN);
  159. }
  160. // LED write data
  161. void LED_WriteData() {
  162. uint8_t tempCounter_LED, tempCounter_Bit;
  163. for (tempCounter_LED = 0; tempCounter_LED < LED_NUM; tempCounter_LED++) {
  164. for (tempCounter_Bit = 0; tempCounter_Bit < 24; tempCounter_Bit++) {
  165. if (LED_DataArray[tempCounter_LED] & (0x800000 >> tempCounter_Bit)) {
  166. LED_DATA_HIGH;
  167. DELAY_NS(300);
  168. LED_DATA_LOW;
  169. DELAY_NS(200);
  170. }
  171. else {
  172. LED_DATA_HIGH;
  173. LED_DATA_LOW;
  174. DELAY_NS(200);
  175. }
  176. }
  177. }
  178. }
  179. // LED control
  180. // RGB_Scale: RGB color ratio
  181. // luminance: brightness (0~0xFF)
  182. void LED_Control(const uint8_t RGB_Scale, const uint8_t luminance) {
  183. for (uint8_t i = 0; i < LED_NUM; i++) {
  184. LED_DataArray[i] = 0;
  185. switch (RGB_Scale) {
  186. case RGB_SCALE_R10_G7_B5: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 7/10) << 16 | luminance * 5/10; break;
  187. case RGB_SCALE_R10_G7_B4: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 7/10) << 16 | luminance * 4/10; break;
  188. case RGB_SCALE_R10_G8_B7: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 8/10) << 16 | luminance * 7/10; break;
  189. }
  190. }
  191. LED_WriteData();
  192. }
  193. // LED gradient control
  194. // RGB_Scale: RGB color ratio
  195. // luminance: brightness (0~0xFF)
  196. // change_Time: gradient time (ms)
  197. void LED_GraduallyControl(const uint8_t RGB_Scale, const uint8_t luminance, const uint16_t change_Interval) {
  198. struct { uint8_t g, r, b; } led_data[LED_NUM];
  199. for (uint8_t i = 0; i < LED_NUM; i++) {
  200. switch (RGB_Scale) {
  201. case RGB_SCALE_R10_G7_B5:
  202. led_data[i] = { luminance * 7/10, luminance * 10/10, luminance * 5/10 };
  203. break;
  204. case RGB_SCALE_R10_G7_B4:
  205. led_data[i] = { luminance * 7/10, luminance * 10/10, luminance * 4/10 };
  206. break;
  207. case RGB_SCALE_R10_G8_B7:
  208. led_data[i] = { luminance * 8/10, luminance * 10/10, luminance * 7/10 };
  209. break;
  210. }
  211. }
  212. struct { bool g, r, b; } led_flag = { false, false, false };
  213. for (uint8_t i = 0; i < LED_NUM; i++) {
  214. while (1) {
  215. const uint8_t g = uint8_t(LED_DataArray[i] >> 16),
  216. r = uint8_t(LED_DataArray[i] >> 8),
  217. b = uint8_t(LED_DataArray[i]);
  218. if (g == led_data[i].g) led_flag.g = true;
  219. else LED_DataArray[i] += (g > led_data[i].g) ? -0x010000 : 0x010000;
  220. if (r == led_data[i].r) led_flag.r = true;
  221. else LED_DataArray[i] += (r > led_data[i].r) ? -0x000100 : 0x000100;
  222. if (b == led_data[i].b) led_flag.b = true;
  223. else LED_DataArray[i] += (b > led_data[i].b) ? -0x000001 : 0x000001;
  224. LED_WriteData();
  225. if (led_flag.r && led_flag.g && led_flag.b) break;
  226. delay(change_Interval);
  227. }
  228. }
  229. }
  230. #endif // LCD_LED
  231. #endif // DWIN_CREALITY_LCD