From aed4bfdb5bf773c850581509d582a281aec4fd59 Mon Sep 17 00:00:00 2001 From: Michael Moon Date: Sat, 30 Aug 2014 19:23:54 +1000 Subject: [PATCH] Canned G-code: implement continuous replay of G-code stored on flash. --- Makefile-common | 12 ++++++++++-- canned_gcode.g | 3 +++ canned_gcode.h | 3 +++ mendel.c | 38 +++++++++++++++++++++++++++++++++++--- 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 canned_gcode.g create mode 100644 canned_gcode.h diff --git a/Makefile-common b/Makefile-common index 15c67e6..31cd81b 100644 --- a/Makefile-common +++ b/Makefile-common @@ -49,7 +49,7 @@ OBJDUMP = $(TOOLCHAIN)objdump OBJCOPY = $(TOOLCHAIN)objcopy -OBJ = $(patsubst %.c,$(BUILDDIR)/%.o,$(SOURCES)) +OBJ = $(patsubst %.c,$(BUILDDIR)/%.o,$(SOURCES)) $(BUILDDIR)/canned_gcode.o .PHONY: all clean doc functionsbysize .PRECIOUS: %.o %.elf @@ -57,7 +57,7 @@ OBJ = $(patsubst %.c,$(BUILDDIR)/%.o,$(SOURCES)) all: $(BUILDDIR) $(TARGET) clean: - rm -rf $(BUILDDIR) $(TARGET) *~ + rm -rf $(BUILDDIR) $(TARGET) *~ canned_gcode.c doc: Doxyfile *.c *.h doxygen $< @@ -107,3 +107,11 @@ $(BUILDDIR)/%.bin: $(BUILDDIR)/%.elf $(BUILDDIR)/%.sym: $(BUILDDIR)/%.elf @echo " SYM $@" @$(OBJDUMP) -t $< | perl -ne 'BEGIN { printf " ADDR NAME SIZE\n"; } /([0-9a-f]+)\s+(\w+)\s+O\s+\.(bss|data)\s+([0-9a-f]+)\s+(\w+)/ && printf "0x%04x %-20s +%d\n", eval("0x$$1") & 0xFFFF, $$5, eval("0x$$4")' | sort -k1 > $@ + +canned_gcode.c: canned_gcode.g + @echo " GEN $@" + @echo "#include \"canned_gcode.h\"" > $@ + @echo "" >> $@ + @echo -n "const char canned_gcode_P[] PROGMEM = \"" >> $@ + @perl -pe 's/\n/\\n/gs' $< >> $@ + @echo "\";" >> $@ diff --git a/canned_gcode.g b/canned_gcode.g new file mode 100644 index 0000000..7605b24 --- /dev/null +++ b/canned_gcode.g @@ -0,0 +1,3 @@ +G28 +G1 X100 +G1 X0 diff --git a/canned_gcode.h b/canned_gcode.h new file mode 100644 index 0000000..6942bc1 --- /dev/null +++ b/canned_gcode.h @@ -0,0 +1,3 @@ +#include + +extern const char canned_gcode_P[] PROGMEM; diff --git a/mendel.c b/mendel.c index 93ee06a..a78abfb 100644 --- a/mendel.c +++ b/mendel.c @@ -254,13 +254,45 @@ int main (void) #endif init(); + #ifdef CANNED_CYCLE + #include "canned_gcode.h" + uint32_t canned_gcode_pos = 0; + #endif /* CANNED_CYCLE */ + // main loop for (;;) { // if queue is full, no point in reading chars- host will just have to wait - if ((serial_rxchars() != 0) && (queue_full() == 0)) { - uint8_t c = serial_popchar(); - gcode_parse_char(c); + if (queue_full() == 0) { + if (serial_rxchars() != 0) { + uint8_t c = serial_popchar(); + gcode_parse_char(c); + } + + #ifdef CANNED_CYCLE + /** + WARNING! + + 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! + + 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. + */ + if (pgm_read_byte(canned_gcode_P[++canned_gcode_pos]) == 0) + canned_gcode_pos = 0; + else + gcode_parse_char(pgm_read_byte(canned_gcode_P[canned_gcode_pos])); + + #endif /* CANNED_CYCLE */ } clock();