My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

parser.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. #pragma once
  23. /**
  24. * parser.h - Parser for a GCode line, providing a parameter interface.
  25. * Codes like M149 control the way the GCode parser behaves,
  26. * so settings for these codes are located in this class.
  27. */
  28. #include "../inc/MarlinConfig.h"
  29. //#define DEBUG_GCODE_PARSER
  30. #if ENABLED(DEBUG_GCODE_PARSER)
  31. #include "../libs/hex_print.h"
  32. #endif
  33. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  34. typedef enum : uint8_t { TEMPUNIT_C, TEMPUNIT_K, TEMPUNIT_F } TempUnit;
  35. #endif
  36. #if ENABLED(INCH_MODE_SUPPORT)
  37. typedef enum : uint8_t { LINEARUNIT_MM, LINEARUNIT_INCH } LinearUnit;
  38. #endif
  39. /**
  40. * GCode parser
  41. *
  42. * - Parse a single G-code line for its letter, code, subcode, and parameters
  43. * - FASTER_GCODE_PARSER:
  44. * - Flags existing params (1 bit each)
  45. * - Stores value offsets (1 byte each)
  46. * - Provide accessors for parameters:
  47. * - Parameter exists
  48. * - Parameter has value
  49. * - Parameter value in different units and types
  50. */
  51. class GCodeParser {
  52. private:
  53. static char *value_ptr; // Set by seen, used to fetch the value
  54. #if ENABLED(FASTER_GCODE_PARSER)
  55. static uint32_t codebits; // Parameters pre-scanned
  56. static uint8_t param[26]; // For A-Z, offsets into command args
  57. #else
  58. static char *command_args; // Args start here, for slow scan
  59. #endif
  60. public:
  61. // Global states for GCode-level units features
  62. static bool volumetric_enabled;
  63. #if ENABLED(INCH_MODE_SUPPORT)
  64. static float linear_unit_factor, volumetric_unit_factor;
  65. #endif
  66. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  67. static TempUnit input_temp_units;
  68. #endif
  69. // Command line state
  70. static char *command_ptr, // The command, so it can be echoed
  71. *string_arg, // string of command line
  72. command_letter; // G, M, or T
  73. static uint16_t codenum; // 123
  74. #if USE_GCODE_SUBCODES
  75. static uint8_t subcode; // .1
  76. #endif
  77. #if ENABLED(GCODE_MOTION_MODES)
  78. static int16_t motion_mode_codenum;
  79. #if USE_GCODE_SUBCODES
  80. static uint8_t motion_mode_subcode;
  81. #endif
  82. FORCE_INLINE static void cancel_motion_mode() { motion_mode_codenum = -1; }
  83. #endif
  84. #if ENABLED(DEBUG_GCODE_PARSER)
  85. static void debug();
  86. #endif
  87. // Reset is done before parsing
  88. static void reset();
  89. #define LETTER_BIT(N) ((N) - 'A')
  90. FORCE_INLINE static bool valid_signless(const char * const p) {
  91. return NUMERIC(p[0]) || (p[0] == '.' && NUMERIC(p[1])); // .?[0-9]
  92. }
  93. FORCE_INLINE static bool valid_float(const char * const p) {
  94. return valid_signless(p) || ((p[0] == '-' || p[0] == '+') && valid_signless(&p[1])); // [-+]?.?[0-9]
  95. }
  96. FORCE_INLINE static bool valid_number(const char * const p) {
  97. // TODO: With MARLIN_DEV_MODE allow HEX values starting with "x"
  98. return valid_float(p);
  99. }
  100. #if ENABLED(FASTER_GCODE_PARSER)
  101. FORCE_INLINE static bool valid_int(const char * const p) {
  102. return NUMERIC(p[0]) || ((p[0] == '-' || p[0] == '+') && NUMERIC(p[1])); // [-+]?[0-9]
  103. }
  104. // Set the flag and pointer for a parameter
  105. static void set(const char c, char * const ptr) {
  106. const uint8_t ind = LETTER_BIT(c);
  107. if (ind >= COUNT(param)) return; // Only A-Z
  108. SBI32(codebits, ind); // parameter exists
  109. param[ind] = ptr ? ptr - command_ptr : 0; // parameter offset or 0
  110. #if ENABLED(DEBUG_GCODE_PARSER)
  111. if (codenum == 800) {
  112. SERIAL_ECHOPGM("Set bit ", ind, " of codebits (", hex_address((void*)(codebits >> 16)));
  113. print_hex_word((uint16_t)(codebits & 0xFFFF));
  114. SERIAL_ECHOLNPGM(") | param = ", param[ind]);
  115. }
  116. #endif
  117. }
  118. // Code seen bit was set. If not found, value_ptr is unchanged.
  119. // This allows "if (seen('A')||seen('B'))" to use the last-found value.
  120. static bool seen(const char c) {
  121. const uint8_t ind = LETTER_BIT(c);
  122. if (ind >= COUNT(param)) return false; // Only A-Z
  123. const bool b = TEST32(codebits, ind);
  124. if (b) {
  125. if (param[ind]) {
  126. char * const ptr = command_ptr + param[ind];
  127. value_ptr = valid_number(ptr) ? ptr : nullptr;
  128. }
  129. else
  130. value_ptr = nullptr;
  131. }
  132. return b;
  133. }
  134. FORCE_INLINE static constexpr uint32_t letter_bits(const char * const str) {
  135. return (str[0] ? _BV32(LETTER_BIT(str[0])) |
  136. (str[1] ? _BV32(LETTER_BIT(str[1])) |
  137. (str[2] ? _BV32(LETTER_BIT(str[2])) |
  138. (str[3] ? _BV32(LETTER_BIT(str[3])) |
  139. (str[4] ? _BV32(LETTER_BIT(str[4])) |
  140. (str[5] ? _BV32(LETTER_BIT(str[5])) |
  141. (str[6] ? _BV32(LETTER_BIT(str[6])) |
  142. (str[7] ? _BV32(LETTER_BIT(str[7])) |
  143. (str[8] ? _BV32(LETTER_BIT(str[8])) |
  144. (str[9] ? _BV32(LETTER_BIT(str[9]))
  145. : 0) : 0) : 0) : 0) : 0) : 0) : 0) : 0) : 0) : 0);
  146. }
  147. // At least one of a list of code letters was seen
  148. #ifdef CPU_32_BIT
  149. FORCE_INLINE static bool seen(const char * const str) { return !!(codebits & letter_bits(str)); }
  150. #else
  151. FORCE_INLINE static bool seen(const char * const str) {
  152. const uint32_t letrbits = letter_bits(str);
  153. const uint8_t * const cb = (uint8_t*)&codebits;
  154. const uint8_t * const lb = (uint8_t*)&letrbits;
  155. return (cb[0] & lb[0]) || (cb[1] & lb[1]) || (cb[2] & lb[2]) || (cb[3] & lb[3]);
  156. }
  157. #endif
  158. static bool seen_any() { return !!codebits; }
  159. FORCE_INLINE static bool seen_test(const char c) { return TEST32(codebits, LETTER_BIT(c)); }
  160. #else // !FASTER_GCODE_PARSER
  161. #if ENABLED(GCODE_CASE_INSENSITIVE)
  162. FORCE_INLINE static char* strgchr(char *p, char g) {
  163. auto uppercase = [](char c) {
  164. return c + (WITHIN(c, 'a', 'z') ? 'A' - 'a' : 0);
  165. };
  166. const char d = uppercase(g);
  167. for (char cc; (cc = uppercase(*p)); p++) if (cc == d) return p;
  168. return nullptr;
  169. }
  170. #else
  171. #define strgchr strchr
  172. #endif
  173. // Code is found in the string. If not found, value_ptr is unchanged.
  174. // This allows "if (seen('A')||seen('B'))" to use the last-found value.
  175. static bool seen(const char c) {
  176. char *p = strgchr(command_args, c);
  177. const bool b = !!p;
  178. if (b) value_ptr = valid_number(&p[1]) ? &p[1] : nullptr;
  179. return b;
  180. }
  181. static bool seen_any() { return *command_args == '\0'; }
  182. FORCE_INLINE static bool seen_test(const char c) { return (bool)strgchr(command_args, c); }
  183. // At least one of a list of code letters was seen
  184. static bool seen(const char * const str) {
  185. for (uint8_t i = 0; const char c = str[i]; i++)
  186. if (seen_test(c)) return true;
  187. return false;
  188. }
  189. #endif // !FASTER_GCODE_PARSER
  190. // Seen any axis parameter
  191. static bool seen_axis() { return seen(STR_AXES_LOGICAL); }
  192. #if ENABLED(GCODE_QUOTED_STRINGS)
  193. static char* unescape_string(char* &src);
  194. #else
  195. FORCE_INLINE static char* unescape_string(char* &src) { return src; }
  196. #endif
  197. // Populate all fields by parsing a single line of GCode
  198. // This uses 54 bytes of SRAM to speed up seen/value
  199. static void parse(char * p);
  200. #if ENABLED(CNC_COORDINATE_SYSTEMS)
  201. // Parse the next parameter as a new command
  202. static bool chain();
  203. #endif
  204. // Test whether the parsed command matches the input
  205. static bool is_command(const char ltr, const uint16_t num) { return command_letter == ltr && codenum == num; }
  206. // The code value pointer was set
  207. FORCE_INLINE static bool has_value() { return !!value_ptr; }
  208. // Seen a parameter with a value
  209. static bool seenval(const char c) { return seen(c) && has_value(); }
  210. // The value as a string
  211. static char* value_string() { return value_ptr; }
  212. // Float removes 'E' to prevent scientific notation interpretation
  213. static float value_float() {
  214. if (!value_ptr) return 0;
  215. char *e = value_ptr;
  216. for (;;) {
  217. const char c = *e;
  218. if (c == '\0' || c == ' ') break;
  219. if (c == 'E' || c == 'e' || c == 'X' || c == 'x') {
  220. *e = '\0';
  221. const float ret = strtof(value_ptr, nullptr);
  222. *e = c;
  223. return ret;
  224. }
  225. ++e;
  226. }
  227. return strtof(value_ptr, nullptr);
  228. }
  229. // Code value as a long or ulong
  230. static int32_t value_long() { return value_ptr ? strtol(value_ptr, nullptr, 10) : 0L; }
  231. static uint32_t value_ulong() { return value_ptr ? strtoul(value_ptr, nullptr, 10) : 0UL; }
  232. // Code value for use as time
  233. static millis_t value_millis() { return value_ulong(); }
  234. static millis_t value_millis_from_seconds() { return (millis_t)SEC_TO_MS(value_float()); }
  235. // Reduce to fewer bits
  236. static int16_t value_int() { return (int16_t)value_long(); }
  237. static uint16_t value_ushort() { return (uint16_t)value_long(); }
  238. static uint8_t value_byte() { return (uint8_t)constrain(value_long(), 0, 255); }
  239. // Bool is true with no value or non-zero
  240. static bool value_bool() { return !has_value() || !!value_byte(); }
  241. // Units modes: Inches, Fahrenheit, Kelvin
  242. #if ENABLED(INCH_MODE_SUPPORT)
  243. static float mm_to_linear_unit(const_float_t mm) { return mm / linear_unit_factor; }
  244. static float mm_to_volumetric_unit(const_float_t mm) { return mm / (volumetric_enabled ? volumetric_unit_factor : linear_unit_factor); }
  245. // Init linear units by constructor
  246. GCodeParser() { set_input_linear_units(LINEARUNIT_MM); }
  247. static void set_input_linear_units(const LinearUnit units) {
  248. switch (units) {
  249. default:
  250. case LINEARUNIT_MM: linear_unit_factor = 1.0f; break;
  251. case LINEARUNIT_INCH: linear_unit_factor = 25.4f; break;
  252. }
  253. volumetric_unit_factor = POW(linear_unit_factor, 3);
  254. }
  255. static float axis_unit_factor(const AxisEnum axis) {
  256. if (false
  257. || TERN0(AXIS4_ROTATES, axis == I_AXIS)
  258. || TERN0(AXIS5_ROTATES, axis == J_AXIS)
  259. || TERN0(AXIS6_ROTATES, axis == K_AXIS)
  260. || TERN0(AXIS7_ROTATES, axis == U_AXIS)
  261. || TERN0(AXIS8_ROTATES, axis == V_AXIS)
  262. || TERN0(AXIS9_ROTATES, axis == W_AXIS)
  263. ) return 1.0f;
  264. #if HAS_EXTRUDERS
  265. if (axis >= E_AXIS && volumetric_enabled) return volumetric_unit_factor;
  266. #endif
  267. return linear_unit_factor;
  268. }
  269. static float linear_value_to_mm(const_float_t v) { return v * linear_unit_factor; }
  270. static float axis_value_to_mm(const AxisEnum axis, const float v) { return v * axis_unit_factor(axis); }
  271. static float per_axis_value(const AxisEnum axis, const float v) { return v / axis_unit_factor(axis); }
  272. #else
  273. static float mm_to_linear_unit(const_float_t mm) { return mm; }
  274. static float mm_to_volumetric_unit(const_float_t mm) { return mm; }
  275. static float linear_value_to_mm(const_float_t v) { return v; }
  276. static float axis_value_to_mm(const AxisEnum, const float v) { return v; }
  277. static float per_axis_value(const AxisEnum, const float v) { return v; }
  278. #endif
  279. static bool using_inch_units() { return mm_to_linear_unit(1.0f) != 1.0f; }
  280. #define IN_TO_MM(I) ((I) * 25.4f)
  281. #define MM_TO_IN(M) ((M) / 25.4f)
  282. #define LINEAR_UNIT(V) parser.mm_to_linear_unit(V)
  283. #define VOLUMETRIC_UNIT(V) parser.mm_to_volumetric_unit(V)
  284. #define I_AXIS_UNIT(V) TERN(AXIS4_ROTATES, (V), LINEAR_UNIT(V))
  285. #define J_AXIS_UNIT(V) TERN(AXIS5_ROTATES, (V), LINEAR_UNIT(V))
  286. #define K_AXIS_UNIT(V) TERN(AXIS6_ROTATES, (V), LINEAR_UNIT(V))
  287. #define U_AXIS_UNIT(V) TERN(AXIS7_ROTATES, (V), LINEAR_UNIT(V))
  288. #define V_AXIS_UNIT(V) TERN(AXIS8_ROTATES, (V), LINEAR_UNIT(V))
  289. #define W_AXIS_UNIT(V) TERN(AXIS9_ROTATES, (V), LINEAR_UNIT(V))
  290. static float value_linear_units() { return linear_value_to_mm(value_float()); }
  291. static float value_axis_units(const AxisEnum axis) { return axis_value_to_mm(axis, value_float()); }
  292. static float value_per_axis_units(const AxisEnum axis) { return per_axis_value(axis, value_float()); }
  293. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  294. static void set_input_temp_units(const TempUnit units) { input_temp_units = units; }
  295. static char temp_units_code() {
  296. return input_temp_units == TEMPUNIT_K ? 'K' : input_temp_units == TEMPUNIT_F ? 'F' : 'C';
  297. }
  298. static FSTR_P temp_units_name() {
  299. return input_temp_units == TEMPUNIT_K ? F("Kelvin") : input_temp_units == TEMPUNIT_F ? F("Fahrenheit") : F("Celsius");
  300. }
  301. #if HAS_MARLINUI_MENU && DISABLED(DISABLE_M503)
  302. static float to_temp_units(celsius_t c) {
  303. switch (input_temp_units) {
  304. default:
  305. case TEMPUNIT_C: return c;
  306. case TEMPUNIT_K: return c + 273.15f;
  307. case TEMPUNIT_F: return c * 0.5555555556f + 32;
  308. }
  309. }
  310. #endif // HAS_MARLINUI_MENU && !DISABLE_M503
  311. static celsius_t value_celsius() {
  312. float f = value_float();
  313. switch (input_temp_units) {
  314. default:
  315. case TEMPUNIT_C: break;
  316. case TEMPUNIT_K: f -= 273.15f;
  317. case TEMPUNIT_F: f = (f - 32) * 0.5555555556f;
  318. }
  319. return LROUND(f);
  320. }
  321. static celsius_t value_celsius_diff() {
  322. float f = value_float();
  323. switch (input_temp_units) {
  324. default:
  325. case TEMPUNIT_C:
  326. case TEMPUNIT_K: break;
  327. case TEMPUNIT_F: f *= 0.5555555556f;
  328. }
  329. return LROUND(f);
  330. }
  331. #else // !TEMPERATURE_UNITS_SUPPORT
  332. static float to_temp_units(int16_t c) { return (float)c; }
  333. static celsius_t value_celsius() { return value_int(); }
  334. static celsius_t value_celsius_diff() { return value_int(); }
  335. #endif // !TEMPERATURE_UNITS_SUPPORT
  336. static feedRate_t value_feedrate() { return MMM_TO_MMS(value_linear_units()); }
  337. void unknown_command_warning();
  338. // Provide simple value accessors with default option
  339. static char* stringval(const char c, char * const dval=nullptr) { return seenval(c) ? value_string() : dval; }
  340. static float floatval(const char c, const float dval=0.0) { return seenval(c) ? value_float() : dval; }
  341. static bool boolval(const char c, const bool dval=false) { return seenval(c) ? value_bool() : (seen(c) ? true : dval); }
  342. static uint8_t byteval(const char c, const uint8_t dval=0) { return seenval(c) ? value_byte() : dval; }
  343. static int16_t intval(const char c, const int16_t dval=0) { return seenval(c) ? value_int() : dval; }
  344. static uint16_t ushortval(const char c, const uint16_t dval=0) { return seenval(c) ? value_ushort() : dval; }
  345. static int32_t longval(const char c, const int32_t dval=0) { return seenval(c) ? value_long() : dval; }
  346. static uint32_t ulongval(const char c, const uint32_t dval=0) { return seenval(c) ? value_ulong() : dval; }
  347. static float linearval(const char c, const float dval=0) { return seenval(c) ? value_linear_units() : dval; }
  348. static float axisunitsval(const char c, const AxisEnum a, const float dval=0)
  349. { return seenval(c) ? value_axis_units(a) : dval; }
  350. static celsius_t celsiusval(const char c, const celsius_t dval=0) { return seenval(c) ? value_celsius() : dval; }
  351. static feedRate_t feedrateval(const char c, const feedRate_t dval=0) { return seenval(c) ? value_feedrate() : dval; }
  352. #if ENABLED(MARLIN_DEV_MODE)
  353. static uint8_t* hex_adr_val(const char c, uint8_t * const dval=nullptr) {
  354. if (!seen(c) || *value_ptr != 'x') return dval;
  355. uint8_t *out = nullptr;
  356. for (char *vp = value_ptr + 1; HEXCHR(*vp) >= 0; vp++)
  357. out = (uint8_t*)((uintptr_t(out) << 8) | HEXCHR(*vp));
  358. return out;
  359. }
  360. static uint16_t hex_val(const char c, uint16_t const dval=0) {
  361. if (!seen(c) || *value_ptr != 'x') return dval;
  362. uint16_t out = 0;
  363. for (char *vp = value_ptr + 1; HEXCHR(*vp) >= 0; vp++)
  364. out = ((out) << 8) | HEXCHR(*vp);
  365. return out;
  366. }
  367. #endif
  368. };
  369. extern GCodeParser parser;