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.

parser.cpp 11KB

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