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.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 <http://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_routines.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 int 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. #if ENABLED(FASTER_GCODE_PARSER)
  97. FORCE_INLINE static bool valid_int(const char * const p) {
  98. return NUMERIC(p[0]) || ((p[0] == '-' || p[0] == '+') && NUMERIC(p[1])); // [-+]?[0-9]
  99. }
  100. // Set the flag and pointer for a parameter
  101. static inline void set(const char c, char * const ptr) {
  102. const uint8_t ind = LETTER_BIT(c);
  103. if (ind >= COUNT(param)) return; // Only A-Z
  104. SBI32(codebits, ind); // parameter exists
  105. param[ind] = ptr ? ptr - command_ptr : 0; // parameter offset or 0
  106. #if ENABLED(DEBUG_GCODE_PARSER)
  107. if (codenum == 800) {
  108. SERIAL_ECHOPAIR("Set bit ", (int)ind, " of codebits (", hex_address((void*)(codebits >> 16)));
  109. print_hex_word((uint16_t)(codebits & 0xFFFF));
  110. SERIAL_ECHOLNPAIR(") | param = ", (int)param[ind]);
  111. }
  112. #endif
  113. }
  114. // Code seen bit was set. If not found, value_ptr is unchanged.
  115. // This allows "if (seen('A')||seen('B'))" to use the last-found value.
  116. static inline bool seen(const char c) {
  117. const uint8_t ind = LETTER_BIT(c);
  118. if (ind >= COUNT(param)) return false; // Only A-Z
  119. const bool b = TEST32(codebits, ind);
  120. if (b) {
  121. char * const ptr = command_ptr + param[ind];
  122. value_ptr = param[ind] && valid_float(ptr) ? ptr : nullptr;
  123. }
  124. return b;
  125. }
  126. FORCE_INLINE static constexpr uint32_t letter_bits(const char * const str) {
  127. return (str[0] ? _BV32(LETTER_BIT(str[0])) |
  128. (str[1] ? _BV32(LETTER_BIT(str[1])) |
  129. (str[2] ? _BV32(LETTER_BIT(str[2])) |
  130. (str[3] ? _BV32(LETTER_BIT(str[3])) |
  131. (str[4] ? _BV32(LETTER_BIT(str[4])) |
  132. (str[5] ? _BV32(LETTER_BIT(str[5])) |
  133. (str[6] ? _BV32(LETTER_BIT(str[6])) |
  134. (str[7] ? _BV32(LETTER_BIT(str[7])) |
  135. (str[8] ? _BV32(LETTER_BIT(str[8])) |
  136. (str[9] ? _BV32(LETTER_BIT(str[9]))
  137. : 0) : 0) : 0) : 0) : 0) : 0) : 0) : 0) : 0) : 0);
  138. }
  139. // At least one of a list of code letters was seen
  140. #ifdef CPU_32_BIT
  141. FORCE_INLINE static bool seen(const char * const str) { return !!(codebits & letter_bits(str)); }
  142. #else
  143. // At least one of a list of code letters was seen
  144. FORCE_INLINE static bool seen(const char * const str) {
  145. const uint32_t letrbits = letter_bits(str);
  146. const uint8_t * const cb = (uint8_t*)&codebits;
  147. const uint8_t * const lb = (uint8_t*)&letrbits;
  148. return (cb[0] & lb[0]) || (cb[1] & lb[1]) || (cb[2] & lb[2]) || (cb[3] & lb[3]);
  149. }
  150. #endif
  151. static inline bool seen_any() { return !!codebits; }
  152. #define SEEN_TEST(L) TEST32(codebits, LETTER_BIT(L))
  153. #else // !FASTER_GCODE_PARSER
  154. // Code is found in the string. If not found, value_ptr is unchanged.
  155. // This allows "if (seen('A')||seen('B'))" to use the last-found value.
  156. static inline bool seen(const char c) {
  157. char *p = strchr(command_args, c);
  158. const bool b = !!p;
  159. if (b) value_ptr = valid_float(&p[1]) ? &p[1] : nullptr;
  160. return b;
  161. }
  162. static inline bool seen_any() { return *command_args == '\0'; }
  163. #define SEEN_TEST(L) !!strchr(command_args, L)
  164. // At least one of a list of code letters was seen
  165. static inline bool seen(const char * const str) {
  166. for (uint8_t i = 0; const char c = str[i]; i++)
  167. if (SEEN_TEST(c)) return true;
  168. return false;
  169. }
  170. #endif // !FASTER_GCODE_PARSER
  171. // Seen any axis parameter
  172. static inline bool seen_axis() {
  173. return SEEN_TEST('X') || SEEN_TEST('Y') || SEEN_TEST('Z') || SEEN_TEST('E');
  174. }
  175. #if ENABLED(GCODE_QUOTED_STRINGS)
  176. static char* unescape_string(char* &src);
  177. #else
  178. FORCE_INLINE static char* unescape_string(char* &src) { return src; }
  179. #endif
  180. // Populate all fields by parsing a single line of GCode
  181. // This uses 54 bytes of SRAM to speed up seen/value
  182. static void parse(char * p);
  183. #if ENABLED(CNC_COORDINATE_SYSTEMS)
  184. // Parse the next parameter as a new command
  185. static bool chain();
  186. #endif
  187. // The code value pointer was set
  188. FORCE_INLINE static bool has_value() { return value_ptr != nullptr; }
  189. // Seen a parameter with a value
  190. static inline bool seenval(const char c) { return seen(c) && has_value(); }
  191. // Float removes 'E' to prevent scientific notation interpretation
  192. static inline char* value_string() { return value_ptr; }
  193. // Float removes 'E' to prevent scientific notation interpretation
  194. static inline float value_float() {
  195. if (value_ptr) {
  196. char *e = value_ptr;
  197. for (;;) {
  198. const char c = *e;
  199. if (c == '\0' || c == ' ') break;
  200. if (c == 'E' || c == 'e') {
  201. *e = '\0';
  202. const float ret = strtof(value_ptr, nullptr);
  203. *e = c;
  204. return ret;
  205. }
  206. ++e;
  207. }
  208. return strtof(value_ptr, nullptr);
  209. }
  210. return 0;
  211. }
  212. // Code value as a long or ulong
  213. static inline int32_t value_long() { return value_ptr ? strtol(value_ptr, nullptr, 10) : 0L; }
  214. static inline uint32_t value_ulong() { return value_ptr ? strtoul(value_ptr, nullptr, 10) : 0UL; }
  215. // Code value for use as time
  216. static inline millis_t value_millis() { return value_ulong(); }
  217. static inline millis_t value_millis_from_seconds() { return (millis_t)(value_float() * 1000); }
  218. // Reduce to fewer bits
  219. static inline int16_t value_int() { return (int16_t)value_long(); }
  220. static inline uint16_t value_ushort() { return (uint16_t)value_long(); }
  221. static inline uint8_t value_byte() { return (uint8_t)constrain(value_long(), 0, 255); }
  222. // Bool is true with no value or non-zero
  223. static inline bool value_bool() { return !has_value() || !!value_byte(); }
  224. // Units modes: Inches, Fahrenheit, Kelvin
  225. #if ENABLED(INCH_MODE_SUPPORT)
  226. static inline float mm_to_linear_unit(const float mm) { return mm / linear_unit_factor; }
  227. static inline float mm_to_volumetric_unit(const float mm) { return mm / (volumetric_enabled ? volumetric_unit_factor : linear_unit_factor); }
  228. // Init linear units by constructor
  229. GCodeParser() { set_input_linear_units(LINEARUNIT_MM); }
  230. static inline void set_input_linear_units(const LinearUnit units) {
  231. switch (units) {
  232. default:
  233. case LINEARUNIT_MM: linear_unit_factor = 1.0f; break;
  234. case LINEARUNIT_INCH: linear_unit_factor = 25.4f; break;
  235. }
  236. volumetric_unit_factor = POW(linear_unit_factor, 3);
  237. }
  238. static inline float axis_unit_factor(const AxisEnum axis) {
  239. return (axis >= E_AXIS && volumetric_enabled ? volumetric_unit_factor : linear_unit_factor);
  240. }
  241. static inline float linear_value_to_mm(const float v) { return v * linear_unit_factor; }
  242. static inline float axis_value_to_mm(const AxisEnum axis, const float v) { return v * axis_unit_factor(axis); }
  243. static inline float per_axis_value(const AxisEnum axis, const float v) { return v / axis_unit_factor(axis); }
  244. #else
  245. static inline float mm_to_linear_unit(const float mm) { return mm; }
  246. static inline float mm_to_volumetric_unit(const float mm) { return mm; }
  247. static inline float linear_value_to_mm(const float v) { return v; }
  248. static inline float axis_value_to_mm(const AxisEnum, const float v) { return v; }
  249. static inline float per_axis_value(const AxisEnum, const float v) { return v; }
  250. #endif
  251. #define LINEAR_UNIT(V) parser.mm_to_linear_unit(V)
  252. #define VOLUMETRIC_UNIT(V) parser.mm_to_volumetric_unit(V)
  253. static inline float value_linear_units() { return linear_value_to_mm(value_float()); }
  254. static inline float value_axis_units(const AxisEnum axis) { return axis_value_to_mm(axis, value_float()); }
  255. static inline float value_per_axis_units(const AxisEnum axis) { return per_axis_value(axis, value_float()); }
  256. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  257. static inline void set_input_temp_units(const TempUnit units) { input_temp_units = units; }
  258. #if HAS_LCD_MENU && DISABLED(DISABLE_M503)
  259. static inline char temp_units_code() {
  260. return input_temp_units == TEMPUNIT_K ? 'K' : input_temp_units == TEMPUNIT_F ? 'F' : 'C';
  261. }
  262. static inline PGM_P temp_units_name() {
  263. return input_temp_units == TEMPUNIT_K ? PSTR("Kelvin") : input_temp_units == TEMPUNIT_F ? PSTR("Fahrenheit") : PSTR("Celsius");
  264. }
  265. static inline float to_temp_units(const float &f) {
  266. switch (input_temp_units) {
  267. case TEMPUNIT_F:
  268. return f * 0.5555555556f + 32;
  269. case TEMPUNIT_K:
  270. return f + 273.15f;
  271. case TEMPUNIT_C:
  272. default:
  273. return f;
  274. }
  275. }
  276. #endif // HAS_LCD_MENU && !DISABLE_M503
  277. static inline float value_celsius() {
  278. const float f = value_float();
  279. switch (input_temp_units) {
  280. case TEMPUNIT_F:
  281. return (f - 32) * 0.5555555556f;
  282. case TEMPUNIT_K:
  283. return f - 273.15f;
  284. case TEMPUNIT_C:
  285. default:
  286. return f;
  287. }
  288. }
  289. static inline float value_celsius_diff() {
  290. switch (input_temp_units) {
  291. case TEMPUNIT_F:
  292. return value_float() * 0.5555555556f;
  293. case TEMPUNIT_C:
  294. case TEMPUNIT_K:
  295. default:
  296. return value_float();
  297. }
  298. }
  299. #define TEMP_UNIT(N) parser.to_temp_units(N)
  300. #else // !TEMPERATURE_UNITS_SUPPORT
  301. static inline float value_celsius() { return value_float(); }
  302. static inline float value_celsius_diff() { return value_float(); }
  303. #define TEMP_UNIT(N) (N)
  304. #endif // !TEMPERATURE_UNITS_SUPPORT
  305. static inline feedRate_t value_feedrate() { return MMM_TO_MMS(value_linear_units()); }
  306. void unknown_command_warning();
  307. // Provide simple value accessors with default option
  308. static inline char* stringval(const char c, char * const dval=nullptr) { return seenval(c) ? value_string() : dval; }
  309. static inline float floatval(const char c, const float dval=0.0) { return seenval(c) ? value_float() : dval; }
  310. static inline bool boolval(const char c, const bool dval=false) { return seenval(c) ? value_bool() : (seen(c) ? true : dval); }
  311. static inline uint8_t byteval(const char c, const uint8_t dval=0) { return seenval(c) ? value_byte() : dval; }
  312. static inline int16_t intval(const char c, const int16_t dval=0) { return seenval(c) ? value_int() : dval; }
  313. static inline uint16_t ushortval(const char c, const uint16_t dval=0) { return seenval(c) ? value_ushort() : dval; }
  314. static inline int32_t longval(const char c, const int32_t dval=0) { return seenval(c) ? value_long() : dval; }
  315. static inline uint32_t ulongval(const char c, const uint32_t dval=0) { return seenval(c) ? value_ulong() : dval; }
  316. static inline float linearval(const char c, const float dval=0) { return seenval(c) ? value_linear_units() : dval; }
  317. static inline float celsiusval(const char c, const float dval=0) { return seenval(c) ? value_celsius() : dval; }
  318. };
  319. extern GCodeParser parser;