My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gcode.cpp 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * gcode.cpp - Temporary container for all gcode handlers
  24. * Most will migrate to classes, by feature.
  25. */
  26. #include "gcode.h"
  27. GcodeSuite gcode;
  28. #include "parser.h"
  29. #include "queue.h"
  30. #include "../module/motion.h"
  31. #if ENABLED(PRINTCOUNTER)
  32. #include "../module/printcounter.h"
  33. #endif
  34. #include "../Marlin.h" // for idle() and suspend_auto_report
  35. uint8_t GcodeSuite::target_extruder;
  36. millis_t GcodeSuite::previous_move_ms;
  37. bool GcodeSuite::axis_relative_modes[] = AXIS_RELATIVE_MODES;
  38. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  39. GcodeSuite::MarlinBusyState GcodeSuite::busy_state = NOT_BUSY;
  40. uint8_t GcodeSuite::host_keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
  41. #endif
  42. #if ENABLED(CNC_WORKSPACE_PLANES)
  43. GcodeSuite::WorkspacePlane GcodeSuite::workspace_plane = PLANE_XY;
  44. #endif
  45. #if ENABLED(CNC_COORDINATE_SYSTEMS)
  46. int8_t GcodeSuite::active_coordinate_system = -1; // machine space
  47. float GcodeSuite::coordinate_system[MAX_COORDINATE_SYSTEMS][XYZ];
  48. #endif
  49. /**
  50. * Set target_extruder from the T parameter or the active_extruder
  51. *
  52. * Returns TRUE if the target is invalid
  53. */
  54. bool GcodeSuite::get_target_extruder_from_command() {
  55. if (parser.seenval('T')) {
  56. const int8_t e = parser.value_byte();
  57. if (e >= EXTRUDERS) {
  58. SERIAL_ECHO_START();
  59. SERIAL_CHAR('M');
  60. SERIAL_ECHO(parser.codenum);
  61. SERIAL_ECHOLNPAIR(" " MSG_INVALID_EXTRUDER " ", e);
  62. return true;
  63. }
  64. target_extruder = e;
  65. }
  66. else
  67. target_extruder = active_extruder;
  68. return false;
  69. }
  70. /**
  71. * Set XYZE destination and feedrate from the current GCode command
  72. *
  73. * - Set destination from included axis codes
  74. * - Set to current for missing axis codes
  75. * - Set the feedrate, if included
  76. */
  77. void GcodeSuite::get_destination_from_command() {
  78. LOOP_XYZE(i) {
  79. if (parser.seen(axis_codes[i])) {
  80. const float v = parser.value_axis_units((AxisEnum)i);
  81. destination[i] = (axis_relative_modes[i] || relative_mode)
  82. ? current_position[i] + v
  83. : (i == E_AXIS) ? v : LOGICAL_TO_NATIVE(v, i);
  84. }
  85. else
  86. destination[i] = current_position[i];
  87. }
  88. if (parser.linearval('F') > 0)
  89. feedrate_mm_s = MMM_TO_MMS(parser.value_feedrate());
  90. #if ENABLED(PRINTCOUNTER)
  91. if (!DEBUGGING(DRYRUN))
  92. print_job_timer.incFilamentUsed(destination[E_AXIS] - current_position[E_AXIS]);
  93. #endif
  94. // Get ABCDHI mixing factors
  95. #if ENABLED(MIXING_EXTRUDER) && ENABLED(DIRECT_MIXING_IN_G1)
  96. M165();
  97. #endif
  98. }
  99. /**
  100. * Dwell waits immediately. It does not synchronize. Use M400 instead of G4
  101. */
  102. void GcodeSuite::dwell(millis_t time) {
  103. time += millis();
  104. while (PENDING(millis(), time)) idle();
  105. }
  106. /**
  107. * When G29_RETRY_AND_RECOVER is enabled, call G29() in
  108. * a loop with recovery and retry handling.
  109. */
  110. #if HAS_LEVELING && ENABLED(G29_RETRY_AND_RECOVER)
  111. #ifndef G29_MAX_RETRIES
  112. #define G29_MAX_RETRIES 0
  113. #endif
  114. void GcodeSuite::G29_with_retry() {
  115. uint8_t retries = G29_MAX_RETRIES;
  116. while (G29()) { // G29 should return true for failed probes ONLY
  117. if (retries--) {
  118. #ifdef G29_ACTION_ON_RECOVER
  119. SERIAL_ECHOLNPGM("//action:" G29_ACTION_ON_RECOVER);
  120. #endif
  121. #ifdef G29_RECOVER_COMMANDS
  122. process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
  123. #endif
  124. }
  125. else {
  126. #ifdef G29_FAILURE_COMMANDS
  127. process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
  128. #endif
  129. #ifdef G29_ACTION_ON_FAILURE
  130. SERIAL_ECHOLNPGM("//action:" G29_ACTION_ON_FAILURE);
  131. #endif
  132. #if ENABLED(G29_HALT_ON_FAILURE)
  133. kill(PSTR(MSG_ERR_PROBING_FAILED));
  134. #endif
  135. return;
  136. }
  137. }
  138. #ifdef G29_SUCCESS_COMMANDS
  139. process_subcommands_now_P(PSTR(G29_SUCCESS_COMMANDS));
  140. #endif
  141. }
  142. #endif // HAS_LEVELING && G29_RETRY_AND_RECOVER
  143. //
  144. // Placeholders for non-migrated codes
  145. //
  146. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  147. extern void M100_dump_routine(PGM_P const title, const char *start, const char *end);
  148. #endif
  149. /**
  150. * Process the parsed command and dispatch it to its handler
  151. */
  152. void GcodeSuite::process_parsed_command(
  153. #if USE_EXECUTE_COMMANDS_IMMEDIATE
  154. const bool no_ok
  155. #endif
  156. ) {
  157. KEEPALIVE_STATE(IN_HANDLER);
  158. // Handle a known G, M, or T
  159. switch (parser.command_letter) {
  160. case 'G': switch (parser.codenum) {
  161. case 0: case 1: G0_G1( // G0: Fast Move, G1: Linear Move
  162. #if IS_SCARA || defined(G0_FEEDRATE)
  163. parser.codenum == 0
  164. #endif
  165. );
  166. break;
  167. #if ENABLED(ARC_SUPPORT) && DISABLED(SCARA)
  168. case 2: case 3: G2_G3(parser.codenum == 2); break; // G2: CW ARC, G3: CCW ARC
  169. #endif
  170. case 4: G4(); break; // G4: Dwell
  171. #if ENABLED(BEZIER_CURVE_SUPPORT)
  172. case 5: G5(); break; // G5: Cubic B_spline
  173. #endif
  174. #if ENABLED(FWRETRACT)
  175. case 10: G10(); break; // G10: Retract / Swap Retract
  176. case 11: G11(); break; // G11: Recover / Swap Recover
  177. #endif
  178. #if ENABLED(NOZZLE_CLEAN_FEATURE)
  179. case 12: G12(); break; // G12: Nozzle Clean
  180. #endif
  181. #if ENABLED(CNC_WORKSPACE_PLANES)
  182. case 17: G17(); break; // G17: Select Plane XY
  183. case 18: G18(); break; // G18: Select Plane ZX
  184. case 19: G19(); break; // G19: Select Plane YZ
  185. #endif
  186. #if ENABLED(INCH_MODE_SUPPORT)
  187. case 20: G20(); break; // G20: Inch Mode
  188. case 21: G21(); break; // G21: MM Mode
  189. #endif
  190. #if ENABLED(G26_MESH_VALIDATION)
  191. case 26: G26(); break; // G26: Mesh Validation Pattern generation
  192. #endif
  193. #if ENABLED(NOZZLE_PARK_FEATURE)
  194. case 27: G27(); break; // G27: Nozzle Park
  195. #endif
  196. case 28: G28(false); break; // G28: Home all axes, one at a time
  197. #if HAS_LEVELING
  198. case 29: // G29: Bed leveling calibration
  199. #if ENABLED(G29_RETRY_AND_RECOVER)
  200. G29_with_retry();
  201. #else
  202. G29();
  203. #endif
  204. break;
  205. #endif // HAS_LEVELING
  206. #if HAS_BED_PROBE
  207. case 30: G30(); break; // G30: Single Z probe
  208. #if ENABLED(Z_PROBE_SLED)
  209. case 31: G31(); break; // G31: dock the sled
  210. case 32: G32(); break; // G32: undock the sled
  211. #endif
  212. #endif
  213. #if ENABLED(DELTA_AUTO_CALIBRATION)
  214. case 33: G33(); break; // G33: Delta Auto-Calibration
  215. #endif
  216. #if ENABLED(Z_STEPPER_AUTO_ALIGN)
  217. case 34: G34(); break; // G34: Z Stepper automatic alignment using probe
  218. #endif
  219. #if ENABLED(G38_PROBE_TARGET)
  220. case 38: // G38.2 & G38.3: Probe towards target
  221. if (parser.subcode == 2 || parser.subcode == 3)
  222. G38(parser.subcode == 2);
  223. break;
  224. #endif
  225. #if ENABLED(GCODE_MOTION_MODES)
  226. case 80: G80(); break; // G80: Reset the current motion mode
  227. #endif
  228. case 90: relative_mode = false; break; // G90: Relative Mode
  229. case 91: relative_mode = true; break; // G91: Absolute Mode
  230. case 92: G92(); break; // G92: Set current axis position(s)
  231. #if HAS_MESH
  232. case 42: G42(); break; // G42: Coordinated move to a mesh point
  233. #endif
  234. #if ENABLED(DEBUG_GCODE_PARSER)
  235. case 800: parser.debug(); break; // G800: GCode Parser Test for G
  236. #endif
  237. default: parser.unknown_command_error(); break;
  238. }
  239. break;
  240. case 'M': switch (parser.codenum) {
  241. #if HAS_RESUME_CONTINUE
  242. case 0: // M0: Unconditional stop - Wait for user button press on LCD
  243. case 1: M0_M1(); break; // M1: Conditional stop - Wait for user button press on LCD
  244. #endif
  245. #if ENABLED(SPINDLE_LASER_ENABLE)
  246. case 3: M3_M4(true ); break; // M3: turn spindle/laser on, set laser/spindle power/speed, set rotation direction CW
  247. case 4: M3_M4(false); break; // M4: turn spindle/laser on, set laser/spindle power/speed, set rotation direction CCW
  248. case 5: M5(); break; // M5 - turn spindle/laser off
  249. #endif
  250. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  251. case 12: M12(); break; // M12: Synchronize and optionally force a CLC set
  252. #endif
  253. case 17: M17(); break; // M17: Enable all stepper motors
  254. #if ENABLED(SDSUPPORT)
  255. case 20: M20(); break; // M20: list SD card
  256. case 21: M21(); break; // M21: init SD card
  257. case 22: M22(); break; // M22: release SD card
  258. case 23: M23(); break; // M23: Select file
  259. case 24: M24(); break; // M24: Start SD print
  260. case 25: M25(); break; // M25: Pause SD print
  261. case 26: M26(); break; // M26: Set SD index
  262. case 27: M27(); break; // M27: Get SD status
  263. case 28: M28(); break; // M28: Start SD write
  264. case 29: M29(); break; // M29: Stop SD write
  265. case 30: M30(); break; // M30 <filename> Delete File
  266. case 32: M32(); break; // M32: Select file and start SD print
  267. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  268. case 33: M33(); break; // M33: Get the long full path to a file or folder
  269. #endif
  270. #if ENABLED(SDCARD_SORT_ALPHA) && ENABLED(SDSORT_GCODE)
  271. case 34: M34(); break; // M34: Set SD card sorting options
  272. #endif
  273. case 928: M928(); break; // M928: Start SD write
  274. #endif // SDSUPPORT
  275. case 31: M31(); break; // M31: Report time since the start of SD print or last M109
  276. case 42: M42(); break; // M42: Change pin state
  277. #if ENABLED(PINS_DEBUGGING)
  278. case 43: M43(); break; // M43: Read pin state
  279. #endif
  280. #if ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST)
  281. case 48: M48(); break; // M48: Z probe repeatability test
  282. #endif
  283. #if ENABLED(G26_MESH_VALIDATION)
  284. case 49: M49(); break; // M49: Turn on or off G26 debug flag for verbose output
  285. #endif
  286. #if ENABLED(ULTRA_LCD) && ENABLED(LCD_SET_PROGRESS_MANUALLY)
  287. case 73: M73(); break; // M73: Set progress percentage (for display on LCD)
  288. #endif
  289. case 75: M75(); break; // M75: Start print timer
  290. case 76: M76(); break; // M76: Pause print timer
  291. case 77: M77(); break; // M77: Stop print timer
  292. #if ENABLED(PRINTCOUNTER)
  293. case 78: M78(); break; // M78: Show print statistics
  294. #endif
  295. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  296. case 100: M100(); break; // M100: Free Memory Report
  297. #endif
  298. case 104: M104(); break; // M104: Set hot end temperature
  299. case 109: M109(); break; // M109: Wait for hotend temperature to reach target
  300. case 110: M110(); break; // M110: Set Current Line Number
  301. case 111: M111(); break; // M111: Set debug level
  302. #if DISABLED(EMERGENCY_PARSER)
  303. case 108: M108(); break; // M108: Cancel Waiting
  304. case 112: M112(); break; // M112: Emergency Stop
  305. case 410: M410(); break; // M410: Quickstop - Abort all the planned moves.
  306. #else
  307. case 108: case 112: case 410: break;
  308. #endif
  309. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  310. case 113: M113(); break; // M113: Set Host Keepalive interval
  311. #endif
  312. #if HAS_HEATED_BED
  313. case 140: M140(); break; // M140: Set bed temperature
  314. case 190: M190(); break; // M190: Wait for bed temperature to reach target
  315. #endif
  316. case 105: M105(); KEEPALIVE_STATE(NOT_BUSY); return; // M105: Report Temperatures (and say "ok")
  317. #if ENABLED(AUTO_REPORT_TEMPERATURES) && HAS_TEMP_SENSOR
  318. case 155: M155(); break; // M155: Set temperature auto-report interval
  319. #endif
  320. #if FAN_COUNT > 0
  321. case 106: M106(); break; // M106: Fan On
  322. case 107: M107(); break; // M107: Fan Off
  323. #endif
  324. #if ENABLED(PARK_HEAD_ON_PAUSE)
  325. case 125: M125(); break; // M125: Store current position and move to filament change position
  326. #endif
  327. #if ENABLED(BARICUDA)
  328. // PWM for HEATER_1_PIN
  329. #if HAS_HEATER_1
  330. case 126: M126(); break; // M126: valve open
  331. case 127: M127(); break; // M127: valve closed
  332. #endif
  333. // PWM for HEATER_2_PIN
  334. #if HAS_HEATER_2
  335. case 128: M128(); break; // M128: valve open
  336. case 129: M129(); break; // M129: valve closed
  337. #endif
  338. #endif // BARICUDA
  339. #if HAS_POWER_SWITCH
  340. case 80: M80(); break; // M80: Turn on Power Supply
  341. #endif
  342. case 81: M81(); break; // M81: Turn off Power, including Power Supply, if possible
  343. case 82: M82(); break; // M82: Set E axis normal mode (same as other axes)
  344. case 83: M83(); break; // M83: Set E axis relative mode
  345. case 18: case 84: M18_M84(); break; // M18/M84: Disable Steppers / Set Timeout
  346. case 85: M85(); break; // M85: Set inactivity stepper shutdown timeout
  347. case 92: M92(); break; // M92: Set the steps-per-unit for one or more axes
  348. case 114: M114(); break; // M114: Report current position
  349. case 115: M115(); break; // M115: Report capabilities
  350. case 117: M117(); break; // M117: Set LCD message text, if possible
  351. case 118: M118(); break; // M118: Display a message in the host console
  352. case 119: M119(); break; // M119: Report endstop states
  353. case 120: M120(); break; // M120: Enable endstops
  354. case 121: M121(); break; // M121: Disable endstops
  355. #if HAS_LCD_MENU
  356. case 145: M145(); break; // M145: Set material heatup parameters
  357. #endif
  358. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  359. case 149: M149(); break; // M149: Set temperature units
  360. #endif
  361. #if HAS_COLOR_LEDS
  362. case 150: M150(); break; // M150: Set Status LED Color
  363. #endif
  364. #if ENABLED(MIXING_EXTRUDER)
  365. case 163: M163(); break; // M163: Set a component weight for mixing extruder
  366. case 164: M164(); break; // M164: Save current mix as a virtual extruder
  367. #if ENABLED(DIRECT_MIXING_IN_G1)
  368. case 165: M165(); break; // M165: Set multiple mix weights
  369. #endif
  370. #endif
  371. #if DISABLED(NO_VOLUMETRICS)
  372. case 200: M200(); break; // M200: Set filament diameter, E to cubic units
  373. #endif
  374. case 201: M201(); break; // M201: Set max acceleration for print moves (units/s^2)
  375. #if 0
  376. case 202: M202(); break; // M202: Not used for Sprinter/grbl gen6
  377. #endif
  378. case 203: M203(); break; // M203: Set max feedrate (units/sec)
  379. case 204: M204(); break; // M204: Set acceleration
  380. case 205: M205(); break; // M205: Set advanced settings
  381. #if HAS_M206_COMMAND
  382. case 206: M206(); break; // M206: Set home offsets
  383. #endif
  384. #if ENABLED(DELTA)
  385. case 665: M665(); break; // M665: Set delta configurations
  386. #endif
  387. #if ENABLED(DELTA) || ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  388. case 666: M666(); break; // M666: Set delta or dual endstop adjustment
  389. #endif
  390. #if ENABLED(FWRETRACT)
  391. case 207: M207(); break; // M207: Set Retract Length, Feedrate, and Z lift
  392. case 208: M208(); break; // M208: Set Recover (unretract) Additional Length and Feedrate
  393. #if ENABLED(FWRETRACT_AUTORETRACT)
  394. case 209:
  395. if (MIN_AUTORETRACT <= MAX_AUTORETRACT) M209(); // M209: Turn Automatic Retract Detection on/off
  396. break;
  397. #endif
  398. #endif
  399. #if HAS_SOFTWARE_ENDSTOPS
  400. case 211: M211(); break; // M211: Enable, Disable, and/or Report software endstops
  401. #endif
  402. #if EXTRUDERS > 1
  403. case 217: M217(); break; // M217: Set filament swap parameters
  404. #endif
  405. #if HOTENDS > 1
  406. case 218: M218(); break; // M218: Set a tool offset
  407. #endif
  408. case 220: M220(); break; // M220: Set Feedrate Percentage: S<percent> ("FR" on your LCD)
  409. case 221: M221(); break; // M221: Set Flow Percentage
  410. case 226: M226(); break; // M226: Wait until a pin reaches a state
  411. #if HAS_SERVOS
  412. case 280: M280(); break; // M280: Set servo position absolute
  413. #if ENABLED(EDITABLE_SERVO_ANGLES)
  414. case 281: M281(); break; // M281: Set servo angles
  415. #endif
  416. #endif
  417. #if ENABLED(BABYSTEPPING)
  418. case 290: M290(); break; // M290: Babystepping
  419. #endif
  420. #if HAS_BUZZER
  421. case 300: M300(); break; // M300: Play beep tone
  422. #endif
  423. #if ENABLED(PIDTEMP)
  424. case 301: M301(); break; // M301: Set hotend PID parameters
  425. #endif
  426. #if ENABLED(PIDTEMPBED)
  427. case 304: M304(); break; // M304: Set bed PID parameters
  428. #endif
  429. #if defined(CHDK) || HAS_PHOTOGRAPH
  430. case 240: M240(); break; // M240: Trigger a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  431. #endif
  432. #if HAS_LCD_CONTRAST
  433. case 250: M250(); break; // M250: Set LCD contrast
  434. #endif
  435. #if ENABLED(EXPERIMENTAL_I2CBUS)
  436. case 260: M260(); break; // M260: Send data to an i2c slave
  437. case 261: M261(); break; // M261: Request data from an i2c slave
  438. #endif
  439. #if ENABLED(PREVENT_COLD_EXTRUSION)
  440. case 302: M302(); break; // M302: Allow cold extrudes (set the minimum extrude temperature)
  441. #endif
  442. case 303: M303(); break; // M303: PID autotune
  443. #if ENABLED(MORGAN_SCARA)
  444. case 360: if (M360()) return; break; // M360: SCARA Theta pos1
  445. case 361: if (M361()) return; break; // M361: SCARA Theta pos2
  446. case 362: if (M362()) return; break; // M362: SCARA Psi pos1
  447. case 363: if (M363()) return; break; // M363: SCARA Psi pos2
  448. case 364: if (M364()) return; break; // M364: SCARA Psi pos3 (90 deg to Theta)
  449. #endif
  450. #if ENABLED(EXT_SOLENOID) || ENABLED(MANUAL_SOLENOID_CONTROL)
  451. case 380: M380(); break; // M380: Activate solenoid on active (or specified) extruder
  452. case 381: M381(); break; // M381: Disable all solenoids
  453. #endif
  454. case 400: M400(); break; // M400: Finish all moves
  455. #if HAS_BED_PROBE
  456. case 401: M401(); break; // M401: Deploy probe
  457. case 402: M402(); break; // M402: Stow probe
  458. #endif
  459. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  460. case 404: M404(); break; // M404: Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or display nominal filament width
  461. case 405: M405(); break; // M405: Turn on filament sensor for control
  462. case 406: M406(); break; // M406: Turn off filament sensor for control
  463. case 407: M407(); break; // M407: Display measured filament diameter
  464. #endif
  465. #if HAS_LEVELING
  466. case 420: M420(); break; // M420: Enable/Disable Bed Leveling
  467. #endif
  468. #if HAS_MESH
  469. case 421: M421(); break; // M421: Set a Mesh Bed Leveling Z coordinate
  470. #endif
  471. #if HAS_M206_COMMAND
  472. case 428: M428(); break; // M428: Apply current_position to home_offset
  473. #endif
  474. case 500: M500(); break; // M500: Store settings in EEPROM
  475. case 501: M501(); break; // M501: Read settings from EEPROM
  476. case 502: M502(); break; // M502: Revert to default settings
  477. #if DISABLED(DISABLE_M503)
  478. case 503: M503(); break; // M503: print settings currently in memory
  479. #endif
  480. #if ENABLED(EEPROM_SETTINGS)
  481. case 504: M504(); break; // M504: Validate EEPROM contents
  482. #endif
  483. #if ENABLED(SDSUPPORT)
  484. case 524: M524(); break; // M524: Abort the current SD print job
  485. #endif
  486. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  487. case 540: M540(); break; // M540: Set abort on endstop hit for SD printing
  488. #endif
  489. #if HAS_BED_PROBE
  490. case 851: M851(); break; // M851: Set Z Probe Z Offset
  491. #endif
  492. #if ENABLED(SKEW_CORRECTION_GCODE)
  493. case 852: M852(); break; // M852: Set Skew factors
  494. #endif
  495. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  496. case 600: M600(); break; // M600: Pause for Filament Change
  497. case 603: M603(); break; // M603: Configure Filament Change
  498. #endif
  499. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  500. case 605: M605(); break; // M605: Set Dual X Carriage movement mode
  501. #endif
  502. #if ENABLED(FILAMENT_LOAD_UNLOAD_GCODES)
  503. case 701: M701(); break; // M701: Load Filament
  504. case 702: M702(); break; // M702: Unload Filament
  505. #endif
  506. #if ENABLED(MAX7219_GCODE)
  507. case 7219: M7219(); break; // M7219: Set LEDs, columns, and rows
  508. #endif
  509. #if ENABLED(LIN_ADVANCE)
  510. case 900: M900(); break; // M900: Set advance K factor.
  511. #endif
  512. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM || ENABLED(DIGIPOT_I2C) || ENABLED(DAC_STEPPER_CURRENT)
  513. case 907: M907(); break; // M907: Set digital trimpot motor current using axis codes.
  514. #if HAS_DIGIPOTSS || ENABLED(DAC_STEPPER_CURRENT)
  515. case 908: M908(); break; // M908: Control digital trimpot directly.
  516. #if ENABLED(DAC_STEPPER_CURRENT)
  517. case 909: M909(); break; // M909: Print digipot/DAC current value
  518. case 910: M910(); break; // M910: Commit digipot/DAC value to external EEPROM
  519. #endif
  520. #endif
  521. #endif
  522. #if HAS_TRINAMIC
  523. #if ENABLED(TMC_DEBUG)
  524. case 122: M122(); break;
  525. #endif
  526. case 906: M906(); break; // M906: Set motor current in milliamps using axis codes X, Y, Z, E
  527. #if ENABLED(MONITOR_DRIVER_STATUS)
  528. case 911: M911(); break; // M911: Report TMC2130 prewarn triggered flags
  529. case 912: M912(); break; // M912: Clear TMC2130 prewarn triggered flags
  530. #endif
  531. #if ENABLED(HYBRID_THRESHOLD)
  532. case 913: M913(); break; // M913: Set HYBRID_THRESHOLD speed.
  533. #endif
  534. #if USE_SENSORLESS
  535. case 914: M914(); break; // M914: Set StallGuard sensitivity.
  536. #endif
  537. #if ENABLED(TMC_Z_CALIBRATION)
  538. case 915: M915(); break; // M915: TMC Z axis calibration.
  539. #endif
  540. #endif
  541. #if HAS_MICROSTEPS
  542. case 350: M350(); break; // M350: Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  543. case 351: M351(); break; // M351: Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  544. #endif
  545. case 355: M355(); break; // M355: Set case light brightness
  546. #if ENABLED(DEBUG_GCODE_PARSER)
  547. case 800: parser.debug(); break; // M800: GCode Parser Test for M
  548. #endif
  549. #if ENABLED(I2C_POSITION_ENCODERS)
  550. case 860: M860(); break; // M860: Report encoder module position
  551. case 861: M861(); break; // M861: Report encoder module status
  552. case 862: M862(); break; // M862: Perform axis test
  553. case 863: M863(); break; // M863: Calibrate steps/mm
  554. case 864: M864(); break; // M864: Change module address
  555. case 865: M865(); break; // M865: Check module firmware version
  556. case 866: M866(); break; // M866: Report axis error count
  557. case 867: M867(); break; // M867: Toggle error correction
  558. case 868: M868(); break; // M868: Set error correction threshold
  559. case 869: M869(); break; // M869: Report axis error
  560. #endif
  561. #if ENABLED(Z_STEPPER_AUTO_ALIGN)
  562. case 422: M422(); break; // M422: Set Z Stepper automatic alignment position using probe
  563. #endif
  564. case 999: M999(); break; // M999: Restart after being Stopped
  565. default: parser.unknown_command_error(); break;
  566. }
  567. break;
  568. case 'T': T(parser.codenum); break; // Tn: Tool Change
  569. default: parser.unknown_command_error();
  570. }
  571. KEEPALIVE_STATE(NOT_BUSY);
  572. #if USE_EXECUTE_COMMANDS_IMMEDIATE
  573. if (!no_ok)
  574. #endif
  575. ok_to_send();
  576. }
  577. /**
  578. * Process a single command and dispatch it to its handler
  579. * This is called from the main loop()
  580. */
  581. void GcodeSuite::process_next_command() {
  582. char * const current_command = command_queue[cmd_queue_index_r];
  583. if (DEBUGGING(ECHO)) {
  584. SERIAL_ECHO_START();
  585. SERIAL_ECHOLN(current_command);
  586. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  587. SERIAL_ECHOPAIR("slot:", cmd_queue_index_r);
  588. M100_dump_routine(PSTR(" Command Queue:"), (const char*)command_queue, (const char*)(command_queue + sizeof(command_queue)));
  589. #endif
  590. }
  591. // Parse the next command in the queue
  592. parser.parse(current_command);
  593. process_parsed_command();
  594. }
  595. #if USE_EXECUTE_COMMANDS_IMMEDIATE
  596. /**
  597. * Run a series of commands, bypassing the command queue to allow
  598. * G-code "macros" to be called from within other G-code handlers.
  599. */
  600. void GcodeSuite::process_subcommands_now_P(PGM_P pgcode) {
  601. char * const saved_cmd = parser.command_ptr; // Save the parser state
  602. for (;;) {
  603. PGM_P const delim = strchr_P(pgcode, '\n'); // Get address of next newline
  604. const size_t len = delim ? delim - pgcode : strlen_P(pgcode); // Get the command length
  605. char cmd[len + 1]; // Allocate a stack buffer
  606. strncpy_P(cmd, pgcode, len); // Copy the command to the stack
  607. cmd[len] = '\0'; // End with a nul
  608. parser.parse(cmd); // Parse the command
  609. process_parsed_command(true); // Process it
  610. if (!delim) break; // Last command?
  611. pgcode = delim + 1; // Get the next command
  612. }
  613. parser.parse(saved_cmd); // Restore the parser state
  614. }
  615. void GcodeSuite::process_subcommands_now(char * gcode) {
  616. char * const saved_cmd = parser.command_ptr; // Save the parser state
  617. for (;;) {
  618. const char * const delim = strchr(gcode, '\n'); // Get address of next newline
  619. if (delim) *delim = '\0'; // Replace with nul
  620. parser.parse(gcode); // Parse the current command
  621. process_parsed_command(true); // Process it
  622. if (!delim) break; // Last command?
  623. gcode = delim + 1; // Get the next command
  624. }
  625. parser.parse(saved_cmd); // Restore the parser state
  626. }
  627. #endif // USE_EXECUTE_COMMANDS_IMMEDIATE
  628. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  629. /**
  630. * Output a "busy" message at regular intervals
  631. * while the machine is not accepting commands.
  632. */
  633. void GcodeSuite::host_keepalive() {
  634. const millis_t ms = millis();
  635. static millis_t next_busy_signal_ms = 0;
  636. if (!suspend_auto_report && host_keepalive_interval && busy_state != NOT_BUSY) {
  637. if (PENDING(ms, next_busy_signal_ms)) return;
  638. switch (busy_state) {
  639. case IN_HANDLER:
  640. case IN_PROCESS:
  641. SERIAL_ECHO_START();
  642. SERIAL_ECHOLNPGM(MSG_BUSY_PROCESSING);
  643. break;
  644. case PAUSED_FOR_USER:
  645. SERIAL_ECHO_START();
  646. SERIAL_ECHOLNPGM(MSG_BUSY_PAUSED_FOR_USER);
  647. break;
  648. case PAUSED_FOR_INPUT:
  649. SERIAL_ECHO_START();
  650. SERIAL_ECHOLNPGM(MSG_BUSY_PAUSED_FOR_INPUT);
  651. break;
  652. default:
  653. break;
  654. }
  655. }
  656. next_busy_signal_ms = ms + host_keepalive_interval * 1000UL;
  657. }
  658. #endif // HOST_KEEPALIVE_FEATURE