From 7bad72359c5f2c78096810fffbf809bb8c30b58b Mon Sep 17 00:00:00 2001
From: Robert F-C <rfairliecuninghame@yahoo.com>
Date: Sun, 3 Mar 2013 18:47:50 +1100
Subject: [PATCH] Support encoder click on PANELOLU2 being read through I2C

This change also handles the case where the pause/stop/restart button on
the VIKI is not used. Make LCD I2C buzz sound the same as the normal
buzz (300ms is too long for quick feedback).
---
 Marlin/Configuration.h                        | 14 ++++---
 Marlin/ultralcd.cpp                           |  6 ++-
 .../ultralcd_implementation_hitachi_HD44780.h | 42 ++++++++++++++-----
 3 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h
index 492a32361bd..ba20c6c04b6 100644
--- a/Marlin/Configuration.h
+++ b/Marlin/Configuration.h
@@ -351,8 +351,10 @@ const bool Z_ENDSTOPS_INVERTING = true; // set to true to invert the logic of th
 //PANELOLU2 LCD with status LEDs, separate encoder and click inputs
 //#define LCD_I2C_PANELOLU2
 #ifdef LCD_I2C_PANELOLU2
-  // This uses the LiquidTWI2 library ( https://github.com/lincomatic/LiquidTWI2 ).
-  // Make sure it is placed in the Arduino libraries directory.
+  // This uses the LiquidTWI2 library ( https://github.com/lincomatic/LiquidTWI2 )
+  // Make sure it is placed in the Arduino or Sketchbook libraries directory.
+  // Note: The PANELOLU2 encoder click input can either be directly connected to a pin 
+  //       (if BTN_ENC defined to != -1) or read through I2C (when BTN_ENC == -1). 
   #define LCD_I2C_TYPE_MCP23017
   #define LCD_I2C_ADDRESS 0x20 // I2C Address of the port expander
   #define LCD_USE_I2C_BUZZER //comment out to disable buzzer on LCD
@@ -360,11 +362,13 @@ const bool Z_ENDSTOPS_INVERTING = true; // set to true to invert the logic of th
   #define ULTIPANEL 
 #endif
 
-//VIKI LCD with status LEDs, integrated click & L/R/U/P buttons, separate encoder inputs
+//Panucatt VIKI LCD with status LEDs, integrated click & L/R/U/P buttons, separate encoder inputs
 //#define LCD_I2C_VIKI
 #ifdef LCD_I2C_VIKI
-  // This uses the LiquidTWI2 library (https://github.com/lincomatic/LiquidTWI2).
-  // Make sure it is placed in the Arduino libraries directory.
+  // This uses the LiquidTWI2 library ( https://github.com/lincomatic/LiquidTWI2 )
+  // Make sure it is placed in the Arduino or Sketchbook libraries directory.
+  // Note: The pause/stop/resume LCD button pin should be connected to the Arduino
+  //       BTN_ENC pin (or set BTN_ENC to -1 if not used)
   #define LCD_I2C_TYPE_MCP23017 
   #define LCD_I2C_ADDRESS 0x20 // I2C Address of the port expander
   #define NEWPANEL
diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp
index ed259196d7e..523fcd46174 100644
--- a/Marlin/ultralcd.cpp
+++ b/Marlin/ultralcd.cpp
@@ -686,11 +686,13 @@ void lcd_init()
 #ifdef NEWPANEL
     pinMode(BTN_EN1,INPUT);
     pinMode(BTN_EN2,INPUT); 
-    pinMode(BTN_ENC,INPUT); 
     pinMode(SDCARDDETECT,INPUT);
     WRITE(BTN_EN1,HIGH);
     WRITE(BTN_EN2,HIGH);
+  #if defined(BTN_ENC) && BTN_ENC > -1
+    pinMode(BTN_ENC,INPUT); 
     WRITE(BTN_ENC,HIGH);
+  #endif    
 #else
     pinMode(SHIFT_CLK,OUTPUT);
     pinMode(SHIFT_LD,OUTPUT);
@@ -809,8 +811,10 @@ void lcd_buttons_update()
     uint8_t newbutton=0;
     if(READ(BTN_EN1)==0)  newbutton|=EN_A;
     if(READ(BTN_EN2)==0)  newbutton|=EN_B;
+  #if defined(BTN_ENC) && BTN_ENC > -1
     if((blocking_enc<millis()) && (READ(BTN_ENC)==0))
         newbutton |= EN_C;
