From 38fa2af9af0189f2e50d14ca227a629d417ddeb4 Mon Sep 17 00:00:00 2001 From: DigitalConfections Date: Sun, 31 May 2020 17:10:11 -0400 Subject: [PATCH] Ver 0.7 o Serial TTY communications improvements to fix backspace bugs and prevent the user from overflowing buffers. --- Arduino-microfox/Arduino-microfox.ino | 59 +++++++++++++++++++-------- Arduino-microfox/defs.h | 2 +- Arduino-microfox/linkbus.h | 2 + 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/Arduino-microfox/Arduino-microfox.ino b/Arduino-microfox/Arduino-microfox.ino index 87a8b01..87ded46 100644 --- a/Arduino-microfox/Arduino-microfox.ino +++ b/Arduino-microfox/Arduino-microfox.ino @@ -147,7 +147,7 @@ void setUpTemp(void); ; /* Initialize variables stored in EEPROM */ } - cli(); /*stop interrupts for setup */ + cli(); /*stop interrupts for setup */ /* set pins as outputs */ pinMode(PIN_NANO_LED, OUTPUT); /* The nano amber LED: This led blinks when off cycle and blinks with code when on cycle. */ @@ -340,7 +340,7 @@ void setUpTemp(void); ************************************************************************/ ISR(USART_RX_vect) { - static char textBuff[LINKBUS_MAX_MSG_FIELD_LENGTH]; + static char textBuff[LINKBUS_MAX_COMMANDLINE_LENGTH]; static LinkbusRxBuffer* buff = NULL; static uint8_t charIndex = 0; static uint8_t field_index = 0; @@ -366,7 +366,7 @@ ISR(USART_RX_vect) rx_char = '\0'; ignoreCount--; } - else if(rx_char == 0x1B) /* ESC sequence start */ + else if(rx_char == 0x1B) /* Ignore ESC sequences */ { rx_char = '\0'; @@ -375,9 +375,9 @@ ISR(USART_RX_vect) rx_char = textBuff[charIndex]; } - ignoreCount = 2; /* throw out the next two characters */ + ignoreCount = 2; /* throw out the next two characters */ } - if(rx_char == 0x0D) /* Handle carriage return */ + else if(rx_char == 0x0D) /* Handle carriage return */ { if(receiving_msg) { @@ -427,18 +427,31 @@ ISR(USART_RX_vect) else if(field_len) { field_len--; + buff->fields[field_index - 1][field_len] = '\0'; + } + else if(textBuff[charIndex] == ' ') + { + field_index--; + field_len = strlen(buff->fields[field_index]); } else { buff->fields[field_index][0] = '\0'; field_index--; } + + textBuff[charIndex] = '\0'; /* replace deleted char with null */ + + if(charIndex == 0) + { + receiving_msg = FALSE; + } } else { if(rx_char == ' ') { - if(textBuff[charIndex - 1] == ' ') + if((textBuff[charIndex - 1] == ' ') || ((field_index + 1) >= LINKBUS_MAX_MSG_NUMBER_OF_FIELDS)) { rx_char = '\0'; } @@ -446,41 +459,55 @@ ISR(USART_RX_vect) { if(field_index > 0) { - buff->fields[field_index - 1][field_len] = 0; + buff->fields[field_index - 1][field_len] = '\0'; } field_index++; field_len = 0; + charIndex = MIN(charIndex + 1, (LINKBUS_MAX_COMMANDLINE_LENGTH - 1)); } } - else + else if(field_len < LINKBUS_MAX_MSG_FIELD_LENGTH) { if(field_index == 0) /* message ID received */ { msg_ID = msg_ID * 10 + rx_char; + field_len++; } else { buff->fields[field_index - 1][field_len++] = rx_char; + buff->fields[field_index - 1][field_len] = '\0'; } - } - charIndex = MIN(charIndex + 1, LINKBUS_MAX_MSG_FIELD_LENGTH); + charIndex = MIN(charIndex + 1, (LINKBUS_MAX_COMMANDLINE_LENGTH - 1)); + } + else + { + rx_char = '\0'; + } } } else { - if((rx_char == 0x7F) || (rx_char == ' ')) /* Handle backspace and Space */ + if(rx_char == 0x7F) /* Handle Backspace */ + { + if(msg_ID <= 0) + { + rx_char = '\0'; + } + + msg_ID = 0; + } + else if(rx_char == ' ') /* Handle Space */ { rx_char = '\0'; } - else /* start of new message */ + else /* start of new message */ { uint8_t i; field_index = 0; - msg_ID = 0; - - msg_ID = msg_ID * 10 + rx_char; + msg_ID = rx_char; /* Empty the field buffers */ for(i = 0; i < LINKBUS_MAX_MSG_NUMBER_OF_FIELDS; i++) @@ -489,7 +516,7 @@ ISR(USART_RX_vect) } receiving_msg = TRUE; - charIndex = MIN(charIndex + 1, LINKBUS_MAX_MSG_FIELD_LENGTH); + charIndex++; } } diff --git a/Arduino-microfox/defs.h b/Arduino-microfox/defs.h index 4ab5295..db73729 100644 --- a/Arduino-microfox/defs.h +++ b/Arduino-microfox/defs.h @@ -57,7 +57,7 @@ /****************************************************** * Set the text that gets displayed to the user */ -#define SW_REVISION "0.6" +#define SW_REVISION "0.7" //#define TRANQUILIZE_WATCHDOG diff --git a/Arduino-microfox/linkbus.h b/Arduino-microfox/linkbus.h index e25292f..55c992f 100644 --- a/Arduino-microfox/linkbus.h +++ b/Arduino-microfox/linkbus.h @@ -38,6 +38,8 @@ #define LINKBUS_MAX_TX_MSG_LENGTH 41 #define LINKBUS_NUMBER_OF_TX_MSG_BUFFERS 4 +#define LINKBUS_MAX_COMMANDLINE_LENGTH ((1+LINKBUS_MAX_MSG_FIELD_LENGTH) * LINKBUS_MAX_MSG_NUMBER_OF_FIELDS) + #define LINKBUS_POWERUP_DELAY_SECONDS 6 #define LINKBUS_MIN_TX_INTERVAL_MS 100