@@ -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
6364const 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
6768const 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
102103static RING_BUFFER_OFFSET btRingBufferTail; // BT Tail advances as it is sent over BT
103104static 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
106108static 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 // ----------------------------------------------------------------------
0 commit comments