#ifndef STA_UTILS_LOGGER_HPP #define STA_UTILS_LOGGER_HPP #include #include #include namespace sta { template 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); /** * @brief Write a new data point to the flash chip as long as the segment's limit wasn't reached. * * @param data A pointer to the data to write to the flash chip. * @return true if successful, false if the segment end was reached. */ bool write(T* data); /** * @brief Write a new data point to the flash chip as long as the segment's limit wasn't reached. * * @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); private: /** * @brief Find the first sector in the segment that was not written yet. * */ void findLast(); /** * @brief A method that checks if a page is empty * * @param buffer A buffer of size 256 containing data from a page. * @return true if the page is empty, false otherwise. */ bool searchCriterion(uint8_t * buffer); private: 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 #endif // STA_UTILS_LOGGER_HPP