From 6f9e9c0e4f97ebb0b22fbb4b9957762d1434ac31 Mon Sep 17 00:00:00 2001
From: Ruwan J Egoda Gamage <janapriya@gmail.com>
Date: Sun, 13 Dec 2015 12:40:48 -0500
Subject: [PATCH] Cleaning up, getting rid of the fake encoder count variable.

---
 Marlin/ultralcd.cpp | 40 +++++++++++++++++++---------------------
 Marlin/ultralcd.h   |  3 ---
 2 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp
index 2f835a3c693..c6a951c7fa4 100644
--- a/Marlin/ultralcd.cpp
+++ b/Marlin/ultralcd.cpp
@@ -218,9 +218,6 @@ static void lcd_status_screen();
   #if ENABLED(REPRAPWORLD_KEYPAD)
     volatile uint8_t buttons_reprapworld_keypad; // to store the keypad shift register values
   #endif
-  #if ENABLED(RIGIDBOT_PANEL)
-    volatile millis_t next_fake_encoder_update_ms;
-  #endif
 
   #if ENABLED(LCD_HAS_SLOW_BUTTONS)
     volatile uint8_t slow_buttons; // Bits of the pressed buttons.
@@ -1555,7 +1552,6 @@ void lcd_init() {
       pinMode(BTN_DWN,INPUT);
       pinMode(BTN_LFT,INPUT);
       pinMode(BTN_RT,INPUT);
-      next_fake_encoder_update_ms = 0;
     #endif
 
   #else  // Not NEWPANEL
@@ -1685,7 +1681,7 @@ void lcd_update() {
               int32_t encoderMovementSteps = abs(encoderDiff) / ENCODER_PULSES_PER_STEP;
 
               if (lastEncoderMovementMillis != 0) {
-                // Note that the rate is always calculated between to passes through the 
+                // Note that the rate is always calculated between to passes through the
                 // loop and that the abs of the encoderDiff value is tracked.
                 float encoderStepRate = (float)(encoderMovementSteps) / ((float)(ms - lastEncoderMovementMillis)) * 1000.0;
 
@@ -1865,25 +1861,27 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; }
         if (READ(BTN_EN2) == 0) newbutton |= EN_B;
       #endif
       #if ENABLED(RIGIDBOT_PANEL)
-        if (millis() > next_fake_encoder_update_ms && READ(BTN_UP) == 0) {
-          encoderDiff = -1 * ENCODER_STEPS_PER_MENU_ITEM;
-          next_fake_encoder_update_ms = millis() + 300;
-        }
-        if (millis() > next_fake_encoder_update_ms && READ(BTN_DWN) == 0) {
-          encoderDiff = ENCODER_STEPS_PER_MENU_ITEM;
-          next_fake_encoder_update_ms = millis() + 300;
-        }
-        if (millis() > next_fake_encoder_update_ms && READ(BTN_LFT) == 0) {
-          encoderDiff = -1 * ENCODER_PULSES_PER_STEP;
-          next_fake_encoder_update_ms = millis() + 300;
-        }
-        if (millis() > next_fake_encoder_update_ms && READ(BTN_RT) == 0) {
-          encoderDiff = ENCODER_PULSES_PER_STEP;
-          next_fake_encoder_update_ms = millis() + 300;
+        millis_t now = millis();
+        if (now > next_button_update_ms) {
+          if (READ(BTN_UP) == 0) {
+            encoderDiff = -1 * ENCODER_STEPS_PER_MENU_ITEM;
+            next_button_update_ms = now + 300;
+          }
+          else if (READ(BTN_DWN) == 0) {
+            encoderDiff = ENCODER_STEPS_PER_MENU_ITEM;
+            next_button_update_ms = now + 300;
+          }
+          else if (READ(BTN_LFT) == 0) {
+            encoderDiff = -1 * ENCODER_PULSES_PER_STEP;
+            next_button_update_ms = now + 300;
+          }
+          else if (READ(BTN_RT) == 0) {
+            encoderDiff = ENCODER_PULSES_PER_STEP;
+            next_button_update_ms = now + 300;
+          }
         }
       #endif
 
-
       #if BTN_ENC > 0
         if (millis() > next_button_update_ms && READ(BTN_ENC) == 0) newbutton |= EN_C;
       #endif
diff --git a/Marlin/ultralcd.h b/Marlin/ultralcd.h
index b73b26f163c..23df71cc442 100644
--- a/Marlin/ultralcd.h
+++ b/Marlin/ultralcd.h
@@ -41,9 +41,6 @@
     #if ENABLED(REPRAPWORLD_KEYPAD)
       extern volatile uint8_t buttons_reprapworld_keypad; // to store the keypad shift register values
     #endif
-    #if ENABLED(RIGIDBOT_PANEL)
-      extern volatile millis_t next_fake_encoder_update_ms;
-    #endif
   #else
     FORCE_INLINE void lcd_buttons_update() {}
   #endif