From 53dfab3be35896fabca701040e79b1c002974db2 Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Wed, 8 Jul 2015 18:27:31 +0200 Subject: [PATCH] G-code parser: check for end of line in the parser. This raises abstraction and even makes the binary a bit smaller (2 bytes without SD, 14 bytes with SD). A G-code file with 16'384 lines of comments, 64 bytes per line ( = 1 MB file size), is read and parsed from SD card in 2:47 minutes, or at a speed of 5924 bytes/second. --- gcode_parse.c | 17 ++++++++++++++--- gcode_parse.h | 2 +- mendel.c | 10 +++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/gcode_parse.c b/gcode_parse.c index 0ea6c2d..f3aa6b6 100644 --- a/gcode_parse.c +++ b/gcode_parse.c @@ -100,9 +100,16 @@ void gcode_init(void) { #endif } -/// Character Received - add it to our command -/// \param c the next character to process -void gcode_parse_char(uint8_t c) { +/** Character received - add it to our command. + + \param c The next character to process. + + \return Wether end of line was reached. + + This parser operates character by character, so there's no need for a + buffer holding the entire line of G-code. +*/ +uint8_t gcode_parse_char(uint8_t c) { uint8_t checksum_char = c; // uppercase @@ -401,6 +408,8 @@ void gcode_parse_char(uint8_t c) { if (next_target.option_all_relative || next_target.option_e_relative) { next_target.target.axis[E] = 0; } + + return 1; } #ifdef SD @@ -417,6 +426,8 @@ void gcode_parse_char(uint8_t c) { } } #endif /* SD */ + + return 0; } /***************************************************************************\ diff --git a/gcode_parse.h b/gcode_parse.h index 5b19b90..8666375 100644 --- a/gcode_parse.h +++ b/gcode_parse.h @@ -72,7 +72,7 @@ extern GCODE_COMMAND next_target; void gcode_init(void); /// accept the next character and process it -void gcode_parse_char(uint8_t c); +uint8_t gcode_parse_char(uint8_t c); // uses the global variable next_target.N void request_resend(void); diff --git a/mendel.c b/mendel.c index c63e5e3..5964f5e 100644 --- a/mendel.c +++ b/mendel.c @@ -271,14 +271,14 @@ int main (void) { // if queue is full, no point in reading chars- host will just have to wait if (queue_full() == 0) { - uint8_t c; + uint8_t c, line_done; if (( ! gcode_active || gcode_active & GCODE_SOURCE_SERIAL) && serial_rxchars() != 0) { gcode_active = GCODE_SOURCE_SERIAL; c = serial_popchar(); - gcode_parse_char(c); - if (c == '\r' || c == '\n') + line_done = gcode_parse_char(c); + if (line_done) gcode_active = 0; } @@ -294,8 +294,8 @@ int main (void) } else { gcode_active = GCODE_SOURCE_SD; - gcode_parse_char(c); - if (c == '\r' || c == '\n') + line_done = gcode_parse_char(c); + if (line_done) gcode_active = 0; } }