diff --git a/Marlin/Conditionals.h b/Marlin/Conditionals.h index 02f5a1959..91760045d 100644 --- a/Marlin/Conditionals.h +++ b/Marlin/Conditionals.h @@ -44,8 +44,6 @@ #ifndef CONFIGURATION_LCD // Get the LCD defines which are needed first - #define PIN_EXISTS(PN) (defined(PN##_PIN) && PN##_PIN >= 0) - #define CONFIGURATION_LCD #if ENABLED(MAKRPANEL) diff --git a/Marlin/macros.h b/Marlin/macros.h index c2ff0044f..4fce8eb53 100644 --- a/Marlin/macros.h +++ b/Marlin/macros.h @@ -55,4 +55,6 @@ #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-') #define COUNT(a) (sizeof(a)/sizeof(*a)) +#define PIN_EXISTS(PN) (defined(PN ##_PIN) && PN ##_PIN >= 0) + #endif //__MACROS_H diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 1de2f136d..75c743b98 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1723,17 +1723,17 @@ void lcd_init() { lcd_implementation_init(); #if ENABLED(NEWPANEL) - #if BTN_EN1 > 0 + #if BUTTON_EXISTS(EN1) SET_INPUT(BTN_EN1); WRITE(BTN_EN1, HIGH); #endif - #if BTN_EN2 > 0 + #if BUTTON_EXISTS(EN2) SET_INPUT(BTN_EN2); WRITE(BTN_EN2, HIGH); #endif - #if BTN_ENC > 0 + #if BUTTON_EXISTS(ENC) SET_INPUT(BTN_ENC); WRITE(BTN_ENC, HIGH); #endif @@ -2055,6 +2055,19 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; } #define encrot3 1 #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 * 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() { #if ENABLED(NEWPANEL) uint8_t newbutton = 0; - #if BTN_EN1 > 0 - if (READ(BTN_EN1) == 0) newbutton |= EN_A; + #if BUTTON_EXISTS(EN1) + if (BUTTON_PRESSED(EN1)) newbutton |= EN_A; #endif - #if BTN_EN2 > 0 - if (READ(BTN_EN2) == 0) newbutton |= EN_B; + #if BUTTON_EXISTS(EN2) + if (BUTTON_PRESSED(EN2)) newbutton |= EN_B; #endif - #if ENABLED(RIGIDBOT_PANEL) || BTN_ENC > 0 + #if ENABLED(RIGIDBOT_PANEL) || BUTTON_EXISTS(ENC) millis_t now = millis(); #endif #if ENABLED(RIGIDBOT_PANEL) if (now > next_button_update_ms) { - if (READ(BTN_UP) == 0) { + if (BUTTON_PRESSED(UP)) { encoderDiff = -1 * (ENCODER_STEPS_PER_MENU_ITEM); next_button_update_ms = now + 300; } - else if (READ(BTN_DWN) == 0) { + else if (BUTTON_PRESSED(DWN)) { encoderDiff = ENCODER_STEPS_PER_MENU_ITEM; next_button_update_ms = now + 300; } - else if (READ(BTN_LFT) == 0) { + else if (BUTTON_PRESSED(LFT)) { encoderDiff = -1 * (ENCODER_PULSES_PER_STEP); next_button_update_ms = now + 300; } - else if (READ(BTN_RT) == 0) { + else if (BUTTON_PRESSED(RT)) { encoderDiff = ENCODER_PULSES_PER_STEP; next_button_update_ms = now + 300; } } #endif - #if BTN_ENC > 0 - if (now > next_button_update_ms && READ(BTN_ENC) == 0) newbutton |= EN_C; + #if BUTTON_EXISTS(ENC) + if (now > next_button_update_ms && BUTTON_PRESSED(ENC)) newbutton |= EN_C; #endif buttons = newbutton; #if ENABLED(LCD_HAS_SLOW_BUTTONS) buttons |= slow_buttons; #endif #if ENABLED(REPRAPWORLD_KEYPAD) - // for the 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 + GET_BUTTON_STATES(buttons_reprapworld_keypad); #endif - #else //read it from the shift register - uint8_t newbutton = 0; - 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 + #else + GET_BUTTON_STATES(buttons); #endif //!NEWPANEL #if ENABLED(REVERSE_MENU_DIRECTION) diff --git a/Marlin/ultralcd.h b/Marlin/ultralcd.h index f3b33b53b..093e9b20c 100644 --- a/Marlin/ultralcd.h +++ b/Marlin/ultralcd.h @@ -24,9 +24,14 @@ #define ULTRALCD_H #include "Marlin.h" + #if ENABLED(ULTRA_LCD) + #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_P(const char* s); void lcd_update(); diff --git a/Marlin/ultralcd_implementation_hitachi_HD44780.h b/Marlin/ultralcd_implementation_hitachi_HD44780.h index 52a4926a0..a8221d3a8 100644 --- a/Marlin/ultralcd_implementation_hitachi_HD44780.h +++ b/Marlin/ultralcd_implementation_hitachi_HD44780.h @@ -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_A (_BV(BLEN_A)) - #if defined(BTN_ENC) && BTN_ENC > -1 + #if BUTTON_EXISTS(ENC) // encoder click is directly connected #define BLEN_C 2 #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< -1 + #if BUTTON_EXISTS(ENC) // 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 #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 #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_MI (PANELOLU2_ENCODER_C<