소스 검색

Simplify encoder handling (#18754)

Mark Langezaal 3 년 전
부모
커밋
f23393c1ec
No account linked to committer's email address
1개의 변경된 파일24개의 추가작업 그리고 27개의 파일을 삭제
  1. 24
    27
      Marlin/src/lcd/ultralcd.cpp

+ 24
- 27
Marlin/src/lcd/ultralcd.cpp 파일 보기

@@ -49,6 +49,8 @@ MarlinUI ui;
49 49
   bool MarlinUI::wait_for_move; // = false
50 50
 #endif
51 51
 
52
+constexpr uint8_t epps = ENCODER_PULSES_PER_STEP;
53
+
52 54
 #if HAS_SPI_LCD
53 55
   #if ENABLED(STATUS_MESSAGE_SCROLLING)
54 56
     uint8_t MarlinUI::status_scroll_offset; // = 0
@@ -440,13 +442,13 @@ bool MarlinUI::get_blink() {
440 442
           #endif
441 443
           {
442 444
             #if HAS_LCD_MENU
443
-                   if (RRK(EN_KEYPAD_UP))     encoderPosition -= ENCODER_PULSES_PER_STEP;
444
-              else if (RRK(EN_KEYPAD_DOWN))   encoderPosition += ENCODER_PULSES_PER_STEP;
445
+                   if (RRK(EN_KEYPAD_UP))     encoderPosition -= epps;
446
+              else if (RRK(EN_KEYPAD_DOWN))   encoderPosition += epps;
445 447
               else if (RRK(EN_KEYPAD_LEFT))   { MenuItem_back::action(); quick_feedback(); }
446 448
               else if (RRK(EN_KEYPAD_RIGHT))  encoderPosition = 0;
447 449
             #else
448
-                   if (RRK(EN_KEYPAD_UP)   || RRK(EN_KEYPAD_LEFT))  encoderPosition -= ENCODER_PULSES_PER_STEP;
449
-              else if (RRK(EN_KEYPAD_DOWN) || RRK(EN_KEYPAD_RIGHT)) encoderPosition += ENCODER_PULSES_PER_STEP;
450
+                   if (RRK(EN_KEYPAD_UP)   || RRK(EN_KEYPAD_LEFT))  encoderPosition -= epps;
451
+              else if (RRK(EN_KEYPAD_DOWN) || RRK(EN_KEYPAD_RIGHT)) encoderPosition += epps;
450 452
             #endif
451 453
           }
452 454
         #endif
@@ -841,7 +843,7 @@ void MarlinUI::update() {
841 843
         RESET_STATUS_TIMEOUT();
842 844
         if (touch_buttons & (EN_A | EN_B)) {              // Menu arrows, in priority
843 845
           if (ELAPSED(ms, next_button_update_ms)) {
844
-            encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * (ENCODER_PULSES_PER_STEP) * encoderDirection;
846
+            encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * epps * encoderDirection;
845 847
             if (touch_buttons & EN_A) encoderDiff *= -1;
846 848
             TERN_(AUTO_BED_LEVELING_UBL, external_encoder());
847 849
             next_button_update_ms = ms + repeat_delay;    // Assume the repeat delay
@@ -901,26 +903,21 @@ void MarlinUI::update() {
901 903
       #if ENCODER_PULSES_PER_STEP > 1
902 904
         // When reversing the encoder direction, a movement step can be missed because
903 905
         // encoderDiff has a non-zero residual value, making the controller unresponsive.
904
-        // The fix clears the residual value when the encoder is reversed.
906
+        // The fix clears the residual value when the encoder is idle.
905 907
         // Also check if past half the threshold to compensate for missed single steps.
906 908
         static int8_t lastEncoderDiff;
907
-        int8_t prevDiff = lastEncoderDiff;
908
-        lastEncoderDiff = encoderDiff;  // Store before updating encoderDiff to save actual steps
909
-
910
-        // When not past threshold, and reversing... or past half the threshold
911
-        if (WITHIN(abs_diff, 1, (ENCODER_PULSES_PER_STEP) - 1)  // Not past threshold
912
-          && (abs_diff > (ENCODER_PULSES_PER_STEP) / 2          // Passed half the threshold? Done! Call it a full step.
913
-            || (ABS(encoderDiff - prevDiff) >= (ENCODER_PULSES_PER_STEP)  // A big change when abs_diff is small implies reverse
914
-                && ABS(prevDiff) < (ENCODER_PULSES_PER_STEP)    // ...especially when starting from a partial or no step.
915
-               )
916
-             )
917
-        ) {
918
-          abs_diff = ENCODER_PULSES_PER_STEP;
919
-          encoderDiff = (encoderDiff < 0 ? -1 : 1) * abs_diff;  // Treat as full step
909
+
910
+        // Timeout? No decoder change since last check. 10 or 20 times per second.
911
+        if (encoderDiff == lastEncoderDiff && abs_diff <= epps / 2)   // Same direction & size but not over a half-step?
912
+          encoderDiff = 0;                                            // Clear residual pulses.
913
+        else if (WITHIN(abs_diff, epps / 2 + 1, epps - 1)) {          // Past half of threshold?
914
+          abs_diff = epps;                                            // Treat as a full step size
915
+          encoderDiff = (encoderDiff < 0 ? -1 : 1) * abs_diff;        // ...in the spin direction.
920 916
         }
917
+        lastEncoderDiff = encoderDiff;
921 918
       #endif
922 919
 
923
-      const bool encoderPastThreshold = (abs_diff >= (ENCODER_PULSES_PER_STEP));
920
+      const bool encoderPastThreshold = (abs_diff >= epps);
924 921
       if (encoderPastThreshold || lcd_clicked) {
925 922
         if (encoderPastThreshold) {
926 923
 
@@ -929,7 +926,7 @@ void MarlinUI::update() {
929 926
             int32_t encoderMultiplier = 1;
930 927
 
931 928
             if (encoderRateMultiplierEnabled) {
932
-              const float encoderMovementSteps = float(abs_diff) / (ENCODER_PULSES_PER_STEP);
929
+              const float encoderMovementSteps = float(abs_diff) / epps;
933 930
 
934 931
               if (lastEncoderMovementMillis) {
935 932
                 // Note that the rate is always calculated between two passes through the
@@ -958,7 +955,7 @@ void MarlinUI::update() {
958 955
 
959 956
           #endif // ENCODER_RATE_MULTIPLIER
960 957
 
961
-          encoderPosition += (encoderDiff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
958
+          encoderPosition += (encoderDiff * encoderMultiplier) / epps;
962 959
           encoderDiff = 0;
963 960
         }
964 961
 
@@ -1191,7 +1188,7 @@ void MarlinUI::update() {
1191 1188
         //
1192 1189
         #if ANY_BUTTON(UP, DWN, LFT, RT)
1193 1190
 
1194
-          const int8_t pulses = (ENCODER_PULSES_PER_STEP) * encoderDirection;
1191
+          const int8_t pulses = epps * encoderDirection;
1195 1192
 
1196 1193
           if (false) {
1197 1194
             // for the else-ifs below
@@ -1547,17 +1544,17 @@ void MarlinUI::update() {
1547 1544
       const int8_t xdir = col < (LCD_WIDTH ) / 2 ? -1 : 1,
1548 1545
                    ydir = row < (LCD_HEIGHT) / 2 ? -1 : 1;
1549 1546
       if (on_edit_screen)
1550
-        encoderDiff = (ENCODER_PULSES_PER_STEP) * ydir;
1547
+        encoderDiff = epps * ydir;
1551 1548
       else if (screen_items > 0) {
1552 1549
         // Last 3 cols act as a scroll :-)
1553 1550
         if (col > (LCD_WIDTH) - 5)
1554 1551
           // 2 * LCD_HEIGHT to scroll to bottom of next page. (LCD_HEIGHT would only go 1 item down.)
1555
-          encoderDiff = (ENCODER_PULSES_PER_STEP) * (encoderLine - encoderTopLine + 2 * (LCD_HEIGHT)) * ydir;
1552
+          encoderDiff = epps * (encoderLine - encoderTopLine + 2 * (LCD_HEIGHT)) * ydir;
1556 1553
         else
1557
-          encoderDiff = (ENCODER_PULSES_PER_STEP) * (row - encoderPosition + encoderTopLine);
1554
+          encoderDiff = epps * (row - encoderPosition + encoderTopLine);
1558 1555
       }
1559 1556
       else if (!on_status_screen())
1560
-        encoderDiff = (ENCODER_PULSES_PER_STEP) * xdir;
1557
+        encoderDiff = epps * xdir;
1561 1558
     }
1562 1559
 
1563 1560
   #endif

Loading…
취소
저장