+  #endif      
     buttons = newbutton;
 #else   //read it from the shift register
     uint8_t newbutton=0;
diff --git a/Marlin/ultralcd_implementation_hitachi_HD44780.h b/Marlin/ultralcd_implementation_hitachi_HD44780.h
index a606f642dd1..c5d1e26c27d 100644
--- a/Marlin/ultralcd_implementation_hitachi_HD44780.h
+++ b/Marlin/ultralcd_implementation_hitachi_HD44780.h
@@ -23,32 +23,52 @@ extern volatile uint8_t buttons;  //the last checked buttons in a bit array.
 #define EN_B (1<<BLEN_B) // The two encoder pins are connected through BTN_EN1 and BTN_EN2
 #define EN_A (1<<BLEN_A)
 
+#if defined(BTN_ENC) && BTN_ENC > -1
+  // encoder click is directly connected
+  #define BLEN_C 2 
+  #define EN_C (1<<BLEN_C) 
+#endif 
+  
 //
 // Setup other button mappings of each panel
 //
 #if defined(LCD_I2C_VIKI)
-  #define BLEN_C 2 // == pause/stop/restart button connected to BTN_ENC pin (named for consistency with NEWPANEL code)
-  #define EN_C (1<<BLEN_C) 
-
-  #define B_I2C_BTN_OFFSET (BLEN_C+1) // (the first three bit positions reserved for EN_A, EN_B, EN_C)
+  #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
   
   // button and encoder bit positions within 'buttons'
-  #define B_ST (EN_C)                             // Map the pause/stop/resume button into its normalized functional name 
   #define B_LE (BUTTON_LEFT<<B_I2C_BTN_OFFSET)    // The remaining normalized buttons are all read via I2C
   #define B_UP (BUTTON_UP<<B_I2C_BTN_OFFSET)
   #define B_MI (BUTTON_SELECT<<B_I2C_BTN_OFFSET)
   #define B_DW (BUTTON_DOWN<<B_I2C_BTN_OFFSET)
   #define B_RI (BUTTON_RIGHT<<B_I2C_BTN_OFFSET)
 
-  #define LCD_CLICKED (buttons&(B_MI|B_RI|B_ST)) // pause/stop button also acts as click until we implement proper pause/stop.
+  #if defined(BTN_ENC) && BTN_ENC > -1 
+    // the pause/stop/restart button is connected to BTN_ENC when used
+    #define B_ST (EN_C)                            // Map the pause/stop/resume button into its normalized functional name 
+    #define LCD_CLICKED (buttons&(B_MI|B_RI|B_ST)) // pause/stop button also acts as click until we implement proper pause/stop.
+  #else
+    #define LCD_CLICKED (buttons&(B_MI|B_RI))
+  #endif  
 
   // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
   #define LCD_HAS_SLOW_BUTTONS
 
+#elif defined(LCD_I2C_PANELOLU2)
+  #if !defined(BTN_ENC) || BTN_ENC == -1 
+    // encoder click is connected through I2C (rather than directly connected)
+    #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
+  
+    #define B_MI (ENCODER_C<<B_I2C_BTN_OFFSET)
+
+    #define LCD_CLICKED (buttons&B_MI)
+
+    // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
+    #define LCD_HAS_SLOW_BUTTONS
+  #else
+    #define LCD_CLICKED (buttons&EN_C)  
+  #endif
+
 #elif defined(NEWPANEL)
-  // Standard Newpanel has just a single button (all direclty connected)
-  #define BLEN_C 2 // == select button
-  #define EN_C (1<<BLEN_C)
   #define LCD_CLICKED (buttons&EN_C)
   
 #else // old style ULTIPANEL
@@ -88,7 +108,7 @@ extern volatile uint8_t buttons;  //the last checked buttons in a bit array.
     #define encrot2 3
     #define encrot3 2
   #endif
-#endif
+#endif 
 
 #endif //ULTIPANEL
 
@@ -629,7 +649,7 @@ static void lcd_implementation_drawmenu_sddirectory(uint8_t row, const char* pst
 static void lcd_implementation_quick_feedback()
 {
 #ifdef LCD_USE_I2C_BUZZER
-    lcd.buzz(300,4000);
+    lcd.buzz(60,1000/6);
 #elif defined(BEEPER) && BEEPER > -1
     SET_OUTPUT(BEEPER);
     for(int8_t i=0;i<10;i++)