diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 302588c7b3..9f194c1ff5 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -252,7 +252,7 @@ static void lcd_status_screen(); #endif //!ENCODER_RATE_MULTIPLIER #define END_MENU() \ if (encoderLine >= _menuItemNr) { encoderPosition = _menuItemNr * (ENCODER_STEPS_PER_MENU_ITEM) - 1; encoderLine = _menuItemNr - 1; }\ - if (encoderLine >= currentMenuViewOffset + LCD_HEIGHT) { currentMenuViewOffset = encoderLine - (LCD_HEIGHT) + 1; lcdDrawUpdate = 1; _lineNr = currentMenuViewOffset - 1; _drawLineNr = -1; } \ + if (encoderLine >= currentMenuViewOffset + LCD_HEIGHT) { currentMenuViewOffset = encoderLine - (LCD_HEIGHT) + 1; lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_REDRAW; _lineNr = currentMenuViewOffset - 1; _drawLineNr = -1; } \ } } while(0) /** Used variables to keep track of the menu */ @@ -280,7 +280,15 @@ uint8_t lcd_status_update_delay; bool ignore_click = false; bool wait_for_unclick; bool defer_return_to_status = false; -uint8_t lcdDrawUpdate = 2; /* Set to none-zero when the LCD needs to draw, decreased after every draw. Set to 2 in LCD routines so the LCD gets at least 1 full redraw (first redraw is partial) */ + +enum LCDHandlerAction { + LCD_DRAW_UPDATE_NONE, + LCD_DRAW_UPDATE_CALL_REDRAW, + LCD_DRAW_UPDATE_CLEAR_CALL_REDRAW, + LCD_DRAW_UPDATE_CALL_NO_REDRAW, +}; + +uint8_t lcdDrawUpdate = LCD_DRAW_UPDATE_CLEAR_CALL_REDRAW; // Set 1 or 2 when the LCD needs to draw, decrements after every draw. Set to 2 in LCD routines so the LCD gets at least 1 full redraw (first redraw is partial) // Variables used when editing values. const char* editLabel; @@ -298,7 +306,7 @@ float raw_Ki, raw_Kd; static void lcd_goto_menu(menuFunc_t menu, const bool feedback = false, const uint32_t encoder = 0) { if (currentMenu != menu) { currentMenu = menu; - lcdDrawUpdate = 2; + lcdDrawUpdate = LCD_DRAW_UPDATE_CLEAR_CALL_REDRAW; #if ENABLED(NEWPANEL) encoderPosition = encoder; if (feedback) lcd_quick_feedback(); @@ -524,7 +532,7 @@ void lcd_set_home_offsets() { if (encoderPosition != 0) { int distance = (int)encoderPosition * BABYSTEP_MULTIPLICATOR; encoderPosition = 0; - lcdDrawUpdate = 1; + lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_REDRAW; #if ENABLED(COREXY) || ENABLED(COREXZ) #if ENABLED(BABYSTEP_XY) switch(axis) { @@ -948,7 +956,7 @@ static void _lcd_move(const char* name, AxisEnum axis, int min, int max) { if (max_software_endstops) NOMORE(current_position[axis], max); encoderPosition = 0; line_to_current(axis); - lcdDrawUpdate = 1; + lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_REDRAW; } if (lcdDrawUpdate) lcd_implementation_drawedit(name, ftostr31(current_position[axis])); if (LCD_CLICKED) lcd_goto_previous_menu(); @@ -977,7 +985,7 @@ static void lcd_move_e( current_position[E_AXIS] += float((int)encoderPosition) * move_menu_scale; encoderPosition = 0; line_to_current(E_AXIS); - lcdDrawUpdate = 1; + lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_REDRAW; } if (lcdDrawUpdate) { PGM_P pos_label; @@ -1449,7 +1457,7 @@ static void lcd_control_volumetric_menu() { lcd_contrast &= 0x3F; #endif encoderPosition = 0; - lcdDrawUpdate = 1; + lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_REDRAW; u8g.setContrast(lcd_contrast); } if (lcdDrawUpdate) { @@ -1590,7 +1598,7 @@ static void lcd_control_volumetric_menu() { static void _menu_action_setting_edit_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue) { \ lcd_save_previous_menu(); \ \ - lcdDrawUpdate = 2; \ + lcdDrawUpdate = LCD_DRAW_UPDATE_CLEAR_CALL_REDRAW; \ currentMenu = menu_edit_ ## _name; \ \ editLabel = pstr; \ @@ -1672,7 +1680,7 @@ menu_edit_type(unsigned long, long5, ftostr5, 0.01) #endif void lcd_quick_feedback() { - lcdDrawUpdate = 2; + lcdDrawUpdate = LCD_DRAW_UPDATE_CLEAR_CALL_REDRAW; next_button_update_ms = millis() + 500; #if ENABLED(LCD_USE_I2C_BUZZER) @@ -1837,11 +1845,27 @@ bool lcd_blink() { * - Act on RepRap World keypad input * - Update the encoder position * - Apply acceleration to the encoder position + * - Set lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_REDRAW on controller events * - Reset the Info Screen timeout if there's any input * - Update status indicators, if any - * - Clear the LCD if lcdDrawUpdate == 2 * - * Warning: This function is called from interrupt context! + * Run the current LCD menu handler callback function: + * - Call the handler only if lcdDrawUpdate != LCD_DRAW_UPDATE_NONE + * - Before calling the handler, LCD_DRAW_UPDATE_CALL_NO_REDRAW => LCD_DRAW_UPDATE_NONE + * - Call the menu handler. Menu handlers should do the following: + * - If a value changes, set lcdDrawUpdate to LCD_DRAW_UPDATE_CALL_REDRAW + * - if (lcdDrawUpdate) { redraw } + * - Before exiting the handler set lcdDrawUpdate to: + * - LCD_DRAW_UPDATE_CALL_REDRAW or LCD_DRAW_UPDATE_NONE for no callbacks until the next controller event. + * - LCD_DRAW_UPDATE_CLEAR_CALL_REDRAW to clear screen, LCD_DRAW_UPDATE_CALL_REDRAW on the next loop. + * - LCD_DRAW_UPDATE_CALL_NO_REDRAW for a callback with no forced redraw on the next loop. + * - NOTE: For some displays, the menu handler may be called 2 or more times per loop. + * + * After the menu handler callback runs (or not): + * - Clear the LCD if lcdDrawUpdate == LCD_DRAW_UPDATE_CLEAR_CALL_REDRAW + * - Update lcdDrawUpdate for the next loop (i.e., move one state down, usually) + * + * No worries. This function is only called from the main thread. */ void lcd_update() { #if ENABLED(ULTIPANEL) @@ -1854,7 +1878,7 @@ void lcd_update() { bool sd_status = IS_SD_INSERTED; if (sd_status != lcd_sd_status && lcd_detected()) { - lcdDrawUpdate = 2; + lcdDrawUpdate = LCD_DRAW_UPDATE_CLEAR_CALL_REDRAW; lcd_implementation_init( // to maybe revive the LCD if static electricity killed it. #if ENABLED(LCD_PROGRESS_BAR) currentMenu == lcd_status_screen @@ -1933,13 +1957,13 @@ void lcd_update() { encoderDiff = 0; } return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS; - lcdDrawUpdate = 1; + lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_REDRAW; } #endif //ULTIPANEL if (currentMenu == lcd_status_screen) { if (!lcd_status_update_delay) { - lcdDrawUpdate = 1; + lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_REDRAW; lcd_status_update_delay = 10; /* redraw the main screen every second. This is easier then trying keep track of all things that change on the screen */ } else { @@ -1948,6 +1972,9 @@ void lcd_update() { } if (lcdDrawUpdate) { + + if (lcdDrawUpdate == LCD_DRAW_UPDATE_CALL_NO_REDRAW) lcdDrawUpdate = LCD_DRAW_UPDATE_NONE; + #if ENABLED(DOGLCD) // Changes due to different driver architecture of the DOGM display bool blink = lcd_blink(); u8g.firstPage(); @@ -1971,14 +1998,29 @@ void lcd_update() { #if ENABLED(ULTIPANEL) // Return to Status Screen after a timeout - if (!defer_return_to_status && currentMenu != lcd_status_screen && millis() > return_to_status_ms) { + if (defer_return_to_status) + return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS; + else if (currentMenu != lcd_status_screen && millis() > return_to_status_ms) { lcd_return_to_status(); - lcdDrawUpdate = 2; } #endif // ULTIPANEL - if (lcdDrawUpdate && --lcdDrawUpdate) lcd_implementation_clear(); + switch (lcdDrawUpdate) { + case LCD_DRAW_UPDATE_NONE: + // do nothing + case LCD_DRAW_UPDATE_CALL_NO_REDRAW: + // changes to LCD_DRAW_UPDATE_NONE before call + break; + case LCD_DRAW_UPDATE_CLEAR_CALL_REDRAW: + lcd_implementation_clear(); + lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_REDRAW; + break; + case LCD_DRAW_UPDATE_CALL_REDRAW: + lcdDrawUpdate = LCD_DRAW_UPDATE_NONE; + break; + } + next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL; } } @@ -1995,7 +2037,7 @@ void lcd_finishstatus(bool persist=false) { expire_status_ms = persist ? 0 : progress_bar_ms + PROGRESS_MSG_EXPIRE; #endif #endif - lcdDrawUpdate = 2; + lcdDrawUpdate = LCD_DRAW_UPDATE_CLEAR_CALL_REDRAW; #if ENABLED(FILAMENT_LCD_DISPLAY) previous_lcd_status_ms = millis(); //get status message to show up for a while @@ -2449,7 +2491,7 @@ char* ftostr52(const float& x) { if (max_software_endstops) NOMORE(current_position[Z_AXIS], Z_MAX_POS); encoderPosition = 0; line_to_current(Z_AXIS); - lcdDrawUpdate = 1; + lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_NO_REDRAW; } if (lcdDrawUpdate) { float v = current_position[Z_AXIS] - MESH_HOME_SEARCH_Z; @@ -2458,7 +2500,7 @@ char* ftostr52(const float& x) { static bool debounce_click = false; if (LCD_CLICKED) { if (!debounce_click) { - debounce_click = true; + debounce_click = true; // ignore multiple "clicks" in a row int ix = _lcd_level_bed_position % (MESH_NUM_X_POINTS), iy = _lcd_level_bed_position / (MESH_NUM_X_POINTS); if (iy & 1) ix = (MESH_NUM_X_POINTS - 1) - ix; // Zig zag @@ -2489,7 +2531,7 @@ char* ftostr52(const float& x) { current_position[X_AXIS] = mbl.get_x(ix); current_position[Y_AXIS] = mbl.get_y(iy); line_to_current(manual_feedrate[X_AXIS] <= manual_feedrate[Y_AXIS] ? X_AXIS : Y_AXIS); - lcdDrawUpdate = 1; + lcdDrawUpdate = LCD_DRAW_UPDATE_CALL_NO_REDRAW; } } }