From badb638df925924c05b3b5e49880590e53faa759 Mon Sep 17 00:00:00 2001 From: Sonny J Date: Mon, 15 Aug 2011 19:39:44 -0600 Subject: [PATCH] Moved comment and block delete handling into protocol.c from gcode.c. Fixes bug when comment and block delete are not isolated. Blank lines ignored. Comments, block delete characters, and blank lines are no longer passed to the gcode parser and should free up some memory by ignoring these characters. Gcode parser now expects clean gcode only. There was a bug if there were block deletes or comments not in the first character (i.e. spindle on/off for proofing geode without turning it on, or a NXX followed by a comment). This should fix it by bypassing the problem. Left a commented line for easily turning on and off block deletes for a later feature, if desired. --- gcode.c | 7 ++----- protocol.c | 46 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/gcode.c b/gcode.c index be62ce8..62fec77 100644 --- a/gcode.c +++ b/gcode.c @@ -117,7 +117,8 @@ static double theta(double x, double y) #endif // Executes one line of 0-terminated G-Code. The line is assumed to contain only uppercase -// characters and signed floating point values (no whitespace). +// characters and signed floating point values (no whitespace). Comments and block delete +// characters have been removed. uint8_t gc_execute_line(char *line) { uint8_t char_counter = 0; char letter; @@ -139,10 +140,6 @@ uint8_t gc_execute_line(char *line) { gc.status_code = STATUS_OK; - // Disregard comments and block delete - if (line[0] == '(') { return(gc.status_code); } - if (line[0] == '/') { char_counter++; } // ignore block delete - // Pass 1: Commands while(next_statement(&letter, &value, line, &char_counter)) { int_value = trunc(value); diff --git a/protocol.c b/protocol.c index 1275f8d..2e2acd3 100644 --- a/protocol.c +++ b/protocol.c @@ -72,20 +72,44 @@ uint8_t protocol_execute_line(char *line) { void protocol_process() { char c; + uint8_t iscomment = false; while((c = serial_read()) != SERIAL_NO_DATA) { - if((char_counter > 0) && ((c == '\n') || (c == '\r'))) { // Line is complete. Then execute! - line[char_counter] = 0; // treminate string - status_message(protocol_execute_line(line)); - char_counter = 0; // reset line buffer index - } else if (c <= ' ') { - // Throw away whitepace and control characters - } else if (char_counter >= LINE_BUFFER_SIZE-1) { - // Throw away any characters beyond the end of the line buffer - } else if (c >= 'a' && c <= 'z') { // Upcase lowercase - line[char_counter++] = c-'a'+'A'; + if ((c == '\n') || (c == '\r')) { // End of block reached + if (char_counter > 0) {// Line is complete. Then execute! + line[char_counter] = 0; // terminate string + status_message(protocol_execute_line(line)); + } else { + // Empty or comment line. Skip block. + status_message(STATUS_OK); // Send status message for syncing purposes. + } + char_counter = 0; // Reset line buffer index + iscomment = false; // Reset comment flag } else { - line[char_counter++] = c; + if (iscomment) { + // Throw away all comment characters + if (c == ')') { + // End of comment. Resume line. + iscomment = false; + } + } else { + if (c <= ' ') { + // Throw away whitepace and control characters + } else if (c == '/') { + // Disable block delete and throw away character + // To enable block delete, uncomment following line. Will ignore until EOL. + // iscomment = true; + } else if (c == '(') { + // Enable comments flag and ignore all characters until ')' or EOL. + iscomment = true; + } else if (char_counter >= LINE_BUFFER_SIZE-1) { + // Throw away any characters beyond the end of the line buffer + } else if (c >= 'a' && c <= 'z') { // Upcase lowercase + line[char_counter++] = c-'a'+'A'; + } else { + line[char_counter++] = c; + } + } } } }