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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. ******************************************************************************
  24. * @file rotary_encoder.cpp
  25. * @author LEO / Creality3D
  26. * @date 2019/07/06
  27. * @version 2.0.1
  28. * @brief 旋转编码器操作函数
  29. ******************************************************************************
  30. **/
  31. #include "../../inc/MarlinConfigPre.h"
  32. #if ENABLED(DWIN_CREALITY_LCD)
  33. #include "rotary_encoder.h"
  34. #include "../../MarlinCore.h"
  35. #include "../../HAL/shared/Delay.h"
  36. #if HAS_BUZZER
  37. #include "../../libs/buzzer.h"
  38. #endif
  39. #include <stdlib.h>
  40. ENCODER_Rate EncoderRate;
  41. /*蜂鸣器响*/
  42. void Encoder_tick(void) {
  43. WRITE(BEEPER_PIN,1);
  44. delay(10);
  45. WRITE(BEEPER_PIN,0);
  46. }
  47. /*编码器初始化 PB12:Encoder_A PB13:Encoder_B PB14:Encoder_C*/
  48. void Encoder_Configuration(void) {
  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. #ifdef BEEPER_PIN
  59. SET_OUTPUT(BEEPER_PIN);
  60. #endif
  61. }
  62. millis_t next_click_update_ms;
  63. /*接收数据解析 返回值:ENCODER_DIFF_NO,无状态; ENCODER_DIFF_CW,顺时针旋转; ENCODER_DIFF_CCW,逆时针旋转; ENCODER_DIFF_ENTER,按下*/
  64. ENCODER_DiffState Encoder_ReceiveAnalyze(void) {
  65. const millis_t now = millis();
  66. static unsigned char lastEncoderBits;
  67. unsigned char newbutton = 0;
  68. static signed char temp_diff = 0;
  69. ENCODER_DiffState temp_diffState = ENCODER_DIFF_NO;
  70. if (BUTTON_PRESSED(EN1)) newbutton |= 0x01;
  71. if (BUTTON_PRESSED(EN2)) newbutton |= 0x02;
  72. if (BUTTON_PRESSED(ENC)) {
  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. return 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.encoderRateEnabled) {
  112. const float abs_diff = ABS(temp_diff);
  113. const float 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. /*取低24位有效 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灯操作*/
  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初始化*/
  144. void LED_Configuration(void) {
  145. SET_OUTPUT(LCD_LED_PIN);
  146. }
  147. /*LED写数据*/
  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控制 RGB_Scale:RGB色彩配比 luminance:亮度(0~0xFF)*/
  167. void LED_Control(unsigned char RGB_Scale, unsigned char luminance) {
  168. unsigned char temp_Counter;
  169. for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) {
  170. LED_DataArray[temp_Counter] = 0;
  171. switch(RGB_Scale) {
  172. case RGB_SCALE_R10_G7_B5: LED_DataArray[temp_Counter] = (luminance*10/10) << 8 | (luminance*7/10) << 16 | luminance*5/10; break;
  173. case RGB_SCALE_R10_G7_B4: LED_DataArray[temp_Counter] = (luminance*10/10) << 8 | (luminance*7/10) << 16 | luminance*4/10; break;
  174. case RGB_SCALE_R10_G8_B7: LED_DataArray[temp_Counter] = (luminance*10/10) << 8 | (luminance*8/10) << 16 | luminance*7/10; break;
  175. }
  176. }
  177. LED_WriteData();
  178. }
  179. /*LED渐变控制 RGB_Scale:RGB色彩配比 luminance:亮度(0~0xFF) change_Time:渐变时间(ms)*/
  180. void LED_GraduallyControl(unsigned char RGB_Scale, unsigned char luminance, unsigned int change_Interval) {
  181. unsigned char temp_Counter;
  182. unsigned char LED_R_Data[LED_NUM], LED_G_Data[LED_NUM], LED_B_Data[LED_NUM];
  183. bool LED_R_Flag = 0, LED_G_Flag = 0, LED_B_Flag = 0;
  184. for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) {
  185. switch(RGB_Scale) {
  186. case RGB_SCALE_R10_G7_B5: {
  187. LED_R_Data[temp_Counter] = luminance*10/10;
  188. LED_G_Data[temp_Counter] = luminance*7/10;
  189. LED_B_Data[temp_Counter] = luminance*5/10;
  190. }break;
  191. case RGB_SCALE_R10_G7_B4: {
  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*4/10;
  195. }break;
  196. case RGB_SCALE_R10_G8_B7: {
  197. LED_R_Data[temp_Counter] = luminance*10/10;
  198. LED_G_Data[temp_Counter] = luminance*8/10;
  199. LED_B_Data[temp_Counter] = luminance*7/10;
  200. }break;
  201. }
  202. }
  203. for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) {
  204. if ((unsigned char)(LED_DataArray[temp_Counter] >> 8) > LED_R_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x000100;
  205. else if ((unsigned char)(LED_DataArray[temp_Counter] >> 8) < LED_R_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x000100;
  206. while (1) {
  207. else LED_R_Flag = 1;
  208. if ((unsigned char)(LED_DataArray[temp_Counter]>>16) > LED_G_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x010000;
  209. else if ((unsigned char)(LED_DataArray[temp_Counter]>>16) < LED_G_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x010000;
  210. else LED_G_Flag = 1;
  211. if ((unsigned char)LED_DataArray[temp_Counter] > LED_B_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x000001;
  212. else if ((unsigned char)LED_DataArray[temp_Counter] < LED_B_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x000001;
  213. else LED_B_Flag = 1;
  214. }
  215. LED_WriteData();
  216. if (LED_R_Flag && LED_G_Flag && LED_B_Flag) break;
  217. else delay(change_Interval);
  218. }
  219. }
  220. #endif
  221. #endif // DWIN_CREALITY_LCD