cleanup in logger code

This commit is contained in:
dario 2024-06-04 22:05:23 +02:00
parent 33e034a429
commit d378bda62c
2 changed files with 20 additions and 8 deletions

View File

@ -58,12 +58,20 @@ namespace sta
size_t capacity(); size_t capacity();
/** /**
* @brief Allows reading a single data point from the segment. * @brief Get the ith element stored in the flash storage.
* *
* @param idx The index of the segement. * @param i The index of the element to read.
* @return T The read data value. * @return T The ith element stored in the flash storage.
*/ */
T operator[](std::size_t idx); 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: private:
void findLast(); void findLast();

View File

@ -115,15 +115,13 @@ namespace sta
} }
template <typename T> template <typename T>
T Logger<T>::operator[](std::size_t idx) T Logger<T>::get(std::size_t i)
{ {
uint32_t address = start_ * W25QXX_SECTOR_SIZE + idx * sizeof(T); uint32_t address = start_ * W25QXX_SECTOR_SIZE + i * sizeof(T);
STA_DEBUG_PRINTF("Reading from address %d\n", address);
// If the requested element is in the cache, read it from there. // If the requested element is in the cache, read it from there.
if (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE) if (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE)
{ {
STA_DEBUG_PRINTLN("Reading data from buffer!");
uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE; uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE;
return *reinterpret_cast<T*>(ptr); return *reinterpret_cast<T*>(ptr);
} }
@ -134,6 +132,12 @@ namespace sta
return *reinterpret_cast<T*>(buffer); return *reinterpret_cast<T*>(buffer);
} }
} }
template <typename T>
T Logger<T>::operator[](std::size_t i)
{
return this->get(i);
}
} // namespace sta } // namespace sta
#endif // STA_UTILS_LOGGER_TPP #endif // STA_UTILS_LOGGER_TPP