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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #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. /**
  34. * GCode parser
  35. *
  36. * - Parse a single gcode line for its letter, code, subcode, and parameters
  37. * - FASTER_GCODE_PARSER:
  38. * - Flags existing params (1 bit each)
  39. * - Stores value offsets (1 byte each)
  40. * - Provide accessors for parameters:
  41. * - Parameter exists
  42. * - Parameter has value
  43. * - Parameter value in different units and types
  44. */
  45. class GCodeParser {
  46. private:
  47. static char *value_ptr; // Set by seen, used to fetch the value
  48. #if ENABLED(FASTER_GCODE_PARSER)
  49. static uint32_t codebits; // Parameters pre-scanned
  50. static uint8_t param[26]; // For A-Z, offsets into command args
  51. #else
  52. static char *command_args; // Args start here, for slow scan
  53. #endif
  54. public:
  55. // Global states for GCode-level units features
  56. static bool volumetric_enabled;
  57. #if ENABLED(INCH_MODE_SUPPORT)
  58. static float linear_unit_factor, volumetric_unit_factor;
  59. #endif
  60. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  61. static TempUnit input_temp_units;
  62. #endif
  63. // Command line state
  64. static char *command_ptr, // The command, so it can be echoed
  65. *string_arg, // string of command line
  66. command_letter; // G, M, or T
  67. static int codenum; // 123
  68. #if USE_GCODE_SUBCODES
  69. static uint8_t subcode; // .1
  70. #endif
  71. #if ENABLED(GCODE_MOTION_MODES)
  72. static int16_t motion_mode_codenum;
  73. #if USE_GCODE_SUBCODES
  74. static uint8_t motion_mode_subcode;
  75. #endif
  76. FORCE_INLINE static void cancel_motion_mode() { motion_mode_codenum = -1; }
  77. #endif
  78. #if ENABLED(DEBUG_GCODE_PARSER)
  79. static void debug();
  80. #endif
  81. // Reset is done before parsing
  82. static void reset();
  83. #define LETTER_BIT(N) ((N) - 'A')
  84. FORCE_INLINE static bool valid_signless(const char * const p) {
  85. return NUMERIC(p[0]) || (p[0] == '.' && NUMERIC(p[1])); // .?[0-9]
  86. }
  87. FORCE_INLINE static bool valid_float(const char * const p) {
  88. return valid_signless(p) || ((p[0] == '-' || p[0] == '+') && valid_signless(&p[1])); // [-+]?.?[0-9]
  89. }
  90. #if ENABLED(FASTER_GCODE_PARSER)
  91. FORCE_INLINE static bool valid_int(const char * const p) {
  92. return NUMERIC(p[0]) || ((p[0] == '-' || p[0] == '+') && NUMERIC(p[1])); // [-+]?[0-9]
  93. }
  94. // Set the flag and pointer for a parameter
  95. static inline void set(const char c, char * const ptr) {
  96. const uint8_t ind = LETTER_BIT(c);
  97. if (ind >= COUNT(param)) return; // Only A-Z
  98. SBI32(codebits, ind); // parameter exists
  99. param[ind] = ptr ? ptr - command_ptr : 0; // parameter offset or 0
  100. #if ENABLED(DEBUG_GCODE_PARSER)
  101. if (codenum == 800) {
  102. SERIAL_ECHOPAIR("Set bit ", (int)ind);
  103. SERIAL_ECHOPAIR(" of codebits (", hex_address((void*)(codebits >> 16)));
  104. print_hex_word((uint16_t)(codebits & 0xFFFF));
  105. SERIAL_ECHOLNPAIR(") | param = ", (int)param[ind]);
  106. }
  107. #endif
  108. }
  109. // Code seen bit was set. If not found, value_ptr is unchanged.
  110. // This allows "if (seen('A')||seen('B'))" to use the last-found value.
  111. static inline bool seen(const char c) {
  112. const uint8_t ind = LETTER_BIT(c);
  113. if (ind >= COUNT(param)) return false; // Only A-Z
  114. const bool b = TEST32(codebits, ind);
  115. if (b) {
  116. char * const ptr = command_ptr + param[ind];
  117. value_ptr = param[ind] && valid_float(ptr) ? ptr : (char*)NULL;
  118. }
  119. return b;
  120. }
  121. FORCE_INLINE static constexpr uint32_t letter_bits(const char * const str) {
  122. return (str[0] ? _BV32(LETTER_BIT(str[0])) |
  123. (str[1] ? _BV32(LETTER_BIT(str[1])) |
  124. (str[2] ? _BV32(LETTER_BIT(str[2])) |
  125. (str[3] ? _BV32(LETTER_BIT(str[3])) |
  126. (str[4] ? _BV32(LETTER_BIT(str[4])) |
  127. (str[5] ? _BV32(LETTER_BIT(str[5])) |
  128. (str[6] ? _BV32(LETTER_BIT(str[6])) |
  129. (str[7] ? _BV32(LETTER_BIT(str[7])) |
  130. (str[8] ? _BV32(LETTER_BIT(str[8])) |
  131. (str[9] ? _BV32(LETTER_BIT(str[9]))
  132. : 0) : 0) : 0) : 0) : 0) : 0) : 0) : 0) : 0) : 0);
  133. }
  134. // At least one of a list of code letters was seen
  135. #ifdef CPU_32_BIT
  136. FORCE_INLINE static bool seen(const char * const str) { return !!(codebits & letter_bits(str)); }
  137. #else
  138. // At least one of a list of code letters was seen
  139. FORCE_INLINE static bool seen(const char * const str) {
  140. const uint32_t letrbits = letter_bits(str);
  141. const uint8_t * const cb = (uint8_t*)&codebits;
  142. const uint8_t * const lb = (uint8_t*)&letrbits;
  143. return (cb[0] & lb[0]) || (cb[1] & lb[1]) || (cb[2] & lb[2]) || (cb[3] & lb[3]);
  144. }
  145. #endif
  146. static inline bool seen_any() { return !!codebits; }
  147. #define SEEN_TEST(L) TEST32(codebits, LETTER_BIT(L))
  148. #else // !FASTER_GCODE_PARSER
  149. // Code is found in the string. If not found, value_ptr is unchanged.
  150. // This allows "if (seen('A')||seen('B'))" to use the last-found value.
  151. static inline bool seen(const char c) {
  152. char *p = strchr(command_args, c);
  153. const bool b = !!p;
  154. if (b) value_ptr = valid_float(&p[1]) ? &p[1] : (char*)NULL;
  155. return b;
  156. }
  157. static inline bool seen_any() { return *command_args == '\0'; }
  158. #define SEEN_TEST(L) !!strchr(command_args, L)
  159. // At least one of a list of code letters was seen
  160. static inline bool seen(const char * const str) {
  161. for (uint8_t i = 0; const char c = str[i]; i++)
  162. if (SEEN_TEST(c)) return true;
  163. return false;
  164. }
  165. #endif // !FASTER_GCODE_PARSER
  166. // Seen any axis parameter
  167. static inline bool seen_axis() {
  168. return SEEN_TEST('X') || SEEN_TEST('Y') || SEEN_TEST('Z') || SEEN_TEST('E');
  169. }
  170. // Populate all fields by parsing a single line of GCode
  171. // This uses 54 bytes of SRAM to speed up seen/value
  172. static void parse(char * p);
  173. #if ENABLED(CNC_COORDINATE_SYSTEMS)
  174. // Parse the next parameter as a new command
  175. static bool chain();
  176. #endif
  177. // The code value pointer was set
  178. FORCE_INLINE static bool has_value() { return value_ptr != NULL; }
  179. // Seen a parameter with a value
  180. static inline bool seenval(const char c) { return seen(c) && has_value(); }
  181. // Float removes 'E' to prevent scientific notation interpretation
  182. static inline float value_float() {
  183. if (value_ptr) {
  184. char *e = value_ptr;
  185. for (;;) {
  186. const char c = *e;
  187. if (c == '\0' || c == ' ') break;
  188. if (c == 'E' || c == 'e') {
  189. *e = '\0';
  190. const float ret = strtof(value_ptr, NULL);
  191. *e = c;
  192. return ret;
  193. }
  194. ++e;
  195. }
  196. return strtof(value_ptr, NULL);
  197. }
  198. return 0;
  199. }
  200. // Code value as a long or ulong
  201. static inline int32_t value_long() { return value_ptr ? strtol(value_ptr, NULL, 10) : 0L; }
  202. static inline uint32_t value_ulong() { return value_ptr ? strtoul(value_ptr, NULL, 10) : 0UL; }
  203. // Code value for use as time
  204. static inline millis_t value_millis() { return value_ulong(); }
  205. static inline millis_t value_millis_from_seconds() { return (millis_t)(value_float() * 1000); }
  206. // Reduce to fewer bits
  207. static inline int16_t value_int() { return (int16_t)value_long(); }
  208. static inline uint16_t value_ushort() { return (uint16_t)value_long(); }
  209. static inline uint8_t value_byte() { return (uint8_t)constrain(value_long(), 0, 255); }
  210. // Bool is true with no value or non-zero
  211. static inline bool value_bool() { return !has_value() || !!value_byte(); }
  212. // Units modes: Inches, Fahrenheit, Kelvin
  213. #if ENABLED(INCH_MODE_SUPPORT)
  214. static inline float mm_to_linear_unit(const float mm) { return mm / linear_unit_factor; }
  215. static inline float mm_to_volumetric_unit(const float mm) { return mm / (volumetric_enabled ? volumetric_unit_factor : linear_unit_factor); }
  216. // Init linear units by constructor
  217. GCodeParser() { set_input_linear_units(LINEARUNIT_MM); }
  218. static inline void set_input_linear_units(const LinearUnit units) {
  219. switch (units) {
  220. case LINEARUNIT_INCH:
  221. linear_unit_factor = 25.4f;
  222. break;
  223. case LINEARUNIT_MM:
  224. default:
  225. linear_unit_factor = 1;
  226. break;
  227. }
  228. volumetric_unit_factor = POW(linear_unit_factor, 3);
  229. }
  230. static inline float axis_unit_factor(const AxisEnum axis) {
  231. return (axis >= E_AXIS && volumetric_enabled ? volumetric_unit_factor : linear_unit_factor);
  232. }
  233. static inline float linear_value_to_mm(const float v) { return v * linear_unit_factor; }
  234. static inline float axis_value_to_mm(const AxisEnum axis, const float v) { return v * axis_unit_factor(axis); }
  235. static inline float per_axis_value(const AxisEnum axis, const float v) { return v / axis_unit_factor(axis); }
  236. #else
  237. static inline float mm_to_linear_unit(const float mm) { return mm; }
  238. static inline float mm_to_volumetric_unit(const float mm) { return mm; }
  239. static inline float linear_value_to_mm(const float v) { return v; }
  240. static inline float axis_value_to_mm(const AxisEnum axis, const float v) { UNUSED(axis); return v; }
  241. static inline float per_axis_value(const AxisEnum axis, const float v) { UNUSED(axis); return v; }
  242. #endif
  243. #define LINEAR_UNIT(V) parser.mm_to_linear_unit(V)
  244. #define VOLUMETRIC_UNIT(V) parser.mm_to_volumetric_unit(V)
  245. static inline float value_linear_units() { return linear_value_to_mm(value_float()); }
  246. static inline float value_axis_units(const AxisEnum axis) { return axis_value_to_mm(axis, value_float()); }
  247. static inline float value_per_axis_units(const AxisEnum axis) { return per_axis_value(axis, value_float()); }
  248. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  249. static inline void set_input_temp_units(TempUnit units) { input_temp_units = units; }
  250. #if HAS_LCD_MENU && DISABLED(DISABLE_M503)
  251. static inline char temp_units_code() {
  252. return input_temp_units == TEMPUNIT_K ? 'K' : input_temp_units == TEMPUNIT_F ? 'F' : 'C';
  253. }
  254. static inline PGM_P temp_units_name() {
  255. return input_temp_units == TEMPUNIT_K ? PSTR("Kelvin") : input_temp_units == TEMPUNIT_F ? PSTR("Fahrenheit") : PSTR("Celsius");
  256. }
  257. static inline float to_temp_units(const float &f) {
  258. switch (input_temp_units) {
  259. case TEMPUNIT_F:
  260. return f * 0.5555555556f + 32;
  261. case TEMPUNIT_K:
  262. return f + 273.15f;
  263. case TEMPUNIT_C:
  264. default:
  265. return f;
  266. }
  267. }
  268. #endif // HAS_LCD_MENU && !DISABLE_M503
  269. static inline float value_celsius() {
  270. const float f = value_float();
  271. switch (input_temp_units) {
  272. case TEMPUNIT_F:
  273. return (f - 32) * 0.5555555556f;
  274. case TEMPUNIT_K:
  275. return f - 273.15f;
  276. case TEMPUNIT_C:
  277. default:
  278. return f;
  279. }
  280. }
  281. static inline float value_celsius_diff() {
  282. switch (input_temp_units) {
  283. case TEMPUNIT_F:
  284. return value_float() * 0.5555555556f;
  285. case TEMPUNIT_C:
  286. case TEMPUNIT_K:
  287. default:
  288. return value_float();
  289. }
  290. }
  291. #define TEMP_UNIT(N) parser.to_temp_units(N)
  292. #else // !TEMPERATURE_UNITS_SUPPORT
  293. static inline float value_celsius() { return value_float(); }
  294. static inline float value_celsius_diff() { return value_float(); }
  295. #define TEMP_UNIT(N) (N)
  296. #endif // !TEMPERATURE_UNITS_SUPPORT
  297. static inline float value_feedrate() { return value_linear_units(); }
  298. void unknown_command_error();
  299. // Provide simple value accessors with default option
  300. static inline float floatval(const char c, const float dval=0.0) { return seenval(c) ? value_float() : dval; }
  301. static inline bool boolval(const char c, const bool dval=false) { return seenval(c) ? value_bool() : (seen(c) ? true : dval); }
  302. static inline uint8_t byteval(const char c, const uint8_t dval=0) { return seenval(c) ? value_byte() : dval; }
  303. static inline int16_t intval(const char c, const int16_t dval=0) { return seenval(c) ? value_int() : dval; }
  304. static inline uint16_t ushortval(const char c, const uint16_t dval=0) { return seenval(c) ? value_ushort() : dval; }
  305. static inline int32_t longval(const char c, const int32_t dval=0) { return seenval(c) ? value_long() : dval; }
  306. static inline uint32_t ulongval(const char c, const uint32_t dval=0) { return seenval(c) ? value_ulong() : dval; }
  307. static inline float linearval(const char c, const float dval=0) { return seenval(c) ? value_linear_units() : dval; }
  308. static inline float celsiusval(const char c, const float dval=0) { return seenval(c) ? value_celsius() : dval; }
  309. };
  310. extern GCodeParser parser;