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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. * parser.cpp - Parser for a GCode line, providing a parameter interface.
  24. */
  25. #include "parser.h"
  26. #include "../MarlinCore.h"
  27. // Must be declared for allocation and to satisfy the linker
  28. // Zero values need no initialization.
  29. bool GCodeParser::volumetric_enabled;
  30. #if ENABLED(INCH_MODE_SUPPORT)
  31. float GCodeParser::linear_unit_factor, GCodeParser::volumetric_unit_factor;
  32. #endif
  33. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  34. TempUnit GCodeParser::input_temp_units = TEMPUNIT_C;
  35. #endif
  36. char *GCodeParser::command_ptr,
  37. *GCodeParser::string_arg,
  38. *GCodeParser::value_ptr;
  39. char GCodeParser::command_letter;
  40. uint16_t GCodeParser::codenum;
  41. #if USE_GCODE_SUBCODES
  42. uint8_t GCodeParser::subcode;
  43. #endif
  44. #if ENABLED(GCODE_MOTION_MODES)
  45. int16_t GCodeParser::motion_mode_codenum = -1;
  46. #if USE_GCODE_SUBCODES
  47. uint8_t GCodeParser::motion_mode_subcode;
  48. #endif
  49. #endif
  50. #if ENABLED(FASTER_GCODE_PARSER)
  51. // Optimized Parameters
  52. uint32_t GCodeParser::codebits; // found bits
  53. uint8_t GCodeParser::param[26]; // parameter offsets from command_ptr
  54. #else
  55. char *GCodeParser::command_args; // start of parameters
  56. #endif
  57. // Create a global instance of the GCode parser singleton
  58. GCodeParser parser;
  59. /**
  60. * Clear all code-seen (and value pointers)
  61. *
  62. * Since each param is set/cleared on seen codes,
  63. * this may be optimized by commenting out ZERO(param)
  64. */
  65. void GCodeParser::reset() {
  66. string_arg = nullptr; // No whole line argument
  67. command_letter = '?'; // No command letter
  68. codenum = 0; // No command code
  69. TERN_(USE_GCODE_SUBCODES, subcode = 0); // No command sub-code
  70. #if ENABLED(FASTER_GCODE_PARSER)
  71. codebits = 0; // No codes yet
  72. //ZERO(param); // No parameters (should be safe to comment out this line)
  73. #endif
  74. }
  75. #if ENABLED(GCODE_QUOTED_STRINGS)
  76. // Pass the address after the first quote (if any)
  77. char* GCodeParser::unescape_string(char* &src) {
  78. if (*src == '"') ++src; // Skip the leading quote
  79. char * const out = src; // Start of the string
  80. char *dst = src; // Prepare to unescape and terminate
  81. for (;;) {
  82. char c = *src++; // Get the next char
  83. switch (c) {
  84. case '\\': c = *src++; break; // Get the escaped char
  85. case '"' : c = '\0'; break; // Convert bare quote to nul
  86. }
  87. if (!(*dst++ = c)) break; // Copy and break on nul
  88. }
  89. return out;
  90. }
  91. #endif
  92. /**
  93. * Populate the command line state (command_letter, codenum, subcode, and string_arg)
  94. * by parsing a single line of GCode. 58 bytes of SRAM are used to speed up seen/value.
  95. */
  96. void GCodeParser::parse(char *p) {
  97. reset(); // No codes to report
  98. auto uppercase = [](char c) {
  99. if (TERN0(GCODE_CASE_INSENSITIVE, WITHIN(c, 'a', 'z')))
  100. c += 'A' - 'a';
  101. return c;
  102. };
  103. // Skip spaces
  104. while (*p == ' ') ++p;
  105. // Skip N[-0-9] if included in the command line
  106. if (uppercase(*p) == 'N' && NUMERIC_SIGNED(p[1])) {
  107. //TERN_(FASTER_GCODE_PARSER, set('N', p + 1)); // (optional) Set the 'N' parameter value
  108. p += 2; // skip N[-0-9]
  109. while (NUMERIC(*p)) ++p; // skip [0-9]*
  110. while (*p == ' ') ++p; // skip [ ]*
  111. }
  112. // *p now points to the current command, which should be G, M, or T
  113. command_ptr = p;
  114. // Get the command letter, which must be G, M, or T
  115. const char letter = uppercase(*p++);
  116. // Nullify asterisk and trailing whitespace
  117. char *starpos = strchr(p, '*');
  118. if (starpos) {
  119. --starpos; // *
  120. while (*starpos == ' ') --starpos; // spaces...
  121. starpos[1] = '\0';
  122. }
  123. #if ANY(MARLIN_DEV_MODE, SWITCHING_TOOLHEAD, MAGNETIC_SWITCHING_TOOLHEAD, ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  124. #define SIGNED_CODENUM 1
  125. #endif
  126. /**
  127. * Screen for good command letters.
  128. * With Realtime Reporting, commands S000, P000, and R000 are allowed.
  129. */
  130. #if ENABLED(REALTIME_REPORTING_COMMANDS)
  131. switch (letter) {
  132. case 'P': case 'R' ... 'S': {
  133. uint8_t digits = 0;
  134. char *a = p;
  135. while (*a++ == '0') digits++; // Count up '0' characters
  136. if (digits == 3) { // Three '0' digits is a good command
  137. codenum = 0;
  138. command_letter = letter;
  139. return;
  140. }
  141. }
  142. }
  143. #endif
  144. /**
  145. * Screen for good command letters. G, M, and T are always accepted.
  146. * With Motion Modes enabled any axis letter can come first.
  147. */
  148. switch (letter) {
  149. case 'G': case 'M': case 'T': TERN_(MARLIN_DEV_MODE, case 'D':) {
  150. // Skip spaces to get the numeric part
  151. while (*p == ' ') p++;
  152. #if HAS_PRUSA_MMU2
  153. if (letter == 'T') {
  154. // check for special MMU2 T?/Tx/Tc commands
  155. if (*p == '?' || *p == 'x' || *p == 'c') {
  156. command_letter = letter;
  157. string_arg = p;
  158. return;
  159. }
  160. }
  161. #endif
  162. // Bail if there's no command code number
  163. if (!TERN(SIGNED_CODENUM, NUMERIC_SIGNED(*p), NUMERIC(*p))) return;
  164. // Save the command letter at this point
  165. // A '?' signifies an unknown command
  166. command_letter = letter;
  167. #if ENABLED(SIGNED_CODENUM)
  168. int sign = 1; // Allow for a negative code like D-1 or T-1
  169. if (*p == '-') { sign = -1; ++p; }
  170. #endif
  171. // Get the code number - integer digits only
  172. codenum = 0;
  173. do { codenum = codenum * 10 + *p++ - '0'; } while (NUMERIC(*p));
  174. // Apply the sign, if any
  175. TERN_(SIGNED_CODENUM, codenum *= sign);
  176. // Allow for decimal point in command
  177. #if USE_GCODE_SUBCODES
  178. if (*p == '.') {
  179. p++;
  180. while (NUMERIC(*p))
  181. subcode = subcode * 10 + *p++ - '0';
  182. }
  183. #endif
  184. // Skip all spaces to get to the first argument, or nul
  185. while (*p == ' ') p++;
  186. #if ENABLED(GCODE_MOTION_MODES)
  187. if (letter == 'G'
  188. && (codenum <= TERN(ARC_SUPPORT, 3, 1) || TERN0(BEZIER_CURVE_SUPPORT, codenum == 5) || TERN0(G38_PROBE_TARGET, codenum == 38))
  189. ) {
  190. motion_mode_codenum = codenum;
  191. TERN_(USE_GCODE_SUBCODES, motion_mode_subcode = subcode);
  192. }
  193. #endif
  194. } break;
  195. #if ENABLED(GCODE_MOTION_MODES)
  196. #if EITHER(BEZIER_CURVE_SUPPORT, ARC_SUPPORT)
  197. case 'I' ... 'J': case 'P':
  198. if (TERN1(BEZIER_CURVE_SUPPORT, motion_mode_codenum != 5)
  199. && TERN1(ARC_P_CIRCLES, !WITHIN(motion_mode_codenum, 2, 3))
  200. ) return;
  201. #endif
  202. #if ENABLED(BEZIER_CURVE_SUPPORT)
  203. case 'Q': if (motion_mode_codenum != 5) return;
  204. #endif
  205. #if ENABLED(ARC_SUPPORT)
  206. case 'R': if (!WITHIN(motion_mode_codenum, 2, 3)) return;
  207. #endif
  208. LOGICAL_AXIS_GANG(case 'E':, case 'X':, case 'Y':, case 'Z':, case AXIS4_NAME:, case AXIS5_NAME:, case AXIS6_NAME:, case AXIS7_NAME:, case AXIS8_NAME:, case AXIS9_NAME:)
  209. case 'F':
  210. if (motion_mode_codenum < 0) return;
  211. command_letter = 'G';
  212. codenum = motion_mode_codenum;
  213. TERN_(USE_GCODE_SUBCODES, subcode = motion_mode_subcode);
  214. p--; // Back up one character to use the current parameter
  215. break;
  216. #endif
  217. default: return;
  218. }
  219. // The command parameters (if any) start here, for sure!
  220. IF_DISABLED(FASTER_GCODE_PARSER, command_args = p); // Scan for parameters in seen()
  221. // Only use string_arg for these M codes
  222. if (letter == 'M') switch (codenum) {
  223. TERN_(GCODE_MACROS, case 810 ... 819:)
  224. TERN_(EXPECTED_PRINTER_CHECK, case 16:)
  225. case 23: case 28: case 30: case 117 ... 118: case 928:
  226. string_arg = unescape_string(p);
  227. return;
  228. default: break;
  229. }
  230. #if ENABLED(DEBUG_GCODE_PARSER)
  231. const bool debug = codenum == 800;
  232. #endif
  233. /**
  234. * Find all parameters, set flags and pointers for fast parsing
  235. *
  236. * Most codes ignore 'string_arg', but those that want a string will get the right pointer.
  237. * The following loop assigns the first "parameter" having no numeric value to 'string_arg'.
  238. * This allows M0/M1 with expire time to work: "M0 S5 You Win!"
  239. * For 'M118' you must use 'E1' and 'A1' rather than just 'E' or 'A'
  240. */
  241. #if ENABLED(GCODE_QUOTED_STRINGS)
  242. bool quoted_string_arg = false;
  243. #endif
  244. string_arg = nullptr;
  245. while (const char param = uppercase(*p++)) { // Get the next parameter. A NUL ends the loop
  246. // Special handling for M32 [P] !/path/to/file.g#
  247. // The path must be the last parameter
  248. if (param == '!' && is_command('M', 32)) {
  249. string_arg = p; // Name starts after '!'
  250. char * const lb = strchr(p, '#'); // Already seen '#' as SD char (to pause buffering)
  251. if (lb) *lb = '\0'; // Safe to mark the end of the filename
  252. return;
  253. }
  254. #if ENABLED(GCODE_QUOTED_STRINGS)
  255. if (!quoted_string_arg && param == '"') {
  256. quoted_string_arg = true;
  257. string_arg = unescape_string(p);
  258. }
  259. #endif
  260. #if ENABLED(FASTER_GCODE_PARSER)
  261. // Arguments MUST be uppercase for fast GCode parsing
  262. #define PARAM_OK(P) WITHIN((P), 'A', 'Z')
  263. #else
  264. #define PARAM_OK(P) true
  265. #endif
  266. if (PARAM_OK(param)) {
  267. while (*p == ' ') p++; // Skip spaces between parameters & values
  268. #if ENABLED(GCODE_QUOTED_STRINGS)
  269. const bool is_str = (*p == '"'), has_val = is_str || valid_float(p);
  270. char * const valptr = has_val ? is_str ? unescape_string(p) : p : nullptr;
  271. #else
  272. const bool has_val = valid_float(p);
  273. #if ENABLED(FASTER_GCODE_PARSER)
  274. char * const valptr = has_val ? p : nullptr;
  275. #endif
  276. #endif
  277. #if ENABLED(DEBUG_GCODE_PARSER)
  278. if (debug) {
  279. SERIAL_ECHOPGM("Got param ", AS_CHAR(param), " at index ", p - command_ptr - 1);
  280. if (has_val) SERIAL_ECHOPGM(" (has_val)");
  281. }
  282. #endif
  283. if (!has_val && !string_arg) { // No value? First time, keep as string_arg
  284. string_arg = p - 1;
  285. #if ENABLED(DEBUG_GCODE_PARSER)
  286. if (debug) SERIAL_ECHOPGM(" string_arg: ", hex_address((void*)string_arg)); // DEBUG
  287. #endif
  288. }
  289. if (TERN0(DEBUG_GCODE_PARSER, debug)) SERIAL_EOL();
  290. TERN_(FASTER_GCODE_PARSER, set(param, valptr)); // Set parameter exists and pointer (nullptr for no value)
  291. }
  292. else if (!string_arg) { // Not A-Z? First time, keep as the string_arg
  293. string_arg = p - 1;
  294. #if ENABLED(DEBUG_GCODE_PARSER)
  295. if (debug) SERIAL_ECHOPGM(" string_arg: ", hex_address((void*)string_arg)); // DEBUG
  296. #endif
  297. }
  298. if (!WITHIN(*p, 'A', 'Z')) { // Another parameter right away?
  299. while (*p && DECIMAL_SIGNED(*p)) p++; // Skip over the value section of a parameter
  300. while (*p == ' ') p++; // Skip over all spaces
  301. }
  302. }
  303. }
  304. #if ENABLED(CNC_COORDINATE_SYSTEMS)
  305. // Parse the next parameter as a new command
  306. bool GCodeParser::chain() {
  307. #if ENABLED(FASTER_GCODE_PARSER)
  308. char *next_command = command_ptr;
  309. if (next_command) {
  310. while (*next_command && *next_command != ' ') ++next_command;
  311. while (*next_command == ' ') ++next_command;
  312. if (!*next_command) next_command = nullptr;
  313. }
  314. #else
  315. const char *next_command = command_args;
  316. #endif
  317. if (next_command) parse(next_command);
  318. return !!next_command;
  319. }
  320. #endif // CNC_COORDINATE_SYSTEMS
  321. void GCodeParser::unknown_command_warning() {
  322. SERIAL_ECHO_MSG(STR_UNKNOWN_COMMAND, command_ptr, "\"");
  323. }
  324. #if ENABLED(DEBUG_GCODE_PARSER)
  325. void GCodeParser::debug() {
  326. SERIAL_ECHOPGM("Command: ", command_ptr, " (", command_letter);
  327. SERIAL_ECHO(codenum);
  328. SERIAL_ECHOLNPGM(")");
  329. #if ENABLED(FASTER_GCODE_PARSER)
  330. SERIAL_ECHOPGM(" args: { ");
  331. for (char c = 'A'; c <= 'Z'; ++c) if (seen(c)) SERIAL_CHAR(c, ' ');
  332. SERIAL_CHAR('}');
  333. #else
  334. SERIAL_ECHOPGM(" args: { ", command_args, " }");
  335. #endif
  336. if (string_arg) {
  337. SERIAL_ECHOPGM(" string: \"", string_arg);
  338. SERIAL_CHAR('"');
  339. }
  340. SERIAL_ECHOLNPGM("\n");
  341. for (char c = 'A'; c <= 'Z'; ++c) {
  342. if (seen(c)) {
  343. SERIAL_ECHOPGM("Code '", c); SERIAL_ECHOPGM("':");
  344. if (has_value()) {
  345. SERIAL_ECHOLNPGM(
  346. "\n float: ", value_float(),
  347. "\n long: ", value_long(),
  348. "\n ulong: ", value_ulong(),
  349. "\n millis: ", value_millis(),
  350. "\n sec-ms: ", value_millis_from_seconds(),
  351. "\n int: ", value_int(),
  352. "\n ushort: ", value_ushort(),
  353. "\n byte: ", value_byte(),
  354. "\n bool: ", value_bool(),
  355. "\n linear: ", value_linear_units(),
  356. "\n celsius: ", value_celsius()
  357. );
  358. }
  359. else
  360. SERIAL_ECHOLNPGM(" (no value)");
  361. }
  362. }
  363. }
  364. #endif // DEBUG_GCODE_PARSER