Avionics
Core avionics package for CURE flight computers
Loading...
Searching...
No Matches
DataSaverSPI Class Reference

SPI flash implementation of IDataSaver with timestamp compression. More...

#include <DataSaverSPI.h>

Inheritance diagram for DataSaverSPI:
Inheritance graph
Collaboration diagram for DataSaverSPI:
Collaboration graph

Public Member Functions

 DataSaverSPI (uint16_t timestampInterval_ms, Adafruit_SPIFlash *flash)
 Construct a new DataSaverSPI object.
int saveDataPoint (const DataPoint &dataPoint, uint8_t name) override
 Saves a DataPoint to SPI flash.
int saveTimestamp (uint32_t timestamp_ms)
 Persist a bare timestamp entry to flash.
virtual bool begin () override
 Initialize the flash chip and metadata.
bool isPostLaunchMode ()
 Returns whether the metadata from the flash chip indicates that it contains post-launch data that hasn't been dumped yet. Call clearPostLaunchMode() to clear this flag. once you are confident that you have the data you want dumped to a more permanent storage or another machine.
void clearPostLaunchMode ()
 Clears the post-launch mode flag on the flash chip **WARNING: This will allow the data to be overwritten Only call this after you have dumped the data to a more permanent storage or another machine.
void launchDetected (uint32_t launchTimestamp_ms)
 Call this when launch is detected to set the post-launch flag and prevent any data from being overwritten until the flag is cleared. All data written from this point on is "post-launch" data which is sacred and should not be overwritten until it has been dumped.
void dumpData (Stream &serial, bool ignoreEmptyPages)
 Dumps all data from flash to Serial.
void clearInternalState ()
 Resets all internal state values (buffer, lastDataPoint, nextWriteAddress, lastTimestamp_ms) Does not erase the flash chip.
void eraseAllData ()
 Clears/erases the entire flash chip to start fresh.
uint32_t getLastTimestamp () const
 Returns the last timestamp that was actually written to flash.
DataPoint getLastDataPoint () const
 Returns the last DataPoint that was written (not necessarily including timestamp, just the data chunk).
uint32_t getLaunchWriteAddress () const
uint32_t getNextWriteAddress () const
void resetTimestamp ()
 Resets the timestamp to 0. Can be used to start a new flight during runtime. Useful for testing.
bool quickGetPostLaunchMode ()
 Returns whether the flash chip is in post-launch mode without updating the postLaunchMode flag or reading from flash.
size_t getBufferIndex () const
 Returns the current buffer index Useful for testing.
uint32_t getBufferFlushes () const
 Returns the current buffer flush count Useful for testing.
bool getIsChipFullDueToPostLaunchProtection () const
bool getRebootedInPostLaunchMode () const
Public Member Functions inherited from IDataSaver
virtual int saveDataPoint (float data, uint32_t timestamp_ms, uint8_t name) final
 Convenience overload to construct and save a data point.

Static Public Attributes

static constexpr size_t BUFFER_SIZE = 256

Detailed Description

SPI flash implementation of IDataSaver with timestamp compression.

Note
When to use: onboard non-volatile logging where SD cards are impractical and data may need to survive power loss or long recovery.

Definition at line 39 of file DataSaverSPI.h.

Constructor & Destructor Documentation

◆ DataSaverSPI()

DataSaverSPI::DataSaverSPI ( uint16_t timestampInterval_ms,
Adafruit_SPIFlash * flash )

Construct a new DataSaverSPI object.

Parameters
timestampInterval_msInterval in ms at which timestamps are stored
flashPointer to an initialized Adafruit_SPIFlash object
Note
timestampInterval_ms must be < 65535 (about 1 minute)

Definition at line 6 of file DataSaverSPI.cpp.

Member Function Documentation

◆ begin()

bool DataSaverSPI::begin ( )
overridevirtual

Initialize the flash chip and metadata.

Note
When to use: call during setup before any saveDataPoint usage.

Reimplemented from IDataSaver.

Definition at line 106 of file DataSaverSPI.cpp.

◆ clearInternalState()

void DataSaverSPI::clearInternalState ( )

Resets all internal state values (buffer, lastDataPoint, nextWriteAddress, lastTimestamp_ms) Does not erase the flash chip.

Reset in-memory pointers without erasing flash contents.

Note
When to use: restart logging logic while preserving prior data on the chip.

Definition at line 227 of file DataSaverSPI.cpp.

◆ clearPostLaunchMode()

void DataSaverSPI::clearPostLaunchMode ( )

Clears the post-launch mode flag on the flash chip **WARNING: This will allow the data to be overwritten Only call this after you have dumped the data to a more permanent storage or another machine.

Definition at line 131 of file DataSaverSPI.cpp.

◆ dumpData()

void DataSaverSPI::dumpData ( Stream & serial,
bool ignoreEmptyPages )

Dumps all data from flash to Serial.

Stream all recorded data to a serial connection.

Parameters
serialOutput stream.
ignoreEmptyPagesSkip pages that appear unwritten.
Note
When to use: post-flight data retrieval before erasing or redeploying the flash.

Definition at line 139 of file DataSaverSPI.cpp.

◆ eraseAllData()

void DataSaverSPI::eraseAllData ( )

Clears/erases the entire flash chip to start fresh.

Erase the entire flash chip to start fresh.

Note
When to use: before a new campaign when previous flights are fully offloaded.

Definition at line 239 of file DataSaverSPI.cpp.

◆ getBufferFlushes()

uint32_t DataSaverSPI::getBufferFlushes ( ) const
inline

Returns the current buffer flush count Useful for testing.

Definition at line 249 of file DataSaverSPI.h.

◆ getBufferIndex()

size_t DataSaverSPI::getBufferIndex ( ) const
inline

Returns the current buffer index Useful for testing.

Definition at line 241 of file DataSaverSPI.h.

◆ getIsChipFullDueToPostLaunchProtection()

bool DataSaverSPI::getIsChipFullDueToPostLaunchProtection ( ) const
inline

Definition at line 253 of file DataSaverSPI.h.

◆ getLastDataPoint()

DataPoint DataSaverSPI::getLastDataPoint ( ) const
inline

Returns the last DataPoint that was written (not necessarily including timestamp, just the data chunk).

Definition at line 167 of file DataSaverSPI.h.

◆ getLastTimestamp()

uint32_t DataSaverSPI::getLastTimestamp ( ) const
inline

Returns the last timestamp that was actually written to flash.

Definition at line 159 of file DataSaverSPI.h.

◆ getLaunchWriteAddress()

uint32_t DataSaverSPI::getLaunchWriteAddress ( ) const
inline

Definition at line 171 of file DataSaverSPI.h.

◆ getNextWriteAddress()

uint32_t DataSaverSPI::getNextWriteAddress ( ) const
inline

Definition at line 175 of file DataSaverSPI.h.

◆ getRebootedInPostLaunchMode()

bool DataSaverSPI::getRebootedInPostLaunchMode ( ) const
inline

Definition at line 257 of file DataSaverSPI.h.

◆ isPostLaunchMode()

bool DataSaverSPI::isPostLaunchMode ( )

Returns whether the metadata from the flash chip indicates that it contains post-launch data that hasn't been dumped yet. Call clearPostLaunchMode() to clear this flag. once you are confident that you have the data you want dumped to a more permanent storage or another machine.

Will also update the postLaunchMode flag.

Returns
true if post-launch mode is active
false if post-launch mode is not active

Definition at line 124 of file DataSaverSPI.cpp.

◆ launchDetected()

void DataSaverSPI::launchDetected ( uint32_t launchTimestamp_ms)
virtual

Call this when launch is detected to set the post-launch flag and prevent any data from being overwritten until the flag is cleared. All data written from this point on is "post-launch" data which is sacred and should not be overwritten until it has been dumped.

Sets postLaunchMode flag to true on the flash chip. Records the address at which the launch was detected and ensures that it's not overwritten

Data is saved in a circular fashion but once the address where the launch detected is reached this will stop saving data entirely. Also keeps 1 minute of data from before the launch was detected (b/c launch can be detected late and we have extra room)

The rocket may not be recovered for several hours, this prevents the cool launch data from being overwitten with boring laying-on-the-ground data.

Reimplemented from IDataSaver.

Definition at line 250 of file DataSaverSPI.cpp.

◆ quickGetPostLaunchMode()

bool DataSaverSPI::quickGetPostLaunchMode ( )
inline

Returns whether the flash chip is in post-launch mode without updating the postLaunchMode flag or reading from flash.

Definition at line 191 of file DataSaverSPI.h.

◆ resetTimestamp()

void DataSaverSPI::resetTimestamp ( )
inline

Resets the timestamp to 0. Can be used to start a new flight during runtime. Useful for testing.

Definition at line 183 of file DataSaverSPI.h.

◆ saveDataPoint()

int DataSaverSPI::saveDataPoint ( const DataPoint & dataPoint,
uint8_t name )
overridevirtual

Saves a DataPoint to SPI flash.

If the new data point’s timestamp differs from the last written timestamp by more than timestampInterval_ms, the new timestamp is also written to flash. Otherwise, the timestamp is omitted (to save space).

Parameters
dataPointThe data point to save
nameAn 8-bit “identifier” for the data point (could be a sensor ID)
Returns
int 0 on success; non-zero on error

Implements IDataSaver.

Definition at line 20 of file DataSaverSPI.cpp.

◆ saveTimestamp()

int DataSaverSPI::saveTimestamp ( uint32_t timestamp_ms)

Persist a bare timestamp entry to flash.

Parameters
timestamp_msTimestamp in milliseconds to record.
Note
When to use: emitted internally when gaps exceed timestampInterval_ms, or explicitly in tests.

Definition at line 42 of file DataSaverSPI.cpp.

Member Data Documentation

◆ BUFFER_SIZE

size_t DataSaverSPI::BUFFER_SIZE = 256
staticconstexpr

Definition at line 42 of file DataSaverSPI.h.


The documentation for this class was generated from the following files: