Skip to content

Commit c76f50c

Browse files
committed
Centralize click-handling in the LCD loop
1 parent 24bcea6 commit c76f50c

File tree

4 files changed

+65
-88
lines changed

4 files changed

+65
-88
lines changed

Marlin/Marlin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ extern bool axis_known_position[XYZ]; // axis[n].is_known
270270
extern bool axis_homed[XYZ]; // axis[n].is_homed
271271
extern volatile bool wait_for_heatup;
272272

273-
#if ENABLED(EMERGENCY_PARSER) && DISABLED(ULTIPANEL)
273+
#if ENABLED(EMERGENCY_PARSER) || ENABLED(ULTIPANEL)
274274
extern volatile bool wait_for_user;
275275
#endif
276276

Marlin/Marlin_main.cpp

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static bool relative_mode = false;
409409
volatile bool wait_for_heatup = true;
410410

411411
// For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
412-
#if ENABLED(EMERGENCY_PARSER) && DISABLED(ULTIPANEL)
412+
#if ENABLED(EMERGENCY_PARSER) || ENABLED(ULTIPANEL)
413413
volatile bool wait_for_user = false;
414414
#endif
415415

@@ -4420,7 +4420,6 @@ inline void gcode_G92() {
44204420
dontExpireStatus();
44214421
#endif
44224422
}
4423-
lcd_ignore_click();
44244423

44254424
#else
44264425

@@ -4431,51 +4430,28 @@ inline void gcode_G92() {
44314430

44324431
#endif
44334432

4434-
#if ENABLED(EMERGENCY_PARSER)
4435-
wait_for_user = true;
4436-
#endif
4437-
4433+
wait_for_user = true;
44384434
KEEPALIVE_STATE(PAUSED_FOR_USER);
44394435

44404436
stepper.synchronize();
44414437
refresh_cmd_timeout();
44424438

4443-
#if ENABLED(ULTIPANEL)
4444-
4445-
#if ENABLED(EMERGENCY_PARSER)
4446-
#define M1_WAIT_CONDITION (!lcd_clicked() && wait_for_user)
4439+
if (codenum > 0) {
4440+
codenum += previous_cmd_ms; // wait until this time for a click
4441+
while (PENDING(millis(), codenum) && wait_for_user) idle();
4442+
}
4443+
else {
4444+
#if ENABLED(ULTIPANEL)
4445+
if (lcd_detected()) {
4446+
while (wait_for_user) idle();
4447+
IS_SD_PRINTING ? LCD_MESSAGEPGM(MSG_RESUMING) : LCD_MESSAGEPGM(WELCOME_MSG);
4448+
}
44474449
#else
4448-
#define M1_WAIT_CONDITION !lcd_clicked()
4450+
while (wait_for_user) idle();
44494451
#endif
4452+
}
44504453

4451-
if (codenum > 0) {
4452-
codenum += previous_cmd_ms; // wait until this time for a click
4453-
while (PENDING(millis(), codenum) && M1_WAIT_CONDITION) idle();
4454-
lcd_ignore_click(false);
4455-
}
4456-
else if (lcd_detected()) {
4457-
while (M1_WAIT_CONDITION) idle();
4458-
}
4459-
else goto ExitM1;
4460-
4461-
IS_SD_PRINTING ? LCD_MESSAGEPGM(MSG_RESUMING) : LCD_MESSAGEPGM(WELCOME_MSG);
4462-
4463-
#else
4464-
4465-
if (codenum > 0) {
4466-
codenum += previous_cmd_ms; // wait until this time for an M108
4467-
while (PENDING(millis(), codenum) && wait_for_user) idle();
4468-
}
4469-
else while (wait_for_user) idle();
4470-
4471-
#endif
4472-
4473-
ExitM1:
4474-
4475-
#if ENABLED(EMERGENCY_PARSER)
4476-
wait_for_user = false;
4477-
#endif
4478-
4454+
wait_for_user = false;
44794455
KEEPALIVE_STATE(IN_HANDLER);
44804456
}
44814457

@@ -4721,7 +4697,7 @@ inline void gcode_M42() {
47214697
pin_state[pin - first_pin] = digitalRead(pin);
47224698
}
47234699

