From 23be2d14495392d19350b90982ac17bee7dbd72f Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Mon, 6 Jul 2015 18:39:39 +0200 Subject: [PATCH] SD card: finally(!) implement printing from SD card. Turned out to be pretty easy with all the more complex bits already in place. Strategy is to always parse a full line from one of the sources. Accordingly, simply sending a character on the serial line stops reading from SD until the line coming in over serial is completed. --- gcode_process.c | 7 +++++++ gcode_process.h | 1 + mendel.c | 34 +++++++++++++++++++--------------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/gcode_process.c b/gcode_process.c index 9b809c8..f0ef33f 100644 --- a/gcode_process.c +++ b/gcode_process.c @@ -42,6 +42,13 @@ uint8_t next_tool; */ enum gcode_source gcode_sources = GCODE_SOURCE_SERIAL; +/** Bitfield for the current source of G-code. + + Only one bit should be set at a time. The bit is set at start reading a + line and cleared when a line is done. +*/ +enum gcode_source gcode_active = 0; + /************************************************************************//** \brief Processes command stored in global \ref next_target. diff --git a/gcode_process.h b/gcode_process.h index 21a8b82..597aa3d 100644 --- a/gcode_process.h +++ b/gcode_process.h @@ -10,6 +10,7 @@ enum gcode_source { extern enum gcode_source gcode_sources; +extern enum gcode_source gcode_active; // the current tool extern uint8_t tool; diff --git a/mendel.c b/mendel.c index d98f885..c63e5e3 100644 --- a/mendel.c +++ b/mendel.c @@ -273,23 +273,31 @@ int main (void) if (queue_full() == 0) { uint8_t c; - if (serial_rxchars() != 0) { + 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') + gcode_active = 0; } #ifdef SD - if (gcode_sources & GCODE_SOURCE_SD) { + if (( ! gcode_active || gcode_active & GCODE_SOURCE_SD) && + gcode_sources & GCODE_SOURCE_SD) { c = sd_read_char(); - /* Demo code: just read all the bytes without doing anything with - them to allow measuring how long it takes to read a file. Report - over serial when the file is done. */ if (c == 0) { serial_writestr_P(PSTR("\nSD file done.\n")); gcode_sources &= ! GCODE_SOURCE_SD; // There is no pf_close(), subsequent reads will stick at EOF // and return zeros. } + else { + gcode_active = GCODE_SOURCE_SD; + gcode_parse_char(c); + if (c == '\r' || c == '\n') + gcode_active = 0; + } } #endif @@ -299,17 +307,13 @@ int main (void) This code works on a per-character basis. - Any data received over serial WILL be randomly distributed through - the canned gcode, and you'll have a big mess! + Unlike with SD reading code above and for historical reasons (was + a quicky doing its job, before SD card was implemented), any data + received over serial WILL be randomly distributed through the canned + G-code, and you'll have a big mess! - The solution is to either store gcode parser state with each source, - or only parse a line at a time. - - This will take extra ram, and may be out of scope for the Teacup - project. - - If ever print-from-SD card is implemented, these changes may become - necessary. + The solution is to join the strategy above and make canned G-code + a third G-code source next to serial and SD. */ static uint32_t canned_gcode_pos = 0;