diff options
author | Peter Wu <peter@lekensteyn.nl> | 2015-06-10 18:22:50 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2015-06-10 18:22:50 +0200 |
commit | d110079f946fa1066d1d29187dcee3a4a99333f5 (patch) | |
tree | 27bbc6b6eb8078312db95e54284ba662987412a2 | |
parent | dad98df26e43dc08876270fa7cb575ed9bb36679 (diff) | |
download | code-d110079f946fa1066d1d29187dcee3a4a99333f5.tar.gz |
Compile fixes for comm, fix debug write
-rw-r--r-- | Venus_Skeleton/comm.cpp | 34 | ||||
-rw-r--r-- | Venus_Skeleton/comm.h | 5 | ||||
-rw-r--r-- | rpi2/app/comm_arduino.py | 11 | ||||
-rw-r--r-- | rpi2/design.txt | 2 |
4 files changed, 37 insertions, 15 deletions
diff --git a/Venus_Skeleton/comm.cpp b/Venus_Skeleton/comm.cpp index f5392e0..d60138e 100644 --- a/Venus_Skeleton/comm.cpp +++ 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: |