My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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