My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

parser.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 gcode 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 inline 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 inline 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 inline 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 inline 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 inline 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 inline 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 inline bool seen_axis() { return seen(LOGICAL_AXES_STRING); }
  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 inline 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 inline bool seenval(const char c) { return seen(c) && has_value(); }
  210. // The value as a string
  211. static inline char* value_string() { return value_ptr; }
  212. // Float removes 'E' to prevent scientific notation interpretation
  213. static inline float value_float() {
  214. if (value_ptr) {
  215. char *e = value_ptr;
  216. for (;;) {
  217. const char c = *e;
  218. if (c == '\0' || c == ' ') break;
  219. if (c == 'E' || c == 'e') {
  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. return 0;
  230. }
  231. // Code value as a long or ulong
  232. static inline int32_t value_long() { return value_ptr ? strtol(value_ptr, nullptr, 10) : 0L; }
  233. static inline uint32_t value_ulong() { return value_ptr ? strtoul(value_ptr, nullptr, 10) : 0UL; }
  234. // Code value for use as time
  235. static inline millis_t value_millis() { return value_ulong(); }
  236. static inline millis_t value_millis_from_seconds() { return (millis_t)SEC_TO_MS(value_float()); }
  237. // Reduce to fewer bits
  238. static inline int16_t value_int() { return (int16_t)value_long(); }
  239. static inline uint16_t value_ushort() { return (uint16_t)value_long(); }
  240. static inline uint8_t value_byte() { return (uint8_t)constrain(value_long(), 0, 255); }
  241. // Bool is true with no value or non-zero
  242. static inline bool value_bool() { return !has_value() || !!value_byte(); }
  243. // Units modes: Inches, Fahrenheit, Kelvin
  244. #if ENABLED(INCH_MODE_SUPPORT)
  245. static inline float mm_to_linear_unit(const_float_t mm) { return mm / linear_unit_factor; }
  246. static inline float mm_to_volumetric_unit(const_float_t mm) { return mm / (volumetric_enabled ? volumetric_unit_factor : linear_unit_factor); }
  247. // Init linear units by constructor
  248. GCodeParser() { set_input_linear_units(LINEARUNIT_MM); }
  249. static inline void set_input_linear_units(const LinearUnit units) {
  250. switch (units) {
  251. default:
  252. case LINEARUNIT_MM: linear_unit_factor = 1.0f; break;
  253. case LINEARUNIT_INCH: linear_unit_factor = 25.4f; break;
  254. }
  255. volumetric_unit_factor = POW(linear_unit_factor, 3);
  256. }
  257. static inline float axis_unit_factor(const AxisEnum axis) {
  258. return (
  259. #if HAS_EXTRUDERS
  260. axis >= E_AXIS && volumetric_enabled ? volumetric_unit_factor : linear_unit_factor
  261. #else
  262. linear_unit_factor
  263. #endif
  264. );
  265. }
  266. static inline float linear_value_to_mm(const_float_t v) { return v * linear_unit_factor; }
  267. static inline float axis_value_to_mm(const AxisEnum axis, const float v) { return v * axis_unit_factor(axis); }
  268. static inline float per_axis_value(const AxisEnum axis, const float v) { return v / axis_unit_factor(axis); }
  269. #else
  270. static inline float mm_to_linear_unit(const_float_t mm) { return mm; }
  271. static inline float mm_to_volumetric_unit(const_float_t mm) { return mm; }
  272. static inline float linear_value_to_mm(const_float_t v) { return v; }
  273. static inline float axis_value_to_mm(const AxisEnum, const float v) { return v; }
  274. static inline float per_axis_value(const AxisEnum, const float v) { return v; }
  275. #endif
  276. static inline bool using_inch_units() { return mm_to_linear_unit(1.0f) != 1.0f; }
  277. #define IN_TO_MM(I) ((I) * 25.4f)
  278. #define MM_TO_IN(M) ((M) / 25.4f)
  279. #define LINEAR_UNIT(V) parser.mm_to_linear_unit(V)
  280. #define VOLUMETRIC_UNIT(V) parser.mm_to_volumetric_unit(V)
  281. static inline float value_linear_units() { return linear_value_to_mm(value_float()); }
  282. static inline float value_axis_units(const AxisEnum axis) { return axis_value_to_mm(axis, value_float()); }
  283. static inline float value_per_axis_units(const AxisEnum axis) { return per_axis_value(axis, value_float()); }
  284. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  285. static inline void set_input_temp_units(const TempUnit units) { input_temp_units = units; }
  286. static inline char temp_units_code() {
  287. return input_temp_units == TEMPUNIT_K ? 'K' : input_temp_units == TEMPUNIT_F ? 'F' : 'C';
  288. }
  289. static inline FSTR_P temp_units_name() {
  290. return input_temp_units == TEMPUNIT_K ? F("Kelvin") : input_temp_units == TEMPUNIT_F ? F("Fahrenheit") : F("Celsius");
  291. }
  292. #if HAS_LCD_MENU && DISABLED(DISABLE_M503)
  293. static inline float to_temp_units(celsius_t c) {
  294. switch (input_temp_units) {
  295. default:
  296. case TEMPUNIT_C: return c;
  297. case TEMPUNIT_K: return c + 273.15f;
  298. case TEMPUNIT_F: return c * 0.5555555556f + 32;
  299. }
  300. }
  301. #endif // HAS_LCD_MENU && !DISABLE_M503
  302. static inline celsius_t value_celsius() {
  303. float f = value_float();
  304. switch (input_temp_units) {
  305. default:
  306. case TEMPUNIT_C: break;
  307. case TEMPUNIT_K: f -= 273.15f;
  308. case TEMPUNIT_F: f = (f - 32) * 0.5555555556f;
  309. }
  310. return LROUND(f);
  311. }
  312. static inline celsius_t value_celsius_diff() {
  313. float f = value_float();
  314. switch (input_temp_units) {
  315. default:
  316. case TEMPUNIT_C:
  317. case TEMPUNIT_K: break;
  318. case TEMPUNIT_F: f *= 0.5555555556f;
  319. }
  320. return LROUND(f);
  321. }
  322. #else // !TEMPERATURE_UNITS_SUPPORT
  323. static inline float to_temp_units(int16_t c) { return (float)c; }
  324. static inline celsius_t value_celsius() { return value_int(); }
  325. static inline celsius_t value_celsius_diff() { return value_int(); }
  326. #endif // !TEMPERATURE_UNITS_SUPPORT
  327. static inline feedRate_t value_feedrate() { return MMM_TO_MMS(value_linear_units()); }
  328. void unknown_command_warning();
  329. // Provide simple value accessors with default option
  330. static inline char* stringval(const char c, char * const dval=nullptr) { return seenval(c) ? value_string() : dval; }
  331. static inline float floatval(const char c, const float dval=0.0) { return seenval(c) ? value_float() : dval; }
  332. static inline bool boolval(const char c, const bool dval=false) { return seenval(c) ? value_bool() : (seen(c) ? true : dval); }
  333. static inline uint8_t byteval(const char c, const uint8_t dval=0) { return seenval(c) ? value_byte() : dval; }
  334. static inline int16_t intval(const char c, const int16_t dval=0) { return seenval(c) ? value_int() : dval; }
  335. static inline uint16_t ushortval(const char c, const uint16_t dval=0) { return seenval(c) ? value_ushort() : dval; }
  336. static inline int32_t longval(const char c, const int32_t dval=0) { return seenval(c) ? value_long() : dval; }
  337. static inline uint32_t ulongval(const char c, const uint32_t dval=0) { return seenval(c) ? value_ulong() : dval; }
  338. static inline float linearval(const char c, const float dval=0) { return seenval(c) ? value_linear_units() : dval; }
  339. static inline float axisunitsval(const char c, const AxisEnum a, const float dval=0)
  340. { return seenval(c) ? value_axis_units(a) : dval; }
  341. static inline celsius_t celsiusval(const char c, const celsius_t dval=0) { return seenval(c) ? value_celsius() : dval; }
  342. static inline feedRate_t feedrateval(const char c, const feedRate_t dval=0) { return seenval(c) ? value_feedrate() : dval; }
  343. #if ENABLED(MARLIN_DEV_MODE)
  344. static inline uint8_t* hex_adr_val(const char c, uint8_t * const dval=nullptr) {
  345. if (!seen(c) || *value_ptr != 'x') return dval;
  346. uint8_t *out = nullptr;
  347. for (char *vp = value_ptr + 1; HEXCHR(*vp) >= 0; vp++)
  348. out = (uint8_t*)((uintptr_t(out) << 8) | HEXCHR(*vp));
  349. return out;
  350. }
  351. static inline uint16_t hex_val(const char c, uint16_t const dval=0) {
  352. if (!seen(c) || *value_ptr != 'x') return dval;
  353. uint16_t out = 0;
  354. for (char *vp = value_ptr + 1; HEXCHR(*vp) >= 0; vp++)
  355. out = ((out) << 8) | HEXCHR(*vp);
  356. return out;
  357. }
  358. #endif
  359. };
  360. extern GCodeParser parser;