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 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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. #if ENABLED(WIFI_CUSTOM_COMMAND)
  29. extern bool wifi_custom_command(char * const command_ptr);
  30. #endif
  31. #include "parser.h"
  32. #include "queue.h"
  33. #include "../module/motion.h"
  34. #if ENABLED(PRINTCOUNTER)
  35. #include "../module/printcounter.h"
  36. #endif
  37. #if ENABLED(HOST_PROMPT_SUPPORT)
  38. #include "../feature/host_actions.h"
  39. #endif
  40. #if ENABLED(POWER_LOSS_RECOVERY)
  41. #include "../sd/cardreader.h"
  42. #include "../feature/powerloss.h"
  43. #endif
  44. #if ENABLED(CANCEL_OBJECTS)
  45. #include "../feature/cancel_object.h"
  46. #endif
  47. #if ENABLED(LASER_MOVE_POWER)
  48. #include "../feature/spindle_laser.h"
  49. #endif
  50. #include "../MarlinCore.h" // for idle()
  51. millis_t GcodeSuite::previous_move_ms;
  52. // Relative motion mode for each logical axis
  53. static constexpr xyze_bool_t ar_init = AXIS_RELATIVE_MODES;
  54. uint8_t GcodeSuite::axis_relative = (
  55. (ar_init.x ? _BV(REL_X) : 0)
  56. | (ar_init.y ? _BV(REL_Y) : 0)
  57. | (ar_init.z ? _BV(REL_Z) : 0)
  58. | (ar_init.e ? _BV(REL_E) : 0)
  59. );
  60. #if EITHER(HAS_AUTO_REPORTING, HOST_KEEPALIVE_FEATURE)
  61. bool GcodeSuite::autoreport_paused; // = false
  62. #endif
  63. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  64. GcodeSuite::MarlinBusyState GcodeSuite::busy_state = NOT_BUSY;
  65. uint8_t GcodeSuite::host_keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
  66. #endif
  67. #if ENABLED(CNC_WORKSPACE_PLANES)
  68. GcodeSuite::WorkspacePlane GcodeSuite::workspace_plane = PLANE_XY;
  69. #endif
  70. #if ENABLED(CNC_COORDINATE_SYSTEMS)
  71. int8_t GcodeSuite::active_coordinate_system = -1; // machine space
  72. xyz_pos_t GcodeSuite::coordinate_system[MAX_COORDINATE_SYSTEMS];
  73. #endif
  74. /**
  75. * Get the target extruder from the T parameter or the active_extruder
  76. * Return -1 if the T parameter is out of range
  77. */
  78. int8_t GcodeSuite::get_target_extruder_from_command() {
  79. if (parser.seenval('T')) {
  80. const int8_t e = parser.value_byte();
  81. if (e < EXTRUDERS) return e;
  82. SERIAL_ECHO_START();
  83. SERIAL_CHAR('M'); SERIAL_ECHO(parser.codenum);
  84. SERIAL_ECHOLNPAIR(" " STR_INVALID_EXTRUDER " ", int(e));
  85. return -1;
  86. }
  87. return active_extruder;
  88. }
  89. /**
  90. * Get the target e stepper from the T parameter
  91. * Return -1 if the T parameter is out of range or unspecified
  92. */
  93. int8_t GcodeSuite::get_target_e_stepper_from_command() {
  94. const int8_t e = parser.intval('T', -1);
  95. if (WITHIN(e, 0, E_STEPPERS - 1)) return e;
  96. SERIAL_ECHO_START();
  97. SERIAL_CHAR('M'); SERIAL_ECHO(parser.codenum);
  98. if (e == -1)
  99. SERIAL_ECHOLNPGM(" " STR_E_STEPPER_NOT_SPECIFIED);
  100. else
  101. SERIAL_ECHOLNPAIR(" " STR_INVALID_E_STEPPER " ", int(e));
  102. return -1;
  103. }
  104. /**
  105. * Set XYZE destination and feedrate from the current GCode command
  106. *
  107. * - Set destination from included axis codes
  108. * - Set to current for missing axis codes
  109. * - Set the feedrate, if included
  110. */
  111. void GcodeSuite::get_destination_from_command() {
  112. xyze_bool_t seen = { false, false, false, false };
  113. #if ENABLED(CANCEL_OBJECTS)
  114. const bool &skip_move = cancelable.skipping;
  115. #else
  116. constexpr bool skip_move = false;
  117. #endif
  118. // Get new XYZ position, whether absolute or relative
  119. LOOP_XYZ(i) {
  120. if ( (seen[i] = parser.seenval(XYZ_CHAR(i))) ) {
  121. const float v = parser.value_axis_units((AxisEnum)i);
  122. if (skip_move)
  123. destination[i] = current_position[i];
  124. else
  125. destination[i] = axis_is_relative(AxisEnum(i)) ? current_position[i] + v : LOGICAL_TO_NATIVE(v, i);
  126. }
  127. else
  128. destination[i] = current_position[i];
  129. }
  130. // Get new E position, whether absolute or relative
  131. if ( (seen.e = parser.seenval('E')) ) {
  132. const float v = parser.value_axis_units(E_AXIS);
  133. destination.e = axis_is_relative(E_AXIS) ? current_position.e + v : v;
  134. }
  135. else
  136. destination.e = current_position.e;
  137. #if ENABLED(POWER_LOSS_RECOVERY) && !PIN_EXISTS(POWER_LOSS)
  138. // Only update power loss recovery on moves with E
  139. if (recovery.enabled && IS_SD_PRINTING() && seen.e && (seen.x || seen.y))
  140. recovery.save();
  141. #endif
  142. if (parser.linearval('F') > 0)
  143. feedrate_mm_s = parser.value_feedrate();
  144. #if ENABLED(PRINTCOUNTER)
  145. if (!DEBUGGING(DRYRUN) && !skip_move)
  146. print_job_timer.incFilamentUsed(destination.e - current_position.e);
  147. #endif
  148. // Get ABCDHI mixing factors
  149. #if BOTH(MIXING_EXTRUDER, DIRECT_MIXING_IN_G1)
  150. M165();
  151. #endif
  152. #if ENABLED(LASER_MOVE_POWER)
  153. // Set the laser power in the planner to configure this move
  154. if (parser.seen('S'))
  155. cutter.inline_power(parser.value_int());
  156. else {
  157. #if ENABLED(LASER_MOVE_G0_OFF)
  158. if (parser.codenum == 0) // G0
  159. cutter.inline_enabled(false);
  160. #endif
  161. }
  162. #endif
  163. }
  164. /**
  165. * Dwell waits immediately. It does not synchronize. Use M400 instead of G4
  166. */
  167. void GcodeSuite::dwell(millis_t time) {
  168. time += millis();
  169. while (PENDING(millis(), time)) idle();
  170. }
  171. /**
  172. * When G29_RETRY_AND_RECOVER is enabled, call G29() in
  173. * a loop with recovery and retry handling.
  174. */
  175. #if BOTH(HAS_LEVELING, G29_RETRY_AND_RECOVER)
  176. #ifndef G29_MAX_RETRIES
  177. #define G29_MAX_RETRIES 0
  178. #endif
  179. void GcodeSuite::G29_with_retry() {
  180. uint8_t retries = G29_MAX_RETRIES;
  181. while (G29()) { // G29 should return true for failed probes ONLY
  182. if (retries--) event_probe_recover();
  183. else {
  184. event_probe_failure();
  185. return;
  186. }
  187. }
  188. TERN_(HOST_PROMPT_SUPPORT, host_action_prompt_end());
  189. #ifdef G29_SUCCESS_COMMANDS
  190. process_subcommands_now_P(PSTR(G29_SUCCESS_COMMANDS));
  191. #endif
  192. }
  193. #endif // HAS_LEVELING && G29_RETRY_AND_RECOVER
  194. //
  195. // Placeholders for non-migrated codes
  196. //
  197. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  198. extern void M100_dump_routine(PGM_P const title, const char * const start, const char * const end);
  199. #endif
  200. /**
  201. * Process the parsed command and dispatch it to its handler
  202. */
  203. void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
  204. KEEPALIVE_STATE(IN_HANDLER);
  205. // Handle a known G, M, or T
  206. switch (parser.command_letter) {
  207. case 'G': switch (parser.codenum) {
  208. case 0: case 1: G0_G1( // G0: Fast Move, G1: Linear Move
  209. #if IS_SCARA || defined(G0_FEEDRATE)
  210. parser.codenum == 0
  211. #endif
  212. );
  213. break;
  214. #if ENABLED(ARC_SUPPORT) && DISABLED(SCARA)
  215. case 2: case 3: G2_G3(parser.codenum == 2); break; // G2: CW ARC, G3: CCW ARC
  216. #endif
  217. case 4: G4(); break; // G4: Dwell
  218. #if ENABLED(BEZIER_CURVE_SUPPORT)
  219. case 5: G5(); break; // G5: Cubic B_spline
  220. #endif
  221. #if ENABLED(FWRETRACT)
  222. case 10: G10(); break; // G10: Retract / Swap Retract
  223. case 11: G11(); break; // G11: Recover / Swap Recover
  224. #endif
  225. #if ENABLED(NOZZLE_CLEAN_FEATURE)
  226. case 12: G12(); break; // G12: Nozzle Clean
  227. #endif
  228. #if ENABLED(CNC_WORKSPACE_PLANES)
  229. case 17: G17(); break; // G17: Select Plane XY
  230. case 18: G18(); break; // G18: Select Plane ZX
  231. case 19: G19(); break; // G19: Select Plane YZ
  232. #endif
  233. #if ENABLED(INCH_MODE_SUPPORT)
  234. case 20: G20(); break; // G20: Inch Mode
  235. case 21: G21(); break; // G21: MM Mode
  236. #else
  237. case 21: NOOP; break; // No error on unknown G21
  238. #endif
  239. #if ENABLED(G26_MESH_VALIDATION)
  240. case 26: G26(); break; // G26: Mesh Validation Pattern generation
  241. #endif
  242. #if ENABLED(NOZZLE_PARK_FEATURE)
  243. case 27: G27(); break; // G27: Nozzle Park
  244. #endif
  245. case 28: G28(); break; // G28: Home one or more axes
  246. #if HAS_LEVELING
  247. case 29: // G29: Bed leveling calibration
  248. #if ENABLED(G29_RETRY_AND_RECOVER)
  249. G29_with_retry();
  250. #else
  251. G29();
  252. #endif
  253. break;
  254. #endif // HAS_LEVELING
  255. #if HAS_BED_PROBE
  256. case 30: G30(); break; // G30: Single Z probe
  257. #if ENABLED(Z_PROBE_SLED)
  258. case 31: G31(); break; // G31: dock the sled
  259. case 32: G32(); break; // G32: undock the sled
  260. #endif
  261. #endif
  262. #if ENABLED(DELTA_AUTO_CALIBRATION)
  263. case 33: G33(); break; // G33: Delta Auto-Calibration
  264. #endif
  265. #if ENABLED(Z_STEPPER_AUTO_ALIGN)
  266. case 34: G34(); break; // G34: Z Stepper automatic alignment using probe
  267. #endif
  268. #if ENABLED(G38_PROBE_TARGET)
  269. case 38: // G38.2, G38.3: Probe towards target
  270. if (WITHIN(parser.subcode, 2,
  271. #if ENABLED(G38_PROBE_AWAY)
  272. 5
  273. #else
  274. 3
  275. #endif
  276. )) G38(parser.subcode); // G38.4, G38.5: Probe away from target
  277. break;
  278. #endif
  279. #if ENABLED(CNC_COORDINATE_SYSTEMS)
  280. case 53: G53(); break; // G53: (prefix) Apply native workspace
  281. case 54: G54(); break; // G54: Switch to Workspace 1
  282. case 55: G55(); break; // G55: Switch to Workspace 2
  283. case 56: G56(); break; // G56: Switch to Workspace 3
  284. case 57: G57(); break; // G57: Switch to Workspace 4
  285. case 58: G58(); break; // G58: Switch to Workspace 5
  286. case 59: G59(); break; // G59.0 - G59.3: Switch to Workspace 6-9
  287. #endif
  288. #if SAVED_POSITIONS
  289. case 60: G60(); break; // G60: save current position
  290. case 61: G61(); break; // G61: Apply/restore saved coordinates.
  291. #endif
  292. #if ENABLED(PROBE_TEMP_COMPENSATION)
  293. case 76: G76(); break; // G76: Calibrate first layer compensation values
  294. #endif
  295. #if ENABLED(GCODE_MOTION_MODES)
  296. case 80: G80(); break; // G80: Reset the current motion mode
  297. #endif
  298. case 90: set_relative_mode(false); break; // G90: Absolute Mode
  299. case 91: set_relative_mode(true); break; // G91: Relative Mode
  300. case 92: G92(); break; // G92: Set current axis position(s)
  301. #if HAS_MESH
  302. case 42: G42(); break; // G42: Coordinated move to a mesh point
  303. #endif
  304. #if ENABLED(CALIBRATION_GCODE)
  305. case 425: G425(); break; // G425: Perform calibration with calibration cube
  306. #endif
  307. #if ENABLED(DEBUG_GCODE_PARSER)
  308. case 800: parser.debug(); break; // G800: GCode Parser Test for G
  309. #endif
  310. default: parser.unknown_command_warning(); break;
  311. }
  312. break;
  313. case 'M': switch (parser.codenum) {
  314. #if HAS_RESUME_CONTINUE
  315. case 0: // M0: Unconditional stop - Wait for user button press on LCD
  316. case 1: M0_M1(); break; // M1: Conditional stop - Wait for user button press on LCD
  317. #endif
  318. #if HAS_CUTTER
  319. case 3: M3_M4(false); break; // M3: Turn ON Laser | Spindle (clockwise), set Power | Speed
  320. case 4: M3_M4(true ); break; // M4: Turn ON Laser | Spindle (counter-clockwise), set Power | Speed
  321. case 5: M5(); break; // M5: Turn OFF Laser | Spindle
  322. #endif
  323. #if ENABLED(COOLANT_CONTROL)
  324. #if ENABLED(COOLANT_MIST)
  325. case 7: M7(); break; // M7: Mist coolant ON
  326. #endif
  327. #if ENABLED(COOLANT_FLOOD)
  328. case 8: M8(); break; // M8: Flood coolant ON
  329. #endif
  330. case 9: M9(); break; // M9: Coolant OFF
  331. #endif
  332. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  333. case 12: M12(); break; // M12: Synchronize and optionally force a CLC set
  334. #endif
  335. #if ENABLED(EXPECTED_PRINTER_CHECK)
  336. case 16: M16(); break; // M16: Expected printer check
  337. #endif
  338. case 17: M17(); break; // M17: Enable all stepper motors
  339. #if ENABLED(SDSUPPORT)
  340. case 20: M20(); break; // M20: List SD card
  341. case 21: M21(); break; // M21: Init SD card
  342. case 22: M22(); break; // M22: Release SD card
  343. case 23: M23(); break; // M23: Select file
  344. case 24: M24(); break; // M24: Start SD print
  345. case 25: M25(); break; // M25: Pause SD print
  346. case 26: M26(); break; // M26: Set SD index
  347. case 27: M27(); break; // M27: Get SD status
  348. case 28: M28(); break; // M28: Start SD write
  349. case 29: M29(); break; // M29: Stop SD write
  350. case 30: M30(); break; // M30 <filename> Delete File
  351. case 32: M32(); break; // M32: Select file and start SD print
  352. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  353. case 33: M33(); break; // M33: Get the long full path to a file or folder
  354. #endif
  355. #if BOTH(SDCARD_SORT_ALPHA, SDSORT_GCODE)
  356. case 34: M34(); break; // M34: Set SD card sorting options
  357. #endif
  358. case 928: M928(); break; // M928: Start SD write
  359. #endif // SDSUPPORT
  360. case 31: M31(); break; // M31: Report time since the start of SD print or last M109
  361. case 42: M42(); break; // M42: Change pin state
  362. #if ENABLED(PINS_DEBUGGING)
  363. case 43: M43(); break; // M43: Read pin state
  364. #endif
  365. #if ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST)
  366. case 48: M48(); break; // M48: Z probe repeatability test
  367. #endif
  368. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  369. case 73: M73(); break; // M73: Set progress percentage (for display on LCD)
  370. #endif
  371. case 75: M75(); break; // M75: Start print timer
  372. case 76: M76(); break; // M76: Pause print timer
  373. case 77: M77(); break; // M77: Stop print timer
  374. #if ENABLED(PRINTCOUNTER)
  375. case 78: M78(); break; // M78: Show print statistics
  376. #endif
  377. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  378. case 100: M100(); break; // M100: Free Memory Report
  379. #endif
  380. #if EXTRUDERS
  381. case 104: M104(); break; // M104: Set hot end temperature
  382. case 109: M109(); break; // M109: Wait for hotend temperature to reach target
  383. #endif
  384. case 105: M105(); return; // M105: Report Temperatures (and say "ok")
  385. #if HAS_FAN
  386. case 106: M106(); break; // M106: Fan On
  387. case 107: M107(); break; // M107: Fan Off
  388. #endif
  389. case 110: M110(); break; // M110: Set Current Line Number
  390. case 111: M111(); break; // M111: Set debug level
  391. #if DISABLED(EMERGENCY_PARSER)
  392. case 108: M108(); break; // M108: Cancel Waiting
  393. case 112: M112(); break; // M112: Full Shutdown
  394. case 410: M410(); break; // M410: Quickstop - Abort all the planned moves.
  395. #if ENABLED(HOST_PROMPT_SUPPORT)
  396. case 876: M876(); break; // M876: Handle Host prompt responses
  397. #endif
  398. #else
  399. case 108: case 112: case 410:
  400. #if ENABLED(HOST_PROMPT_SUPPORT)
  401. case 876:
  402. #endif
  403. break;
  404. #endif
  405. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  406. case 113: M113(); break; // M113: Set Host Keepalive interval
  407. #endif
  408. #if HAS_HEATED_BED
  409. case 140: M140(); break; // M140: Set bed temperature
  410. case 190: M190(); break; // M190: Wait for bed temperature to reach target
  411. #endif
  412. #if HAS_HEATED_CHAMBER
  413. case 141: M141(); break; // M141: Set chamber temperature
  414. case 191: M191(); break; // M191: Wait for chamber temperature to reach target
  415. #endif
  416. #if BOTH(AUTO_REPORT_TEMPERATURES, HAS_TEMP_SENSOR)
  417. case 155: M155(); break; // M155: Set temperature auto-report interval
  418. #endif
  419. #if ENABLED(PARK_HEAD_ON_PAUSE)
  420. case 125: M125(); break; // M125: Store current position and move to filament change position
  421. #endif
  422. #if ENABLED(BARICUDA)
  423. // PWM for HEATER_1_PIN
  424. #if HAS_HEATER_1
  425. case 126: M126(); break; // M126: valve open
  426. case 127: M127(); break; // M127: valve closed
  427. #endif
  428. // PWM for HEATER_2_PIN
  429. #if HAS_HEATER_2
  430. case 128: M128(); break; // M128: valve open
  431. case 129: M129(); break; // M129: valve closed
  432. #endif
  433. #endif // BARICUDA
  434. #if ENABLED(PSU_CONTROL)
  435. case 80: M80(); break; // M80: Turn on Power Supply
  436. #endif
  437. case 81: M81(); break; // M81: Turn off Power, including Power Supply, if possible
  438. case 82: M82(); break; // M82: Set E axis normal mode (same as other axes)
  439. case 83: M83(); break; // M83: Set E axis relative mode
  440. case 18: case 84: M18_M84(); break; // M18/M84: Disable Steppers / Set Timeout
  441. case 85: M85(); break; // M85: Set inactivity stepper shutdown timeout
  442. case 92: M92(); break; // M92: Set the steps-per-unit for one or more axes
  443. case 114: M114(); break; // M114: Report current position
  444. case 115: M115(); break; // M115: Report capabilities
  445. case 117: M117(); break; // M117: Set LCD message text, if possible
  446. case 118: M118(); break; // M118: Display a message in the host console
  447. case 119: M119(); break; // M119: Report endstop states
  448. case 120: M120(); break; // M120: Enable endstops
  449. case 121: M121(); break; // M121: Disable endstops
  450. #if HOTENDS && HAS_LCD_MENU
  451. case 145: M145(); break; // M145: Set material heatup parameters
  452. #endif
  453. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  454. case 149: M149(); break; // M149: Set temperature units
  455. #endif
  456. #if HAS_COLOR_LEDS
  457. case 150: M150(); break; // M150: Set Status LED Color
  458. #endif
  459. #if ENABLED(MIXING_EXTRUDER)
  460. case 163: M163(); break; // M163: Set a component weight for mixing extruder
  461. case 164: M164(); break; // M164: Save current mix as a virtual extruder
  462. #if ENABLED(DIRECT_MIXING_IN_G1)
  463. case 165: M165(); break; // M165: Set multiple mix weights
  464. #endif
  465. #if ENABLED(GRADIENT_MIX)
  466. case 166: M166(); break; // M166: Set Gradient Mix
  467. #endif
  468. #endif
  469. #if DISABLED(NO_VOLUMETRICS)
  470. case 200: M200(); break; // M200: Set filament diameter, E to cubic units
  471. #endif
  472. case 201: M201(); break; // M201: Set max acceleration for print moves (units/s^2)
  473. #if 0
  474. case 202: M202(); break; // M202: Not used for Sprinter/grbl gen6
  475. #endif
  476. case 203: M203(); break; // M203: Set max feedrate (units/sec)
  477. case 204: M204(); break; // M204: Set acceleration
  478. case 205: M205(); break; // M205: Set advanced settings
  479. #if HAS_M206_COMMAND
  480. case 206: M206(); break; // M206: Set home offsets
  481. #endif
  482. #if ENABLED(FWRETRACT)
  483. case 207: M207(); break; // M207: Set Retract Length, Feedrate, and Z lift
  484. case 208: M208(); break; // M208: Set Recover (unretract) Additional Length and Feedrate
  485. #if ENABLED(FWRETRACT_AUTORETRACT)
  486. case 209:
  487. if (MIN_AUTORETRACT <= MAX_AUTORETRACT) M209(); // M209: Turn Automatic Retract Detection on/off
  488. break;
  489. #endif
  490. #endif
  491. #if HAS_SOFTWARE_ENDSTOPS
  492. case 211: M211(); break; // M211: Enable, Disable, and/or Report software endstops
  493. #endif
  494. #if EXTRUDERS > 1
  495. case 217: M217(); break; // M217: Set filament swap parameters
  496. #endif
  497. #if HAS_HOTEND_OFFSET
  498. case 218: M218(); break; // M218: Set a tool offset
  499. #endif
  500. case 220: M220(); break; // M220: Set Feedrate Percentage: S<percent> ("FR" on your LCD)
  501. #if EXTRUDERS
  502. case 221: M221(); break; // M221: Set Flow Percentage
  503. #endif
  504. case 226: M226(); break; // M226: Wait until a pin reaches a state
  505. #if HAS_SERVOS
  506. case 280: M280(); break; // M280: Set servo position absolute
  507. #if ENABLED(EDITABLE_SERVO_ANGLES)
  508. case 281: M281(); break; // M281: Set servo angles
  509. #endif
  510. #endif
  511. #if ENABLED(BABYSTEPPING)
  512. case 290: M290(); break; // M290: Babystepping
  513. #endif
  514. #if HAS_BUZZER
  515. case 300: M300(); break; // M300: Play beep tone
  516. #endif
  517. #if ENABLED(PIDTEMP)
  518. case 301: M301(); break; // M301: Set hotend PID parameters
  519. #endif
  520. #if ENABLED(PIDTEMPBED)
  521. case 304: M304(); break; // M304: Set bed PID parameters
  522. #endif
  523. #if ENABLED(PHOTO_GCODE)
  524. case 240: M240(); break; // M240: Trigger a camera
  525. #endif
  526. #if HAS_LCD_CONTRAST
  527. case 250: M250(); break; // M250: Set LCD contrast
  528. #endif
  529. #if ENABLED(EXPERIMENTAL_I2CBUS)
  530. case 260: M260(); break; // M260: Send data to an i2c slave
  531. case 261: M261(); break; // M261: Request data from an i2c slave
  532. #endif
  533. #if ENABLED(PREVENT_COLD_EXTRUSION)
  534. case 302: M302(); break; // M302: Allow cold extrudes (set the minimum extrude temperature)
  535. #endif
  536. #if HAS_PID_HEATING
  537. case 303: M303(); break; // M303: PID autotune
  538. #endif
  539. #if HAS_USER_THERMISTORS
  540. case 305: M305(); break; // M305: Set user thermistor parameters
  541. #endif
  542. #if ENABLED(MORGAN_SCARA)
  543. case 360: if (M360()) return; break; // M360: SCARA Theta pos1
  544. case 361: if (M361()) return; break; // M361: SCARA Theta pos2
  545. case 362: if (M362()) return; break; // M362: SCARA Psi pos1
  546. case 363: if (M363()) return; break; // M363: SCARA Psi pos2
  547. case 364: if (M364()) return; break; // M364: SCARA Psi pos3 (90 deg to Theta)
  548. #endif
  549. #if EITHER(EXT_SOLENOID, MANUAL_SOLENOID_CONTROL)
  550. case 380: M380(); break; // M380: Activate solenoid on active (or specified) extruder
  551. case 381: M381(); break; // M381: Disable all solenoids or, if MANUAL_SOLENOID_CONTROL, active (or specified) solenoid
  552. #endif
  553. case 400: M400(); break; // M400: Finish all moves
  554. #if HAS_BED_PROBE
  555. case 401: M401(); break; // M401: Deploy probe
  556. case 402: M402(); break; // M402: Stow probe
  557. #endif
  558. #if ENABLED(PRUSA_MMU2)
  559. case 403: M403(); break;
  560. #endif
  561. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  562. case 404: M404(); break; // M404: Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or display nominal filament width
  563. case 405: M405(); break; // M405: Turn on filament sensor for control
  564. case 406: M406(); break; // M406: Turn off filament sensor for control
  565. case 407: M407(); break; // M407: Display measured filament diameter
  566. #endif
  567. #if HAS_FILAMENT_SENSOR
  568. case 412: M412(); break; // M412: Enable/Disable filament runout detection
  569. #endif
  570. #if HAS_LEVELING
  571. case 420: M420(); break; // M420: Enable/Disable Bed Leveling
  572. #endif
  573. #if HAS_MESH
  574. case 421: M421(); break; // M421: Set a Mesh Bed Leveling Z coordinate
  575. #endif
  576. #if ENABLED(BACKLASH_GCODE)
  577. case 425: M425(); break; // M425: Tune backlash compensation
  578. #endif
  579. #if HAS_M206_COMMAND
  580. case 428: M428(); break; // M428: Apply current_position to home_offset
  581. #endif
  582. #if ENABLED(CANCEL_OBJECTS)
  583. case 486: M486(); break; // M486: Identify and cancel objects
  584. #endif
  585. case 500: M500(); break; // M500: Store settings in EEPROM
  586. case 501: M501(); break; // M501: Read settings from EEPROM
  587. case 502: M502(); break; // M502: Revert to default settings
  588. #if DISABLED(DISABLE_M503)
  589. case 503: M503(); break; // M503: print settings currently in memory
  590. #endif
  591. #if ENABLED(EEPROM_SETTINGS)
  592. case 504: M504(); break; // M504: Validate EEPROM contents
  593. #endif
  594. #if ENABLED(SDSUPPORT)
  595. case 524: M524(); break; // M524: Abort the current SD print job
  596. #endif
  597. #if ENABLED(SD_ABORT_ON_ENDSTOP_HIT)
  598. case 540: M540(); break; // M540: Set abort on endstop hit for SD printing
  599. #endif
  600. #if ENABLED(BAUD_RATE_GCODE)
  601. case 575: M575(); break; // M575: Set serial baudrate
  602. #endif
  603. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  604. case 600: M600(); break; // M600: Pause for Filament Change
  605. case 603: M603(); break; // M603: Configure Filament Change
  606. #endif
  607. #if HAS_DUPLICATION_MODE
  608. case 605: M605(); break; // M605: Set Dual X Carriage movement mode
  609. #endif
  610. #if ENABLED(DELTA)
  611. case 665: M665(); break; // M665: Set delta configurations
  612. #endif
  613. #if ENABLED(DELTA) || HAS_EXTRA_ENDSTOPS
  614. case 666: M666(); break; // M666: Set delta or multiple endstop adjustment
  615. #endif
  616. #if ENABLED(SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  617. case 672: M672(); break; // M672: Set/clear Duet Smart Effector sensitivity
  618. #endif
  619. #if ENABLED(FILAMENT_LOAD_UNLOAD_GCODES)
  620. case 701: M701(); break; // M701: Load Filament
  621. case 702: M702(); break; // M702: Unload Filament
  622. #endif
  623. #if ENABLED(CONTROLLER_FAN_EDITABLE)
  624. case 710: M710(); break; // M710: Set Controller Fan settings
  625. #endif
  626. #if ENABLED(GCODE_MACROS)
  627. case 810: case 811: case 812: case 813: case 814:
  628. case 815: case 816: case 817: case 818: case 819:
  629. M810_819(); break; // M810-M819: Define/execute G-code macro
  630. #endif
  631. #if HAS_BED_PROBE
  632. case 851: M851(); break; // M851: Set Z Probe Z Offset
  633. #endif
  634. #if ENABLED(SKEW_CORRECTION_GCODE)
  635. case 852: M852(); break; // M852: Set Skew factors
  636. #endif
  637. #if ENABLED(PROBE_TEMP_COMPENSATION)
  638. case 871: M871(); break; // M871: Print/reset/clear first layer temperature offset values
  639. #endif
  640. #if ENABLED(LIN_ADVANCE)
  641. case 900: M900(); break; // M900: Set advance K factor.
  642. #endif
  643. #if ANY(HAS_DIGIPOTSS, HAS_MOTOR_CURRENT_PWM, HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT)
  644. case 907: M907(); break; // M907: Set digital trimpot motor current using axis codes.
  645. #if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT)
  646. case 908: M908(); break; // M908: Control digital trimpot directly.
  647. #if ENABLED(DAC_STEPPER_CURRENT)
  648. case 909: M909(); break; // M909: Print digipot/DAC current value
  649. case 910: M910(); break; // M910: Commit digipot/DAC value to external EEPROM
  650. #endif
  651. #endif
  652. #endif
  653. #if HAS_TRINAMIC_CONFIG
  654. case 122: M122(); break; // M122: Report driver configuration and status
  655. case 906: M906(); break; // M906: Set motor current in milliamps using axis codes X, Y, Z, E
  656. #if HAS_STEALTHCHOP
  657. case 569: M569(); break; // M569: Enable stealthChop on an axis.
  658. #endif
  659. #if ENABLED(MONITOR_DRIVER_STATUS)
  660. case 911: M911(); break; // M911: Report TMC2130 prewarn triggered flags
  661. case 912: M912(); break; // M912: Clear TMC2130 prewarn triggered flags
  662. #endif
  663. #if ENABLED(HYBRID_THRESHOLD)
  664. case 913: M913(); break; // M913: Set HYBRID_THRESHOLD speed.
  665. #endif
  666. #if USE_SENSORLESS
  667. case 914: M914(); break; // M914: Set StallGuard sensitivity.
  668. #endif
  669. #endif
  670. #if HAS_L64XX
  671. case 122: M122(); break; // M122: Report status
  672. case 906: M906(); break; // M906: Set or get motor drive level
  673. case 916: M916(); break; // M916: L6470 tuning: Increase drive level until thermal warning
  674. case 917: M917(); break; // M917: L6470 tuning: Find minimum current thresholds
  675. case 918: M918(); break; // M918: L6470 tuning: Increase speed until max or error
  676. #endif
  677. #if HAS_MICROSTEPS
  678. case 350: M350(); break; // M350: Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  679. case 351: M351(); break; // M351: Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  680. #endif
  681. #if HAS_CASE_LIGHT
  682. case 355: M355(); break; // M355: Set case light brightness
  683. #endif
  684. #if ENABLED(DEBUG_GCODE_PARSER)
  685. case 800: parser.debug(); break; // M800: GCode Parser Test for M
  686. #endif
  687. #if ENABLED(I2C_POSITION_ENCODERS)
  688. case 860: M860(); break; // M860: Report encoder module position
  689. case 861: M861(); break; // M861: Report encoder module status
  690. case 862: M862(); break; // M862: Perform axis test
  691. case 863: M863(); break; // M863: Calibrate steps/mm
  692. case 864: M864(); break; // M864: Change module address
  693. case 865: M865(); break; // M865: Check module firmware version
  694. case 866: M866(); break; // M866: Report axis error count
  695. case 867: M867(); break; // M867: Toggle error correction
  696. case 868: M868(); break; // M868: Set error correction threshold
  697. case 869: M869(); break; // M869: Report axis error
  698. #endif
  699. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  700. case 951: M951(); break; // M951: Set Magnetic Parking Extruder parameters
  701. #endif
  702. #if ENABLED(Z_STEPPER_AUTO_ALIGN)
  703. case 422: M422(); break; // M422: Set Z Stepper automatic alignment position using probe
  704. #endif
  705. #if ENABLED(PLATFORM_M997_SUPPORT)
  706. case 997: M997(); break; // M997: Perform in-application firmware update
  707. #endif
  708. case 999: M999(); break; // M999: Restart after being Stopped
  709. #if ENABLED(POWER_LOSS_RECOVERY)
  710. case 413: M413(); break; // M413: Enable/disable/query Power-Loss Recovery
  711. case 1000: M1000(); break; // M1000: [INTERNAL] Resume from power-loss
  712. #endif
  713. #if ENABLED(SDSUPPORT)
  714. case 1001: M1001(); break; // M1001: [INTERNAL] Handle SD completion
  715. #endif
  716. #if ENABLED(MAX7219_GCODE)
  717. case 7219: M7219(); break; // M7219: Set LEDs, columns, and rows
  718. #endif
  719. default: parser.unknown_command_warning(); break;
  720. }
  721. break;
  722. case 'T': T(parser.codenum); break; // Tn: Tool Change
  723. default:
  724. #if ENABLED(WIFI_CUSTOM_COMMAND)
  725. if (wifi_custom_command(parser.command_ptr)) break;
  726. #endif
  727. parser.unknown_command_warning();
  728. }
  729. if (!no_ok) queue.ok_to_send();
  730. }
  731. /**
  732. * Process a single command and dispatch it to its handler
  733. * This is called from the main loop()
  734. */
  735. void GcodeSuite::process_next_command() {
  736. char * const current_command = queue.command_buffer[queue.index_r];
  737. PORT_REDIRECT(queue.port[queue.index_r]);
  738. #if ENABLED(POWER_LOSS_RECOVERY)
  739. recovery.queue_index_r = queue.index_r;
  740. #endif
  741. if (DEBUGGING(ECHO)) {
  742. SERIAL_ECHO_START();
  743. SERIAL_ECHOLN(current_command);
  744. #if ENABLED(M100_FREE_MEMORY_DUMPER)
  745. SERIAL_ECHOPAIR("slot:", queue.index_r);
  746. M100_dump_routine(PSTR(" Command Queue:"), &queue.command_buffer[0][0], &queue.command_buffer[BUFSIZE - 1][MAX_CMD_SIZE - 1]);
  747. #endif
  748. }
  749. // Parse the next command in the queue
  750. parser.parse(current_command);
  751. process_parsed_command();
  752. }
  753. /**
  754. * Run a series of commands, bypassing the command queue to allow
  755. * G-code "macros" to be called from within other G-code handlers.
  756. */
  757. void GcodeSuite::process_subcommands_now_P(PGM_P pgcode) {
  758. char * const saved_cmd = parser.command_ptr; // Save the parser state
  759. for (;;) {
  760. PGM_P const delim = strchr_P(pgcode, '\n'); // Get address of next newline
  761. const size_t len = delim ? delim - pgcode : strlen_P(pgcode); // Get the command length
  762. char cmd[len + 1]; // Allocate a stack buffer
  763. strncpy_P(cmd, pgcode, len); // Copy the command to the stack
  764. cmd[len] = '\0'; // End with a nul
  765. parser.parse(cmd); // Parse the command
  766. process_parsed_command(true); // Process it
  767. if (!delim) break; // Last command?
  768. pgcode = delim + 1; // Get the next command
  769. }
  770. parser.parse(saved_cmd); // Restore the parser state
  771. }
  772. void GcodeSuite::process_subcommands_now(char * gcode) {
  773. char * const saved_cmd = parser.command_ptr; // Save the parser state
  774. for (;;) {
  775. char * const delim = strchr(gcode, '\n'); // Get address of next newline
  776. if (delim) *delim = '\0'; // Replace with nul
  777. parser.parse(gcode); // Parse the current command
  778. if (delim) *delim = '\n'; // Put back the newline
  779. process_parsed_command(true); // Process it
  780. if (!delim) break; // Last command?
  781. gcode = delim + 1; // Get the next command
  782. }
  783. parser.parse(saved_cmd); // Restore the parser state
  784. }
  785. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  786. /**
  787. * Output a "busy" message at regular intervals
  788. * while the machine is not accepting commands.
  789. */
  790. void GcodeSuite::host_keepalive() {
  791. const millis_t ms = millis();
  792. static millis_t next_busy_signal_ms = 0;
  793. if (!autoreport_paused && host_keepalive_interval && busy_state != NOT_BUSY) {
  794. if (PENDING(ms, next_busy_signal_ms)) return;
  795. switch (busy_state) {
  796. case IN_HANDLER:
  797. case IN_PROCESS:
  798. SERIAL_ECHO_MSG(STR_BUSY_PROCESSING);
  799. break;
  800. case PAUSED_FOR_USER:
  801. SERIAL_ECHO_MSG(STR_BUSY_PAUSED_FOR_USER);
  802. break;
  803. case PAUSED_FOR_INPUT:
  804. SERIAL_ECHO_MSG(STR_BUSY_PAUSED_FOR_INPUT);
  805. break;
  806. default:
  807. break;
  808. }
  809. }
  810. next_busy_signal_ms = ms + SEC_TO_MS(host_keepalive_interval);
  811. }
  812. #endif // HOST_KEEPALIVE_FEATURE