Skip to content

Commit b0d93ac

Browse files
committed
Centralize click-handling in the LCD loop
1 parent c4c5385 commit b0d93ac

File tree

4 files changed

+60
-98
lines changed

4 files changed

+60
-98
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(ULTIPANEL) || ENABLED(EMERGENCY_PARSER)
273+
#if ENABLED(EMERGENCY_PARSER) || ENABLED(ULTIPANEL)
274274
extern volatile bool wait_for_user;
275275
#endif
276276

Marlin/Marlin_main.cpp

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4420,7 +4420,6 @@ inline void gcode_G92() {
44204420
dontExpireStatus();
44214421
#endif
44224422
}
4423-
lcd_ignore_click();
44244423

44254424
#else
44264425

@@ -4431,53 +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-
#if ENABLED(ULTIPANEL)
4474-
ExitM1:
4475-
#endif
4476-
4477-
#if ENABLED(EMERGENCY_PARSER)
4478-
wait_for_user = false;
4479-
#endif
4480-
4454+
wait_for_user = false;
44814455
KEEPALIVE_STATE(IN_HANDLER);
44824456
}
44834457

@@ -6880,7 +6854,10 @@ inline void gcode_M503() {
68806854
// Wait for filament insert by user and press button
68816855
lcd_filament_change_show_message(FILAMENT_CHANGE_MESSAGE_INSERT);
68826856

6883-
while (!lcd_clicked()) {
6857+
// LCD click or M108 will clear this
6858+
wait_for_user = true;
6859+
6860+
while (wait_for_user) {
68846861
#if HAS_BUZZER
68856862
millis_t ms = millis();
68866863
if (ms >= next_buzz) {
@@ -6890,9 +6867,9 @@ inline void gcode_M503() {
68906867
#endif
68916868
idle(true);
68926869
}
6893-
delay(100);
6894-
while (lcd_clicked()) idle(true);
6895-
delay(100);
6870+
6871+
// Wait for unclick before accepting another click
6872+
lcd_ignore_click(false);
68966873

68976874
// Show load message
68986875
lcd_filament_change_show_message(FILAMENT_CHANGE_MESSAGE_LOAD);

Marlin/ultralcd.cpp

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to
243243
*
244244
* START_MENU Opening code for a screen with menu items.
245245
* Scroll as-needed to keep the selected line in view.
246-
* 'wasClicked' indicates the controller was clicked.
247246
*/
248247
#define START_SCREEN() \
249248
START_SCREEN_OR_MENU(LCD_HEIGHT); \
@@ -257,7 +256,6 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to
257256
if (encoderLine >= encoderTopLine + LCD_HEIGHT) { \
258257
encoderTopLine = encoderLine - (LCD_HEIGHT - 1); \
259258
} \
260-
bool wasClicked = LCD_CLICKED; \
261259
bool _skipStatic = true; \
262260
SCREEN_OR_MENU_LOOP()
263261

@@ -288,8 +286,7 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to
288286
if (_menuLineNr == _thisItemNr) { \
289287
if (lcdDrawUpdate) \
290288
lcd_implementation_drawmenu_ ## TYPE(encoderLine == _thisItemNr, _lcdLineNr, PSTR(LABEL), ## __VA_ARGS__); \
291-
if (wasClicked && encoderLine == _thisItemNr) { \
292-
lcd_quick_feedback()
289+
if (lcd_clicked && encoderLine == _thisItemNr) {
293290

294291
#define _MENU_ITEM_PART_2(TYPE, ...) \
295292
menu_action_ ## TYPE(__VA_ARGS__); \
@@ -381,9 +378,8 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to
381378
menuPosition screen_history[10];
382379
uint8_t screen_history_depth = 0;
383380

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

388384
// Variables used when editing values.
389385
const char* editLabel;
@@ -435,11 +431,6 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to
435431
lcd_return_to_status();
436432
}
437433

438-
void lcd_ignore_click(bool b) {
439-
ignore_click = b;
440-
wait_for_unclick = false;
441-
}
442-
443434
#endif // ULTIPANEL
444435

445436
/**
@@ -493,23 +484,7 @@ static void lcd_status_screen() {
493484

494485
#if ENABLED(ULTIPANEL)
495486

496-
bool current_click = LCD_CLICKED;
497-
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-
}
510-
}
511-
512-
if (current_click) {
487+
if (lcd_clicked) {
513488
#if ENABLED(FILAMENT_LCD_DISPLAY)
514489
previous_lcd_status_ms = millis(); // get status message to show up for a while
515490
#endif
@@ -676,7 +651,7 @@ void kill_screen(const char* lcd_msg) {
676651
long babysteps_done = 0;
677652

678653
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; }
654+
if (lcd_clicked) { defer_return_to_status = false; return lcd_goto_previous_menu(true); }
680655
ENCODER_DIRECTION_NORMAL();
681656
if (encoderPosition) {
682657
int babystep_increment = (int32_t)encoderPosition * BABYSTEP_MULTIPLICATOR;
@@ -1092,7 +1067,7 @@ void kill_screen(const char* lcd_msg) {
10921067
}
10931068

10941069
static bool debounce_click = false;
1095-
if (LCD_CLICKED) {
1070+
if (lcd_clicked) {
10961071
if (!debounce_click) {
10971072
debounce_click = true; // ignore multiple "clicks" in a row
10981073
mbl.set_zigzag_z(_lcd_level_bed_position++, current_position[Z_AXIS]);
@@ -1171,7 +1146,7 @@ void kill_screen(const char* lcd_msg) {
11711146
*/
11721147
static void _lcd_level_bed_homing_done() {
11731148
if (lcdDrawUpdate) lcd_implementation_drawedit(PSTR(MSG_LEVEL_BED_WAITING));
1174-
if (LCD_CLICKED) {
1149+
if (lcd_clicked) {
11751150
_lcd_level_bed_position = 0;
11761151
current_position[Z_AXIS] = MESH_HOME_SEARCH_Z
11771152
#if Z_HOME_DIR > 0
@@ -1385,7 +1360,7 @@ void kill_screen(const char* lcd_msg) {
13851360
*/
13861361

13871362
static void _lcd_move_xyz(const char* name, AxisEnum axis) {
1388-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
1363+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
13891364
ENCODER_DIRECTION_NORMAL();
13901365
if (encoderPosition) {
13911366
refresh_cmd_timeout();
@@ -1425,7 +1400,7 @@ void kill_screen(const char* lcd_msg) {
14251400
int8_t eindex=-1
14261401
#endif
14271402
) {
1428-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
1403+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
14291404
ENCODER_DIRECTION_NORMAL();
14301405
if (encoderPosition) {
14311406
current_position[E_AXIS] += float((int32_t)encoderPosition) * move_menu_scale;
@@ -1924,7 +1899,7 @@ void kill_screen(const char* lcd_msg) {
19241899
*/
19251900
#if HAS_LCD_CONTRAST
19261901
static void lcd_set_contrast() {
1927-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
1902+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
19281903
ENCODER_DIRECTION_NORMAL();
19291904
if (encoderPosition) {
19301905
set_lcd_contrast(lcd_contrast + encoderPosition);
@@ -1991,7 +1966,7 @@ void kill_screen(const char* lcd_msg) {
19911966
*/
19921967
void lcd_sdcard_menu() {
19931968
ENCODER_DIRECTION_MENUS();
1994-
if (lcdDrawUpdate == 0 && LCD_CLICKED == 0) return; // nothing to do (so don't thrash the SD card)
1969+
if (!lcdDrawUpdate && !lcd_clicked) return; // nothing to do (so don't thrash the SD card)
19951970
uint16_t fileCnt = card.getnrfilenames();
19961971
START_MENU();
19971972
MENU_BACK(MSG_MAIN);
@@ -2037,7 +2012,7 @@ void kill_screen(const char* lcd_msg) {
20372012
*
20382013
*/
20392014
static void lcd_info_stats_menu() {
2040-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
2015+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
20412016

20422017
char buffer[21];
20432018
printStatistics stats = print_job_timer.getStats();
@@ -2071,7 +2046,7 @@ void kill_screen(const char* lcd_msg) {
20712046
*
20722047
*/
20732048
static void lcd_info_thermistors_menu() {
2074-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
2049+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
20752050
START_SCREEN();
20762051
#define THERMISTOR_ID TEMP_SENSOR_0
20772052
#include "thermistornames.h"
@@ -2123,7 +2098,7 @@ void kill_screen(const char* lcd_msg) {
21232098
*
21242099
*/
21252100
static void lcd_info_board_menu() {
2126-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
2101+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
21272102
START_SCREEN();
21282103
STATIC_ITEM(BOARD_NAME, true, true); // MyPrinterController
21292104
STATIC_ITEM(MSG_INFO_BAUDRATE ": " STRINGIFY(BAUDRATE), true); // Baud: 250000
@@ -2144,7 +2119,7 @@ void kill_screen(const char* lcd_msg) {
21442119
*
21452120
*/
21462121
static void lcd_info_printer_menu() {
2147-
if (LCD_CLICKED) { lcd_goto_previous_menu(true); return; }
2122+
if (lcd_clicked) { return lcd_goto_previous_menu(true); }
21482123
START_SCREEN();
21492124
STATIC_ITEM(MSG_MARLIN, true, true); // Marlin
21502125
STATIC_ITEM(SHORT_BUILD_VERSION, true); // x.x.x-Branch
@@ -2334,16 +2309,15 @@ void kill_screen(const char* lcd_msg) {
23342309
#define menu_edit_type(_type, _name, _strFunc, scale) \
23352310
bool _menu_edit_ ## _name () { \
23362311
ENCODER_DIRECTION_NORMAL(); \
2337-
bool isClicked = LCD_CLICKED; \
23382312
if ((int32_t)encoderPosition < 0) encoderPosition = 0; \
23392313
if ((int32_t)encoderPosition > maxEditValue) encoderPosition = maxEditValue; \
23402314
if (lcdDrawUpdate) \
23412315
lcd_implementation_drawedit(editLabel, _strFunc(((_type)((int32_t)encoderPosition + minEditValue)) / scale)); \
2342-
if (isClicked) { \
2316+
if (lcd_clicked) { \
23432317
*((_type*)editValue) = ((_type)((int32_t)encoderPosition + minEditValue)) / scale; \
23442318
lcd_goto_previous_menu(true); \
23452319
} \
2346-
return isClicked; \
2320+
return lcd_clicked; \
23472321
} \
23482322
void menu_edit_ ## _name () { _menu_edit_ ## _name(); } \
23492323
void menu_edit_callback_ ## _name () { if (_menu_edit_ ## _name ()) (*callbackFunc)(); } \
@@ -2622,6 +2596,17 @@ void lcd_update() {
26222596

26232597
lcd_buttons_update();
26242598

2599+
// If the action button is pressed...
2600+
if (LCD_CLICKED) {
2601+
if (!wait_for_unclick) { // If not waiting for a debounce release:
2602+
wait_for_unclick = true; // Set debounce flag to ignore continous clicks
2603+
lcd_clicked = !wait_for_user; // Any click clears wait for user
2604+
wait_for_user = false; // Keep the click if not waiting for a user-click
2605+
lcd_quick_feedback(); // Always make a click sound
2606+
}
2607+
}
2608+
else if (wait_for_unclick) wait_for_unclick = false;
2609+
26252610
#if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
26262611

26272612
bool sd_status = IS_SD_INSERTED;
@@ -2695,7 +2680,7 @@ void lcd_update() {
26952680
#endif // REPRAPWORLD_KEYPAD
26962681

26972682
bool encoderPastThreshold = (abs(encoderDiff) >= ENCODER_PULSES_PER_STEP);
2698-
if (encoderPastThreshold || LCD_CLICKED) {
2683+
if (encoderPastThreshold || lcd_clicked) {
26992684
if (encoderPastThreshold) {
27002685
int32_t encoderMultiplier = 1;
27012686

@@ -2781,6 +2766,9 @@ void lcd_update() {
27812766
#else
27822767
CURRENTSCREEN();
27832768
#endif
2769+
2770+
// The previous click was used. Clear until the next one.
2771+
lcd_clicked = false;
27842772
}
27852773

27862774
#if ENABLED(ULTIPANEL)
@@ -3024,8 +3012,6 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; }
30243012
#endif
30253013
}
30263014

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

30313017
#endif // ULTRA_LCD

Marlin/ultralcd.h

Lines changed: 6 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;

0 commit comments

Comments
 (0)