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.
This commit is contained in:
Markus Hitter 2015-07-08 18:27:31 +02:00
parent cb1b9b3feb
commit 53dfab3be3
3 changed files with 20 additions and 9 deletions

View File

@ -100,9 +100,16 @@ void gcode_init(void) {
#endif #endif
} }
/// Character Received - add it to our command /** Character received - add it to our command.
/// \param c the next character to process
void gcode_parse_char(uint8_t c) { \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; uint8_t checksum_char = c;
// uppercase // uppercase
@ -401,6 +408,8 @@ void gcode_parse_char(uint8_t c) {
if (next_target.option_all_relative || next_target.option_e_relative) { if (next_target.option_all_relative || next_target.option_e_relative) {
next_target.target.axis[E] = 0; next_target.target.axis[E] = 0;
} }
return 1;
} }
#ifdef SD #ifdef SD
@ -417,6 +426,8 @@ void gcode_parse_char(uint8_t c) {
} }
} }
#endif /* SD */ #endif /* SD */
return 0;
} }
/***************************************************************************\ /***************************************************************************\

View File

@ -72,7 +72,7 @@ extern GCODE_COMMAND next_target;
void gcode_init(void); void gcode_init(void);
/// accept the next character and process it /// 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 // uses the global variable next_target.N
void request_resend(void); void request_resend(void);

View File

@ -271,14 +271,14 @@ int main (void)
{ {
// if queue is full, no point in reading chars- host will just have to wait // if queue is full, no point in reading chars- host will just have to wait
if (queue_full() == 0) { if (queue_full() == 0) {
uint8_t c; uint8_t c, line_done;
if (( ! gcode_active || gcode_active & GCODE_SOURCE_SERIAL) && if (( ! gcode_active || gcode_active & GCODE_SOURCE_SERIAL) &&
serial_rxchars() != 0) { serial_rxchars() != 0) {
gcode_active = GCODE_SOURCE_SERIAL; gcode_active = GCODE_SOURCE_SERIAL;
c = serial_popchar(); c = serial_popchar();
gcode_parse_char(c); line_done = gcode_parse_char(c);
if (c == '\r' || c == '\n') if (line_done)
gcode_active = 0; gcode_active = 0;
} }
@ -294,8 +294,8 @@ int main (void)
} }
else { else {
gcode_active = GCODE_SOURCE_SD; gcode_active = GCODE_SOURCE_SD;
gcode_parse_char(c); line_done = gcode_parse_char(c);
if (c == '\r' || c == '\n') if (line_done)
gcode_active = 0; gcode_active = 0;
} }
} }