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

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