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 9 years ago
parent
commit
f543aaa54e

+ 0
- 2
Marlin/Conditionals.h View File

33
 
33
 
34
 #ifndef CONFIGURATION_LCD // Get the LCD defines which are needed first
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
   #define CONFIGURATION_LCD
36
   #define CONFIGURATION_LCD
39
 
37
 
40
   #if ENABLED(MAKRPANEL)
38
   #if ENABLED(MAKRPANEL)

+ 2
- 0
Marlin/macros.h View File

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

+ 30
- 37
Marlin/ultralcd.cpp View File

1723
   lcd_implementation_init();
1723
   lcd_implementation_init();
1724
 
1724
 
1725
   #if ENABLED(NEWPANEL)
1725
   #if ENABLED(NEWPANEL)
1726
-    #if BTN_EN1 > 0
1726
+    #if BUTTON_EXISTS(EN1)
1727
       SET_INPUT(BTN_EN1);
1727
       SET_INPUT(BTN_EN1);
1728
       WRITE(BTN_EN1, HIGH);
1728
       WRITE(BTN_EN1, HIGH);
1729
     #endif
1729
     #endif
1730
 
1730
 
1731
-    #if BTN_EN2 > 0
1731
+    #if BUTTON_EXISTS(EN2)
1732
       SET_INPUT(BTN_EN2);
1732
       SET_INPUT(BTN_EN2);
1733
       WRITE(BTN_EN2, HIGH);
1733
       WRITE(BTN_EN2, HIGH);
1734
     #endif
1734
     #endif
1735
 
1735
 
1736
-    #if BTN_ENC > 0
1736
+    #if BUTTON_EXISTS(ENC)
1737
       SET_INPUT(BTN_ENC);
1737
       SET_INPUT(BTN_ENC);
1738
       WRITE(BTN_ENC, HIGH);
1738
       WRITE(BTN_ENC, HIGH);
1739
     #endif
1739
     #endif
2055
     #define encrot3 1
2055
     #define encrot3 1
2056
   #endif
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
    * Read encoder buttons from the hardware registers
2072
    * Read encoder buttons from the hardware registers
2060
    * Warning: This function is called from interrupt context!
2073
    * Warning: This function is called from interrupt context!
