Merge pull request #3304 from thinkyhead/rc_slow_buttons

Macros for buttons, some cleanup for slow buttons
This commit is contained in:
Scott Lahteine 2016-04-01 15:31:54 -07:00
commit 2512d8fd5e
5 changed files with 54 additions and 53 deletions

View File

@ -44,8 +44,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)
@ -942,16 +946,15 @@ void lcd_implementation_drawedit(const char* pstr, const char* value) {
static uint8_t lcd_implementation_read_slow_buttons() { static uint8_t lcd_implementation_read_slow_buttons() {
#if ENABLED(LCD_I2C_TYPE_MCP23017) #if ENABLED(LCD_I2C_TYPE_MCP23017)
uint8_t slow_buttons;
// Reading these buttons this is likely to be too slow to call inside interrupt context // Reading these buttons this is likely to be too slow to call inside interrupt context
// so they are called during normal lcd_update // so they are called during normal lcd_update
slow_buttons = lcd.readButtons() << B_I2C_BTN_OFFSET; uint8_t slow_bits = lcd.readButtons() << B_I2C_BTN_OFFSET;
#if ENABLED(LCD_I2C_VIKI) #if ENABLED(LCD_I2C_VIKI)
if ((slow_buttons & (B_MI | B_RI)) && millis() < next_button_update_ms) // LCD clicked if ((slow_bits & (B_MI | B_RI)) && millis() < next_button_update_ms) // LCD clicked
slow_buttons &= ~(B_MI | B_RI); // Disable LCD clicked buttons if screen is updated slow_bits &= ~(B_MI | B_RI); // Disable LCD clicked buttons if screen is updated
#endif #endif // LCD_I2C_VIKI
return slow_buttons; return slow_bits;
#endif #endif // LCD_I2C_TYPE_MCP23017
} }
#endif // LCD_HAS_SLOW_BUTTONS #endif // LCD_HAS_SLOW_BUTTONS