Scott Lahteine
8 years ago
committed by
GitHub
36 changed files with 1225 additions and 733 deletions
File diff suppressed because it is too large
@ -0,0 +1,279 @@ |
|||
/**
|
|||
* Marlin 3D Printer Firmware |
|||
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|||
* |
|||
* Based on Sprinter and grbl. |
|||
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
* |
|||
*/ |
|||
|
|||
/**
|
|||
* gcode.cpp - Parser for a GCode line, providing a parameter interface. |
|||
*/ |
|||
|
|||
#include "gcode.h" |
|||
|
|||
#include "Marlin.h" |
|||
#include "language.h" |
|||
|
|||
// Must be declared for allocation and to satisfy the linker
|
|||
// Zero values need no initialization.
|
|||
|
|||
#if ENABLED(INCH_MODE_SUPPORT) |
|||
float GCodeParser::linear_unit_factor, GCodeParser::volumetric_unit_factor; |
|||
#endif |
|||
|
|||
#if ENABLED(TEMPERATURE_UNITS_SUPPORT) |
|||
TempUnit GCodeParser::input_temp_units; |
|||
#endif |
|||
|
|||
char *GCodeParser::command_ptr, |
|||
*GCodeParser::string_arg, |
|||
*GCodeParser::value_ptr; |
|||
char GCodeParser::command_letter; |
|||
int GCodeParser::codenum; |
|||
#if USE_GCODE_SUBCODES |
|||
int GCodeParser::subcode; |
|||
#endif |
|||
|
|||
#if ENABLED(FASTER_GCODE_PARSER) |
|||
// Optimized Parameters
|
|||
byte GCodeParser::codebits[4]; // found bits
|
|||
uint8_t GCodeParser::param[26]; // parameter offsets from command_ptr
|
|||
#else |
|||
char *GCodeParser::command_args; // start of parameters
|
|||
#endif |
|||
|
|||
// Create a global instance of the GCode parser singleton
|
|||
GCodeParser parser; |
|||
|
|||
/**
|
|||
* Clear all code-seen (and value pointers) |
|||
* |
|||
* Since each param is set/cleared on seen codes, |
|||
* this may be optimized by commenting out ZERO(param) |
|||
*/ |
|||
void GCodeParser::reset() { |
|||
string_arg = NULL; // No whole line argument
|
|||
command_letter = '?'; // No command letter
|
|||
codenum = 0; // No command code
|
|||
#if USE_GCODE_SUBCODES |
|||
subcode = 0; // No command sub-code
|
|||
#endif |
|||
#if ENABLED(FASTER_GCODE_PARSER) |
|||
ZERO(codebits); // No codes yet
|
|||
//ZERO(param); // No parameters (should be safe to comment out this line)
|
|||
#endif |
|||
} |
|||
|
|||
// Populate all fields by parsing a single line of GCode
|
|||
// 58 bytes of SRAM are used to speed up seen/value
|
|||
void GCodeParser::parse(char *p) { |
|||
|
|||
reset(); // No codes to report
|
|||
|
|||
// Skip spaces
|
|||
while (*p == ' ') ++p; |
|||
|
|||
// Skip N[-0-9] if included in the command line
|
|||
if (*p == 'N' && NUMERIC_SIGNED(p[1])) { |
|||
#if ENABLED(FASTER_GCODE_PARSER) |
|||
//set('N', p + 1); // (optional) Set the 'N' parameter value
|
|||
#endif |
|||
p += 2; // skip N[-0-9]
|
|||
while (NUMERIC(*p)) ++p; // skip [0-9]*
|
|||
while (*p == ' ') ++p; // skip [ ]*
|
|||
} |
|||
|
|||
// *p now points to the current command, which should be G, M, or T
|
|||
command_ptr = p; |
|||
|
|||
// Get the command letter, which must be G, M, or T
|
|||
const char letter = *p++; |
|||
|
|||
// Nullify asterisk and trailing whitespace
|
|||
char *starpos = strchr(p, '*'); |
|||
if (starpos) { |
|||
--starpos; // *
|
|||
while (*starpos == ' ') --starpos; // spaces...
|
|||
starpos[1] = '\0'; |
|||
} |
|||
|
|||
// Bail if the letter is not G, M, or T
|
|||
switch (letter) { case 'G': case 'M': case 'T': break; default: return; } |
|||
|
|||
// Skip spaces to get the numeric part
|
|||
while (*p == ' ') p++; |
|||
|
|||
// Bail if there's no command code number
|
|||
if (!NUMERIC(*p)) return; |
|||
|
|||
// Save the command letter at this point
|
|||
// A '?' signifies an unknown command
|
|||
command_letter = letter; |
|||
|
|||
// Get the code number - integer digits only
|
|||
codenum = 0; |
|||
do { |
|||
codenum *= 10, codenum += *p++ - '0'; |
|||
} while (NUMERIC(*p)); |
|||
|
|||
// Allow for decimal point in command
|
|||
#if USE_GCODE_SUBCODES |
|||
if (*p == '.') { |
|||
p++; |
|||
while (NUMERIC(*p)) |
|||
subcode *= 10, subcode += *p++ - '0'; |
|||
} |
|||
#endif |
|||
|
|||
// Skip all spaces to get to the first argument, or nul
|
|||
while (*p == ' ') p++; |
|||
|
|||
// The command parameters (if any) start here, for sure!
|
|||
|
|||
#if DISABLED(FASTER_GCODE_PARSER) |
|||
command_args = p; // Scan for parameters in seen()
|
|||
#endif |
|||
|
|||
// Only use string_arg for these M codes
|
|||
if (letter == 'M') switch (codenum) { case 23: case 28: case 30: case 117: case 928: string_arg = p; return; default: break; } |
|||
|
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
const bool debug = codenum == 800; |
|||
#endif |
|||
|
|||
/**
|
|||
* Find all parameters, set flags and pointers for fast parsing |
|||
* |
|||
* Most codes ignore 'string_arg', but those that want a string will get the right pointer. |
|||
* The following loop assigns the first "parameter" having no numeric value to 'string_arg'. |
|||
* This allows M0/M1 with expire time to work: "M0 S5 You Win!" |
|||
*/ |
|||
string_arg = NULL; |
|||
while (char code = *p++) { // Get the next parameter. A NUL ends the loop
|
|||
|
|||
// Special handling for M32 [P] !/path/to/file.g#
|
|||
// The path must be the last parameter
|
|||
if (code == '!' && letter == 'M' && codenum == 32) { |
|||
string_arg = p; // Name starts after '!'
|
|||
char * const lb = strchr(p, '#'); // Already seen '#' as SD char (to pause buffering)
|
|||
if (lb) *lb = '\0'; // Safe to mark the end of the filename
|
|||
return; |
|||
} |
|||
|
|||
// Arguments MUST be uppercase for fast GCode parsing
|
|||
#if ENABLED(FASTER_GCODE_PARSER) |
|||
#define PARAM_TEST WITHIN(code, 'A', 'Z') |
|||
#else |
|||
#define PARAM_TEST true |
|||
#endif |
|||
|
|||
if (PARAM_TEST) { |
|||
|
|||
const bool has_num = DECIMAL_SIGNED(*p); // The parameter has a number [-+0-9.]
|
|||
|
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
if (debug) { |
|||
SERIAL_ECHOPAIR("Got letter ", code); // DEBUG
|
|||
SERIAL_ECHOPAIR(" at index ", (int)(p - command_ptr - 1)); // DEBUG
|
|||
if (has_num) SERIAL_ECHOPGM(" (has_num)"); |
|||
} |
|||
#endif |
|||
|
|||
if (!has_num && !string_arg) { // No value? First time, keep as string_arg
|
|||
string_arg = p - 1; |
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
if (debug) SERIAL_ECHOPAIR(" string_arg: ", hex_address((void*)string_arg)); // DEBUG
|
|||
#endif |
|||
} |
|||
|
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
if (debug) SERIAL_EOL; |
|||
#endif |
|||
|
|||
#if ENABLED(FASTER_GCODE_PARSER) |
|||
set(code, has_num ? p : NULL // Set parameter exists and pointer (NULL for no number)
|
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
, debug |
|||
#endif |
|||
); |
|||
#endif |
|||
} |
|||
else if (!string_arg) { // Not A-Z? First time, keep as the string_arg
|
|||
string_arg = p - 1; |
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
if (debug) SERIAL_ECHOPAIR(" string_arg: ", hex_address((void*)string_arg)); // DEBUG
|
|||
#endif |
|||
} |
|||
|
|||
while (*p && *p != ' ') p++; // Skip over the parameter
|
|||
while (*p == ' ') p++; // Skip over all spaces
|
|||
} |
|||
} |
|||
|
|||
void GCodeParser::unknown_command_error() { |
|||
SERIAL_ECHO_START; |
|||
SERIAL_ECHOPAIR(MSG_UNKNOWN_COMMAND, command_ptr); |
|||
SERIAL_CHAR('"'); |
|||
SERIAL_EOL; |
|||
} |
|||
|
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
|
|||
void GCodeParser::debug() { |
|||
SERIAL_ECHOPAIR("Command: ", command_ptr); |
|||
SERIAL_ECHOPAIR(" (", command_letter); |
|||
SERIAL_ECHO(codenum); |
|||
SERIAL_ECHOLNPGM(")"); |
|||
#if ENABLED(FASTER_GCODE_PARSER) |
|||
SERIAL_ECHO(" args: \""); |
|||
for (char c = 'A'; c <= 'Z'; ++c) |
|||
if (seen(c)) { SERIAL_CHAR(c); SERIAL_CHAR(' '); } |
|||
#else |
|||
SERIAL_ECHOPAIR(" args: \"", command_args); |
|||
#endif |
|||
SERIAL_ECHOPGM("\""); |
|||
if (string_arg) { |
|||
SERIAL_ECHOPGM(" string: \""); |
|||
SERIAL_ECHO(string_arg); |
|||
SERIAL_CHAR('"'); |
|||
} |
|||
SERIAL_ECHOPGM("\n\n"); |
|||
for (char c = 'A'; c <= 'Z'; ++c) { |
|||
if (seen(c)) { |
|||
SERIAL_ECHOPAIR("Code '", c); SERIAL_ECHOPGM("':"); |
|||
if (has_value()) { |
|||
SERIAL_ECHOPAIR("\n float: ", value_float()); |
|||
SERIAL_ECHOPAIR("\n long: ", value_long()); |
|||
SERIAL_ECHOPAIR("\n ulong: ", value_ulong()); |
|||
SERIAL_ECHOPAIR("\n millis: ", value_millis()); |
|||
SERIAL_ECHOPAIR("\n sec-ms: ", value_millis_from_seconds()); |
|||
SERIAL_ECHOPAIR("\n int: ", value_int()); |
|||
SERIAL_ECHOPAIR("\n ushort: ", value_ushort()); |
|||
SERIAL_ECHOPAIR("\n byte: ", (int)value_byte()); |
|||
SERIAL_ECHOPAIR("\n bool: ", (int)value_bool()); |
|||
SERIAL_ECHOPAIR("\n linear: ", value_linear_units()); |
|||
SERIAL_ECHOPAIR("\n celsius: ", value_celsius()); |
|||
} |
|||
else |
|||
SERIAL_ECHOPGM(" (no value)"); |
|||
SERIAL_ECHOPGM("\n\n"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
#endif // DEBUG_GCODE_PARSER
|
@ -0,0 +1,285 @@ |
|||
/**
|
|||
* Marlin 3D Printer Firmware |
|||
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|||
* |
|||
* Based on Sprinter and grbl. |
|||
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
* |
|||
*/ |
|||
|
|||
/**
|
|||
* gcode.h - Parser for a GCode line, providing a parameter interface. |
|||
* Codes like M149 control the way the GCode parser behaves, |
|||
* so settings for these codes are located in this class. |
|||
*/ |
|||
|
|||
#ifndef GCODE_H |
|||
#define GCODE_H |
|||
|
|||
#include "enum.h" |
|||
#include "types.h" |
|||
#include "MarlinConfig.h" |
|||
|
|||
//#define DEBUG_GCODE_PARSER
|
|||
|
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
#include "hex_print_routines.h" |
|||
#include "serial.h" |
|||
#endif |
|||
|
|||
#if ENABLED(INCH_MODE_SUPPORT) |
|||
extern bool volumetric_enabled; |
|||
#endif |
|||
|
|||
/**
|
|||
* GCode parser |
|||
* |
|||
* - Parse a single gcode line for its letter, code, subcode, and parameters |
|||
* - FASTER_GCODE_PARSER: |
|||
* - Flags existing params (1 bit each) |
|||
* - Stores value offsets (1 byte each) |
|||
* - Provide accessors for parameters: |
|||
* - Parameter exists |
|||
* - Parameter has value |
|||
* - Parameter value in different units and types |
|||
*/ |
|||
class GCodeParser { |
|||
|
|||
private: |
|||
static char *value_ptr; // Set by seen, used to fetch the value
|
|||
|
|||
#if ENABLED(FASTER_GCODE_PARSER) |
|||
static byte codebits[4]; // Parameters pre-scanned
|
|||
static uint8_t param[26]; // For A-Z, offsets into command args
|
|||
#else |
|||
static char *command_args; // Args start here, for slow scan
|
|||
#endif |
|||
|
|||
public: |
|||
|
|||
// Global states for GCode-level units features
|
|||
|
|||
#if ENABLED(INCH_MODE_SUPPORT) |
|||
static float linear_unit_factor, volumetric_unit_factor; |
|||
#endif |
|||
|
|||
#if ENABLED(TEMPERATURE_UNITS_SUPPORT) |
|||
static TempUnit input_temp_units; |
|||
#endif |
|||
|
|||
// Command line state
|
|||
static char *command_ptr, // The command, so it can be echoed
|
|||
*string_arg; // string of command line
|
|||
|
|||
static char command_letter; // G, M, or T
|
|||
static int codenum; // 123
|
|||
#if USE_GCODE_SUBCODES |
|||
static int subcode; // .1
|
|||
#endif |
|||
|
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
void debug(); |
|||
#endif |
|||
|
|||
// Reset is done before parsing
|
|||
static void reset(); |
|||
|
|||
#if ENABLED(FASTER_GCODE_PARSER) |
|||
|
|||
// Set the flag and pointer for a parameter
|
|||
static void set(const char c, char * const ptr |
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
, const bool debug=false |
|||
#endif |
|||
) { |
|||
const uint8_t ind = c - 'A'; |
|||
if (ind >= COUNT(param)) return; // Only A-Z
|
|||
SBI(codebits[ind >> 3], ind & 0x7); // parameter exists
|
|||
param[ind] = ptr ? ptr - command_ptr : 0; // parameter offset or 0
|
|||
#if ENABLED(DEBUG_GCODE_PARSER) |
|||
if (debug) { |
|||
SERIAL_ECHOPAIR("Set bit ", (int)(ind & 0x7)); |
|||
SERIAL_ECHOPAIR(" of index ", (int)(ind >> 3)); |
|||
SERIAL_ECHOLNPAIR(" | param = ", hex_address((void*)param[ind])); |
|||
} |
|||
#endif |
|||
} |
|||
|
|||
// Code seen bit was set. If not found, value_ptr is unchanged.
|
|||
// This allows "if (seen('A')||seen('B'))" to use the last-found value.
|
|||
static bool seen(const char c) { |
|||
const uint8_t ind = c - 'A'; |
|||
if (ind >= COUNT(param)) return false; // Only A-Z
|
|||
const bool b = TEST(codebits[ind >> 3], ind & 0x7); |
|||
if (b) value_ptr = command_ptr + param[ind]; |
|||
return b; |
|||
} |
|||
|
|||
#else |
|||
|
|||
// Code is found in the string. If not found, value_ptr is unchanged.
|
|||
// This allows "if (seen('A')||seen('B'))" to use the last-found value.
|
|||
static bool seen(const char c) { |
|||
char *p = strchr(command_args, c); |
|||
const bool b = !!p; |
|||
if (b) value_ptr = DECIMAL_SIGNED(*p) ? p + 1 : NULL; |
|||
return b; |
|||
} |
|||
|
|||
#endif // FASTER_GCODE_PARSER
|
|||
|
|||
// Populate all fields by parsing a single line of GCode
|
|||
// This uses 54 bytes of SRAM to speed up seen/value
|
|||
static void parse(char * p); |
|||
|
|||
// Code value pointer was set
|
|||
FORCE_INLINE static bool has_value() { return value_ptr != NULL; } |
|||
|
|||
// Float removes 'E' to prevent scientific notation interpretation
|
|||
inline static float value_float() { |
|||
if (value_ptr) { |
|||
char *e = value_ptr; |
|||
for (;;) { |
|||
const char c = *e; |
|||
if (c == '\0' || c == ' ') break; |
|||
if (c == 'E' || c == 'e') { |
|||
*e = '\0'; |
|||
const float ret = strtod(value_ptr, NULL); |
|||
*e = c; |
|||
return ret; |
|||
} |
|||
++e; |
|||
} |
|||
return strtod(value_ptr, NULL); |
|||
} |
|||
return 0.0; |
|||
} |
|||
|
|||
// Code value as a long or ulong
|
|||
inline static long value_long() { return value_ptr ? strtol(value_ptr, NULL, 10) : 0L; } |
|||
inline unsigned static long value_ulong() { return value_ptr ? strtoul(value_ptr, NULL, 10) : 0UL; } |
|||
|
|||
// Code value for use as time
|
|||
FORCE_INLINE static millis_t value_millis() { return value_ulong(); } |
|||
FORCE_INLINE static millis_t value_millis_from_seconds() { return value_float() * 1000UL; } |
|||
|
|||
// Reduce to fewer bits
|
|||
FORCE_INLINE static int value_int() { return (int)value_long(); } |
|||
FORCE_INLINE uint16_t value_ushort() { return (uint16_t)value_long(); } |
|||
inline static uint8_t value_byte() { return (uint8_t)(constrain(value_long(), 0, 255)); } |
|||
|
|||
// Bool is true with no value or non-zero
|
|||
inline static bool value_bool() { return !has_value() || value_byte(); } |
|||
|
|||
// Units modes: Inches, Fahrenheit, Kelvin
|
|||
|
|||
#if ENABLED(INCH_MODE_SUPPORT) |
|||
|
|||
inline static void set_input_linear_units(LinearUnit units) { |
|||
switch (units) { |
|||
case LINEARUNIT_INCH: |
|||
linear_unit_factor = 25.4; |
|||
break; |
|||
case LINEARUNIT_MM: |
|||
default: |
|||
linear_unit_factor = 1.0; |
|||
break; |
|||
} |
|||
volumetric_unit_factor = pow(linear_unit_factor, 3.0); |
|||
} |
|||
|
|||
inline static float axis_unit_factor(const AxisEnum axis) { |
|||
return (axis >= E_AXIS && volumetric_enabled ? volumetric_unit_factor : linear_unit_factor); |
|||
} |
|||
|
|||
inline static float value_linear_units() { return value_float() * linear_unit_factor; } |
|||
inline static float value_axis_units(const AxisEnum axis) { return value_float() * axis_unit_factor(axis); } |
|||
inline static float value_per_axis_unit(const AxisEnum axis) { return value_float() / axis_unit_factor(axis); } |
|||
|
|||
#else |
|||
|
|||
FORCE_INLINE static float value_linear_units() { return value_float(); } |
|||
FORCE_INLINE static float value_axis_units(const AxisEnum a) { UNUSED(a); return value_float(); } |
|||
FORCE_INLINE static float value_per_axis_unit(const AxisEnum a) { UNUSED(a); return value_float(); } |
|||
|
|||
#endif |
|||
|
|||
#if ENABLED(TEMPERATURE_UNITS_SUPPORT) |
|||
|
|||
inline static void set_input_temp_units(TempUnit units) { input_temp_units = units; } |
|||
|
|||
#if ENABLED(ULTIPANEL) && DISABLED(DISABLE_M503) |
|||
|
|||
FORCE_INLINE static char temp_units_code() { |
|||
return input_temp_units == TEMPUNIT_K ? 'K' : input_temp_units == TEMPUNIT_F ? 'F' : 'C'; |
|||
} |
|||
FORCE_INLINE static char* temp_units_name() { |
|||
return input_temp_units == TEMPUNIT_K ? PSTR("Kelvin") : input_temp_units == TEMPUNIT_F ? PSTR("Fahrenheit") : PSTR("Celsius") |
|||
} |
|||
inline static float to_temp_units(const float &f) { |
|||
switch (input_temp_units) { |
|||
case TEMPUNIT_F: |
|||
return f * 0.5555555556 + 32.0; |
|||
case TEMPUNIT_K: |
|||
return f + 273.15; |
|||
case TEMPUNIT_C: |
|||
default: |
|||
return f; |
|||
} |
|||
} |
|||
|
|||
#endif // ULTIPANEL && !DISABLE_M503
|
|||
|
|||
inline static float value_celsius() { |
|||
const float f = value_float(); |
|||
switch (input_temp_units) { |
|||
case TEMPUNIT_F: |
|||
return (f - 32.0) * 0.5555555556; |
|||
case TEMPUNIT_K: |
|||
return f - 273.15; |
|||
case TEMPUNIT_C: |
|||
default: |
|||
return f; |
|||
} |
|||
} |
|||
|
|||
inline static float value_celsius_diff() { |
|||
switch (input_temp_units) { |
|||
case TEMPUNIT_F: |
|||
return value_float() * 0.5555555556; |
|||
case TEMPUNIT_C: |
|||
case TEMPUNIT_K: |
|||
default: |
|||
return value_float(); |
|||
} |
|||
} |
|||
|
|||
#else |
|||
|
|||
FORCE_INLINE static float value_celsius() { return value_float(); } |
|||
FORCE_INLINE static float value_celsius_diff() { return value_float(); } |
|||
|
|||
#endif |
|||
|
|||
FORCE_INLINE static float value_feedrate() { return value_linear_units(); } |
|||
|
|||
void unknown_command_error(); |
|||
|
|||
}; |
|||
|
|||
extern GCodeParser parser; |
|||
|
|||
#endif // GCODE_H
|
Loading…
Reference in new issue