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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 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 "../../../MarlinCore.h"
  33. #include "../../../HAL/shared/Delay.h"
  34. #if HAS_BUZZER
  35. #include "../../../libs/buzzer.h"
  36. #endif
  37. #include <stdlib.h>
  38. ENCODER_Rate EncoderRate;
  39. // Buzzer
  40. void Encoder_tick(void) {
  41. WRITE(BEEPER_PIN, 1);
  42. delay(10);
  43. WRITE(BEEPER_PIN, 0);
  44. }
  45. // Encoder initialization
  46. void Encoder_Configuration(void) {
  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. #ifdef BEEPER_PIN
  57. SET_OUTPUT(BEEPER_PIN);
  58. #endif
  59. }
  60. // Analyze encoder value and return state
  61. ENCODER_DiffState Encoder_ReceiveAnalyze(void) {
  62. const millis_t now = millis();
  63. static unsigned char lastEncoderBits;
  64. unsigned char newbutton = 0;
  65. static signed char temp_diff = 0;
  66. ENCODER_DiffState temp_diffState = ENCODER_DIFF_NO;
  67. if (BUTTON_PRESSED(EN1)) newbutton |= 0x01;
  68. if (BUTTON_PRESSED(EN2)) newbutton |= 0x02;
  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. const bool was_waiting = wait_for_user;
  78. wait_for_user = false;
  79. return was_waiting ? ENCODER_DIFF_NO : ENCODER_DIFF_ENTER;
  80. }
  81. else return ENCODER_DIFF_NO;
  82. }
  83. if (newbutton != lastEncoderBits) {
  84. switch (newbutton) {
  85. case ENCODER_PHASE_0: {
  86. if (lastEncoderBits == ENCODER_PHASE_3) temp_diff++;
  87. else if (lastEncoderBits == ENCODER_PHASE_1) temp_diff--;
  88. }break;
  89. case ENCODER_PHASE_1: {
  90. if (lastEncoderBits == ENCODER_PHASE_0) temp_diff++;
  91. else if (lastEncoderBits == ENCODER_PHASE_2) temp_diff--;
  92. }break;
  93. case ENCODER_PHASE_2: {
  94. if (lastEncoderBits == ENCODER_PHASE_1) temp_diff++;
  95. else if (lastEncoderBits == ENCODER_PHASE_3) temp_diff--;
  96. }break;
  97. case ENCODER_PHASE_3: {
  98. if (lastEncoderBits == ENCODER_PHASE_2) temp_diff++;
  99. else if (lastEncoderBits == ENCODER_PHASE_0) temp_diff--;
  100. }break;
  101. }
  102. lastEncoderBits = newbutton;
  103. }
  104. if (abs(temp_diff) >= ENCODER_PULSES_PER_STEP) {
  105. if (temp_diff > 0) temp_diffState = ENCODER_DIFF_CW;
  106. else temp_diffState = ENCODER_DIFF_CCW;
  107. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  108. millis_t ms = millis();
  109. int32_t encoderMultiplier = 1;
  110. // if must encoder rati multiplier
  111. if (EncoderRate.enabled) {
  112. const float abs_diff = ABS(temp_diff),
  113. encoderMovementSteps = abs_diff / (ENCODER_PULSES_PER_STEP);
  114. if (EncoderRate.lastEncoderTime) {
  115. // Note that the rate is always calculated between two passes through the
  116. // loop and that the abs of the temp_diff value is tracked.
  117. const float encoderStepRate = encoderMovementSteps / float(ms - EncoderRate.lastEncoderTime) * 1000;
  118. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  119. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  120. else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoderMultiplier = 5;
  121. }
  122. EncoderRate.lastEncoderTime = ms;
  123. }
  124. #else
  125. constexpr int32_t encoderMultiplier = 1;
  126. #endif // ENCODER_RATE_MULTIPLIER
  127. // EncoderRate.encoderMoveValue += (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  128. EncoderRate.encoderMoveValue = (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  129. if (EncoderRate.encoderMoveValue < 0) EncoderRate.encoderMoveValue = -EncoderRate.encoderMoveValue;
  130. temp_diff = 0;
  131. }
  132. return temp_diffState;
  133. }
  134. #if PIN_EXISTS(LCD_LED)
  135. // 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
  136. unsigned int LED_DataArray[LED_NUM];
  137. // LED light operation
  138. void LED_Action(void) {
  139. LED_Control(RGB_SCALE_WARM_WHITE,0x0F);
  140. delay(30);
  141. LED_Control(RGB_SCALE_WARM_WHITE,0x00);
  142. }
  143. // LED initialization
  144. void LED_Configuration(void) {
  145. SET_OUTPUT(LCD_LED_PIN);
  146. }
  147. // LED write data
  148. void LED_WriteData(void) {
  149. unsigned char tempCounter_LED, tempCounter_Bit;
  150. for (tempCounter_LED = 0; tempCounter_LED < LED_NUM; tempCounter_LED++) {
  151. for (tempCounter_Bit = 0; tempCounter_Bit < 24; tempCounter_Bit++) {
  152. if (LED_DataArray[tempCounter_LED] & (0x800000 >> tempCounter_Bit)) {
  153. LED_DATA_HIGH;
  154. DELAY_NS(300);
  155. LED_DATA_LOW;
  156. DELAY_NS(200);
  157. }
  158. else {
  159. LED_DATA_HIGH;
  160. LED_DATA_LOW;
  161. DELAY_NS(200);
  162. }
  163. }
  164. }
  165. }
  166. // LED control
  167. // RGB_Scale: RGB color ratio
  168. // luminance: brightness (0~0xFF)
  169. void LED_Control(unsigned char RGB_Scale, unsigned char luminance) {
  170. unsigned char temp_Counter;
  171. for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) {
  172. LED_DataArray[temp_Counter] = 0;
  173. switch (RGB_Scale) {
  174. case RGB_SCALE_R10_G7_B5: LED_DataArray[temp_Counter] = (luminance*10/10) << 8 | (luminance*7/10) << 16 | luminance*5/10; break;
  175. case RGB_SCALE_R10_G7_B4: LED_DataArray[temp_Counter] = (luminance*10/10) << 8 | (luminance*7/10) << 16 | luminance*4/10; break;
  176. case RGB_SCALE_R10_G8_B7: LED_DataArray[temp_Counter] = (luminance*10/10) << 8 | (luminance*8/10) << 16 | luminance*7/10; break;
  177. }
  178. }
  179. LED_WriteData();
  180. }
  181. // LED gradient control
  182. // RGB_Scale: RGB color ratio
  183. // luminance: brightness (0~0xFF)
  184. // change_Time: gradient time (ms)
  185. void LED_GraduallyControl(unsigned char RGB_Scale, unsigned char luminance, unsigned int change_Interval) {
  186. unsigned char temp_Counter;
  187. unsigned char LED_R_Data[LED_NUM], LED_G_Data[LED_NUM], LED_B_Data[LED_NUM];
  188. bool LED_R_Flag = 0, LED_G_Flag = 0, LED_B_Flag = 0;
  189. for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) {
  190. switch (RGB_Scale) {
  191. case RGB_SCALE_R10_G7_B5: {
  192. LED_R_Data[temp_Counter] = luminance*10/10;
  193. LED_G_Data[temp_Counter] = luminance*7/10;
  194. LED_B_Data[temp_Counter] = luminance*5/10;
  195. }break;
  196. case RGB_SCALE_R10_G7_B4: {
  197. LED_R_Data[temp_Counter] = luminance*10/10;
  198. LED_G_Data[temp_Counter] = luminance*7/10;
  199. LED_B_Data[temp_Counter] = luminance*4/10;
  200. }break;
  201. case RGB_SCALE_R10_G8_B7: {
  202. LED_R_Data[temp_Counter] = luminance*10/10;
  203. LED_G_Data[temp_Counter] = luminance*8/10;
  204. LED_B_Data[temp_Counter] = luminance*7/10;
  205. }break;
  206. }
  207. }
  208. for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) {
  209. if ((unsigned char)(LED_DataArray[temp_Counter] >> 8) > LED_R_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x000100;
  210. else if ((unsigned char)(LED_DataArray[temp_Counter] >> 8) < LED_R_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x000100;
  211. while (1) {
  212. else LED_R_Flag = 1;
  213. if ((unsigned char)(LED_DataArray[temp_Counter]>>16) > LED_G_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x010000;
  214. else if ((unsigned char)(LED_DataArray[temp_Counter]>>16) < LED_G_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x010000;
  215. else LED_G_Flag = 1;
  216. if ((unsigned char)LED_DataArray[temp_Counter] > LED_B_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x000001;
  217. else if ((unsigned char)LED_DataArray[temp_Counter] < LED_B_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x000001;
  218. else LED_B_Flag = 1;
  219. }
  220. LED_WriteData();
  221. if (LED_R_Flag && LED_G_Flag && LED_B_Flag) break;
  222. else delay(change_Interval);
  223. }
  224. }
  225. #endif // LCD_LED
  226. #endif // DWIN_CREALITY_LCD