My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

parser.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. case 'X' ... 'Z': case 'E' ... 'F':
  209. if (motion_mode_codenum < 0) return;
  210. command_letter = 'G';
  211. codenum = motion_mode_codenum;
  212. TERN_(USE_GCODE_SUBCODES, subcode = motion_mode_subcode);
  213. p--; // Back up one character to use the current parameter
  214. break;
  215. #endif
  216. default: return;
  217. }
  218. // The command parameters (if any) start here, for sure!
  219. IF_DISABLED(FASTER_GCODE_PARSER, command_args = p); // Scan for parameters in seen()
  220. // Only use string_arg for these M codes
  221. if (letter == 'M') switch (codenum) {
  222. TERN_(GCODE_MACROS, case 810 ... 819:)
  223. TERN_(EXPECTED_PRINTER_CHECK, case 16:)
  224. case 23: case 28: case 30: case 117 ... 118: case 928:
  225. string_arg = unescape_string(p);
  226. return;
  227. default: break;
  228. }
  229. #if ENABLED(DEBUG_GCODE_PARSER)
  230. const bool debug = codenum == 800;
  231. #endif
  232. /**
  233. * Find all parameters, set flags and pointers for fast parsing
  234. *
  235. * Most codes ignore 'string_arg', but those that want a string will get the right pointer.
  236. * The following loop assigns the first "parameter" having no numeric value to 'string_arg'.
  237. * This allows M0/M1 with expire time to work: "M0 S5 You Win!"
  238. * For 'M118' you must use 'E1' and 'A1' rather than just 'E' or 'A'
  239. */
  240. #if ENABLED(GCODE_QUOTED_STRINGS)
  241. bool quoted_string_arg = false;
  242. #endif
  243. string_arg = nullptr;
  244. while (const char param = uppercase(*p++)) { // Get the next parameter. A NUL ends the loop
  245. // Special handling for M32 [P] !/path/to/file.g#
  246. // The path must be the last parameter
  247. if (param == '!' && is_command('M', 32)) {
  248. string_arg = p; // Name starts after '!'
  249. char * const lb = strchr(p, '#'); // Already seen '#' as SD char (to pause buffering)
  250. if (lb) *lb = '\0'; // Safe to mark the end of the filename
  251. return;
  252. }
  253. #if ENABLED(GCODE_QUOTED_STRINGS)
  254. if (!quoted_string_arg && param == '"') {
  255. quoted_string_arg = true;
  256. string_arg = unescape_string(p);
  257. }
  258. #endif
  259. #if ENABLED(FASTER_GCODE_PARSER)
  260. // Arguments MUST be uppercase for fast GCode parsing
  261. #define PARAM_OK(P) WITHIN((P), 'A', 'Z')
  262. #else
  263. #define PARAM_OK(P) true
  264. #endif
  265. if (PARAM_OK(param)) {
  266. while (*p == ' ') p++; // Skip spaces between parameters & values
  267. #if ENABLED(GCODE_QUOTED_STRINGS)
  268. const bool is_str = (*p == '"'), has_val = is_str || valid_float(p);
  269. char * const valptr = has_val ? is_str ? unescape_string(p) : p : nullptr;
  270. #else
  271. const bool has_val = valid_float(p);
  272. #if ENABLED(FASTER_GCODE_PARSER)
  273. char * const valptr = has_val ? p : nullptr;
  274. #endif
  275. #endif
  276. #if ENABLED(DEBUG_GCODE_PARSER)
  277. if (debug) {
  278. SERIAL_ECHOPAIR("Got param ", AS_CHAR(param), " at index ", p - command_ptr - 1);
  279. if (has_val) SERIAL_ECHOPGM(" (has_val)");
  280. }
  281. #endif
  282. if (!has_val && !string_arg) { // No value? First time, keep as string_arg
  283. string_arg = p - 1;
  284. #if ENABLED(DEBUG_GCODE_PARSER)
  285. if (debug) SERIAL_ECHOPAIR(" string_arg: ", hex_address((void*)string_arg)); // DEBUG
  286. #endif
  287. }
  288. if (TERN0(DEBUG_GCODE_PARSER, debug)) SERIAL_EOL();
  289. TERN_(FASTER_GCODE_PARSER, set(param, valptr)); // Set parameter exists and pointer (nullptr for no value)
  290. }
  291. else if (!string_arg) { // Not A-Z? First time, keep as the string_arg
  292. string_arg = p - 1;
  293. #if ENABLED(DEBUG_GCODE_PARSER)
  294. if (debug) SERIAL_ECHOPAIR(" string_arg: ", hex_address((void*)string_arg)); // DEBUG
  295. #endif
  296. }
  297. if (!WITHIN(*p, 'A', 'Z')) { // Another parameter right away?
  298. while (*p && DECIMAL_SIGNED(*p)) p++; // Skip over the value section of a parameter
  299. while (*p == ' ') p++; // Skip over all spaces
  300. }
  301. }
  302. }
  303. #if ENABLED(CNC_COORDINATE_SYSTEMS)
  304. // Parse the next parameter as a new command
  305. bool GCodeParser::chain() {
  306. #if ENABLED(FASTER_GCODE_PARSER)
  307. char *next_command = command_ptr;
  308. if (next_command) {
  309. while (*next_command && *next_command != ' ') ++next_command;
  310. while (*next_command == ' ') ++next_command;
  311. if (!*next_command) next_command = nullptr;
  312. }
  313. #else
  314. const char *next_command = command_args;
  315. #endif
  316. if (next_command) parse(next_command);
  317. return !!next_command;
  318. }
  319. #endif // CNC_COORDINATE_SYSTEMS
  320. void GCodeParser::unknown_command_warning() {
  321. SERIAL_ECHO_MSG(STR_UNKNOWN_COMMAND, command_ptr, "\"");
  322. }
  323. #if ENABLED(DEBUG_GCODE_PARSER)
  324. void GCodeParser::debug() {
  325. SERIAL_ECHOPAIR("Command: ", command_ptr, " (", command_letter);
  326. SERIAL_ECHO(codenum);
  327. SERIAL_ECHOLNPGM(")");
  328. #if ENABLED(FASTER_GCODE_PARSER)
  329. SERIAL_ECHOPGM(" args: { ");
  330. for (char c = 'A'; c <= 'Z'; ++c) if (seen(c)) SERIAL_CHAR(c, ' ');
  331. SERIAL_CHAR('}');
  332. #else
  333. SERIAL_ECHOPAIR(" args: { ", command_args, " }");
  334. #endif
  335. if (string_arg) {
  336. SERIAL_ECHOPAIR(" string: \"", string_arg);
  337. SERIAL_CHAR('"');
  338. }
  339. SERIAL_ECHOLNPGM("\n");
  340. for (char c = 'A'; c <= 'Z'; ++c) {
  341. if (seen(c)) {
  342. SERIAL_ECHOPAIR("Code '", c); SERIAL_ECHOPGM("':");
  343. if (has_value()) {
  344. SERIAL_ECHOLNPAIR(
  345. "\n float: ", value_float(),
  346. "\n long: ", value_long(),
  347. "\n ulong: ", value_ulong(),
  348. "\n millis: ", value_millis(),
  349. "\n sec-ms: ", value_millis_from_seconds(),
  350. "\n int: ", value_int(),
  351. "\n ushort: ", value_ushort(),
  352. "\n byte: ", value_byte(),
  353. "\n bool: ", value_bool(),
  354. "\n linear: ", value_linear_units(),
  355. "\n celsius: ", value_celsius()
  356. );
  357. }
  358. else
  359. SERIAL_ECHOLNPGM(" (no value)");
  360. }
  361. }
  362. }
  363. #endif // DEBUG_GCODE_PARSER