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 15KB

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