Skip to content

Commit d1c1e8d

Browse files
authored
Merge pull request #630 from LeeLeahy2/rb-tails
Rename btTail and sdTail and make local to module
2 parents f679296 + f951fc1 commit d1c1e8d

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

Firmware/RTK_Surveyor/Tasks.ino

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ uint8_t bluetoothOutgoingToZed[100];
7171
uint16_t bluetoothOutgoingToZedHead;
7272
unsigned long lastZedI2CSend; // Timestamp of the last time we sent RTCM ZED over I2C
7373

74+
// Ring buffer tails
75+
static RING_BUFFER_OFFSET btRingBufferTail; // BT Tail advances as it is sent over BT
76+
static RING_BUFFER_OFFSET sdRingBufferTail; // SD Tail advances as it is recorded to SD
77+
7478
//----------------------------------------
7579
// Task routines
7680
//----------------------------------------
@@ -235,27 +239,27 @@ void feedWdt()
235239
// 250ms worst case, we should record incoming all data. Bluetooth congestion
236240
// or conflicts with the SD card semaphore should clear within this time.
237241
//
238-
// Ring buffer empty when (dataHead == btTail) and (dataHead == sdTail)
242+
// Ring buffer empty when all the tails == dataHead
239243
//
240244
// +---------+
241245
// | |
242246
// | |
243247
// | |
244248
// | |
245-
// +---------+ <-- dataHead, btTail, sdTail
249+
// +---------+ <-- dataHead, btRingBufferTail, sdRingBufferTail, etc.
246250
//
247-
// Ring buffer contains data when (dataHead != btTail) or (dataHead != sdTail)
251+
// Ring buffer contains data when any tail != dataHead
248252
//
249253
// +---------+
250254
// | |
251255
// | |
252256
// | yyyyyyy | <-- dataHead
253-
// | xxxxxxx | <-- btTail (1 byte in buffer)
254-
// +---------+ <-- sdTail (2 bytes in buffer)
257+
// | xxxxxxx | <-- btRingBufferTail (1 byte in buffer)
258+
// +---------+ <-- sdRingBufferTail (2 bytes in buffer)
255259
//
256260
// +---------+
257-
// | yyyyyyy | <-- btTail (1 byte in buffer)
258-
// | xxxxxxx | <-- sdTail (2 bytes in buffer)
261+
// | yyyyyyy | <-- btRingBufferTail (1 byte in buffer)
262+
// | xxxxxxx | <-- sdRingBufferTail (2 bytes in buffer)
259263
// | |
260264
// | |
261265
// +---------+ <-- dataHead
@@ -438,14 +442,11 @@ void handleGnssDataTask(void *e)
438442
uint32_t startMillis;
439443
int32_t usedSpace;
440444

441-
static RING_BUFFER_OFFSET btTail; // BT Tail advances as it is sent over BT
442-
static RING_BUFFER_OFFSET sdTail; // SD Tail advances as it is recorded to SD
443-
444445
// Initialize the tails
445-
btTail = 0;
446+
btRingBufferTail = 0;
446447
pvtClientZeroTail();
447448
pvtServerZeroTail();
448-
sdTail = 0;
449+
sdRingBufferTail = 0;
449450

