My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

gcode.h 11KB

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