Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

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