450451
while (true)
451452
{
@@ -469,24 +470,24 @@ void handleGnssDataTask(void *e)
469470
(systemState != STATE_BASE_TEMP_SURVEY_STARTED);
470471
if (!connected)
471472
// Discard the data
472-
btTail = dataHead;
473+
btRingBufferTail = dataHead;
473474
else
474475
{
475476
// Determine the amount of Bluetooth data in the buffer
476-
bytesToSend = dataHead - btTail;
477+
bytesToSend = dataHead - btRingBufferTail;
477478
if (bytesToSend < 0)
478479
bytesToSend += settings.gnssHandlerBufferSize;
479480
if (bytesToSend > 0)
480481
{
481482
// Reduce bytes to send if we have more to send then the end of
482483
// the buffer, we'll wrap next loop
483-
if ((btTail + bytesToSend) > settings.gnssHandlerBufferSize)
484-
bytesToSend = settings.gnssHandlerBufferSize - btTail;
484+
if ((btRingBufferTail + bytesToSend) > settings.gnssHandlerBufferSize)
485+
bytesToSend = settings.gnssHandlerBufferSize - btRingBufferTail;
485486

486487
// If we are in the config menu, supress data flowing from ZED to cell phone
487488
if (btPrintEcho == false)
488489
// Push new data to BT SPP
489-
bytesToSend = bluetoothWrite(&ringBuffer[btTail], bytesToSend);
490+
bytesToSend = bluetoothWrite(&ringBuffer[btRingBufferTail], bytesToSend);
490491

491492
// Account for the data that was sent
492493
if (bytesToSend > 0)
@@ -496,9 +497,9 @@ void handleGnssDataTask(void *e)
496497
bluetoothOutgoingRTCM = true;
497498

498499
// Account for the sent or dropped data
499-
btTail += bytesToSend;
500-
if (btTail >= settings.gnssHandlerBufferSize)
501-
btTail -= settings.gnssHandlerBufferSize;
500+
btRingBufferTail += bytesToSend;
501+
if (btRingBufferTail >= settings.gnssHandlerBufferSize)
502+
btRingBufferTail -= settings.gnssHandlerBufferSize;
502503

503504
// Remember the maximum transfer time
504505
deltaMillis = millis() - startMillis;
@@ -516,7 +517,7 @@ void handleGnssDataTask(void *e)
516517
log_w("BT failed to send");
517518

518519
// Determine the amount of data that remains in the buffer
519-
bytesToSend = dataHead - btTail;
520+
bytesToSend = dataHead - btRingBufferTail;
520521
if (bytesToSend < 0)
521522
bytesToSend += settings.gnssHandlerBufferSize;
522523
if (usedSpace < bytesToSend)
@@ -571,11 +572,11 @@ void handleGnssDataTask(void *e)
571572
// If user wants to log, record to SD
572573
if (!connected)
573574
// Discard the data
574-
sdTail = dataHead;
575+
sdRingBufferTail = dataHead;
575576
else
576577
{
577578
// Determine the amount of microSD card logging data in the buffer
578-
bytesToSend = dataHead - sdTail;
579+
bytesToSend = dataHead - sdRingBufferTail;
579580
if (bytesToSend < 0)
580581
bytesToSend += settings.gnssHandlerBufferSize;
581582
if (bytesToSend > 0)
@@ -587,8 +588,8 @@ void handleGnssDataTask(void *e)
587588
markSemaphore(FUNCTION_WRITESD);
588589

589590
// Reduce bytes to record if we have more then the end of the buffer
590-
if ((sdTail + bytesToSend) > settings.gnssHandlerBufferSize)
591-
bytesToSend = settings.gnssHandlerBufferSize - sdTail;
591+
if ((sdRingBufferTail + bytesToSend) > settings.gnssHandlerBufferSize)
592+
bytesToSend = settings.gnssHandlerBufferSize - sdRingBufferTail;
592593

593594
if (settings.enablePrintSDBuffers && (!inMainMenu))
594595
{
@@ -617,7 +618,7 @@ void handleGnssDataTask(void *e)
617618
long startTime = millis();
618619
startMillis = millis();
619620

620-
bytesToSend = ubxFile->write(&ringBuffer[sdTail], bytesToSend);
621+
bytesToSend = ubxFile->write(&ringBuffer[sdRingBufferTail], bytesToSend);
621622
if (PERIODIC_DISPLAY(PD_SD_LOG_WRITE) && (bytesToSend > 0))
622623
{
623624
PERIODIC_CLEAR(PD_SD_LOG_WRITE);
@@ -673,9 +674,9 @@ void handleGnssDataTask(void *e)
673674
// Account for the sent data or dropped
674675
if (bytesToSend > 0)
675676
{
676-
sdTail += bytesToSend;
677-
if (sdTail >= settings.gnssHandlerBufferSize)
678-
sdTail -= settings.gnssHandlerBufferSize;
677+
sdRingBufferTail += bytesToSend;
678+
if (sdRingBufferTail >= settings.gnssHandlerBufferSize)
679+
sdRingBufferTail -= settings.gnssHandlerBufferSize;
679680
}
680681
} // End sdCardSemaphore
681682
else
@@ -690,7 +691,7 @@ void handleGnssDataTask(void *e)
690691
}
691692

692693
// Update space available for use in UART task
693-
bytesToSend = dataHead - sdTail;
694+
bytesToSend = dataHead - sdRingBufferTail;
694695
if (bytesToSend < 0)
695696
bytesToSend += settings.gnssHandlerBufferSize;
696697
if (usedSpace < bytesToSend)

0 commit comments

Comments
 (0)