Browse Source

Apply standard pin test to buttons

This is the easiest way to make button pin testing consistent without
renaming all the button pins. Just make a macro especially for testing
if button pins are set, since they are named consistently in the pins
files.
Scott Lahteine 8 years ago
parent
commit
f543aaa54e
5 changed files with 48 additions and 46 deletions
  1. 0
    2
      Marlin/Conditionals.h
  2. 2
    0
      Marlin/macros.h
  3. 30
    37
      Marlin/ultralcd.cpp
  4. 5
    0
      Marlin/ultralcd.h
  5. 11
    7
      Marlin/ultralcd_implementation_hitachi_HD44780.h

+ 0
- 2
Marlin/Conditionals.h View File

@@ -33,8 +33,6 @@
33 33
 
34 34
 #ifndef CONFIGURATION_LCD // Get the LCD defines which are needed first
35 35
 
36
-  #define PIN_EXISTS(PN) (defined(PN##_PIN) && PN##_PIN >= 0)
37
-
38 36
   #define CONFIGURATION_LCD
39 37
 
40 38
   #if ENABLED(MAKRPANEL)

+ 2
- 0
Marlin/macros.h View File

@@ -55,4 +55,6 @@
55 55
 #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-')
56 56
 #define COUNT(a) (sizeof(a)/sizeof(*a))
57 57
 
58
+#define PIN_EXISTS(PN) (defined(PN ##_PIN) && PN ##_PIN >= 0)
59
+
58 60
 #endif //__MACROS_H

+ 30
- 37
Marlin/ultralcd.cpp View File

@@ -1723,17 +1723,17 @@ void lcd_init() {
1723 1723
   lcd_implementation_init();
1724 1724
 
1725 1725
   #if ENABLED(NEWPANEL)
1726
-    #if BTN_EN1 > 0
1726
+    #if BUTTON_EXISTS(EN1)
1727 1727
       SET_INPUT(BTN_EN1);
1728 1728
       WRITE(BTN_EN1, HIGH);
1729 1729
     #endif
1730 1730
 
1731
-    #if BTN_EN2 > 0
1731
+    #if BUTTON_EXISTS(EN2)
1732 1732
       SET_INPUT(BTN_EN2);
1733 1733
       WRITE(BTN_EN2, HIGH);
1734 1734
     #endif
1735 1735
 
1736
-    #if BTN_ENC > 0
1736
+    #if BUTTON_EXISTS(ENC)
1737 1737
       SET_INPUT(BTN_ENC);
1738 1738
       WRITE(BTN_ENC, HIGH);
1739 1739
     #endif
@@ -2055,6 +2055,19 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; }
2055 2055
     #define encrot3 1
2056 2056
   #endif
2057 2057
 
