96 lines
2.7 KiB
C++

#ifndef STA_UTILS_LOGGER_HPP
#define STA_UTILS_LOGGER_HPP
#include <sta/drivers/w25qxx.hpp>
#include <sta/debug/assert.hpp>
#include <cstring>
namespace sta
{
template <typename T>
class Logger
{
public:
/**
* @brief Constructs a logger object which manages reading and writing data to a segment of a flash chip.
*
* @param flash The flash chip to use for logging.
* @param startSector The index of the start sector (1 LSB = 4096 bytes).
* @param endSector The index of the end sector (1 LSB = 4096 bytes).
*/
Logger(W25Qxx * flash, uint32_t startSector, uint32_t endSector);
void findLast();
bool write(T* data);
/**
* @brief Write a new data point to the flash chip.
* @note If the total capacity of this logger was exceeded, it restarts at the first sector, overwriting its data.
*
* @param data The data to write to the flash chip.
* @return true if successful, false if the segment end was reached.
*/
bool write(T data);
/**
* @brief Clear the flash memory used by the logger.
*
*/
void clear();
/**
* @brief Get the number of data points currently written to the memory segment.
*
* @return size_t The number of data points.
*/
size_t count();
/**
* @brief Get the number of data points that can be written to the logger before an overflow occurs.
*
* @return size_t The number of data points.
*/
size_t remaining();
/**
* @brief Get the total number of data points that fit into the logger.
*
* @return size_t The number of data points.
*/
size_t capacity();
/**
* @brief Get the ith element stored in the flash storage.
*
* @param i The index of the element to read.
* @return T The ith element stored in the flash storage.
*/
T get(std::size_t i);
/**
* @brief Get the ith element stored in the flash storage.
*
* @param i The index of the element to read.
* @return T The ith element stored in the flash storage.
*/
T operator[](std::size_t i);
private:
bool searchCriterion(uint8_t * buffer);
W25Qxx * flash_;
uint32_t start_;
uint32_t end_;
uint32_t address_;
uint8_t buffer_[W25QXX_PAGE_SIZE];
bool flushed_;
uint32_t ptr_;
};
} // namespace sta
#include <sta/utils/logger.tpp>
#endif // STA_UTILS_LOGGER_HPP