diff --git a/include/sta/utils/logger.hpp b/include/sta/utils/logger.hpp index 86fe87a..51d995f 100644 --- a/include/sta/utils/logger.hpp +++ b/include/sta/utils/logger.hpp @@ -48,23 +48,23 @@ namespace sta * * @return size_t The number of data points. */ - size_t count(); + uint32_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(); + uint32_t remaining(); /** * @brief Get the total number of data points that fit into the logger. * - * @return size_t The number of data points. + * @return uint32_t The number of data points. */ - size_t capacity(); + uint32_t capacity(); - bool isValid(std::size_t i); + bool isValid(uint32_t i); /** * @brief Get the ith element stored in the flash storage. @@ -72,7 +72,7 @@ namespace sta * @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); + T get(uint32_t i); private: /** * @brief Find the first sector in the segment that was not written yet. diff --git a/include/sta/utils/logger.tpp b/include/sta/utils/logger.tpp index a81f14a..93ad67f 100644 --- a/include/sta/utils/logger.tpp +++ b/include/sta/utils/logger.tpp @@ -4,6 +4,8 @@ #include #include +#define STA_LOGGER_VALID_DATA_BYTE 0x42 + namespace sta { template @@ -73,8 +75,8 @@ namespace sta middle = (left + right) / 2; - address_ = ((middle * (sizeof(T)+1)) / W25QXX_PAGE_SIZE) * W25QXX_PAGE_SIZE; - ptr_ = 0; + ptr_ = (middle * sizeof(T)+1) % W25QXX_PAGE_SIZE; + address_ = (middle * sizeof(T)+1) - ptr_; } template @@ -97,7 +99,7 @@ namespace sta std::memcpy(buffer+1, data, sizeof(T)); // Set the first byte to 0x42 to mark it as a valid data point. - buffer[0] = 0x42; + buffer[0] = STA_LOGGER_VALID_DATA_BYTE; uint8_t * buff_ptr = buffer; uint32_t length = sizeof(T)+1; @@ -163,25 +165,25 @@ namespace sta } template - size_t Logger::count() + uint32_t Logger::count() { return ((address_ + ptr_) - start_ * W25QXX_SECTOR_SIZE) / (sizeof(T)+1); } template - size_t Logger::remaining() + uint32_t Logger::remaining() { return (end_ * W25QXX_SECTOR_SIZE - (address_ + ptr_)) / (sizeof(T)+1); } template - size_t Logger::capacity() + uint32_t Logger::capacity() { - return (end_ - start_) * W25QXX_SECTOR_SIZE / (sizeof(T)+1); + return ((end_ - start_) * W25QXX_SECTOR_SIZE) / (sizeof(T)+1); } template - bool Logger::isValid(std::size_t i) + bool Logger::isValid(uint32_t i) { uint32_t address = start_ * W25QXX_SECTOR_SIZE + i * (sizeof(T)+1); @@ -189,18 +191,18 @@ namespace sta if (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE) { uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE; - return (bool)*ptr; + return *ptr == STA_LOGGER_VALID_DATA_BYTE; } else { uint8_t buffer[sizeof(T)+1]; flash_->readData(address, buffer, sizeof(T)+1); - return (bool)*buffer; + return buffer[0] == STA_LOGGER_VALID_DATA_BYTE; } } template - T Logger::get(std::size_t i) + T Logger::get(uint32_t i) { uint32_t address = start_ * W25QXX_SECTOR_SIZE + i * (sizeof(T)+1);