parser.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. #include "language.h"
  28. // Must be declared for allocation and to satisfy the linker
  29. // Zero values need no initialization.
  30. bool GCodeParser::volumetric_enabled;
  31. #if ENABLED(INCH_MODE_SUPPORT)
  32. float GCodeParser::linear_unit_factor, GCodeParser::volumetric_unit_factor;
  33. #endif
  34. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  35. TempUnit GCodeParser::input_temp_units;
  36. #endif
  37. char *GCodeParser::command_ptr,
  38. *GCodeParser::string_arg,
  39. *GCodeParser::value_ptr;
  40. char GCodeParser::command_letter;
  41. int GCodeParser::codenum;
  42. #if USE_GCODE_SUBCODES
  43. uint8_t GCodeParser::subcode;
  44. #endif
  45. #if ENABLED(FASTER_GCODE_PARSER)
  46. // Optimized Parameters
  47. uint32_t GCodeParser::codebits; // found bits
  48. uint8_t GCodeParser::param[26]; // parameter offsets from command_ptr
  49. #else
  50. char *GCodeParser::command_args; // start of parameters
  51. #endif
  52. // Create a global instance of the GCode parser singleton
  53. GCodeParser parser;
  54. /**
  55. * Clear all code-seen (and value pointers)
  56. *
  57. * Since each param is set/cleared on seen codes,
  58. * this may be optimized by commenting out ZERO(param)
  59. */
  60. void GCodeParser::reset() {
  61. string_arg = NULL; // No whole line argument
  62. command_letter = '?'; // No command letter
  63. codenum = 0; // No command code
  64. #if USE_GCODE_SUBCODES
  65. subcode = 0; // No command sub-code
  66. #endif
  67. #if ENABLED(FASTER_GCODE_PARSER)
  68. codebits = 0; // No codes yet
  69. //ZERO(param); // No parameters (should be safe to comment out this line)
  70. #endif
  71. }
  72. // Populate all fields by parsing a single line of GCode
  73. // 58 bytes of SRAM are used to speed up seen/value
  74. void GCodeParser::parse(char *p) {
  75. reset(); // No codes to report
  76. // Skip spaces
  77. while (*p == ' ') ++p;
  78. // Skip N[-0-9] if included in the command line
  79. if (*p == 'N' && NUMERIC_SIGNED(p[1])) {
  80. #if ENABLED(FASTER_GCODE_PARSER)
  81. //set('N', p + 1); // (optional) Set the 'N' parameter value
  82. #endif
  83. p += 2; // skip N[-0-9]
  84. while (NUMERIC(*p)) ++p; // skip [0-9]*
  85. while (*p == ' ') ++p; // skip [ ]*
  86. }
  87. // *p now points to the current command, which should be G, M, or T
  88. command_ptr = p;
  89. // Get the command letter, which must be G, M, or T
  90. const char letter = *p++;
  91. // Nullify asterisk and trailing whitespace
  92. char *starpos = strchr(p, '*');
  93. if (starpos) {
  94. --starpos; // *
  95. while (*starpos == ' ') --starpos; // spaces...
  96. starpos[1] = '\0';
  97. }
  98. // Bail if the letter is not G, M, or T
  99. switch (letter) { case 'G': case 'M': case 'T': break; default: return; }
  100. // Skip spaces to get the numeric part
  101. while (*p == ' ') p++;
  102. // Bail if there's no command code number
  103. if (!NUMERIC(*p)) return;
  104. // Save the command letter at this point
  105. // A '?' signifies an unknown command
  106. command_letter = letter;
  107. // Get the code number - integer digits only
  108. codenum = 0;
  109. do {
  110. codenum *= 10, codenum += *p++ - '0';
  111. } while (NUMERIC(*p));
  112. // Allow for decimal point in command
  113. #if USE_GCODE_SUBCODES
  114. if (*p == '.') {
  115. p++;
  116. while (NUMERIC(*p))
  117. subcode *= 10, subcode += *p++ - '0';
  118. }
  119. #endif
  120. // Skip all spaces to get to the first argument, or nul
  121. while (*p == ' ') p++;
  122. // The command parameters (if any) start here, for sure!
  123. #if DISABLED(FASTER_GCODE_PARSER)
  124. command_args = p; // Scan for parameters in seen()
  125. #endif
  126. // Only use string_arg for these M codes
  127. if (letter == 'M') switch (codenum) { case 23: case 28: case 30: case 117: case 118: case 928: string_arg = p; return; default: break; }
  128. #if ENABLED(DEBUG_GCODE_PARSER)
  129. const bool debug = codenum == 800;
  130. #endif
  131. /**
  132. * Find all parameters, set flags and pointers for fast parsing
  133. *
  134. * Most codes ignore 'string_arg', but those that want a string will get the right pointer.
  135. * The following loop assigns the first "parameter" having no numeric value to 'string_arg'.
  136. * This allows M0/M1 with expire time to work: "M0 S5 You Win!"
  137. * For 'M118' you must use 'E1' and 'A1' rather than just 'E' or 'A'
  138. */
  139. string_arg = NULL;
  140. while (const char code = *p++) { // Get the next parameter. A NUL ends the loop
  141. // Special handling for M32 [P] !/path/to/file.g#
  142. // The path must be the last parameter
  143. if (code == '!' && letter == 'M' && codenum == 32) {
  144. string_arg = p; // Name starts after '!'
  145. char * const lb = strchr(p, '#'); // Already seen '#' as SD char (to pause buffering)
  146. if (lb) *lb = '\0'; // Safe to mark the end of the filename
  147. return;
  148. }
  149. // Arguments MUST be uppercase for fast GCode parsing
  150. #if ENABLED(FASTER_GCODE_PARSER)
  151. #define PARAM_TEST WITHIN(code, 'A', 'Z')
  152. #else
  153. #define PARAM_TEST true
  154. #endif
  155. if (PARAM_TEST) {
  156. while (*p == ' ') p++; // Skip spaces between parameters & values
  157. const bool has_num = valid_float(p);
  158. #if ENABLED(DEBUG_GCODE_PARSER)
  159. if (debug) {
  160. SERIAL_ECHOPAIR("Got letter ", code);
  161. SERIAL_ECHOPAIR(" at index ", (int)(p - command_ptr - 1));
  162. if (has_num) SERIAL_ECHOPGM(" (has_num)");
  163. }
  164. #endif
  165. if (!has_num && !string_arg) { // No value? First time, keep as string_arg
  166. string_arg = p - 1;
  167. #if ENABLED(DEBUG_GCODE_PARSER)
  168. if (debug) SERIAL_ECHOPAIR(" string_arg: ", hex_address((void*)string_arg)); // DEBUG
  169. #endif
  170. }
  171. #if ENABLED(DEBUG_GCODE_PARSER)
  172. if (debug) SERIAL_EOL();
  173. #endif
  174. #if ENABLED(FASTER_GCODE_PARSER)
  175. set(code, has_num ? p : NULL); // Set parameter exists and pointer (NULL for no number)
  176. #endif
  177. }
  178. else if (!string_arg) { // Not A-Z? First time, keep as the string_arg
  179. string_arg = p - 1;
  180. #if ENABLED(DEBUG_GCODE_PARSER)
  181. if (debug) SERIAL_ECHOPAIR(" string_arg: ", hex_address((void*)string_arg)); // DEBUG
  182. #endif
  183. }
  184. if (!WITHIN(*p, 'A', 'Z')) { // Another parameter right away?
  185. while (*p && DECIMAL_SIGNED(*p)) p++; // Skip over the value section of a parameter
  186. while (*p == ' ') p++; // Skip over all spaces
  187. }
  188. }
  189. }
  190. #if ENABLED(CNC_COORDINATE_SYSTEMS)
  191. // Parse the next parameter as a new command
  192. bool GCodeParser::chain() {
  193. #if ENABLED(FASTER_GCODE_PARSER)
  194. char *next_command = command_ptr;
  195. if (next_command) {
  196. while (*next_command && *next_command != ' ') ++next_command;
  197. while (*next_command == ' ') ++next_command;
  198. if (!*next_command) next_command = NULL;
  199. }
  200. #else
  201. const char *next_command = command_args;
  202. #endif
  203. if (next_command) parse(next_command);
  204. return !!next_command;
  205. }
  206. #endif // CNC_COORDINATE_SYSTEMS
  207. void GCodeParser::unknown_command_error() {
  208. SERIAL_ECHO_START();
  209. SERIAL_ECHOPAIR(MSG_UNKNOWN_COMMAND, command_ptr);
  210. SERIAL_CHAR('"');
  211. SERIAL_EOL();
  212. }
  213. #if ENABLED(DEBUG_GCODE_PARSER)
  214. void GCodeParser::debug() {
  215. SERIAL_ECHOPAIR("Command: ", command_ptr);
  216. SERIAL_ECHOPAIR(" (", command_letter);
  217. SERIAL_ECHO(codenum);
  218. SERIAL_ECHOLNPGM(")");
  219. #if ENABLED(FASTER_GCODE_PARSER)
  220. SERIAL_ECHOPGM(" args: \"");
  221. for (char c = 'A'; c <= 'Z'; ++c)
  222. if (seen(c)) { SERIAL_CHAR(c); SERIAL_CHAR(' '); }
  223. #else
  224. SERIAL_ECHOPAIR(" args: \"", command_args);
  225. #endif
  226. SERIAL_CHAR('"');
  227. if (string_arg) {
  228. SERIAL_ECHOPGM(" string: \"");
  229. SERIAL_ECHO(string_arg);
  230. SERIAL_CHAR('"');
  231. }
  232. SERIAL_ECHOPGM("\n\n");
  233. for (char c = 'A'; c <= 'Z'; ++c) {
  234. if (seen(c)) {
  235. SERIAL_ECHOPAIR("Code '", c); SERIAL_ECHOPGM("':");
  236. if (has_value()) {
  237. SERIAL_ECHOPAIR("\n float: ", value_float());
  238. SERIAL_ECHOPAIR("\n long: ", value_long());
  239. SERIAL_ECHOPAIR("\n ulong: ", value_ulong());
  240. SERIAL_ECHOPAIR("\n millis: ", value_millis());
  241. SERIAL_ECHOPAIR("\n sec-ms: ", value_millis_from_seconds());
  242. SERIAL_ECHOPAIR("\n int: ", value_int());
  243. SERIAL_ECHOPAIR("\n ushort: ", value_ushort());
  244. SERIAL_ECHOPAIR("\n byte: ", (int)value_byte());
  245. SERIAL_ECHOPAIR("\n bool: ", (int)value_bool());
  246. SERIAL_ECHOPAIR("\n linear: ", value_linear_units());
  247. SERIAL_ECHOPAIR("\n celsius: ", value_celsius());
  248. }
  249. else
  250. SERIAL_ECHOPGM(" (no value)");
  251. SERIAL_ECHOPGM("\n\n");
  252. }
  253. }
  254. }
  255. #endif // DEBUG_GCODE_PARSER