Skip to content

Commit 3849a25

Browse files
committed
v2.10 - restructure serial logging
1 parent 7f2b612 commit 3849a25

File tree

1 file changed

+62
-47
lines changed

1 file changed

+62
-47
lines changed

Firmware/OpenLog_Artemis/OpenLog_Artemis.ino

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,13 @@
163163
164164
v2.9
165165
Adds support for TMP102 low(er) cost temperature sensor
166+
167+
v2.10
168+
Restructure the serial logging code in loop()
166169
*/
167170

168171
const int FIRMWARE_VERSION_MAJOR = 2;
169-
const int FIRMWARE_VERSION_MINOR = 9;
172+
const int FIRMWARE_VERSION_MINOR = 10;
170173

171174
//Define the OLA board identifier:
172175
// This is an int which is unique to this variant of the OLA and which allows us
@@ -176,7 +179,7 @@ const int FIRMWARE_VERSION_MINOR = 9;
176179
// the variant * 0x100 (OLA = 1; GNSS_LOGGER = 2; GEOPHONE_LOGGER = 3)
177180
// the major firmware version * 0x10
178181
// the minor firmware version
179-
#define OLA_IDENTIFIER 0x128 // Stored as 296 decimal in OLA_settings.txt
182+
#define OLA_IDENTIFIER 0x12A // Stored as 298 decimal in OLA_settings.txt
180183

181184
//#define noPowerLossProtection // Uncomment this line to disable the sleep-on-power-loss functionality
182185

