My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

gcode.cpp 8.7KB

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