Skip to content

Commit

Permalink
Major refactoring
Browse files Browse the repository at this point in the history
Fixes all the issues I've found so far.

Roughly 60B of flash saved. Need to double check that later.
  • Loading branch information
gudnimg authored and DRracer committed Apr 5, 2023
1 parent d89e6de commit a7e9ccf
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Firmware/Marlin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9455,11 +9455,11 @@ void manage_inactivity(bool ignore_stepper_queue/*=false*/) //default argument s
// handle longpress
if(lcd_longpress_trigger)
{
lcd_consume_click(); // Reset trigger to prevent recursion
// long press is not possible in modal mode, wait until ready
if (lcd_longpress_func && lcd_update_enabled)
{
lcd_longpress_func();
lcd_longpress_trigger = 0;
}
}

Expand Down
33 changes: 15 additions & 18 deletions Firmware/lcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,6 @@ uint8_t lcd_encoder_bits = 0;
static int8_t lcd_encoder_diff = 0;

uint8_t lcd_buttons = 0;
uint8_t lcd_button_pressed = 0;
uint8_t lcd_update_enabled = 1;
static bool lcd_backlight_wake_trigger; // Flag set by interrupt when the knob is pressed or rotated

Expand Down Expand Up @@ -689,7 +688,6 @@ Sound_MakeSound(e_SOUND_TYPE_ButtonEcho);
void lcd_quick_feedback(void)
{
lcd_draw_update = 2;
lcd_button_pressed = false;
lcd_beeper_quick_feedback();
}

Expand All @@ -708,6 +706,11 @@ void lcd_update(uint8_t lcdDrawUpdateOverride)
} else {
Sound_MakeSound(e_SOUND_TYPE_ButtonEcho);
}

if (lcd_draw_update == 0) {
// Update LCD rendering at minimum
lcd_draw_update = 1;
}
}

backlight_update();
Expand Down Expand Up @@ -750,16 +753,12 @@ bool lcd_longpress_trigger = 0;
void lcd_buttons_update(void)
{
static uint8_t lcd_long_press_active = 0;
uint8_t newbutton = 0;
if (READ(BTN_EN1) == 0) newbutton |= EN_A;
if (READ(BTN_EN2) == 0) newbutton |= EN_B;

static uint8_t lcd_button_pressed = 0;
if (READ(BTN_ENC) == 0)
{ //button is pressed
if (!buttonBlanking.running() || buttonBlanking.expired(BUTTON_BLANKING_TIME)) {
if (buttonBlanking.expired_cont(BUTTON_BLANKING_TIME)) {
buttonBlanking.start();
safetyTimer.start();
lcd_backlight_wake_trigger = true; // flag event, knob pressed
if ((lcd_button_pressed == 0) && (lcd_long_press_active == 0))
{
longPressTimer.start();
Expand All @@ -776,22 +775,20 @@ void lcd_buttons_update(void)
{ //button not pressed
if (lcd_button_pressed)
{ //button was released
buttonBlanking.start();
if (lcd_long_press_active == 0)
lcd_button_pressed = 0; // Reset to prevent double triggering
if (!lcd_long_press_active)
{ //button released before long press gets activated
newbutton |= EN_C;
lcd_buttons |= EN_C; // This flag is reset when the event is consumed
}
//else if (menu_menu == lcd_move_z) lcd_quick_feedback();
//lcd_button_pressed is set back to false via lcd_quick_feedback function
lcd_backlight_wake_trigger = true; // flag event, knob pressed
lcd_long_press_active = 0;
}
lcd_long_press_active = 0;
}

lcd_buttons = newbutton;
//manage encoder rotation
uint8_t enc = 0;
if (lcd_buttons & EN_A) enc |= B01;
if (lcd_buttons & EN_B) enc |= B10;
if (READ(BTN_EN1) == 0) enc |= B01;
if (READ(BTN_EN2) == 0) enc |= B10;
if (enc != lcd_encoder_bits)
{
switch (enc)
Expand Down Expand Up @@ -825,8 +822,8 @@ void lcd_buttons_update(void)
if (abs(lcd_encoder_diff) >= ENCODER_PULSES_PER_STEP) {
lcd_backlight_wake_trigger = true; // flag event, knob rotated
}
lcd_encoder_bits = enc;
}
lcd_encoder_bits = enc;
}


Expand Down
5 changes: 1 addition & 4 deletions Firmware/lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ extern uint8_t lcd_encoder_bits;
//the last checked lcd_buttons in a bit array.
extern uint8_t lcd_buttons;

extern uint8_t lcd_button_pressed;

extern uint8_t lcd_update_enabled;

extern LongTimer lcd_timeoutToStatus;
Expand Down Expand Up @@ -212,8 +210,7 @@ extern void lcd_set_custom_characters_nextpage(void);
//! @brief Consume click and longpress event
inline void lcd_consume_click()
{
lcd_button_pressed = 0;
lcd_buttons &= 0xff^EN_C;
lcd_buttons = 0;
lcd_longpress_trigger = 0;
}

Expand Down
35 changes: 13 additions & 22 deletions Firmware/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,18 @@ void menu_data_reset(void)
memset(&menu_data, 0, sizeof(menu_data));
}

void menu_goto(menu_func_t menu, const uint32_t encoder, const bool feedback, bool reset_menu_state)
void menu_goto(menu_func_t menu, const uint32_t encoder, bool reset_menu_state, const bool feedback)
{
CRITICAL_SECTION_START;
if (menu_menu != menu)
{
menu_menu = menu;
lcd_encoder = encoder;
menu_top = 0; //reset menu view. Needed if menu_back() is called from deep inside a menu, such as Support
lcd_draw_update = 2; // Full LCD re-draw
CRITICAL_SECTION_END;
if (reset_menu_state)
menu_data_reset();

if (feedback) lcd_quick_feedback();
if (feedback) lcd_beeper_quick_feedback();
if (reset_menu_state) menu_data_reset();
}
else
CRITICAL_SECTION_END;
Expand All @@ -74,7 +73,7 @@ void menu_start(void)
if (lcd_encoder < menu_top)
menu_top = lcd_encoder;
menu_line = menu_top;
menu_clicked = LCD_CLICKED;
menu_clicked = lcd_clicked(); // Consume click event
}

void menu_end(void)
Expand All @@ -96,7 +95,7 @@ void menu_end(void)
void menu_back(uint8_t nLevel)
{
menu_depth = ((menu_depth > nLevel) ? (menu_depth - nLevel) : 0);
menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, true);
menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true);
}

void menu_back(void)
Expand All @@ -109,7 +108,7 @@ void menu_back_no_reset(void)
if (menu_depth > 0)
{
menu_depth--;
menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, false);
menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, false);
}
}

