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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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()
  35. uint8_t GcodeSuite::target_extruder;
  36. millis_t GcodeSuite::previous_cmd_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. /**
  46. * Set target_extruder from the T parameter or the active_extruder
  47. *
  48. * Returns TRUE if the target is invalid
  49. */
  50. bool GcodeSuite::get_target_extruder_from_command() {
  51. if (parser.seenval('T')) {
  52. const int8_t e = parser.value_byte();
  53. if (e >= EXTRUDERS) {
  54. SERIAL_ECHO_START();
  55. SERIAL_CHAR('M');
  56. SERIAL_ECHO(parser.codenum);
  57. SERIAL_ECHOLNPAIR(" " MSG_INVALID_EXTRUDER " ", e);
  58. return true;
  59. }
  60. target_extruder = e;
  61. }
  62. else
  63. target_extruder = active_extruder;
  64. return false;
  65. }
  66. /**
  67. * Set XYZE destination and feedrate from the current GCode command
  68. *
  69. * - Set destination from included axis codes
  70. * - Set to current for missing axis codes
  71. * - Set the feedrate, if included
  72. */
  73. void GcodeSuite::get_destination_from_command() {
  74. LOOP_XYZE(i) {
  75. if (parser.seen(axis_codes[i]))
  76. destination[i] = parser.value_axis_units((AxisEnum)i) + (axis_relative_modes[i] || relative_mode ? current_position[i] : 0);
  77. else
  78. destination[i] = current_position[i];
  79. }
  80. if (parser.linearval('F') > 0.0)
  81. feedrate_mm_s = MMM_TO_MMS(parser.value_feedrate());
  82. #if ENABLED(PRINTCOUNTER)
  83. if (!DEBUGGING(DRYRUN))
  84. print_job_timer.incFilamentUsed(destination[E_AXIS] - current_position[E_AXIS]);
  85. #endif
  86. // Get ABCDHI mixing factors
  87. #if ENABLED(MIXING_EXTRUDER) && ENABLED(DIRECT_MIXING_IN_G1)
  88. gcode_get_mix();
  89. #endif
  90. }
  91. /**
  92. * Dwell waits immediately. It does not synchronize. Use M400 instead of G4
  93. */
  94. void GcodeSuite::dwell(millis_t time) {
  95. refresh_cmd_timeout();
  96. time += previous_cmd_ms;
  97. while (PENDING(millis(), time)) idle();
  98. }
  99. //
  100. // Placeholders for non-migrated codes
  101. //
  102. extern void gcode_M163();
  103. extern void gcode_M164();
  104. extern void gcode_M165();
  105. extern void gcode_M203();
  106. extern void gcode_M204();
  107. extern void gcode_M205();
  108. extern void gcode_M206();
  109. extern void gcode_M211();
  110. extern void gcode_M220();
  111. extern void gcode_M226();
  112. extern void gcode_M240();
  113. extern void gcode_M250();
  114. extern void gcode_M260();
  115. extern void gcode_M261();
  116. extern void gcode_M280();
  117. extern void gcode_M300();
  118. extern void gcode_M301();
  119. extern void gcode_M302();
  120. extern void gcode_M304();
  121. extern void gcode_M350();
  122. extern void gcode_M351();
  123. extern void gcode_M355();
  124. extern bool gcode_M360();
  125. extern bool gcode_M361();
  126. extern bool gcode_M362();
  127. extern bool gcode_M363();
  128. extern bool gcode_M364();
  129. extern void gcode_M380();
  130. extern void gcode_M381();
  131. extern void gcode_M400();
  132. extern void gcode_M401();
  133. extern void gcode_M402();
  134. extern void gcode_M428();
  135. extern void gcode_M500();
  136. extern void gcode_M501();
  137. extern void gcode_M502();
  138. extern void gcode_M503();
  139. extern void gcode_M540();
  140. extern void gcode_M605();
  141. extern void gcode_M665();
  142. extern void gcode_M666();
  143. extern void gcode_M702();
  144. extern void gcode_M900();
  145. extern void gcode_M906();
  146. extern void gcode_M911();
  147. extern void gcode_M912();
  148. extern void gcode_M913();
  149. extern void gcode_M914();
  150. extern void gcode_M907();
  151. extern void gcode_M908();
  152. extern void gcode_M909();
  153. extern void gcode_M910();
  154. extern void gcode_M999();
  155. extern void gcode_T(uint8_t tmp_extruder);
  156. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  157. extern void M100_dump_routine(const char * const title, const char *start, const char *end);
  158. #endif
  159. /**
  160. * Process a single command and dispatch it to its handler
  161. * This is called from the main loop()
  162. */
  163. void GcodeSuite::process_next_command() {
  164. char * const current_command = command_queue[cmd_queue_index_r];
  165. if (DEBUGGING(ECHO)) {
  166. SERIAL_ECHO_START();
  167. SERIAL_ECHOLN(current_command);
  168. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  169. SERIAL_ECHOPAIR("slot:", cmd_queue_index_r);
  170. M100_dump_routine(" Command Queue:", (const char*)command_queue, (const char*)(command_queue + sizeof(command_queue)));
  171. #endif
  172. }
  173. KEEPALIVE_STATE(IN_HANDLER);
  174. // Parse the next command in the queue
  175. parser.parse(current_command);
  176. // Handle a known G, M, or T
  177. switch (parser.command_letter) {
  178. case 'G': switch (parser.codenum) {
  179. // G0, G1
  180. case 0:
  181. case 1:
  182. #if IS_SCARA
  183. G0_G1(parser.codenum == 0);
  184. #else
  185. G0_G1();
  186. #endif
  187. break;
  188. // G2, G3
  189. #if ENABLED(ARC_SUPPORT) && DISABLED(SCARA)
  190. case 2: // G2: CW ARC
  191. case 3: // G3: CCW ARC
  192. G2_G3(parser.codenum == 2);
  193. break;
  194. #endif
  195. // G4 Dwell
  196. case 4:
  197. G4();
  198. break;
  199. #if ENABLED(BEZIER_CURVE_SUPPORT)
  200. case 5: // G5: Cubic B_spline
  201. G5();
  202. break;
  203. #endif // BEZIER_CURVE_SUPPORT
  204. #if ENABLED(FWRETRACT)
  205. case 10: // G10: retract
  206. G10();
  207. break;
  208. case 11: // G11: retract_recover
  209. G11();
  210. break;
  211. #endif // FWRETRACT
  212. #if ENABLED(NOZZLE_CLEAN_FEATURE)
  213. case 12:
  214. G12(); // G12: Nozzle Clean
  215. break;
  216. #endif // NOZZLE_CLEAN_FEATURE
  217. #if ENABLED(CNC_WORKSPACE_PLANES)
  218. case 17: // G17: Select Plane XY
  219. G17();
  220. break;
  221. case 18: // G18: Select Plane ZX
  222. G18();
  223. break;
  224. case 19: // G19: Select Plane YZ
  225. G19();
  226. break;
  227. #endif // CNC_WORKSPACE_PLANES
  228. #if ENABLED(INCH_MODE_SUPPORT)
  229. case 20: // G20: Inch Mode
  230. G20();
  231. break;
  232. case 21: // G21: MM Mode
  233. G21();
  234. break;
  235. #endif // INCH_MODE_SUPPORT
  236. #if ENABLED(UBL_G26_MESH_VALIDATION)
  237. case 26: // G26: Mesh Validation Pattern generation
  238. G26();
  239. break;
  240. #endif // AUTO_BED_LEVELING_UBL
  241. #if ENABLED(NOZZLE_PARK_FEATURE)
  242. case 27: // G27: Nozzle Park
  243. G27();
  244. break;
  245. #endif // NOZZLE_PARK_FEATURE
  246. case 28: // G28: Home all axes, one at a time
  247. G28(false);
  248. break;
  249. #if HAS_LEVELING
  250. case 29: // G29 Detailed Z probe, probes the bed at 3 or more points,
  251. // or provides access to the UBL System if enabled.
  252. G29();
  253. break;
  254. #endif // HAS_LEVELING
  255. #if HAS_BED_PROBE
  256. case 30: // G30 Single Z probe
  257. G30();
  258. break;
  259. #if ENABLED(Z_PROBE_SLED)
  260. case 31: // G31: dock the sled
  261. G31();
  262. break;
  263. case 32: // G32: undock the sled
  264. G32();
  265. break;
  266. #endif // Z_PROBE_SLED
  267. #endif // HAS_BED_PROBE
  268. #if ENABLED(DELTA_AUTO_CALIBRATION)
  269. case 33: // G33: Delta Auto-Calibration
  270. G33();
  271. break;
  272. #endif // DELTA_AUTO_CALIBRATION
  273. #if ENABLED(G38_PROBE_TARGET)
  274. case 38: // G38.2 & G38.3
  275. if (parser.subcode == 2 || parser.subcode == 3)
  276. G38(parser.subcode == 2);
  277. break;
  278. #endif
  279. case 90: // G90
  280. relative_mode = false;
  281. break;
  282. case 91: // G91
  283. relative_mode = true;
  284. break;
  285. case 92: // G92 - Set current axis position(s)
  286. G92();
  287. break;
  288. #if HAS_MESH
  289. case 42: G42(); break; // G42: Coordinated move to a mesh point
  290. #endif
  291. #if ENABLED(DEBUG_GCODE_PARSER)
  292. case 800:
  293. parser.debug(); // GCode Parser Test for G
  294. break;
  295. #endif
  296. }
  297. break;
  298. case 'M': switch (parser.codenum) {
  299. #if HAS_RESUME_CONTINUE
  300. case 0: // M0: Unconditional stop - Wait for user button press on LCD
  301. case 1: // M1: Conditional stop - Wait for user button press on LCD
  302. M0_M1();
  303. break;
  304. #endif // ULTIPANEL
  305. #if ENABLED(SPINDLE_LASER_ENABLE)
  306. // These synchronize with movement commands...
  307. case 3: M3_M4(true ); break; // M3: turn spindle/laser on, set laser/spindle power/speed, set rotation direction CW
  308. case 4: M3_M4(false); break; // M4: turn spindle/laser on, set laser/spindle power/speed, set rotation direction CCW
  309. case 5: M5(); break; // M5 - turn spindle/laser off
  310. #endif
  311. case 17: // M17: Enable all stepper motors
  312. M17();
  313. break;
  314. #if ENABLED(SDSUPPORT)
  315. case 20: M20(); break; // M20: list SD card
  316. case 21: M21(); break; // M21: init SD card
  317. case 22: M22(); break; // M22: release SD card
  318. case 23: M23(); break; // M23: Select file
  319. case 24: M24(); break; // M24: Start SD print
  320. case 25: M25(); break; // M25: Pause SD print
  321. case 26: M26(); break; // M26: Set SD index
  322. case 27: M27(); break; // M27: Get SD status
  323. case 28: M28(); break; // M28: Start SD write
  324. case 29: M29(); break; // M29: Stop SD write
  325. case 30: M30(); break; // M30 <filename> Delete File
  326. case 32: M32(); break; // M32: Select file and start SD print
  327. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  328. case 33: M33(); break; // M33: Get the long full path to a file or folder
  329. #endif
  330. #if ENABLED(SDCARD_SORT_ALPHA) && ENABLED(SDSORT_GCODE)
  331. case 34: M34(); break; // M34: Set SD card sorting options
  332. #endif
  333. case 928: M928(); break; // M928: Start SD write
  334. #endif // SDSUPPORT
  335. case 31: M31(); break; // M31: Report time since the start of SD print or last M109
  336. case 42: M42(); break; // M42: Change pin state
  337. #if ENABLED(PINS_DEBUGGING)
  338. case 43: M43(); break; // M43: Read pin state
  339. #endif
  340. #if ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST)
  341. case 48: M48(); break; // M48: Z probe repeatability test
  342. #endif
  343. #if ENABLED(UBL_G26_MESH_VALIDATION)
  344. case 49: M49(); break; // M49: Turn on or off G26 debug flag for verbose output
  345. #endif
  346. case 75: M75(); break; // M75: Start print timer
  347. case 76: M76(); break; // M76: Pause print timer
  348. case 77: M77(); break; // M77: Stop print timer
  349. #if ENABLED(PRINTCOUNTER)
  350. case 78: M78(); break; // M78: Show print statistics
  351. #endif
  352. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  353. case 100: M100(); break; // M100: Free Memory Report
  354. #endif
  355. case 104: M104(); break; // M104: Set hot end temperature
  356. case 109: M109(); break; // M109: Wait for hotend temperature to reach target
  357. case 110: M110(); break; // M110: Set Current Line Number
  358. case 111: M111(); break; // M111: Set debug level
  359. #if DISABLED(EMERGENCY_PARSER)
  360. case 108: M108(); break; // M108: Cancel Waiting
  361. case 112: M112(); break; // M112: Emergency Stop
  362. case 410: M410(); break; // M410: Quickstop - Abort all the planned moves.
  363. #endif
  364. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  365. case 113: M113(); break; // M113: Set Host Keepalive interval
  366. #endif
  367. #if HAS_HEATER_BED && HAS_TEMP_BED
  368. case 140: M140(); break; // M140: Set bed temperature
  369. case 190: M190(); break; // M190: Wait for bed temperature to reach target
  370. #endif
  371. case 105: // M105: Report current temperature
  372. M105();
  373. KEEPALIVE_STATE(NOT_BUSY);
  374. return; // "ok" already printed
  375. #if ENABLED(AUTO_REPORT_TEMPERATURES) && (HAS_TEMP_HOTEND || HAS_TEMP_BED)
  376. case 155: M155(); break; // M155: Set temperature auto-report interval
  377. #endif
  378. #if FAN_COUNT > 0
  379. case 106: M106(); break; // M106: Fan On
  380. case 107: M107(); break; // M107: Fan Off
  381. #endif
  382. #if ENABLED(PARK_HEAD_ON_PAUSE)
  383. case 125: // M125: Store current position and move to filament change position
  384. M125(); break;
  385. #endif
  386. #if ENABLED(BARICUDA)
  387. // PWM for HEATER_1_PIN
  388. #if HAS_HEATER_1
  389. case 126: M126(); break; // M126: valve open
  390. case 127: M127(); break; // M127: valve closed
  391. #endif
  392. // PWM for HEATER_2_PIN
  393. #if HAS_HEATER_2
  394. case 128: M128(); break; // M128: valve open
  395. case 129: M129(); break; // M129: valve closed
  396. #endif
  397. #endif // BARICUDA
  398. #if HAS_POWER_SWITCH
  399. case 80: M80(); break; // M80: Turn on Power Supply
  400. #endif
  401. case 81: M81(); break; // M81: Turn off Power, including Power Supply, if possible
  402. case 82: M82(); break; // M82: Set E axis normal mode (same as other axes)
  403. case 83: M83(); break; // M83: Set E axis relative mode
  404. case 18: // M18 => M84
  405. case 84: M18_M84(); break; // M84: Disable all steppers or set timeout
  406. case 85: M85(); break; // M85: Set inactivity stepper shutdown timeout
  407. case 92: M92(); break; // M92: Set the steps-per-unit for one or more axes
  408. case 114: M114(); break; // M114: Report current position
  409. case 115: M115(); break; // M115: Report capabilities
  410. case 117: M117(); break; // M117: Set LCD message text, if possible
  411. case 118: M118(); break; // M118: Display a message in the host console
  412. case 119: M119(); break; // M119: Report endstop states
  413. case 120: M120(); break; // M120: Enable endstops
  414. case 121: M121(); break; // M121: Disable endstops
  415. #if ENABLED(ULTIPANEL)
  416. case 145: M145(); break; // M145: Set material heatup parameters
  417. #endif
  418. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  419. case 149: M149(); break; // M149: Set temperature units
  420. #endif
  421. #if HAS_COLOR_LEDS
  422. case 150: M150(); break; // M150: Set Status LED Color
  423. #endif
  424. #if ENABLED(MIXING_EXTRUDER)
  425. case 163: // M163: Set a component weight for mixing extruder
  426. gcode_M163();
  427. break;
  428. #if MIXING_VIRTUAL_TOOLS > 1
  429. case 164: // M164: Save current mix as a virtual extruder
  430. gcode_M164();
  431. break;
  432. #endif
  433. #if ENABLED(DIRECT_MIXING_IN_G1)
  434. case 165: // M165: Set multiple mix weights
  435. gcode_M165();
  436. break;
  437. #endif
  438. #endif
  439. case 200: // M200: Set filament diameter, E to cubic units
  440. M200();
  441. break;
  442. case 201: M201(); break; // M201: Set max acceleration for print moves (units/s^2)
  443. #if 0
  444. case 202: M202(); break; // Not used for Sprinter/grbl gen6
  445. #endif
  446. case 203: // M203: Set max feedrate (units/sec)
  447. gcode_M203();
  448. break;
  449. case 204: // M204: Set acceleration
  450. gcode_M204();
  451. break;
  452. case 205: // M205: Set advanced settings
  453. gcode_M205();
  454. break;
  455. #if HAS_M206_COMMAND
  456. case 206: // M206: Set home offsets
  457. gcode_M206();
  458. break;
  459. #endif
  460. #if ENABLED(DELTA)
  461. case 665: // M665: Set delta configurations
  462. gcode_M665();
  463. break;
  464. #endif
  465. #if ENABLED(DELTA) || ENABLED(Z_DUAL_ENDSTOPS)
  466. case 666: // M666: Set delta or dual endstop adjustment
  467. gcode_M666();
  468. break;
  469. #endif
  470. #if ENABLED(FWRETRACT)
  471. case 207: M207(); break; // M207: Set Retract Length, Feedrate, and Z lift
  472. case 208: M208(); break; // M208: Set Recover (unretract) Additional Length and Feedrate
  473. case 209: if (MIN_AUTORETRACT <= MAX_AUTORETRACT) M209(); break; // M209: Turn Automatic Retract Detection on/off
  474. #endif
  475. case 211: // M211: Enable, Disable, and/or Report software endstops
  476. gcode_M211();
  477. break;
  478. #if HOTENDS > 1
  479. case 218: // M218: Set a tool offset
  480. M218();
  481. break;
  482. #endif
  483. case 220: // M220: Set Feedrate Percentage: S<percent> ("FR" on your LCD)
  484. gcode_M220();
  485. break;
  486. case 221: // M221: Set Flow Percentage
  487. M221();
  488. break;
  489. case 226: // M226: Wait until a pin reaches a state
  490. gcode_M226();
  491. break;
  492. #if HAS_SERVOS
  493. case 280: // M280: Set servo position absolute
  494. gcode_M280();
  495. break;
  496. #endif // HAS_SERVOS
  497. #if HAS_BUZZER
  498. case 300: // M300: Play beep tone
  499. gcode_M300();
  500. break;
  501. #endif // HAS_BUZZER
  502. #if ENABLED(PIDTEMP)
  503. case 301: // M301: Set hotend PID parameters
  504. gcode_M301();
  505. break;
  506. #endif // PIDTEMP
  507. #if ENABLED(PIDTEMPBED)
  508. case 304: // M304: Set bed PID parameters
  509. gcode_M304();
  510. break;
  511. #endif // PIDTEMPBED
  512. #if defined(CHDK) || HAS_PHOTOGRAPH
  513. case 240: // M240: Trigger a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  514. gcode_M240();
  515. break;
  516. #endif // CHDK || PHOTOGRAPH_PIN
  517. #if HAS_LCD_CONTRAST
  518. case 250: // M250: Set LCD contrast
  519. gcode_M250();
  520. break;
  521. #endif // HAS_LCD_CONTRAST
  522. #if ENABLED(EXPERIMENTAL_I2CBUS)
  523. case 260: // M260: Send data to an i2c slave
  524. gcode_M260();
  525. break;
  526. case 261: // M261: Request data from an i2c slave
  527. gcode_M261();
  528. break;
  529. #endif // EXPERIMENTAL_I2CBUS
  530. #if ENABLED(PREVENT_COLD_EXTRUSION)
  531. case 302: // M302: Allow cold extrudes (set the minimum extrude temperature)
  532. gcode_M302();
  533. break;
  534. #endif // PREVENT_COLD_EXTRUSION
  535. case 303: // M303: PID autotune
  536. M303();
  537. break;
  538. #if ENABLED(MORGAN_SCARA)
  539. case 360: // M360: SCARA Theta pos1
  540. if (gcode_M360()) return;
  541. break;
  542. case 361: // M361: SCARA Theta pos2
  543. if (gcode_M361()) return;
  544. break;
  545. case 362: // M362: SCARA Psi pos1
  546. if (gcode_M362()) return;
  547. break;
  548. case 363: // M363: SCARA Psi pos2
  549. if (gcode_M363()) return;
  550. break;
  551. case 364: // M364: SCARA Psi pos3 (90 deg to Theta)
  552. if (gcode_M364()) return;
  553. break;
  554. #endif // SCARA
  555. #if ENABLED(EXT_SOLENOID)
  556. case 380: // M380: Activate solenoid on active extruder
  557. gcode_M380();
  558. break;
  559. case 381: // M381: Disable all solenoids
  560. gcode_M381();
  561. break;
  562. #endif
  563. case 400: // M400: Finish all moves
  564. gcode_M400();
  565. break;
  566. #if HAS_BED_PROBE
  567. case 401: // M401: Deploy probe
  568. gcode_M401();
  569. break;
  570. case 402: // M402: Stow probe
  571. gcode_M402();
  572. break;
  573. #endif // HAS_BED_PROBE
  574. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  575. case 404: // M404: Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or display nominal filament width
  576. M404();
  577. break;
  578. case 405: // M405: Turn on filament sensor for control
  579. M405();
  580. break;
  581. case 406: // M406: Turn off filament sensor for control
  582. M406();
  583. break;
  584. case 407: // M407: Display measured filament diameter
  585. M407();
  586. break;
  587. #endif // FILAMENT_WIDTH_SENSOR
  588. #if HAS_LEVELING
  589. case 420: // M420: Enable/Disable Bed Leveling
  590. M420();
  591. break;
  592. #endif
  593. #if HAS_MESH
  594. case 421: // M421: Set a Mesh Bed Leveling Z coordinate
  595. M421();
  596. break;
  597. #endif
  598. #if HAS_M206_COMMAND
  599. case 428: // M428: Apply current_position to home_offset
  600. gcode_M428();
  601. break;
  602. #endif
  603. case 500: // M500: Store settings in EEPROM
  604. gcode_M500();
  605. break;
  606. case 501: // M501: Read settings from EEPROM
  607. gcode_M501();
  608. break;
  609. case 502: // M502: Revert to default settings
  610. gcode_M502();
  611. break;
  612. #if DISABLED(DISABLE_M503)
  613. case 503: // M503: print settings currently in memory
  614. gcode_M503();
  615. break;
  616. #endif
  617. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  618. case 540: // M540: Set abort on endstop hit for SD printing
  619. gcode_M540();
  620. break;
  621. #endif
  622. #if HAS_BED_PROBE
  623. case 851: // M851: Set Z Probe Z Offset
  624. M851();
  625. break;
  626. #endif // HAS_BED_PROBE
  627. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  628. case 600: // M600: Pause for filament change
  629. M600();
  630. break;
  631. #endif // ADVANCED_PAUSE_FEATURE
  632. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  633. case 605: // M605: Set Dual X Carriage movement mode
  634. gcode_M605();
  635. break;
  636. #endif // DUAL_X_CARRIAGE
  637. #if ENABLED(MK2_MULTIPLEXER)
  638. case 702: // M702: Unload all extruders
  639. gcode_M702();
  640. break;
  641. #endif
  642. #if ENABLED(LIN_ADVANCE)
  643. case 900: // M900: Set advance K factor.
  644. gcode_M900();
  645. break;
  646. #endif
  647. #if ENABLED(HAVE_TMC2130)
  648. case 906: // M906: Set motor current in milliamps using axis codes X, Y, Z, E
  649. gcode_M906();
  650. break;
  651. #endif
  652. case 907: // M907: Set digital trimpot motor current using axis codes.
  653. gcode_M907();
  654. break;
  655. #if HAS_DIGIPOTSS || ENABLED(DAC_STEPPER_CURRENT)
  656. case 908: // M908: Control digital trimpot directly.
  657. gcode_M908();
  658. break;
  659. #if ENABLED(DAC_STEPPER_CURRENT) // As with Printrbot RevF
  660. case 909: // M909: Print digipot/DAC current value
  661. gcode_M909();
  662. break;
  663. case 910: // M910: Commit digipot/DAC value to external EEPROM
  664. gcode_M910();
  665. break;
  666. #endif
  667. #endif // HAS_DIGIPOTSS || DAC_STEPPER_CURRENT
  668. #if ENABLED(HAVE_TMC2130)
  669. case 911: // M911: Report TMC2130 prewarn triggered flags
  670. gcode_M911();
  671. break;
  672. case 912: // M911: Clear TMC2130 prewarn triggered flags
  673. gcode_M912();
  674. break;
  675. #if ENABLED(HYBRID_THRESHOLD)
  676. case 913: // M913: Set HYBRID_THRESHOLD speed.
  677. gcode_M913();
  678. break;
  679. #endif
  680. #if ENABLED(SENSORLESS_HOMING)
  681. case 914: // M914: Set SENSORLESS_HOMING sensitivity.
  682. gcode_M914();
  683. break;
  684. #endif
  685. #endif
  686. #if HAS_MICROSTEPS
  687. case 350: // M350: Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  688. gcode_M350();
  689. break;
  690. case 351: // M351: Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  691. gcode_M351();
  692. break;
  693. #endif // HAS_MICROSTEPS
  694. case 355: // M355 set case light brightness
  695. gcode_M355();
  696. break;
  697. #if ENABLED(DEBUG_GCODE_PARSER)
  698. case 800:
  699. parser.debug(); // GCode Parser Test for M
  700. break;
  701. #endif
  702. #if ENABLED(I2C_POSITION_ENCODERS)
  703. case 860: M860(); break; // M860: Report encoder module position
  704. case 861: M861(); break; // M861: Report encoder module status
  705. case 862: M862(); break; // M862: Perform axis test
  706. case 863: M863(); break; // M863: Calibrate steps/mm
  707. case 864: M864(); break; // M864: Change module address
  708. case 865: M865(); break; // M865: Check module firmware version
  709. case 866: M866(); break; // M866: Report axis error count
  710. case 867: M867(); break; // M867: Toggle error correction
  711. case 868: M868(); break; // M868: Set error correction threshold
  712. case 869: M869(); break; // M869: Report axis error
  713. #endif
  714. case 999: // M999: Restart after being Stopped
  715. gcode_M999();
  716. break;
  717. }
  718. break;
  719. case 'T':
  720. gcode_T(parser.codenum);
  721. break;
  722. default: parser.unknown_command_error();
  723. }
  724. KEEPALIVE_STATE(NOT_BUSY);
  725. ok_to_send();
  726. }
  727. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  728. /**
  729. * Output a "busy" message at regular intervals
  730. * while the machine is not accepting commands.
  731. */
  732. void GcodeSuite::host_keepalive() {
  733. const millis_t ms = millis();
  734. static millis_t next_busy_signal_ms = 0;
  735. if (host_keepalive_interval && busy_state != NOT_BUSY) {
  736. if (PENDING(ms, next_busy_signal_ms)) return;
  737. switch (busy_state) {
  738. case IN_HANDLER:
  739. case IN_PROCESS:
  740. SERIAL_ECHO_START();
  741. SERIAL_ECHOLNPGM(MSG_BUSY_PROCESSING);
  742. break;
  743. case PAUSED_FOR_USER:
  744. SERIAL_ECHO_START();
  745. SERIAL_ECHOLNPGM(MSG_BUSY_PAUSED_FOR_USER);
  746. break;
  747. case PAUSED_FOR_INPUT:
  748. SERIAL_ECHO_START();
  749. SERIAL_ECHOLNPGM(MSG_BUSY_PAUSED_FOR_INPUT);
  750. break;
  751. default:
  752. break;
  753. }
  754. }
  755. next_busy_signal_ms = ms + host_keepalive_interval * 1000UL;
  756. }
  757. #endif // HOST_KEEPALIVE_FEATURE