2058
+  #define GET_BUTTON_STATES(DST) \
2059
+    uint8_t new_##DST = 0; \
2060
+    WRITE(SHIFT_LD, LOW); \
2061
+    WRITE(SHIFT_LD, HIGH); \
2062
+    for (int8_t i = 0; i < 8; i++) { \
2063
+      new_##DST >>= 1; \
2064
+      if (READ(SHIFT_OUT)) SBI(new_##DST, 7); \
2065
+      WRITE(SHIFT_CLK, HIGH); \
2066
+      WRITE(SHIFT_CLK, LOW); \
2067
+    } \
2068
+    DST = ~new_##DST; //invert it, because a pressed switch produces a logical 0
2069
+
2070
+
2058 2071
   /**
2059 2072
    * Read encoder buttons from the hardware registers
2060 2073
    * Warning: This function is called from interrupt context!
@@ -2062,67 +2075,47 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; }
2062 2075
   void lcd_buttons_update() {
2063 2076
     #if ENABLED(NEWPANEL)
2064 2077
       uint8_t newbutton = 0;
2065
-      #if BTN_EN1 > 0
2066
-        if (READ(BTN_EN1) == 0) newbutton |= EN_A;
2078
+      #if BUTTON_EXISTS(EN1)
2079
+        if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
2067 2080
       #endif
2068
-      #if BTN_EN2 > 0
2069
-        if (READ(BTN_EN2) == 0) newbutton |= EN_B;
2081
+      #if BUTTON_EXISTS(EN2)
2082
+        if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
2070 2083
       #endif
2071
-      #if ENABLED(RIGIDBOT_PANEL) || BTN_ENC > 0
2084
+      #if ENABLED(RIGIDBOT_PANEL) || BUTTON_EXISTS(ENC)
2072 2085
         millis_t now = millis();
2073 2086
       #endif
2074 2087
       #if ENABLED(RIGIDBOT_PANEL)
2075 2088
         if (now > next_button_update_ms) {
2076
-          if (READ(BTN_UP) == 0) {
2089
+          if (BUTTON_PRESSED(UP)) {
2077 2090
             encoderDiff = -1 * (ENCODER_STEPS_PER_MENU_ITEM);
2078 2091
             next_button_update_ms = now + 300;
2079 2092
           }
2080
-          else if (READ(BTN_DWN) == 0) {
2093
+          else if (BUTTON_PRESSED(DWN)) {
2081 2094
             encoderDiff = ENCODER_STEPS_PER_MENU_ITEM;
2082 2095
             next_button_update_ms = now + 300;
2083 2096
           }
2084
-          else if (READ(BTN_LFT) == 0) {
2097
+          else if (BUTTON_PRESSED(LFT)) {
2085 2098
             encoderDiff = -1 * (ENCODER_PULSES_PER_STEP);
2086 2099
             next_button_update_ms = now + 300;
2087 2100
           }
2088
-          else if (READ(BTN_RT) == 0) {
2101
+          else if (BUTTON_PRESSED(RT)) {
2089 2102
             encoderDiff = ENCODER_PULSES_PER_STEP;
2090 2103
             next_button_update_ms = now + 300;
2091 2104
           }
2092 2105
         }
2093 2106
       #endif
2094
-      #if BTN_ENC > 0
2095
-        if (now > next_button_update_ms && READ(BTN_ENC) == 0) newbutton |= EN_C;
2107
+      #if BUTTON_EXISTS(ENC)
2108
+        if (now > next_button_update_ms && BUTTON_PRESSED(ENC)) newbutton |= EN_C;
2096 2109
       #endif
2097 2110
       buttons = newbutton;
2098 2111
       #if ENABLED(LCD_HAS_SLOW_BUTTONS)
2099 2112
         buttons |= slow_buttons;
2100 2113
       #endif
2101 2114
       #if ENABLED(REPRAPWORLD_KEYPAD)
2102
-        // for the reprapworld_keypad
2103
-        uint8_t newbutton_reprapworld_keypad = 0;
2104
-        WRITE(SHIFT_LD, LOW);
2105
-        WRITE(SHIFT_LD, HIGH);
2106
-        for (int8_t i = 0; i < 8; i++) {
2107
-          newbutton_reprapworld_keypad >>= 1;
2108
-          if (READ(SHIFT_OUT)) SBI(newbutton_reprapworld_keypad, 7);
2109
-          WRITE(SHIFT_CLK, HIGH);
2110
-          WRITE(SHIFT_CLK, LOW);
2111
-        }
2112
-        buttons_reprapworld_keypad = ~newbutton_reprapworld_keypad; //invert it, because a pressed switch produces a logical 0
2115
+        GET_BUTTON_STATES(buttons_reprapworld_keypad);
2113 2116
       #endif
2114
-    #else   //read it from the shift register
2115
-      uint8_t newbutton = 0;
2116
-      WRITE(SHIFT_LD, LOW);
2117
-      WRITE(SHIFT_LD, HIGH);
2118
-      unsigned char tmp_buttons = 0;
2119
-      for (int8_t i = 0; i < 8; i++) {
2120
-        newbutton >>= 1;
2121
-        if (READ(SHIFT_OUT)) SBI(newbutton, 7);
2122
-        WRITE(SHIFT_CLK, HIGH);
2123
-        WRITE(SHIFT_CLK, LOW);
2124
-      }
2125
-      buttons = ~newbutton; //invert it, because a pressed switch produces a logical 0
2117
+    #else
2118
+      GET_BUTTON_STATES(buttons);
2126 2119
     #endif //!NEWPANEL
2127 2120
 
2128 2121
     #if ENABLED(REVERSE_MENU_DIRECTION)

+ 5
- 0
Marlin/ultralcd.h View File

@@ -24,9 +24,14 @@
24 24
 #define ULTRALCD_H
25 25
 
26 26
 #include "Marlin.h"
27
+
27 28
 #if ENABLED(ULTRA_LCD)
29
+
28 30
   #include "buzzer.h"
29 31
 
32
+  #define BUTTON_EXISTS(BN) (defined(BTN_## BN) && BTN_## BN >= 0)
33
+  #define BUTTON_PRESSED(BN) !READ(BTN_## BN)
34
+
30 35
   int lcd_strlen(const char* s);
31 36
   int lcd_strlen_P(const char* s);
32 37
   void lcd_update();

+ 11
- 7
Marlin/ultralcd_implementation_hitachi_HD44780.h View File

@@ -44,7 +44,7 @@ extern volatile uint8_t buttons;  //an extended version of the last checked butt
44 44
   #define EN_B (_BV(BLEN_B)) // The two encoder pins are connected through BTN_EN1 and BTN_EN2
45 45
   #define EN_A (_BV(BLEN_A))
46 46
 
47
-  #if defined(BTN_ENC) && BTN_ENC > -1
47
+  #if BUTTON_EXISTS(ENC)
48 48
     // encoder click is directly connected
49 49
     #define BLEN_C 2
50 50
     #define EN_C (_BV(BLEN_C))
@@ -63,7 +63,7 @@ extern volatile uint8_t buttons;  //an extended version of the last checked butt
63 63
     #define B_DW (BUTTON_DOWN<<B_I2C_BTN_OFFSET)
64 64
     #define B_RI (BUTTON_RIGHT<<B_I2C_BTN_OFFSET)
65 65
 
66
-    #if defined(BTN_ENC) && BTN_ENC > -1
66
+    #if BUTTON_EXISTS(ENC)
67 67
       // the pause/stop/restart button is connected to BTN_ENC when used
68 68
       #define B_ST (EN_C)                            // Map the pause/stop/resume button into its normalized functional name
69 69
       #undef LCD_CLICKED
@@ -77,8 +77,14 @@ extern volatile uint8_t buttons;  //an extended version of the last checked butt
77 77
     #define LCD_HAS_SLOW_BUTTONS
78 78
 
79 79
   #elif ENABLED(LCD_I2C_PANELOLU2)
80
-    // encoder click can be read through I2C if not directly connected
81
-    #if BTN_ENC <= 0
80
+
81
+    #if BUTTON_EXISTS(ENC)
82
+
83
+      #undef LCD_CLICKED
84
+      #define LCD_CLICKED (buttons&EN_C)
85
+
86
+    #else // Read through I2C if not directly connected to a pin
87
+
82 88
       #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
83 89
 
84 90
       #define B_MI (PANELOLU2_ENCODER_C<<B_I2C_BTN_OFFSET) // requires LiquidTWI2 library v1.2.3 or later
@@ -88,9 +94,7 @@ extern volatile uint8_t buttons;  //an extended version of the last checked butt
88 94
 
89 95
       // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
90 96
       #define LCD_HAS_SLOW_BUTTONS
91
-    #else
92
-      #undef LCD_CLICKED
93
-      #define LCD_CLICKED (buttons&EN_C)
97
+
94 98
     #endif
95 99
 
96 100
   #elif ENABLED(REPRAPWORLD_KEYPAD)

Loading…
Cancel
Save