Expand All @@ -119,29 +118,29 @@ void menu_back_if_clicked(void)
menu_back();
}

void menu_submenu(menu_func_t submenu)
void menu_submenu(menu_func_t submenu, const bool feedback)
{
if (menu_depth < MENU_DEPTH_MAX)
{
menu_stack[menu_depth].menu = menu_menu;
menu_stack[menu_depth++].position = lcd_encoder;
menu_goto(submenu, 0, true, true);
menu_goto(submenu, 0, true, feedback);
}
}

void menu_submenu_no_reset(menu_func_t submenu)
void menu_submenu_no_reset(menu_func_t submenu, const bool feedback)
{
if (menu_depth < MENU_DEPTH_MAX)
{
menu_stack[menu_depth].menu = menu_menu;
menu_stack[menu_depth++].position = lcd_encoder;
menu_goto(submenu, 0, true, false);
menu_goto(submenu, 0, false, feedback);
}
}

uint8_t menu_item_ret(void)
{
lcd_quick_feedback();
lcd_draw_update = 2;
return 1;
}

Expand Down Expand Up @@ -294,8 +293,6 @@ uint8_t __attribute__((noinline)) menu_item_function_E(const Sheet &sheet, menu_
if (lcd_draw_update) menu_draw_item_select_sheet_E(' ', sheet);
if (menu_clicked && (lcd_encoder == menu_item))
{
menu_clicked = false;
lcd_consume_click();
lcd_update_enabled = 0;
if (func) func();
lcd_update_enabled = 1;
Expand Down Expand Up @@ -332,8 +329,6 @@ uint8_t menu_item_function_P(const char* str, menu_func_t func)
if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
if (menu_clicked && (lcd_encoder == menu_item))
{
menu_clicked = false;
lcd_consume_click();
lcd_update_enabled = 0;
if (func) func();
lcd_update_enabled = 1;
Expand All @@ -360,8 +355,6 @@ uint8_t menu_item_function_P(const char* str, char number, void (*func)(uint8_t)
if (lcd_draw_update) menu_draw_item_puts_P(' ', str, number);
if (menu_clicked && (lcd_encoder == menu_item))
{
menu_clicked = false;
lcd_consume_click();
lcd_update_enabled = 0;
if (func) func(fn_par);
lcd_update_enabled = 1;
Expand All @@ -386,8 +379,6 @@ uint8_t menu_item_toggle_P(const char* str, const char* toggle, menu_func_t func
}
else // do the actual toggling
{
menu_clicked = false;
lcd_consume_click();
lcd_update_enabled = 0;
if (func) func();
lcd_update_enabled = 1;
Expand Down Expand Up @@ -499,7 +490,7 @@ static void _menu_edit_P(void)
lcd_set_cursor(0, 1);
menu_draw_P<T>(' ', _md->editLabel, (int)lcd_encoder);
}
if (LCD_CLICKED)
if (lcd_clicked())
{
*((T)(_md->editValue)) = lcd_encoder;
menu_back_no_reset();
Expand Down
6 changes: 3 additions & 3 deletions Firmware/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extern menu_func_t menu_menu;

extern void menu_data_reset(void);

extern void menu_goto(menu_func_t menu, const uint32_t encoder, const bool feedback, bool reset_menu_state);
extern void menu_goto(menu_func_t menu, const uint32_t encoder, bool reset_menu_state, const bool feedback=false);

#define MENU_BEGIN() menu_start(); for(menu_row = 0; menu_row < LCD_HEIGHT; menu_row++, menu_line++) { menu_item = 0;
void menu_start(void);
Expand All @@ -78,8 +78,8 @@ extern void menu_back(uint8_t nLevel);

extern void menu_back_if_clicked(void);

extern void menu_submenu(menu_func_t submenu);
extern void menu_submenu_no_reset(menu_func_t submenu);
extern void menu_submenu(menu_func_t submenu, const bool feedback=false);
extern void menu_submenu_no_reset(menu_func_t submenu, const bool feedback=false);

extern uint8_t menu_item_ret(void);

Expand Down
Loading

0 comments on commit a7e9ccf

Please sign in to comment.