My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

parser.cpp 13KB

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