2062
   void lcd_buttons_update() {
2075
   void lcd_buttons_update() {
2063
     #if ENABLED(NEWPANEL)
2076
     #if ENABLED(NEWPANEL)
2064
       uint8_t newbutton = 0;
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
       #endif
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
       #endif
2083
       #endif
2071
-      #if ENABLED(RIGIDBOT_PANEL) || BTN_ENC > 0
2084
+      #if ENABLED(RIGIDBOT_PANEL) || BUTTON_EXISTS(ENC)
2072
         millis_t now = millis();
2085
         millis_t now = millis();
2073
       #endif
2086
       #endif
2074
       #if ENABLED(RIGIDBOT_PANEL)
2087
       #if ENABLED(RIGIDBOT_PANEL)
2075
         if (now > next_button_update_ms) {
2088
         if (now > next_button_update_ms) {
2076
-          if (READ(BTN_UP) == 0) {
2089
+          if (BUTTON_PRESSED(UP)) {
2077
             encoderDiff = -1 * (ENCODER_STEPS_PER_MENU_ITEM);
2090
             encoderDiff = -1 * (ENCODER_STEPS_PER_MENU_ITEM);
2078
             next_button_update_ms = now + 300;
2091
             next_button_update_ms = now + 300;
2079
           }
2092
           }
2080
-          else if (READ(BTN_DWN) == 0) {
2093
+          else if (BUTTON_PRESSED(DWN)) {
2081
             encoderDiff = ENCODER_STEPS_PER_MENU_ITEM;
2094
             encoderDiff = ENCODER_STEPS_PER_MENU_ITEM;
2082
             next_button_update_ms = now + 300;
2095
             next_button_update_ms = now + 300;
2083
           }
2096
           }
2084
-          else if (READ(BTN_LFT) == 0) {
2097
+          else if (BUTTON_PRESSED(LFT)) {
2085
             encoderDiff = -1 * (ENCODER_PULSES_PER_STEP);
2098
             encoderDiff = -1 * (ENCODER_PULSES_PER_STEP);
2086
             next_button_update_ms = now + 300;
2099
             next_button_update_ms = now + 300;
2087
           }
2100
           }
2088
-          else if (READ(BTN_RT) == 0) {
2101
+          else if (BUTTON_PRESSED(RT)) {
2089
             encoderDiff = ENCODER_PULSES_PER_STEP;
2102
             encoderDiff = ENCODER_PULSES_PER_STEP;
2090
             next_button_update_ms = now + 300;
2103
             next_button_update_ms = now + 300;
2091
           }
2104
           }
2092
         }
2105
         }
2093
       #endif
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
       #endif
2109
       #endif
2097
       buttons = newbutton;
2110
       buttons = newbutton;
2098
       #if ENABLED(LCD_HAS_SLOW_BUTTONS)
2111
       #if ENABLED(LCD_HAS_SLOW_BUTTONS)
2099
         buttons |= slow_buttons;
2112
         buttons |= slow_buttons;
2100
       #endif
2113
       #endif
2101
       #if ENABLED(REPRAPWORLD_KEYPAD)
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
       #endif
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
     #endif //!NEWPANEL
2119
     #endif //!NEWPANEL
2127
 
2120
 
2128
     #if ENABLED(REVERSE_MENU_DIRECTION)
2121
     #if ENABLED(REVERSE_MENU_DIRECTION)

+ 5
- 0
Marlin/ultralcd.h View File

24
 #define ULTRALCD_H
24
 #define ULTRALCD_H
25
 
25
 
26
 #include "Marlin.h"
26
 #include "Marlin.h"
27
+
27
 #if ENABLED(ULTRA_LCD)
28
 #if ENABLED(ULTRA_LCD)
29
+
28
   #include "buzzer.h"
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
   int lcd_strlen(const char* s);
35
   int lcd_strlen(const char* s);
31
   int lcd_strlen_P(const char* s);
36
   int lcd_strlen_P(const char* s);
32
   void lcd_update();
37
   void lcd_update();

+ 11
- 7
Marlin/ultralcd_implementation_hitachi_HD44780.h View File

44
   #define EN_B (_BV(BLEN_B)) // The two encoder pins are connected through BTN_EN1 and BTN_EN2
44
   #define EN_B (_BV(BLEN_B)) // The two encoder pins are connected through BTN_EN1 and BTN_EN2
45
   #define EN_A (_BV(BLEN_A))
45
   #define EN_A (_BV(BLEN_A))
46
 
46
 
47
-  #if defined(BTN_ENC) && BTN_ENC > -1
47
+  #if BUTTON_EXISTS(ENC)
48
     // encoder click is directly connected
48
     // encoder click is directly connected
49
     #define BLEN_C 2
49
     #define BLEN_C 2
50
     #define EN_C (_BV(BLEN_C))
50
     #define EN_C (_BV(BLEN_C))
63
     #define B_DW (BUTTON_DOWN<<B_I2C_BTN_OFFSET)
63
     #define B_DW (BUTTON_DOWN<<B_I2C_BTN_OFFSET)
64
     #define B_RI (BUTTON_RIGHT<<B_I2C_BTN_OFFSET)
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
       // the pause/stop/restart button is connected to BTN_ENC when used
67
       // the pause/stop/restart button is connected to BTN_ENC when used
68
       #define B_ST (EN_C)                            // Map the pause/stop/resume button into its normalized functional name
68
       #define B_ST (EN_C)                            // Map the pause/stop/resume button into its normalized functional name
69
       #undef LCD_CLICKED
69
       #undef LCD_CLICKED
77
     #define LCD_HAS_SLOW_BUTTONS
77
     #define LCD_HAS_SLOW_BUTTONS
78
 
78
 
79
   #elif ENABLED(LCD_I2C_PANELOLU2)
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
       #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
88
       #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
83
 
89
 
84
       #define B_MI (PANELOLU2_ENCODER_C<<B_I2C_BTN_OFFSET) // requires LiquidTWI2 library v1.2.3 or later
90
       #define B_MI (PANELOLU2_ENCODER_C<<B_I2C_BTN_OFFSET) // requires LiquidTWI2 library v1.2.3 or later
88
 
94
 
89
       // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
95
       // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
90
       #define LCD_HAS_SLOW_BUTTONS
96
       #define LCD_HAS_SLOW_BUTTONS
91
-    #else
92
-      #undef LCD_CLICKED
93
-      #define LCD_CLICKED (buttons&EN_C)
97
+
94
     #endif
98
     #endif
95
 
99
 
96
   #elif ENABLED(REPRAPWORLD_KEYPAD)
100
   #elif ENABLED(REPRAPWORLD_KEYPAD)

Loading…
Cancel
Save