@@ -291,7 +294,6 @@ Apollo3RTC myRTC; //Create instance of RTC class
291294
uint64_t lastSeriaLogSyncTime = 0;
292295
uint64_t lastAwakeTimeMillis;
293296
const int MAX_IDLE_TIME_MSEC = 500;
294-
bool newSerialData = false;
295297
char incomingBuffer[256 * 2]; //This size of this buffer is sensitive. Do not change without analysis using OpenLog_Serial.
296298
int incomingBufferSpot = 0;
297299
int charsReceived = 0; //Used for verifying/debugging serial reception
@@ -611,39 +613,19 @@ void loop() {
611613

612614
if (settings.logSerial == true && online.serialLogging == true && settings.useTxRxPinsForTerminal == false)
613615
{
614-
size_t timestampCharsLeftToWrite = strlen(serialTimestamp);
615-
//SerialPrintf2("timestampCharsLeftToWrite is %d\r\n", timestampCharsLeftToWrite);
616-
//SerialFlush();
616+
// v2.10 - this code has been restructured....
617617

618-
if (Serial1.available() || (timestampCharsLeftToWrite > 0))
618+
// The writing of the timestamp has the highest priority
619+
if (strlen(serialTimestamp) > 0)
619620
{
620-
while (Serial1.available() || (timestampCharsLeftToWrite > 0))
621+
while (strlen(serialTimestamp) > 0) // Copy all timestamp chars into incomingBuffer
621622
{
622-
if (timestampCharsLeftToWrite > 0) // Based on code written by @DennisMelamed in PR #70
623-
{
624-
incomingBuffer[incomingBufferSpot++] = serialTimestamp[0]; // Add a timestamp character to incomingBuffer
623+
incomingBuffer[incomingBufferSpot++] = serialTimestamp[0]; // Add a timestamp character to incomingBuffer
625624

626-
for (size_t i = 0; i < timestampCharsLeftToWrite; i++)
627-
{
628-
serialTimestamp[i] = serialTimestamp[i+1]; // Shuffle the remaining chars along by one, including the NULL terminator
629-
}
630-
631-
timestampCharsLeftToWrite -= 1; // Now decrement timestampCharsLeftToWrite
632-
}
633-
else
625+
size_t timestampCharsLeftToWrite = strlen(serialTimestamp);
626+
for (size_t i = 0; i < timestampCharsLeftToWrite; i++)
634627
{
635-
incomingBuffer[incomingBufferSpot++] = Serial1.read();
636-
charsReceived++;
637-
638-
//Get the RTC timestamp if we just received the timestamp token
639-
if (settings.timestampSerial && (incomingBuffer[incomingBufferSpot-1] == settings.timeStampToken))
640-
{
641-
getTimeString(&serialTimestamp[2]);
642-
serialTimestamp[0] = 0x0A; // Add Line Feed at the start of the timestamp
643-
serialTimestamp[1] = '^'; // Add an up-arrow to indicate the timestamp relates to the preceeding data
644-
serialTimestamp[strlen(serialTimestamp) - 1] = 0x0A; // Change the final comma of the timestamp to a Line Feed
645-
timestampCharsLeftToWrite = strlen(serialTimestamp); // Update timestampCharsLeftToWrite now, so timestamp is printed immediately (#192)
646-
}
628+
serialTimestamp[i] = serialTimestamp[i+1]; // Shuffle the remaining chars along by one, including the NULL terminator
647629
}
648630

649631
if (incomingBufferSpot == sizeof(incomingBuffer))
@@ -652,35 +634,68 @@ void loop() {
652634
serialDataFile.write(incomingBuffer, sizeof(incomingBuffer)); //Record the buffer to the card
653635
digitalWrite(PIN_STAT_LED, LOW);
654636
incomingBufferSpot = 0;
637+
638+
//If we are sleeping between readings then we cannot rely on millis() as it is powered down
639+
//Use RTC instead
640+
lastSeriaLogSyncTime = rtcMillis(); //Reset the last sync time to now
641+
642+
checkBattery();
655643
}
656-
checkBattery();
657644
}
658-
659-
//If we are sleeping between readings then we cannot rely on millis() as it is powered down
660-
//Use RTC instead
661-
lastSeriaLogSyncTime = rtcMillis(); //Reset the last sync time to now
662-
newSerialData = true;
663645
}
664-
else if (newSerialData == true)
646+
647+
// Now check for incoming serial data. Process bytes until a timestamp token is detected
648+
if (Serial1.available())
665649
{
666-
if ((rtcMillis() - lastSeriaLogSyncTime) > MAX_IDLE_TIME_MSEC) //If we haven't received any characters recently then sync log file
650+
while ((Serial1.available()) && (strlen(serialTimestamp) == 0))
667651
{
668-
if (incomingBufferSpot > 0)
652+
incomingBuffer[incomingBufferSpot++] = Serial1.read();
653+
charsReceived++;
654+
655+
//Get the RTC timestamp if we just received the timestamp token
656+
if (settings.timestampSerial && (incomingBuffer[incomingBufferSpot-1] == settings.timeStampToken))
657+
{
658+
getTimeString(&serialTimestamp[2]);
659+
serialTimestamp[0] = 0x0A; // Add Line Feed at the start of the timestamp
660+
serialTimestamp[1] = '^'; // Add an up-arrow to indicate the timestamp relates to the preceeding data
661+
serialTimestamp[strlen(serialTimestamp) - 1] = 0x0A; // Change the final comma of the timestamp to a Line Feed
662+
}
663+
664+
if (incomingBufferSpot == sizeof(incomingBuffer))
669665
{
670-
//Write the remainder of the buffer
671666
digitalWrite(PIN_STAT_LED, HIGH); //Toggle stat LED to indicating log recording
672-
serialDataFile.write(incomingBuffer, incomingBufferSpot); //Record the buffer to the card
673-
serialDataFile.sync();
674-
if (settings.frequentFileAccessTimestamps == true)
675-
updateDataFileAccess(&serialDataFile); // Update the file access time & date
667+
serialDataFile.write(incomingBuffer, sizeof(incomingBuffer)); //Record the buffer to the card
676668
digitalWrite(PIN_STAT_LED, LOW);
677-
678669
incomingBufferSpot = 0;
670+
671+
//If we are sleeping between readings then we cannot rely on millis() as it is powered down
672+
//Use RTC instead
673+
lastSeriaLogSyncTime = rtcMillis(); //Reset the last sync time to now
674+
675+
checkBattery();
679676
}
677+
}
678+
}
679+
680+
// Periodically sync data to SD
681+
if ((rtcMillis() - lastSeriaLogSyncTime) > MAX_IDLE_TIME_MSEC) //If we haven't logged any characters recently then sync log file
682+
{
683+
if (incomingBufferSpot > 0)
684+
{
685+
//Write the remainder of the buffer
686+
digitalWrite(PIN_STAT_LED, HIGH); //Toggle stat LED to indicating log recording
687+
serialDataFile.write(incomingBuffer, incomingBufferSpot); //Record the buffer to the card
688+
serialDataFile.sync();
689+
if (settings.frequentFileAccessTimestamps == true)
690+
updateDataFileAccess(&serialDataFile); // Update the file access time & date
691+
digitalWrite(PIN_STAT_LED, LOW);
692+
693+
incomingBufferSpot = 0;
680694

681-
newSerialData = false;
682695
lastSeriaLogSyncTime = rtcMillis(); //Reset the last sync time to now
683696
printDebug("Total chars received: " + (String)charsReceived + "\r\n");
697+
698+
checkBattery();
684699
}
685700
}
686701
}

0 commit comments

Comments
 (0)