summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter de Kok <p.j.s.d.kok@gmail.com>2015-06-10 18:35:59 +0200
committerPeter de Kok <p.j.s.d.kok@gmail.com>2015-06-10 18:35:59 +0200
commit0185ca207cf25b3621b40ee35718e1decdd5fa08 (patch)
treee3b26cbf30fed62aad587441ed08c42dd3594a60
parenta8b4777f6f395a4d0576bdccaee4704e22d23d01 (diff)
parentd110079f946fa1066d1d29187dcee3a4a99333f5 (diff)
downloadcode-0185ca207cf25b3621b40ee35718e1decdd5fa08.tar.gz
Merge branch 'master' of git.lekensteyn.nl:tue/5XIB0-Venus/code
-rw-r--r--.gitignore4
-rw-r--r--Venus_Skeleton/Makefile31
-rw-r--r--Venus_Skeleton/comm.cpp (renamed from Venus_Skeleton/comm.c)34
-rw-r--r--Venus_Skeleton/comm.h5
-rw-r--r--rpi2/app/comm_arduino.py11
-rw-r--r--rpi2/design.txt2
6 files changed, 69 insertions, 18 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4fe7862
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+*.o
+.*.sw?
+__pycache__/
+*.py[oc]
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,
diff --git a/rpi2/app/comm_arduino.py b/rpi2/app/comm_arduino.py
index 884c325..b34f1d1 100644
--- a/rpi2/app/comm_arduino.py
+++ b/rpi2/app/comm_arduino.py
@@ -25,10 +25,7 @@ class Ctrl(object):
def is_control(b):
return (b & 0xC0) == 0x80
-def get_data_length(b):
- if (b & 0xC0) != 0x80:
- return 0
- return (b & 0x3f) - 4
+DATA_DEBUG_PREFIX = b'\xC0'
def configure_serial(path):
# Disables parameters which are not controlled by pyserial
@@ -63,8 +60,8 @@ class SerialState(object):
while True:
try:
return self.ser.read()
- except serial.SerialTimeoutException:
- pass
+ except serial.SerialTimeoutException as e:
+ _logger.warn('Read timeout: %s', e)
def handle_one(self, ser):
b = self._read_one(ser)
@@ -192,7 +189,7 @@ def handle_serial(path):
while True:
try:
data = state.handle_one(ser)
- if data and data[0] == b'\x80': # Debug packet
+ if data and data.startswith(DATA_DEBUG_PREFIX):
handle_data_debug(data[1:])
elif data:
_logger.info('PI DATA: %r', data)
diff --git a/rpi2/design.txt b/rpi2/design.txt
index 6957ad5..10c0c34 100644
--- a/rpi2/design.txt
+++ b/rpi2/design.txt
@@ -68,7 +68,7 @@ Data is interpreted as follows:
0... .... .... .... Bits that need to be updated (max 15). The length of the
following data depends on this.
-1000 0000 Debug packet, max len is determined by higher-level hdr.
+1100 0000 Debug packet, max len is determined by higher-level hdr.
Serial state machine: