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()
169+ Where possible, write residual serial data to file before closing
166170*/
167171
168172const int FIRMWARE_VERSION_MAJOR = 2 ;
169- const int FIRMWARE_VERSION_MINOR = 9 ;
173+ const int FIRMWARE_VERSION_MINOR = 10 ;
170174
171175// Define the OLA board identifier:
172176// This is an int which is unique to this variant of the OLA and which allows us
@@ -176,7 +180,7 @@ const int FIRMWARE_VERSION_MINOR = 9;
176180// the variant * 0x100 (OLA = 1; GNSS_LOGGER = 2; GEOPHONE_LOGGER = 3)
177181// the major firmware version * 0x10
178182// the minor firmware version
179- #define OLA_IDENTIFIER 0x128 // Stored as 296 decimal in OLA_settings.txt
183+ #define OLA_IDENTIFIER 0x12A // Stored as 298 decimal in OLA_settings.txt
180184
181185// #define noPowerLossProtection // Uncomment this line to disable the sleep-on-power-loss functionality
182186
@@ -291,7 +295,6 @@ Apollo3RTC myRTC; //Create instance of RTC class
291295uint64_t lastSeriaLogSyncTime = 0 ;
292296uint64_t lastAwakeTimeMillis;
293297const int MAX_IDLE_TIME_MSEC = 500 ;
294- bool newSerialData = false ;
295298char incomingBuffer[256 * 2 ]; // This size of this buffer is sensitive. Do not change without analysis using OpenLog_Serial.
296299int incomingBufferSpot = 0 ;
297300int charsReceived = 0 ; // Used for verifying/debugging serial reception
@@ -611,39 +614,19 @@ void loop() {
611614
612615 if (settings.logSerial == true && online.serialLogging == true && settings.useTxRxPinsForTerminal == false )
613616 {
614- size_t timestampCharsLeftToWrite = strlen (serialTimestamp);
615- // SerialPrintf2("timestampCharsLeftToWrite is %d\r\n", timestampCharsLeftToWrite);
616- // SerialFlush();
617+ // v2.10 - this code has been restructured....
617618
618- if (Serial1.available () || (timestampCharsLeftToWrite > 0 ))
619+ // The writing of the timestamp has the highest priority
620+ if (strlen (serialTimestamp) > 0 )
619621 {
620- while (Serial1. available () || (timestampCharsLeftToWrite > 0 ))
622+ while (strlen (serialTimestamp) > 0 ) // Copy all timestamp chars into incomingBuffer
621623 {
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
625-
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- }
624+ incomingBuffer[incomingBufferSpot++] = serialTimestamp[0 ]; // Add a timestamp character to incomingBuffer
630625
631- timestampCharsLeftToWrite -= 1 ; // Now decrement timestampCharsLeftToWrite
632- }
633- else
626+ size_t timestampCharsLeftToWrite = strlen (serialTimestamp);
627+ for (size_t i = 0 ; i < timestampCharsLeftToWrite; i++)
634628 {
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- }
629+ serialTimestamp[i] = serialTimestamp[i+1 ]; // Shuffle the remaining chars along by one, including the NULL terminator
647630 }
648631
649632 if (incomingBufferSpot == sizeof (incomingBuffer))
@@ -652,35 +635,68 @@ void loop() {
652635 serialDataFile.write (incomingBuffer, sizeof (incomingBuffer)); // Record the buffer to the card
653636 digitalWrite (PIN_STAT_LED, LOW);
654637 incomingBufferSpot = 0 ;
638+
639+ // If we are sleeping between readings then we cannot rely on millis() as it is powered down
640+ // Use RTC instead
641+ lastSeriaLogSyncTime = rtcMillis (); // Reset the last sync time to now
642+
643+ checkBattery ();
655644 }
656- checkBattery ();
657645 }
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 ;
663646 }
664- else if (newSerialData == true )
647+
648+ // Now check for incoming serial data. Process bytes until a timestamp token is detected
649+ if (Serial1.available ())
665650 {
666- if ((rtcMillis () - lastSeriaLogSyncTime) > MAX_IDLE_TIME_MSEC) // If we haven't received any characters recently then sync log file
651+ while ((Serial1. available ()) && ( strlen (serialTimestamp) == 0 ))
667652 {
668- if (incomingBufferSpot > 0 )
653+ incomingBuffer[incomingBufferSpot++] = Serial1.read ();
654+ charsReceived++;
655+
656+ // Get the RTC timestamp if we just received the timestamp token
657+ if (settings.timestampSerial && (incomingBuffer[incomingBufferSpot-1 ] == settings.timeStampToken ))
658+ {
659+ getTimeString (&serialTimestamp[2 ]);
660+ serialTimestamp[0 ] = 0x0A ; // Add Line Feed at the start of the timestamp
661+ serialTimestamp[1 ] = ' ^' ; // Add an up-arrow to indicate the timestamp relates to the preceeding data
662+ serialTimestamp[strlen (serialTimestamp) - 1 ] = 0x0A ; // Change the final comma of the timestamp to a Line Feed
663+ }
664+
665+ if (incomingBufferSpot == sizeof (incomingBuffer))
669666 {
670- // Write the remainder of the buffer
671667 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
668+ serialDataFile.write (incomingBuffer, sizeof (incomingBuffer)); // Record the buffer to the card
676669 digitalWrite (PIN_STAT_LED, LOW);
677-
678670 incomingBufferSpot = 0 ;
671+
672+ // If we are sleeping between readings then we cannot rely on millis() as it is powered down
673+ // Use RTC instead
674+ lastSeriaLogSyncTime = rtcMillis (); // Reset the last sync time to now
675+
676+ checkBattery ();
679677 }
678+ }
679+ }
680+
681+ // Periodically sync data to SD
682+ if ((rtcMillis () - lastSeriaLogSyncTime) > MAX_IDLE_TIME_MSEC) // If we haven't logged any characters recently then sync log file
683+ {
684+ if (incomingBufferSpot > 0 )
685+ {
686+ // Write the remainder of the buffer
687+ digitalWrite (PIN_STAT_LED, HIGH); // Toggle stat LED to indicating log recording
688+ serialDataFile.write (incomingBuffer, incomingBufferSpot); // Record the buffer to the card
689+ serialDataFile.sync ();
690+ if (settings.frequentFileAccessTimestamps == true )
691+ updateDataFileAccess (&serialDataFile); // Update the file access time & date
692+ digitalWrite (PIN_STAT_LED, LOW);
693+
694+ incomingBufferSpot = 0 ;
680695
681- newSerialData = false ;
682696 lastSeriaLogSyncTime = rtcMillis (); // Reset the last sync time to now
683697 printDebug (" Total chars received: " + (String)charsReceived + " \r\n " );
698+
699+ checkBattery ();
684700 }
685701 }
686702 }
@@ -797,6 +813,18 @@ void loop() {
797813 }
798814 if (online.serialLogging == true )
799815 {
816+ if (incomingBufferSpot > 0 )
817+ {
818+ // Write the remainder of the buffer
819+ digitalWrite (PIN_STAT_LED, HIGH); // Toggle stat LED to indicating log recording
820+ serialDataFile.write (incomingBuffer, incomingBufferSpot); // Record the buffer to the card
821+ digitalWrite (PIN_STAT_LED, LOW);
822+
823+ incomingBufferSpot = 0 ;
824+
825+ lastSeriaLogSyncTime = rtcMillis (); // Reset the last sync time to now
826+ }
827+
800828 serialDataFile.sync ();
801829 updateDataFileAccess (&serialDataFile); // Update the file access time & date
802830 serialDataFile.close ();
0 commit comments