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.
This commit is contained in:
Scott Lahteine 2016-03-27 04:25:33 -07:00
parent f2ffc8b28b
commit f543aaa54e
5 changed files with 48 additions and 46 deletions

View File

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

View File

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

View File

@ -1723,17 +1723,17 @@ void lcd_init() {
lcd_implementation_init(); lcd_implementation_init();
#if ENABLED(NEWPANEL) #if ENABLED(NEWPANEL)
#if BTN_EN1 > 0 #if BUTTON_EXISTS(EN1)
SET_INPUT(BTN_EN1); SET_INPUT(BTN_EN1);
WRITE(BTN_EN1, HIGH); WRITE(BTN_EN1, HIGH);
#endif #endif
#if BTN_EN2 > 0 #if BUTTON_EXISTS(EN2)
SET_INPUT(BTN_EN2); SET_INPUT(BTN_EN2);
WRITE(BTN_EN2, HIGH); WRITE(BTN_EN2, HIGH);
#endif #endif
#if BTN_ENC > 0 #if BUTTON_EXISTS(ENC)
SET_INPUT(BTN_ENC); SET_INPUT(BTN_ENC);
WRITE(BTN_ENC, HIGH); WRITE(BTN_ENC, HIGH);
#endif #endif
@ -2055,6 +2055,19 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; }
#define encrot3 1 #define encrot3 1
#endif #endif
#define GET_BUTTON_STATES(DST) \
uint8_t new_##DST = 0; \
WRITE(SHIFT_LD, LOW); \
WRITE(SHIFT_LD, HIGH); \
for (int8_t i = 0; i < 8; i++) { \
new_##DST >>= 1; \
if (READ(SHIFT_OUT)) SBI(new_##DST, 7); \
WRITE(SHIFT_CLK, HIGH); \
WRITE(SHIFT_CLK, LOW); \
} \
DST = ~new_##DST; //invert it, because a pressed switch produces a logical 0
/** /**
* Read encoder buttons from the hardware registers * Read encoder buttons from the hardware registers
* Warning: This function is called from interrupt context! * Warning: This function is called from interrupt context!
@ -2062,67 +2075,47 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; }
void lcd_buttons_update() { void lcd_buttons_update() {
#if ENABLED(NEWPANEL) #if ENABLED(NEWPANEL)
uint8_t newbutton = 0; uint8_t newbutton = 0;
#if BTN_EN1 > 0 #if BUTTON_EXISTS(EN1)
if (READ(BTN_EN1) == 0) newbutton |= EN_A; if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
#endif #endif
#if BTN_EN2 > 0 #if BUTTON_EXISTS(EN2)
if (READ(BTN_EN2) == 0) newbutton |= EN_B; if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
#endif #endif
#if ENABLED(RIGIDBOT_PANEL) || BTN_ENC > 0 #if ENABLED(RIGIDBOT_PANEL) || BUTTON_EXISTS(ENC)
millis_t now = millis(); millis_t now = millis();
#endif #endif
#if ENABLED(RIGIDBOT_PANEL) #if ENABLED(RIGIDBOT_PANEL)
if (now > next_button_update_ms) { if (now > next_button_update_ms) {
if (READ(BTN_UP) == 0) { if (BUTTON_PRESSED(UP)) {
encoderDiff = -1 * (ENCODER_STEPS_PER_MENU_ITEM); encoderDiff = -1 * (ENCODER_STEPS_PER_MENU_ITEM);
next_button_update_ms = now + 300; next_button_update_ms = now + 300;
} }
else if (READ(BTN_DWN) == 0) { else if (BUTTON_PRESSED(DWN)) {
encoderDiff = ENCODER_STEPS_PER_MENU_ITEM; encoderDiff = ENCODER_STEPS_PER_MENU_ITEM;
next_button_update_ms = now + 300; next_button_update_ms = now + 300;
} }
else if (READ(BTN_LFT) == 0) { else if (BUTTON_PRESSED(LFT)) {
encoderDiff = -1 * (ENCODER_PULSES_PER_STEP); encoderDiff = -1 * (ENCODER_PULSES_PER_STEP);
next_button_update_ms = now + 300; next_button_update_ms = now + 300;
} }
else if (READ(BTN_RT) == 0) { else if (BUTTON_PRESSED(RT)) {
encoderDiff = ENCODER_PULSES_PER_STEP; encoderDiff = ENCODER_PULSES_PER_STEP;
next_button_update_ms = now + 300; next_button_update_ms = now + 300;
} }
} }
#endif #endif
#if BTN_ENC > 0 #if BUTTON_EXISTS(ENC)
if (now > next_button_update_ms && READ(BTN_ENC) == 0) newbutton |= EN_C; if (now > next_button_update_ms && BUTTON_PRESSED(ENC)) newbutton |= EN_C;
#endif #endif
buttons = newbutton; buttons = newbutton;
#if ENABLED(LCD_HAS_SLOW_BUTTONS) #if ENABLED(LCD_HAS_SLOW_BUTTONS)
buttons |= slow_buttons; buttons |= slow_buttons;
#endif #endif
#if ENABLED(REPRAPWORLD_KEYPAD) #if ENABLED(REPRAPWORLD_KEYPAD)
// for the reprapworld_keypad GET_BUTTON_STATES(buttons_reprapworld_keypad);
uint8_t newbutton_reprapworld_keypad = 0;
WRITE(SHIFT_LD, LOW);
WRITE(SHIFT_LD, HIGH);
for (int8_t i = 0; i < 8; i++) {
newbutton_reprapworld_keypad >>= 1;
if (READ(SHIFT_OUT)) SBI(newbutton_reprapworld_keypad, 7);
WRITE(SHIFT_CLK, HIGH);
WRITE(SHIFT_CLK, LOW);
}
buttons_reprapworld_keypad = ~newbutton_reprapworld_keypad; //invert it, because a pressed switch produces a logical 0
#endif #endif
#else //read it from the shift register #else
uint8_t newbutton = 0; GET_BUTTON_STATES(buttons);
WRITE(SHIFT_LD, LOW);
WRITE(SHIFT_LD, HIGH);
unsigned char tmp_buttons = 0;
for (int8_t i = 0; i < 8; i++) {
newbutton >>= 1;
if (READ(SHIFT_OUT)) SBI(newbutton, 7);
WRITE(SHIFT_CLK, HIGH);
WRITE(SHIFT_CLK, LOW);
}
buttons = ~newbutton; //invert it, because a pressed switch produces a logical 0
#endif //!NEWPANEL #endif //!NEWPANEL
#if ENABLED(REVERSE_MENU_DIRECTION) #if ENABLED(REVERSE_MENU_DIRECTION)

View File

@ -24,9 +24,14 @@
#define ULTRALCD_H #define ULTRALCD_H
#include "Marlin.h" #include "Marlin.h"
#if ENABLED(ULTRA_LCD) #if ENABLED(ULTRA_LCD)
#include "buzzer.h" #include "buzzer.h"
#define BUTTON_EXISTS(BN) (defined(BTN_## BN) && BTN_## BN >= 0)
#define BUTTON_PRESSED(BN) !READ(BTN_## BN)
int lcd_strlen(const char* s); int lcd_strlen(const char* s);
int lcd_strlen_P(const char* s); int lcd_strlen_P(const char* s);
void lcd_update(); void lcd_update();

View File

@ -44,7 +44,7 @@ extern volatile uint8_t buttons; //an extended version of the last checked butt
#define EN_B (_BV(BLEN_B)) // The two encoder pins are connected through BTN_EN1 and BTN_EN2 #define EN_B (_BV(BLEN_B)) // The two encoder pins are connected through BTN_EN1 and BTN_EN2
#define EN_A (_BV(BLEN_A)) #define EN_A (_BV(BLEN_A))
#if defined(BTN_ENC) && BTN_ENC > -1 #if BUTTON_EXISTS(ENC)
// encoder click is directly connected // encoder click is directly connected
#define BLEN_C 2 #define BLEN_C 2
#define EN_C (_BV(BLEN_C)) #define EN_C (_BV(BLEN_C))
@ -63,7 +63,7 @@ extern volatile uint8_t buttons; //an extended version of the last checked butt
#define B_DW (BUTTON_DOWN<<B_I2C_BTN_OFFSET) #define B_DW (BUTTON_DOWN<<B_I2C_BTN_OFFSET)
#define B_RI (BUTTON_RIGHT<<B_I2C_BTN_OFFSET) #define B_RI (BUTTON_RIGHT<<B_I2C_BTN_OFFSET)
#if defined(BTN_ENC) && BTN_ENC > -1 #if BUTTON_EXISTS(ENC)
// the pause/stop/restart button is connected to BTN_ENC when used // 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 B_ST (EN_C) // Map the pause/stop/resume button into its normalized functional name
#undef LCD_CLICKED #undef LCD_CLICKED
@ -77,8 +77,14 @@ extern volatile uint8_t buttons; //an extended version of the last checked butt
#define LCD_HAS_SLOW_BUTTONS #define LCD_HAS_SLOW_BUTTONS
#elif ENABLED(LCD_I2C_PANELOLU2) #elif ENABLED(LCD_I2C_PANELOLU2)
// encoder click can be read through I2C if not directly connected
#if BTN_ENC <= 0 #if BUTTON_EXISTS(ENC)
#undef LCD_CLICKED
#define LCD_CLICKED (buttons&EN_C)
#else // Read through I2C if not directly connected to a pin
#define B_I2C_BTN_OFFSET 3 // (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)
#define B_MI (PANELOLU2_ENCODER_C<<B_I2C_BTN_OFFSET) // requires LiquidTWI2 library v1.2.3 or later #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
// I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
#define LCD_HAS_SLOW_BUTTONS #define LCD_HAS_SLOW_BUTTONS
#else
#undef LCD_CLICKED
#define LCD_CLICKED (buttons&EN_C)
#endif #endif
#elif ENABLED(REPRAPWORLD_KEYPAD) #elif ENABLED(REPRAPWORLD_KEYPAD)