4724-
#if ENABLED(EMERGENCY_PARSER) && DISABLED(ULTIPANEL)
4700+
#if ENABLED(EMERGENCY_PARSER) || ENABLED(ULTIPANEL)
47254701
wait_for_user = true;
47264702
#endif
47274703

@@ -4739,7 +4715,7 @@ inline void gcode_M42() {
47394715
}
47404716
}
47414717

4742-
#if ENABLED(EMERGENCY_PARSER) && DISABLED(ULTIPANEL)
4718+
#if ENABLED(EMERGENCY_PARSER) || ENABLED(ULTIPANEL)
47434719
if (!wait_for_user) break;
47444720
#endif
47454721

@@ -6878,7 +6854,10 @@ inline void gcode_M503() {
68786854
// Wait for filament insert by user and press button
68796855
lcd_filament_change_show_message(FILAMENT_CHANGE_MESSAGE_INSERT);
68806856

6881-
while (!lcd_clicked()) {
6857+
// LCD click or M108 will clear this
6858+
wait_for_user = true;
6859+
6860+
while (wait_for_user) {
68826861
#if HAS_BUZZER
68836862
millis_t ms = millis();
68846863
if (ms >= next_buzz) {
@@ -6888,9 +6867,9 @@ inline void gcode_M503() {
68886867
#endif
68896868
idle(true);
68906869
}
6891-
delay(100);
6892-
while (lcd_clicked()) idle(true);
6893-
delay(100);
6870+
6871+
// Wait for unclick before accepting another click
6872+
lcd_ignore_click(false);
68946873

68956874
// Show load message
68966875
lcd_filament_change_show_message(FILAMENT_CHANGE_MESSAGE_LOAD);

Marlin/ultralcd.cpp

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to
257257
if (encoderLine >= encoderTopLine + LCD_HEIGHT) { \
258258
encoderTopLine = encoderLine - (LCD_HEIGHT - 1); \
259259
} \
260-
bool wasClicked = LCD_CLICKED; \
261-
bool _skipStatic = true; \
260+
bool wasClicked = lcd_clicked, \
261+
_skipStatic = true; \
262262
SCREEN_OR_MENU_LOOP()
263263

264264
/**
@@ -381,9 +381,8 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to
381381
menuPosition screen_history[10];
382382
uint8_t screen_history_depth = 0;
383383

384-
bool ignore_click = false;
385-
bool wait_for_unclick;
386-
bool defer_return_to_status = false;
384+
// LCD and menu clicks
385+
bool lcd_clicked, ignore_click, wait_for_unclick, defer_return_to_status;
387386

388387
// Variables used when editing values.
389388
const char* editLabel;
@@ -437,7 +436,7 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to
437436

438437
void lcd_ignore_click(bool b) {
439438
ignore_click = b;
440-
wait_for_unclick = false;
439+
lcd_clicked = wait_for_unclick = false;
441440
}
442441

443442
#endif // ULTIPANEL
@@ -493,23 +492,23 @@ static void lcd_status_screen() {
493492

494493
#if ENABLED(ULTIPANEL)
495494

496-
bool current_click = LCD_CLICKED;
495+
lcd_clicked = LCD_CLICKED;
497496

498-
if (ignore_click) {
499-
if (wait_for_unclick) {
500-
if (!current_click)
501-
ignore_click = wait_for_unclick = false;
502-
else
503-
current_click = false;
504-
}
505-
else if (current_click) {
506-
lcd_quick_feedback();
507-
wait_for_unclick = true;
508-
current_click = false;
509-
}
497+
// Might be waiting for unclick
498+
if (wait_for_unclick) {
499+
if (!lcd_clicked) ignore_click = wait_for_unclick = false;
500+
lcd_clicked = false;
510501
}
511502

512-
if (current_click) {
503+
// If ignoring the click or using it for something else, swallow it
504+
if (lcd_clicked && (ignore_click || wait_for_user)) {
505+
lcd_quick_feedback();
506+
wait_for_unclick = true;
507+
wait_for_user = false;
508+
lcd_clicked = false;
509+
}
510+
511+
if (lcd_clicked) {
513512
#if ENABLED(FILAMENT_LCD_DISPLAY)
514513
previous_lcd_status_ms = millis(); // get status message to show up for a while
515514
#endif
@@ -676,7 +675,7 @@ void kill_screen(const char* lcd_msg) {
676675
long babysteps_done = 0;
677676

678677
static void _lcd_babystep(const AxisEnum axis, const char* msg) {
679-
if (LCD_CLICKED) { defer_return_to_status = false; lcd_goto_previous_menu(true); return; }
678+
if (lcd_clicked) { defer_return_to_status = false; return lcd_goto_previous_menu(true); }
680679
ENCODER_DIRECTION_NORMAL();
681680
if (encoderPosition) {
682681
int babystep_increment = (int32_t)encoderPosition * BABYSTEP_MULTIPLICATOR;
@@ -1092,7 +1091,7 @@ void kill_screen(const char* lcd_msg) {
10921091
}
10931092

10941093
static bool debounce_click = false;
1095-
if (LCD_CLICKED) {
1094+
if (lcd_clicked) {
10961095
if (!debounce_click) {
10971096
debounce_click = true; // ignore multiple "clicks" in a row
10981097
mbl.set_zigzag_z(_lcd_level_bed_position++, current_position[Z_AXIS]);
@@ -1171,7 +1170,7 @@ void kill_screen(const char* lcd_msg) {
11711170
*/
11721171
static void _lcd_level_bed_homing_done() {
11731172
if (lcdDrawUpdate) lcd_implementation_drawedit(PSTR(MSG_LEVEL_BED_WAITING));
1174-
if (LCD_CLICKED) {
1173+
if (lcd_clicked) {
11751174
_lcd_level_bed_position = 0;
11761175
current_position[Z_AXIS] = MESH_HOME_SEARCH_Z
11771176
#if Z_HOME_DIR > 0
@@ -1385,7 +1384,7 @@ void kill_screen(const char* lcd_msg) {
13851384
*/
13861385

13871386
static void _lcd_move_xyz(const char* name, AxisEnum axis) {
1388-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
1387+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
13891388
ENCODER_DIRECTION_NORMAL();
13901389
if (encoderPosition) {
13911390
refresh_cmd_timeout();
@@ -1425,7 +1424,7 @@ void kill_screen(const char* lcd_msg) {
14251424
int8_t eindex=-1
14261425
#endif
14271426
) {
1428-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
1427+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
14291428
ENCODER_DIRECTION_NORMAL();
14301429
if (encoderPosition) {
14311430
current_position[E_AXIS] += float((int32_t)encoderPosition) * move_menu_scale;
@@ -1924,7 +1923,7 @@ void kill_screen(const char* lcd_msg) {
19241923
*/
19251924
#if HAS_LCD_CONTRAST
19261925
static void lcd_set_contrast() {
1927-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
1926+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
19281927
ENCODER_DIRECTION_NORMAL();
19291928
if (encoderPosition) {
19301929
set_lcd_contrast(lcd_contrast + encoderPosition);
@@ -1991,7 +1990,7 @@ void kill_screen(const char* lcd_msg) {
19911990
*/
19921991
void lcd_sdcard_menu() {
19931992
ENCODER_DIRECTION_MENUS();
1994-
if (lcdDrawUpdate == 0 && LCD_CLICKED == 0) return; // nothing to do (so don't thrash the SD card)
1993+
if (!lcdDrawUpdate && !lcd_clicked) return; // nothing to do (so don't thrash the SD card)
19951994
uint16_t fileCnt = card.getnrfilenames();
19961995
START_MENU();
19971996
MENU_BACK(MSG_MAIN);
@@ -2037,7 +2036,7 @@ void kill_screen(const char* lcd_msg) {
20372036
*
20382037
*/
20392038
static void lcd_info_stats_menu() {
2040-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
2039+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
20412040

20422041
char buffer[21];
20432042
printStatistics stats = print_job_timer.getStats();
@@ -2071,7 +2070,7 @@ void kill_screen(const char* lcd_msg) {
20712070
*
20722071
*/
20732072
static void lcd_info_thermistors_menu() {
2074-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
2073+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
20752074
START_SCREEN();
20762075
#define THERMISTOR_ID TEMP_SENSOR_0
20772076
#include "thermistornames.h"
@@ -2123,7 +2122,7 @@ void kill_screen(const char* lcd_msg) {
21232122
*
21242123
*/
21252124
static void lcd_info_board_menu() {
2126-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
2125+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
21272126
START_SCREEN();
21282127
STATIC_ITEM(BOARD_NAME, true, true); // MyPrinterController
21292128
STATIC_ITEM(MSG_INFO_BAUDRATE ": " STRINGIFY(BAUDRATE), true); // Baud: 250000
@@ -2144,7 +2143,7 @@ void kill_screen(const char* lcd_msg) {
21442143
*
21452144
*/
21462145
static void lcd_info_printer_menu() {
2147-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
2146+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
21482147
START_SCREEN();
21492148
STATIC_ITEM(MSG_MARLIN, true, true); // Marlin
21502149
STATIC_ITEM(SHORT_BUILD_VERSION, true); // x.x.x-Branch
@@ -2334,7 +2333,7 @@ void kill_screen(const char* lcd_msg) {
23342333
#define menu_edit_type(_type, _name, _strFunc, scale) \
23352334
bool _menu_edit_ ## _name () { \
23362335
ENCODER_DIRECTION_NORMAL(); \
2337-
bool isClicked = LCD_CLICKED; \
2336+
bool isClicked = lcd_clicked; \
23382337
if ((int32_t)encoderPosition < 0) encoderPosition = 0; \
23392338
if ((int32_t)encoderPosition > maxEditValue) encoderPosition = maxEditValue; \
23402339
if (lcdDrawUpdate) \
@@ -2695,7 +2694,7 @@ void lcd_update() {
26952694
#endif // REPRAPWORLD_KEYPAD
26962695

26972696
bool encoderPastThreshold = (abs(encoderDiff) >= ENCODER_PULSES_PER_STEP);
2698-
if (encoderPastThreshold || LCD_CLICKED) {
2697+
if (encoderPastThreshold || lcd_clicked) {
26992698
if (encoderPastThreshold) {
27002699
int32_t encoderMultiplier = 1;
27012700

@@ -3024,8 +3023,6 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; }
30243023
#endif
30253024
}
30263025

3027-
bool lcd_clicked() { return LCD_CLICKED; }
3028-
30293026
#endif // ULTIPANEL
30303027

30313028
#endif // ULTRA_LCD

Marlin/ultralcd.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
extern volatile uint8_t buttons; //the last checked buttons in a bit array.
6969
void lcd_buttons_update();
7070
void lcd_quick_feedback(); // Audible feedback for a button click - could also be visual
71-
bool lcd_clicked();
7271
void lcd_ignore_click(bool b=true);
7372

7473
#if ENABLED(FILAMENT_CHANGE_FEATURE)
@@ -79,12 +78,12 @@
7978
FORCE_INLINE void lcd_buttons_update() {}
8079
#endif
8180

82-
extern int preheatHotendTemp1;
83-
extern int preheatBedTemp1;
84-
extern int preheatFanSpeed1;
85-
extern int preheatHotendTemp2;
86-
extern int preheatBedTemp2;
87-
extern int preheatFanSpeed2;
81+
extern int preheatHotendTemp1,
82+
preheatBedTemp1,
83+
preheatFanSpeed1,
84+
preheatHotendTemp2,
85+
preheatBedTemp2,
86+
preheatFanSpeed2;
8887

8988
#if ENABLED(FILAMENT_LCD_DISPLAY)
9089
extern millis_t previous_lcd_status_ms;
@@ -152,6 +151,8 @@
152151
#define LCD_CLICKED (buttons & EN_C)
153152
#endif
154153

154+
extern bool lcd_clicked;
155+
155156
#else //no LCD
156157
FORCE_INLINE void lcd_update() {}
157158
FORCE_INLINE void lcd_init() {}

0 commit comments

Comments
 (0)