diff options
author | Peter de Kok <p.j.s.d.kok@gmail.com> | 2015-06-10 18:35:59 +0200 |
---|---|---|
committer | Peter de Kok <p.j.s.d.kok@gmail.com> | 2015-06-10 18:35:59 +0200 |
commit | 0185ca207cf25b3621b40ee35718e1decdd5fa08 (patch) | |
tree | e3b26cbf30fed62aad587441ed08c42dd3594a60 /Venus_Skeleton | |
parent | a8b4777f6f395a4d0576bdccaee4704e22d23d01 (diff) | |
parent | d110079f946fa1066d1d29187dcee3a4a99333f5 (diff) | |
download | code-0185ca207cf25b3621b40ee35718e1decdd5fa08.tar.gz |
Merge branch 'master' of git.lekensteyn.nl:tue/5XIB0-Venus/code
Diffstat (limited to 'Venus_Skeleton')
-rw-r--r-- | Venus_Skeleton/Makefile | 31 | ||||
-rw-r--r-- | Venus_Skeleton/comm.cpp (renamed from Venus_Skeleton/comm.c) | 34 | ||||
-rw-r--r-- | Venus_Skeleton/comm.h | 5 |
3 files changed, 60 insertions, 10 deletions
diff --git a/Venus_Skeleton/Makefile b/Venus_Skeleton/Makefile index 26b4ee2..c2c5198 100644 --- a/Venus_Skeleton/Makefile +++ b/Venus_Skeleton/Makefile @@ -2,9 +2,34 @@ CC = avr-gcc WFLAGS = -Wall -Wextra -Wno-attributes WFLAGS += -fdiagnostics-color=auto -CFLAGS = +EXTRA_CFLAGS = $(shell cat .syntastic_c_config) -O2 $(CFLAGS) -SOURCES = Venus_Skeleton.ino +MY_SOURCES = Venus_Skeleton.ino comm.cpp +MY_OBJECTS = $(patsubst %.cpp,%.o,$(patsubst %.ino,%.o,$(MY_SOURCES))) +LIBS_SOURCES = $(wildcard libs/*/*.cpp) +LIBS_OBJECTS = $(patsubst %.cpp,%.o,$(LIBS_SOURCES)) +SOURCES = $(MY_SOURCES) $(LIBS_SOURCES) +OBJECTS = $(MY_OBJECTS) $(LIBS_OBJECTS) +PROGRAM ?= main +_V_0 = @ +_V = $(_V_$(V)) + +.PHONY: main clean check check: - $(CC) $(WFLAGS) $$(cat .syntastic_c_config) $(CFLAGS) -x c++ $(SOURCES) -o /dev/null -O2 + rm -f $(MY_OBJECTS) + $(MAKE) $(MY_OBJECTS) +# Note: does not compile due to link errors. +$(PROGRAM): $(OBJECTS) + $(_V)$(CC) $(WFLAGS) $(EXTRA_CFLAGS) $(OBJECTS) -o $@ + +clean: + $(_V)rm -f $(PROGRAM) $(OBJECTS) + +# Not my code... +libs/%.o: libs/%.cpp + $(_V)$(CC) -w $(EXTRA_CFLAGS) -c -o $@ $< +%.o: %.cpp + $(_V)$(CC) $(WFLAGS) $(EXTRA_CFLAGS) -c -o $@ $< +%.o: %.ino + $(_V)$(CC) $(WFLAGS) $(EXTRA_CFLAGS) -c -x c++ -o $@ $< diff --git a/Venus_Skeleton/comm.c b/Venus_Skeleton/comm.cpp index f5392e0..d60138e 100644 --- a/Venus_Skeleton/comm.c +++ b/Venus_Skeleton/comm.cpp @@ -3,7 +3,9 @@ */ #include <stdbool.h> +#include "definitions.h" #include "dataTypes.h" +#include "comm.h" serial_state_t serial_state = SERIAL_UNKNOWN; @@ -38,7 +40,7 @@ static void handle_ctrl(int r) { void handle_serial(data_t *data, int changedBits) { // first attempt sync while (Serial.available()) { - r = Serial.read(); + int r = Serial.read(); if ((r & 0xC0) == 0x80) { // handle control packets @@ -59,18 +61,20 @@ void handle_serial(data_t *data, int changedBits) { } // writes data of length len -static void serial_write_data(const char *data, unsigned len) { +static void serial_write_len(unsigned len) { len--; // length on wire is one smaller (00 means 1 byte, 01 means 2, etc.) - if (len >= 0 && len < 56) { + if (len < 56) { Serial.write(0x80 + len + 7); - } else if (len >= 0 && len < 256) { + } else if (len < DATA_MAX_SIZE) { Serial.write(0x88 | (len >> 5)); Serial.write(0xA0 | (len & 0x1F)); } else { // zero length or too long. - return; } - // write remainder +} + +// write remainder of data +static void serial_write_data(const char *data, unsigned len) { for (unsigned i = 0; i < len; i++) { char c = data[i]; if (c & 0x80) { @@ -83,7 +87,23 @@ static void serial_write_data(const char *data, unsigned len) { } void serial_print_debug(const char *str) { - serial_print_debug(str, strlen(str)); + unsigned len = strlen(str); + const char c = DATA_DEBUG_PREFIX; + + // only write data when the serial connection is ready + if (serial_state != SERIAL_READY) { + return; + } + + while (len > 0) { + // len is no more than DATA_MAX_SIZE - 1 because of debug prefix + unsigned datalen = len >= DATA_MAX_SIZE ? DATA_MAX_SIZE - 1 : len; + // add one for the debug prefix + serial_write_len(1 + datalen); + serial_write_data(&c, 1); /* write prefix */ + serial_write_data(str, datalen); /* write actual data payload */ + len -= datalen; + } } static void handle_recv(int r) { diff --git a/Venus_Skeleton/comm.h b/Venus_Skeleton/comm.h index 5cdf77e..7f3b43c 100644 --- a/Venus_Skeleton/comm.h +++ b/Venus_Skeleton/comm.h @@ -9,6 +9,11 @@ typedef enum { DATA_ESCAPE = 0x84, } serial_control_command_t; +// prefix for Debug Data packets +#define DATA_DEBUG_PREFIX 0xC0 +// maximum size of a data packet +#define DATA_MAX_SIZE 256 + typedef enum { // waiting for RESET1 SERIAL_UNKNOWN, |