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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. ******************************************************************************
  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. const bool was_waiting = wait_for_user;
  80. wait_for_user = false;
  81. return was_waiting ? ENCODER_DIFF_NO : ENCODER_DIFF_ENTER;
  82. }
  83. else return ENCODER_DIFF_NO;
  84. }
  85. if (newbutton != lastEncoderBits) {
  86. switch (newbutton) {
  87. case ENCODER_PHASE_0: {
  88. if (lastEncoderBits == ENCODER_PHASE_3) temp_diff++;
  89. else if (lastEncoderBits == ENCODER_PHASE_1) temp_diff--;
  90. }break;
  91. case ENCODER_PHASE_1: {
  92. if (lastEncoderBits == ENCODER_PHASE_0) temp_diff++;
  93. else if (lastEncoderBits == ENCODER_PHASE_2) temp_diff--;
  94. }break;
  95. case ENCODER_PHASE_2: {
  96. if (lastEncoderBits == ENCODER_PHASE_1) temp_diff++;
  97. else if (lastEncoderBits == ENCODER_PHASE_3) temp_diff--;
  98. }break;
  99. case ENCODER_PHASE_3: {
  100. if (lastEncoderBits == ENCODER_PHASE_2) temp_diff++;
  101. else if (lastEncoderBits == ENCODER_PHASE_0) temp_diff--;
  102. }break;
  103. }
  104. lastEncoderBits = newbutton;
  105. }
  106. if (abs(temp_diff) >= ENCODER_PULSES_PER_STEP) {
  107. if (temp_diff > 0) temp_diffState = ENCODER_DIFF_CW;
  108. else temp_diffState = ENCODER_DIFF_CCW;
  109. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  110. millis_t ms = millis();
  111. int32_t encoderMultiplier = 1;
  112. // if must encoder rati multiplier
  113. if (EncoderRate.encoderRateEnabled) {
  114. const float abs_diff = ABS(temp_diff);
  115. const float encoderMovementSteps = abs_diff / (ENCODER_PULSES_PER_STEP);
  116. if (EncoderRate.lastEncoderTime) {
  117. // Note that the rate is always calculated between two passes through the
  118. // loop and that the abs of the temp_diff value is tracked.
  119. const float encoderStepRate = encoderMovementSteps / float(ms - EncoderRate.lastEncoderTime) * 1000;
  120. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  121. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  122. else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoderMultiplier = 5;
  123. }
  124. EncoderRate.lastEncoderTime = ms;
  125. }
  126. #else
  127. constexpr int32_t encoderMultiplier = 1;
  128. #endif // ENCODER_RATE_MULTIPLIER
  129. // EncoderRate.encoderMoveValue += (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  130. EncoderRate.encoderMoveValue = (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  131. if (EncoderRate.encoderMoveValue < 0) EncoderRate.encoderMoveValue = -EncoderRate.encoderMoveValue;
  132. temp_diff = 0;
  133. }
  134. return temp_diffState;
  135. }
  136. #if PIN_EXISTS(LCD_LED)
  137. /*取低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*/
  138. unsigned int LED_DataArray[LED_NUM];
  139. /*LED灯操作*/
  140. void LED_Action(void) {
  141. LED_Control(RGB_SCALE_WARM_WHITE,0x0F);
  142. delay(30);
  143. LED_Control(RGB_SCALE_WARM_WHITE,0x00);
  144. }
  145. /*LED初始化*/
  146. void LED_Configuration(void) {
  147. SET_OUTPUT(LCD_LED_PIN);
  148. }
  149. /*LED写数据*/
  150. void LED_WriteData(void) {
  151. unsigned char tempCounter_LED, tempCounter_Bit;
  152. for (tempCounter_LED = 0; tempCounter_LED < LED_NUM; tempCounter_LED++) {
  153. for (tempCounter_Bit = 0; tempCounter_Bit < 24; tempCounter_Bit++) {
  154. if (LED_DataArray[tempCounter_LED] & (0x800000 >> tempCounter_Bit)) {
  155. LED_DATA_HIGH;
  156. DELAY_NS(300);
  157. LED_DATA_LOW;
  158. DELAY_NS(200);
  159. }
  160. else {
  161. LED_DATA_HIGH;
  162. LED_DATA_LOW;
  163. DELAY_NS(200);
  164. }
  165. }
  166. }
  167. }
  168. /*LED控制 RGB_Scale:RGB色彩配比 luminance:亮度(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渐变控制 RGB_Scale:RGB色彩配比 luminance:亮度(0~0xFF) change_Time:渐变时间(ms)*/
  182. void LED_GraduallyControl(unsigned char RGB_Scale, unsigned char luminance, unsigned int change_Interval) {
  183. unsigned char temp_Counter;
  184. unsigned char LED_R_Data[LED_NUM], LED_G_Data[LED_NUM], LED_B_Data[LED_NUM];
  185. bool LED_R_Flag = 0, LED_G_Flag = 0, LED_B_Flag = 0;
  186. for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) {
  187. switch(RGB_Scale) {
  188. case RGB_SCALE_R10_G7_B5: {
  189. LED_R_Data[temp_Counter] = luminance*10/10;
  190. LED_G_Data[temp_Counter] = luminance*7/10;
  191. LED_B_Data[temp_Counter] = luminance*5/10;
  192. }break;
  193. case RGB_SCALE_R10_G7_B4: {
  194. LED_R_Data[temp_Counter] = luminance*10/10;
  195. LED_G_Data[temp_Counter] = luminance*7/10;
  196. LED_B_Data[temp_Counter] = luminance*4/10;
  197. }break;
  198. case RGB_SCALE_R10_G8_B7: {
  199. LED_R_Data[temp_Counter] = luminance*10/10;
  200. LED_G_Data[temp_Counter] = luminance*8/10;
  201. LED_B_Data[temp_Counter] = luminance*7/10;
  202. }break;
  203. }
  204. }
  205. for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) {
  206. if ((unsigned char)(LED_DataArray[temp_Counter] >> 8) > LED_R_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x000100;
  207. else if ((unsigned char)(LED_DataArray[temp_Counter] >> 8) < LED_R_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x000100;
  208. while (1) {
  209. else LED_R_Flag = 1;
  210. if ((unsigned char)(LED_DataArray[temp_Counter]>>16) > LED_G_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x010000;
  211. else if ((unsigned char)(LED_DataArray[temp_Counter]>>16) < LED_G_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x010000;
  212. else LED_G_Flag = 1;
  213. if ((unsigned char)LED_DataArray[temp_Counter] > LED_B_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x000001;
  214. else if ((unsigned char)LED_DataArray[temp_Counter] < LED_B_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x000001;
  215. else LED_B_Flag = 1;
  216. }
  217. LED_WriteData();
  218. if (LED_R_Flag && LED_G_Flag && LED_B_Flag) break;
  219. else delay(change_Interval);
  220. }
  221. }
  222. #endif
  223. #endif // DWIN_CREALITY_LCD