* companion_radio_usb: encoding in ArduinoSerialInterface changed to 16-bit frame lengths

* MAX_FRAME_SIZE now 172 (to fit max 160 byte text msg)
This commit is contained in:
Scott Powell 2025-01-29 10:12:22 +11:00
parent e53f0d0725
commit 4f0acbd8da
3 changed files with 18 additions and 11 deletions

View file

@ -1,8 +1,9 @@
#include "ArduinoSerialInterface.h" #include "ArduinoSerialInterface.h"
#define RECV_STATE_IDLE 0 #define RECV_STATE_IDLE 0
#define RECV_STATE_HDR_FOUND 1 #define RECV_STATE_HDR_FOUND 1
#define RECV_STATE_LEN_FOUND 2 #define RECV_STATE_LEN1_FOUND 2
#define RECV_STATE_LEN2_FOUND 3
void ArduinoSerialInterface::enable() { void ArduinoSerialInterface::enable() {
_isEnabled = true; _isEnabled = true;
@ -26,10 +27,12 @@ size_t ArduinoSerialInterface::writeFrame(const uint8_t src[], size_t len) {
return 0; return 0;
} }
uint8_t hdr[2]; uint8_t hdr[3];
hdr[0] = '>'; hdr[0] = '>';
hdr[1] = len; hdr[1] = (len & 0xFF); // LSB
_serial->write(hdr, 2); hdr[2] = (len >> 8); // MSB
_serial->write(hdr, 3);
return _serial->write(src, len); return _serial->write(src, len);
} }
@ -45,9 +48,13 @@ size_t ArduinoSerialInterface::checkRecvFrame(uint8_t dest[]) {
} }
break; break;
case RECV_STATE_HDR_FOUND: case RECV_STATE_HDR_FOUND:
_frame_len = (uint8_t)c; _frame_len = (uint8_t)c; // LSB
_state = RECV_STATE_LEN1_FOUND;
break;
case RECV_STATE_LEN1_FOUND:
_frame_len |= ((uint16_t)c) << 8; // MSB
rx_len = 0; rx_len = 0;
_state = _frame_len > 0 ? RECV_STATE_LEN_FOUND : RECV_STATE_IDLE; _state = _frame_len > 0 ? RECV_STATE_LEN2_FOUND : RECV_STATE_IDLE;
break; break;
default: default:
if (rx_len < MAX_FRAME_SIZE) { if (rx_len < MAX_FRAME_SIZE) {

View file

@ -6,8 +6,8 @@
class ArduinoSerialInterface : public BaseSerialInterface { class ArduinoSerialInterface : public BaseSerialInterface {
bool _isEnabled; bool _isEnabled;
uint8_t _state; uint8_t _state;
uint8_t _frame_len; uint16_t _frame_len;
uint8_t rx_len; uint16_t rx_len;
HardwareSerial* _serial; HardwareSerial* _serial;
uint8_t rx_buf[MAX_FRAME_SIZE]; uint8_t rx_buf[MAX_FRAME_SIZE];

View file

@ -2,7 +2,7 @@
#include <Arduino.h> #include <Arduino.h>
#define MAX_FRAME_SIZE 160 #define MAX_FRAME_SIZE 172
class BaseSerialInterface { class BaseSerialInterface {
protected: protected: