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

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