Skip to content

Commit f3db255

Browse files
MoellerDiandroid444thinkyhead
committed
G-code Digital Buttons (#18389)
Co-Authored-By: android444 <[email protected]> Co-authored-by: Scott Lahteine <[email protected]>
1 parent 7eace53 commit f3db255

3 files changed

Lines changed: 284 additions & 1 deletion

File tree

Marlin/Configuration_adv.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,6 +3369,37 @@
33693369
#define GCODE_MACROS_SLOT_SIZE 50 // Maximum length of a single macro
33703370
#endif
33713371

3372+
/**
3373+
* User-defined buttons to run custom G-code.
3374+
* Up to 25 may be defined.
3375+
*/
3376+
//#define CUSTOM_USER_BUTTONS
3377+
#if ENABLED(CUSTOM_USER_BUTTONS)
3378+
//#define BUTTON1_PIN -1
3379+
#if PIN_EXISTS(BUTTON1_PIN)
3380+
#define BUTTON1_HIT_STATE LOW // State of the triggered button. NC=LOW. NO=HIGH.
3381+
#define BUTTON1_WHEN_PRINTING false // Button allowed to trigger during printing?
3382+
#define BUTTON1_GCODE "G28"
3383+
#define BUTTON1_DESC "Homing" // Optional string to set the LCD status
3384+
#endif
3385+
3386+
//#define BUTTON2_PIN -1
3387+
#if PIN_EXISTS(BUTTON2_PIN)
3388+
#define BUTTON2_HIT_STATE LOW
3389+
#define BUTTON2_WHEN_PRINTING false
3390+
#define BUTTON2_GCODE "M140 S" STRINGIFY(PREHEAT_1_TEMP_BED) "\nM104 S" STRINGIFY(PREHEAT_1_TEMP_HOTEND)
3391+
#define BUTTON2_DESC "Preheat for " PREHEAT_1_LABEL
3392+
#endif
3393+
3394+
//#define BUTTON3_PIN -1
3395+
#if PIN_EXISTS(BUTTON3_PIN)
3396+
#define BUTTON3_HIT_STATE LOW
3397+
#define BUTTON3_WHEN_PRINTING false
3398+
#define BUTTON3_GCODE "M140 S" STRINGIFY(PREHEAT_2_TEMP_BED) "\nM104 S" STRINGIFY(PREHEAT_2_TEMP_HOTEND)
3399+
#define BUTTON3_DESC "Preheat for " PREHEAT_2_LABEL
3400+
#endif
3401+
#endif
3402+
33723403
/**
33733404
* User-defined menu items to run custom G-code.
33743405
* Up to 25 may be defined, but the actual number is LCD-dependent.

Marlin/src/MarlinCore.cpp

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ void startOrResumeJob() {
406406
* - Check if CHDK_PIN needs to go LOW
407407
* - Check for KILL button held down
408408
* - Check for HOME button held down
409+
* - Check for CUSTOM USER button held down
409410
* - Check if cooling fan needs to be switched on
410411
* - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
411412
* - Pulse FET_SAFETY_PIN if it exists
@@ -498,6 +499,102 @@ inline void manage_inactivity(const bool ignore_stepper_queue=false) {
498499
}
499500
#endif
500501

502+
#if ENABLED(CUSTOM_USER_BUTTONS)
503+
// Handle a custom user button if defined
504+
const bool printer_not_busy = !printingIsActive();
505+
#define HAS_CUSTOM_USER_BUTTON(N) (PIN_EXISTS(BUTTON##N) && defined(BUTTON##N##_HIT_STATE) && defined(BUTTON##N##_GCODE) && defined(BUTTON##N##_DESC))
506+
#define CHECK_CUSTOM_USER_BUTTON(N) do{ \
507+
constexpr millis_t CUB_DEBOUNCE_DELAY_##N = 250UL; \
508+
static millis_t next_cub_ms_##N; \
509+
if (BUTTON##N##_HIT_STATE == READ(BUTTON##N##_PIN) \
510+
&& (ENABLED(BUTTON##N##_WHEN_PRINTING) || printer_not_busy)) { \
511+
const millis_t ms = millis(); \
512+
if (ELAPSED(ms, next_cub_ms_##N)) { \
513+
next_cub_ms_##N = ms + CUB_DEBOUNCE_DELAY_##N; \
514+
if (strlen(BUTTON##N##_DESC)) \
515+
LCD_MESSAGEPGM_P(PSTR(BUTTON##N##_DESC)); \
516+
queue.inject_P(PSTR(BUTTON##N##_GCODE)); \
517+
} \
518+
} \
519+
}while(0)
520+
521+
#if HAS_CUSTOM_USER_BUTTON(1)
522+
CHECK_CUSTOM_USER_BUTTON(1);
523+
#endif
524+
#if HAS_CUSTOM_USER_BUTTON(2)
525+
CHECK_CUSTOM_USER_BUTTON(2);
526+
#endif
527+
#if HAS_CUSTOM_USER_BUTTON(3)
528+
CHECK_CUSTOM_USER_BUTTON(3);
529+
#endif
530+
#if HAS_CUSTOM_USER_BUTTON(4)
531+
CHECK_CUSTOM_USER_BUTTON(4);
532+
#endif
533+
#if HAS_CUSTOM_USER_BUTTON(5)
534+
CHECK_CUSTOM_USER_BUTTON(5);
535+
#endif
536+
#if HAS_CUSTOM_USER_BUTTON(6)
537+
CHECK_CUSTOM_USER_BUTTON(6);
538+
#endif
539+
#if HAS_CUSTOM_USER_BUTTON(7)
540+
CHECK_CUSTOM_USER_BUTTON(7);
541+
#endif
542+
#if HAS_CUSTOM_USER_BUTTON(8)
543+
CHECK_CUSTOM_USER_BUTTON(8);
544+
#endif
545+
#if HAS_CUSTOM_USER_BUTTON(9)
546+
CHECK_CUSTOM_USER_BUTTON(9);
547+
#endif
548+
#if HAS_CUSTOM_USER_BUTTON(10)
549+
CHECK_CUSTOM_USER_BUTTON(10);
550+
#endif
551+
#if HAS_CUSTOM_USER_BUTTON(11)
552+
CHECK_CUSTOM_USER_BUTTON(11);
553+
#endif
554+
#if HAS_CUSTOM_USER_BUTTON(12)
555+
CHECK_CUSTOM_USER_BUTTON(12);
556+
#endif
557+
#if HAS_CUSTOM_USER_BUTTON(13)
558+
CHECK_CUSTOM_USER_BUTTON(13);
559+
#endif
560+
#if HAS_CUSTOM_USER_BUTTON(14)
561+
CHECK_CUSTOM_USER_BUTTON(14);
562+
#endif
563+
#if HAS_CUSTOM_USER_BUTTON(15)
564+
CHECK_CUSTOM_USER_BUTTON(15);
565+
#endif
566+
#if HAS_CUSTOM_USER_BUTTON(16)
567+
CHECK_CUSTOM_USER_BUTTON(16);
568+
#endif
569+
#if HAS_CUSTOM_USER_BUTTON(17)
570+
CHECK_CUSTOM_USER_BUTTON(17);
571+
#endif
572+
#if HAS_CUSTOM_USER_BUTTON(18)
573+
CHECK_CUSTOM_USER_BUTTON(18);
574+
#endif
575+
#if HAS_CUSTOM_USER_BUTTON(19)
576+
CHECK_CUSTOM_USER_BUTTON(19);
577+
#endif
578+
#if HAS_CUSTOM_USER_BUTTON(20)
579+
CHECK_CUSTOM_USER_BUTTON(20);
580+
#endif
581+
#if HAS_CUSTOM_USER_BUTTON(21)
582+
CHECK_CUSTOM_USER_BUTTON(21);
583+
#endif
584+
#if HAS_CUSTOM_USER_BUTTON(22)
585+
CHECK_CUSTOM_USER_BUTTON(22);
586+
#endif
587+
#if HAS_CUSTOM_USER_BUTTON(23)
588+
CHECK_CUSTOM_USER_BUTTON(23);
589+
#endif
590+
#if HAS_CUSTOM_USER_BUTTON(24)
591+
CHECK_CUSTOM_USER_BUTTON(24);
592+
#endif
593+
#if HAS_CUSTOM_USER_BUTTON(25)
594+
CHECK_CUSTOM_USER_BUTTON(25);
595+
#endif
596+
#endif
597+
501598
TERN_(USE_CONTROLLER_FAN, controllerFan.update()); // Check if fan should be turned on to cool stepper drivers down
502599

503600
TERN_(AUTO_POWER_CONTROL, powerManager.check());
@@ -857,7 +954,7 @@ inline void tmc_standby_setup() {
857954

858955
/**
859956
* Marlin entry-point: Set up before the program loop
860-
* - Set up the kill pin, filament runout, power hold
957+
* - Set up the kill pin, filament runout, power hold, custom user buttons
861958
* - Start the serial port
862959
* - Print startup messages and diagnostics
863960
* - Get EEPROM or default settings
@@ -1131,6 +1228,86 @@ void setup() {
11311228
SET_INPUT_PULLUP(HOME_PIN);
11321229
#endif
11331230

1231+
#if ENABLED(CUSTOM_USER_BUTTONS)
1232+
#define INIT_CUSTOM_USER_BUTTON_PIN(N) do{ SET_INPUT(BUTTON##N##_PIN); WRITE(BUTTON##N##_PIN, !BUTTON##N##_HIT_STATE); }while(0)
1233+
1234+
#if HAS_CUSTOM_USER_BUTTON(1)
1235+
INIT_CUSTOM_USER_BUTTON_PIN(1);
1236+
#endif
1237+
#if HAS_CUSTOM_USER_BUTTON(2)
1238+
INIT_CUSTOM_USER_BUTTON_PIN(2);
1239+
#endif
1240+
#if HAS_CUSTOM_USER_BUTTON(3)
1241+
INIT_CUSTOM_USER_BUTTON_PIN(3);
1242+
#endif
1243+
#if HAS_CUSTOM_USER_BUTTON(4)
1244+
INIT_CUSTOM_USER_BUTTON_PIN(4);
1245+
#endif
1246+
#if HAS_CUSTOM_USER_BUTTON(5)
1247+
INIT_CUSTOM_USER_BUTTON_PIN(5);
1248+
#endif
1249+
#if HAS_CUSTOM_USER_BUTTON(6)
1250+
INIT_CUSTOM_USER_BUTTON_PIN(6);
1251+
#endif
1252+
#if HAS_CUSTOM_USER_BUTTON(7)
1253+
INIT_CUSTOM_USER_BUTTON_PIN(7);
1254+
#endif
1255+
#if HAS_CUSTOM_USER_BUTTON(8)
1256+
INIT_CUSTOM_USER_BUTTON_PIN(8);
1257+
#endif
1258+
#if HAS_CUSTOM_USER_BUTTON(9)
1259+
INIT_CUSTOM_USER_BUTTON_PIN(9);
1260+
#endif
1261+
#if HAS_CUSTOM_USER_BUTTON(10)
1262+
INIT_CUSTOM_USER_BUTTON_PIN(10);
1263+
#endif
1264+
#if HAS_CUSTOM_USER_BUTTON(11)
1265+
INIT_CUSTOM_USER_BUTTON_PIN(11);
1266+
#endif
1267+
#if HAS_CUSTOM_USER_BUTTON(12)
1268+
INIT_CUSTOM_USER_BUTTON_PIN(12);
1269+
#endif
1270+
#if HAS_CUSTOM_USER_BUTTON(13)
1271+
INIT_CUSTOM_USER_BUTTON_PIN(13);
1272+
#endif
1273+
#if HAS_CUSTOM_USER_BUTTON(14)
1274+
INIT_CUSTOM_USER_BUTTON_PIN(14);
1275+
#endif
1276+
#if HAS_CUSTOM_USER_BUTTON(15)
1277+
INIT_CUSTOM_USER_BUTTON_PIN(15);
1278+
#endif
1279+
#if HAS_CUSTOM_USER_BUTTON(16)
1280+
INIT_CUSTOM_USER_BUTTON_PIN(16);
1281+
#endif
1282+
#if HAS_CUSTOM_USER_BUTTON(17)
1283+
INIT_CUSTOM_USER_BUTTON_PIN(17);
1284+
#endif
1285+
#if HAS_CUSTOM_USER_BUTTON(18)
1286+
INIT_CUSTOM_USER_BUTTON_PIN(18);
1287+
#endif
1288+
#if HAS_CUSTOM_USER_BUTTON(19)
1289+
INIT_CUSTOM_USER_BUTTON_PIN(19);
1290+
#endif
1291+
#if HAS_CUSTOM_USER_BUTTON(20)
1292+
INIT_CUSTOM_USER_BUTTON_PIN(20);
1293+
#endif
1294+
#if HAS_CUSTOM_USER_BUTTON(21)
1295+
INIT_CUSTOM_USER_BUTTON_PIN(21);
1296+
#endif
1297+
#if HAS_CUSTOM_USER_BUTTON(22)
1298+
INIT_CUSTOM_USER_BUTTON_PIN(22);
1299+
#endif
1300+
#if HAS_CUSTOM_USER_BUTTON(23)
1301+
INIT_CUSTOM_USER_BUTTON_PIN(23);
1302+
#endif
1303+
#if HAS_CUSTOM_USER_BUTTON(24)
1304+
INIT_CUSTOM_USER_BUTTON_PIN(24);
1305+
#endif
1306+
#if HAS_CUSTOM_USER_BUTTON(25)
1307+
INIT_CUSTOM_USER_BUTTON_PIN(25);
1308+
#endif
1309+
#endif
1310+
11341311
#if PIN_EXISTS(STAT_LED_RED)
11351312
OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
11361313
#endif

Marlin/src/pins/pinsDebug_list.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,81 @@
183183
#if PIN_EXISTS(JOY_EN)
184184
REPORT_NAME_DIGITAL(__LINE__, JOY_EN_PIN)
185185
#endif
186+
#if PIN_EXISTS(BUTTON1)
187+
REPORT_NAME_DIGITAL(__LINE__, BUTTON1_PIN)
188+
#endif
189+
#if PIN_EXISTS(BUTTON2)
190+
REPORT_NAME_DIGITAL(__LINE__, BUTTON2_PIN)
191+
#endif
192+
#if PIN_EXISTS(BUTTON3)
193+
REPORT_NAME_DIGITAL(__LINE__, BUTTON3_PIN)
194+
#endif
195+
#if PIN_EXISTS(BUTTON4)
196+
REPORT_NAME_DIGITAL(__LINE__, BUTTON4_PIN)
197+
#endif
198+
#if PIN_EXISTS(BUTTON5)
199+
REPORT_NAME_DIGITAL(__LINE__, BUTTON5_PIN)
200+
#endif
201+
#if PIN_EXISTS(BUTTON6)
202+
REPORT_NAME_DIGITAL(__LINE__, BUTTON6_PIN)
203+
#endif
204+
#if PIN_EXISTS(BUTTON7)
205+
REPORT_NAME_DIGITAL(__LINE__, BUTTON7_PIN)
206+
#endif
207+
#if PIN_EXISTS(BUTTON8)
208+
REPORT_NAME_DIGITAL(__LINE__, BUTTON8_PIN)
209+
#endif
210+
#if PIN_EXISTS(BUTTON9)
211+
REPORT_NAME_DIGITAL(__LINE__, BUTTON9_PIN)
212+
#endif
213+
#if PIN_EXISTS(BUTTON10)
214+
REPORT_NAME_DIGITAL(__LINE__, BUTTON10_PIN)
215+
#endif
216+
#if PIN_EXISTS(BUTTON11)
217+
REPORT_NAME_DIGITAL(__LINE__, BUTTON11_PIN)
218+
#endif
219+
#if PIN_EXISTS(BUTTON12)
220+
REPORT_NAME_DIGITAL(__LINE__, BUTTON12_PIN)
221+
#endif
222+
#if PIN_EXISTS(BUTTON13)
223+
REPORT_NAME_DIGITAL(__LINE__, BUTTON13_PIN)
224+
#endif
225+
#if PIN_EXISTS(BUTTON14)
226+
REPORT_NAME_DIGITAL(__LINE__, BUTTON14_PIN)
227+
#endif
228+
#if PIN_EXISTS(BUTTON15)
229+
REPORT_NAME_DIGITAL(__LINE__, BUTTON15_PIN)
230+
#endif
231+
#if PIN_EXISTS(BUTTON16)
232+
REPORT_NAME_DIGITAL(__LINE__, BUTTON16_PIN)
233+
#endif
234+
#if PIN_EXISTS(BUTTON17)
235+
REPORT_NAME_DIGITAL(__LINE__, BUTTON17_PIN)
236+
#endif
237+
#if PIN_EXISTS(BUTTON18)
238+
REPORT_NAME_DIGITAL(__LINE__, BUTTON18_PIN)
239+
#endif
240+
#if PIN_EXISTS(BUTTON19)
241+
REPORT_NAME_DIGITAL(__LINE__, BUTTON19_PIN)
242+
#endif
243+
#if PIN_EXISTS(BUTTON20)
244+
REPORT_NAME_DIGITAL(__LINE__, BUTTON20_PIN)
245+
#endif
246+
#if PIN_EXISTS(BUTTON21)
247+
REPORT_NAME_DIGITAL(__LINE__, BUTTON21_PIN)
248+
#endif
249+
#if PIN_EXISTS(BUTTON22)
250+
REPORT_NAME_DIGITAL(__LINE__, BUTTON22_PIN)
251+
#endif
252+
#if PIN_EXISTS(BUTTON23)
253+
REPORT_NAME_DIGITAL(__LINE__, BUTTON23_PIN)
254+
#endif
255+
#if PIN_EXISTS(BUTTON24)
256+
REPORT_NAME_DIGITAL(__LINE__, BUTTON24_PIN)
257+
#endif
258+
#if PIN_EXISTS(BUTTON25)
259+
REPORT_NAME_DIGITAL(__LINE__, BUTTON25_PIN)
260+
#endif
186261
#if PIN_EXISTS(CASE_LIGHT)
187262
REPORT_NAME_DIGITAL(__LINE__, CASE_LIGHT_PIN)
188263
#endif

0 commit comments

Comments
 (0)