My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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