diff --git a/firmware/lib/session/SessionManager.h b/firmware/lib/session/SessionManager.h index e6f95b3..790658e 100644 --- a/firmware/lib/session/SessionManager.h +++ b/firmware/lib/session/SessionManager.h @@ -103,7 +103,7 @@ void SessionManager::iteration() while(!measurementDone) measurementDone = scale_.measure(measurement); bool addPointSuccessful = session_.addPoint(measurement); - Serial.printf("Measured: %d\n", measurement); + //Serial.printf("Measured: %d\n", measurement); if (!addPointSuccessful) { Serial.println("Maximum time of session reached - stopping"); @@ -121,7 +121,7 @@ void SessionManager::iteration() else { const long skipped = (cycleDuration / CONFIG_MEASURE_DELAY); - Serial.printf("Warning: measurements skipped: %ld, cycleDuration %ld", skipped, cycleDuration); + Serial.printf("Measurements skipped: %ld, cycleDuration %ld\n", skipped, cycleDuration); for (int i = 0; i < skipped; ++i) session_.addPoint(measurement); diff --git a/firmware/lib/session/SimpleMeasurementSession.h b/firmware/lib/session/SimpleMeasurementSession.h index 4dcf3fb..950f61d 100644 --- a/firmware/lib/session/SimpleMeasurementSession.h +++ b/firmware/lib/session/SimpleMeasurementSession.h @@ -74,6 +74,10 @@ public: private: void saveToFileSystem() { + static const uint32_t arrayHeaderOffset = ChunkT::arrayHeaderOffset(); + Serial.printf(" -------- Array header offset ---- %u\n", arrayHeaderOffset); + const uint32_t numMeasurements = chunk->numMeasurements(); + // todo: check this! free doesn't mean that the file writing actually works ok // use error codes of write instead? anyway: test it! Serial.printf("%ld saveToFileSystem start()\n", millis()); @@ -83,18 +87,19 @@ private: String filename = String(CONFIG_DATA_PATH) + "/" + String(chunk->getStartTime()); if (portablefs::exists(filename.c_str())) { - auto file = portablefs::open(filename.c_str(), "a"); + auto file = portablefs::open(filename.c_str(), "r+"); file.seek(0, SeekEnd); size_t existingMeasurements = (file.size() - ChunkT::valueOffset()) / sizeof(Measurement_T); - Serial.printf("Incremental save, existing %d\n", existingMeasurements); - size_t measurementsToWrite = chunk->numMeasurements() - existingMeasurements; + size_t measurementsToWrite = numMeasurements - existingMeasurements; Measurement_T *startPtr = chunk->getDataPointer() + existingMeasurements; file.write((uint8_t *)(startPtr), measurementsToWrite * sizeof(Measurement_T)); Serial.printf("%ld Incr Save: before header patch\n", millis()); - file.seek(ChunkT::arrayHeaderOffset(), SeekSet); + file.seek(arrayHeaderOffset); + StreamingMsgPackEncoder encoder(&file); - encoder.template sendArrayHeader(numMeasurements()); + encoder.template sendArrayHeader(numMeasurements); + Serial.printf("%ld total measurements up to now %u\n", millis(), numMeasurements); } else { @@ -103,7 +108,7 @@ private: StreamingMsgPackEncoder encoder(&file); chunk->serialize(encoder); } - Serial.printf("%ld saveToFileSystem done\n", millis()); + Serial.printf("%ld saveToFileSystem done-------------\n", millis()); } void deleteUntilBytesFree(size_t requiredSpace)