From c5b542f43846d4c8c25c105c34775538de02dc78 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Fri, 13 Nov 2020 12:09:10 +0000 Subject: [PATCH] Allow serial data to be sent back to the host. --- SerialArduino.cpp | 2 +- SerialPort.cpp | 51 ++++++++++++++++++++++++++----- SerialPort.h | 8 ++++- SerialSTM.cpp | 2 +- SerialSTM_CMSIS.cpp | 2 +- I2CTeensy.cpp => SerialTeensy.cpp | 2 +- 6 files changed, 55 insertions(+), 12 deletions(-) rename I2CTeensy.cpp => SerialTeensy.cpp (98%) diff --git a/SerialArduino.cpp b/SerialArduino.cpp index db49759..74fa70f 100644 --- a/SerialArduino.cpp +++ b/SerialArduino.cpp @@ -43,7 +43,7 @@ void CSerialPort::beginInt(uint8_t n, int speed) } } -int CSerialPort::availableInt(uint8_t n) +int CSerialPort::availableForReadInt(uint8_t n) { switch (n) { case 1U: diff --git a/SerialPort.cpp b/SerialPort.cpp index 2d8db36..c55659a 100644 --- a/SerialPort.cpp +++ b/SerialPort.cpp @@ -81,7 +81,7 @@ const uint8_t MMDVM_FM_EOT = 0x67U; const uint8_t MMDVM_ACK = 0x70U; const uint8_t MMDVM_NAK = 0x7FU; -const uint8_t MMDVM_SERIAL = 0x80U; +const uint8_t MMDVM_SERIAL_DATA = 0x80U; const uint8_t MMDVM_TRANSPARENT = 0x90U; const uint8_t MMDVM_QSO_INFO = 0x91U; @@ -122,13 +122,18 @@ const char HARDWARE[] = concat(HW_TYPE, VERSION, TCXO, __TIME__, __DATE__); const uint8_t PROTOCOL_VERSION = 2U; +// Parameters for batching serial data +const int MAX_SERIAL_DATA = 250; +const uint16_t MAX_SERIAL_COUNT = 100U; CSerialPort::CSerialPort() : m_buffer(), m_ptr(0U), m_len(0U), m_debug(false), -m_repeat() +m_repeat(), +m_lastAvail(0), +m_lastAvailCount(0U) { } @@ -849,7 +854,7 @@ void CSerialPort::start() void CSerialPort::process() { - while (availableInt(1U)) { + while (availableForReadInt(1U)) { uint8_t c = readInt(1U); if (m_ptr == 0U) { @@ -912,9 +917,22 @@ void CSerialPort::process() } } - // Read any incoming serial data - while (availableInt(3U)) - readInt(3U); + // Read any incoming serial data, and send out in batches + int avail = availableForReadInt(3U); + if ((avail > 0 && avail == m_lastAvail && m_lastAvailCount >= MAX_SERIAL_COUNT) || (avail >= MAX_SERIAL_DATA)) { + uint8_t buffer[MAX_SERIAL_DATA]; + for (int i = 0; i < avail && i < MAX_SERIAL_DATA; i++) { + buffer[i] = readInt(3U); + m_lastAvail--; + } + writeSerialData(buffer, avail - m_lastAvail); + m_lastAvailCount = 0U; + } else if (avail > 0U && avail == m_lastAvail) { + m_lastAvailCount++; + } else { + m_lastAvail = avail; + m_lastAvailCount = 0U; + } #endif } @@ -1284,7 +1302,7 @@ void CSerialPort::processMessage(const uint8_t* buffer, uint16_t length) break; #if defined(SERIAL_REPEATER) - case MMDVM_SERIAL: { + case MMDVM_SERIAL_DATA: { for (uint8_t i = 3U; i < m_len; i++) m_repeat.put(m_buffer[i]); } @@ -1718,6 +1736,25 @@ void CSerialPort::writeAX25Data(const uint8_t* data, uint16_t length) } #endif +#if defined(SERIAL_REPEATER) +void CSerialPort::writeSerialData(const uint8_t* data, uint8_t length) +{ + uint8_t reply[255U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_SERIAL_DATA; + + uint8_t count = 3U; + for (uint8_t i = 0U; i < length; i++, count++) + reply[count] = data[i]; + + reply[1U] = count; + + writeInt(1U, reply, count); +} +#endif + void CSerialPort::writeCalData(const uint8_t* data, uint8_t length) { if (m_modemState != STATE_DSTARCAL) diff --git a/SerialPort.h b/SerialPort.h index a9bec4d..4561dc5 100644 --- a/SerialPort.h +++ b/SerialPort.h @@ -79,6 +79,10 @@ public: void writeFMEOT(); #endif +#if defined(SERIAL_REPEATER) + void writeSerialData(const uint8_t* data, uint8_t length); +#endif + void writeCalData(const uint8_t* data, uint8_t length); void writeRSSIData(const uint8_t* data, uint8_t length); @@ -94,6 +98,8 @@ private: uint16_t m_len; bool m_debug; CRingBuffer m_repeat; + int m_lastAvail; + uint16_t m_lastAvailCount; void sendACK(); void sendNAK(uint8_t err); @@ -113,7 +119,7 @@ private: // Hardware versions void beginInt(uint8_t n, int speed); - int availableInt(uint8_t n); + int availableForReadInt(uint8_t n); int availableForWriteInt(uint8_t n); uint8_t readInt(uint8_t n); void writeInt(uint8_t n, const uint8_t* data, uint16_t length, bool flush = false); diff --git a/SerialSTM.cpp b/SerialSTM.cpp index a2e8490..31c2174 100644 --- a/SerialSTM.cpp +++ b/SerialSTM.cpp @@ -327,7 +327,7 @@ void CSerialPort::beginInt(uint8_t n, int speed) } } -int CSerialPort::availableInt(uint8_t n) +int CSerialPort::availableForReadInt(uint8_t n) { switch (n) { case 1U: diff --git a/SerialSTM_CMSIS.cpp b/SerialSTM_CMSIS.cpp index 38127ac..71e4ff2 100644 --- a/SerialSTM_CMSIS.cpp +++ b/SerialSTM_CMSIS.cpp @@ -118,7 +118,7 @@ void CSerialPort::beginInt(uint8_t n, int speed) } } -int CSerialPort::availableInt(uint8_t n) +int CSerialPort::availableForReadInt(uint8_t n) { switch (n) { case 1U: diff --git a/I2CTeensy.cpp b/SerialTeensy.cpp similarity index 98% rename from I2CTeensy.cpp rename to SerialTeensy.cpp index cfa5bb1..9e86721 100644 --- a/I2CTeensy.cpp +++ b/SerialTeensy.cpp @@ -182,7 +182,7 @@ void CSerialPort::beginInt(uint8_t n, int speed) } } -int CSerialPort::availableInt(uint8_t n) +int CSerialPort::availableForReadInt(uint8_t n) { switch (n) { case 1U: