Skip to content

Commit 785422d

Browse files
committed
Switch USB serial output between status & dbg msgs to GNSS data
1 parent 88a47b4 commit 785422d

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

Firmware/RTK_Everywhere/Tasks.ino

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ enum RingBufferConsumers
5656
RBC_TCP_SERVER,
5757
RBC_SD_CARD,
5858
RBC_UDP_SERVER,
59+
RBC_USB_SERIAL,
5960
// Insert new consumers here
6061
RBC_MAX
6162
};
6263

6364
const char *const ringBufferConsumer[] = {
64-
"Bluetooth", "TCP Client", "TCP Server", "SD Card", "UDP Server",
65+
"Bluetooth", "TCP Client", "TCP Server", "SD Card", "UDP Server", "USB Serial",
6566
};
6667

6768
const int ringBufferConsumerEntries = sizeof(ringBufferConsumer) / sizeof(ringBufferConsumer[0]);
@@ -101,6 +102,7 @@ unsigned long lastGnssSend; // Timestamp of the last time we sent RTCM to GNSS
101102
// Ring buffer tails
102103
static RING_BUFFER_OFFSET btRingBufferTail; // BT Tail advances as it is sent over BT
103104
static RING_BUFFER_OFFSET sdRingBufferTail; // SD Tail advances as it is recorded to SD
105+
static RING_BUFFER_OFFSET usbRingBufferTail; // USB Tail advances as it is sent over USB serial
104106

105107
// Ring buffer offsets
106108
static uint16_t rbOffsetHead;
@@ -898,6 +900,58 @@ void handleGnssDataTask(void *e)
898900
}
899901
}
900902

903+
//----------------------------------------------------------------------
904+
// Send data over USB serial
905+
//----------------------------------------------------------------------
906+
907+
startMillis = millis();
908+
909+
// Determine USB serial connection state
910+
if (!forwardGnssDataToUsbSerial)
911+
// Discard the data
912+
usbRingBufferTail = dataHead;
913+
else
914+
{
915+
// Determine the amount of USB serial data in the buffer
916+
bytesToSend = dataHead - usbRingBufferTail;
917+
if (bytesToSend < 0)
918+
bytesToSend += settings.gnssHandlerBufferSize;
919+
if (bytesToSend > 0)
920+
{
921+
// Reduce bytes to send if we have more to send then the end of
922+
// the buffer, we'll wrap next loop
923+
if ((usbRingBufferTail + bytesToSend) > settings.gnssHandlerBufferSize)
924+
bytesToSend = settings.gnssHandlerBufferSize - usbRingBufferTail;
925+
926+
// Send data over USB serial to the PC
927+
bytesToSend = systemWriteGnssDataToUsbSerial(&ringBuffer[usbRingBufferTail], bytesToSend);
928+
929+
// Account for the data that was sent
930+
if (bytesToSend > 0)
931+
{
932+
// Account for the sent or dropped data
933+
usbRingBufferTail += bytesToSend;
934+
if (usbRingBufferTail >= settings.gnssHandlerBufferSize)
935+
usbRingBufferTail -= settings.gnssHandlerBufferSize;
936+
937+
// Remember the maximum transfer time
938+
deltaMillis = millis() - startMillis;
939+
if (maxMillis[RBC_USB_SERIAL] < deltaMillis)
940+
maxMillis[RBC_USB_SERIAL] = deltaMillis;
941+
}
942+
943+
// Determine the amount of data that remains in the buffer
944+
bytesToSend = dataHead - usbRingBufferTail;
945+
if (bytesToSend < 0)
946+
bytesToSend += settings.gnssHandlerBufferSize;
947+
if (usedSpace < bytesToSend)
948+
{
949+
usedSpace = bytesToSend;
950+
slowConsumer = "USB Serial";
951+
}
952+
}
953+
}
954+
901955
//----------------------------------------------------------------------
902956
// Send data to the network clients
903957
//----------------------------------------------------------------------

Firmware/RTK_Everywhere/support.ino

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,28 @@ int systemRead()
1919
// Output a buffer of the specified length to the serial port
2020
void systemWrite(const uint8_t *buffer, uint16_t length)
2121
{
22-
if (printEndpoint == PRINT_ENDPOINT_ALL)
23-
{
24-
Serial.write(buffer, length);
25-
bluetoothWrite(buffer, length);
26-
}
27-
else if (printEndpoint == PRINT_ENDPOINT_BLUETOOTH)
22+
// Output data to bluetooth if necessary
23+
if ((printEndpoint == PRINT_ENDPOINT_ALL)
24+
|| (printEndpoint == PRINT_ENDPOINT_BLUETOOTH))
2825
bluetoothWrite(buffer, length);
29-
else
26+
27+
// Output data to USB serial if necessary
28+
if ((printEndpoint != PRINT_ENDPOINT_BLUETOOTH)
29+
&& (!forwardGnssDataToUsbSerial))
3030
Serial.write(buffer, length);
3131
}
3232

33+
// Forward GNSS data to the USB serial port
34+
size_t systemWriteGnssDataToUsbSerial(const uint8_t *buffer, uint16_t length)
35+
{
36+
// Determine if status and debug messages are being output to USB serial
37+
if (!forwardGnssDataToUsbSerial)
38+
return length;
39+
40+
// Output GNSS data to USB serial
41+
return Serial.write(buffer, length);
42+
}
43+
3344
// Ensure all serial output has been transmitted, FIFOs are empty
3445
void systemFlush()
3546
{

0 commit comments

Comments
 (0)