Updated logger to add a flag byte

This commit is contained in:
dario 2024-06-24 01:25:18 +02:00
parent 82e6dd388c
commit e8f0bcdf38

View File

@ -100,8 +100,12 @@ namespace sta
} }
// Convert the data to a byte array. // Convert the data to a byte array.
uint8_t * bytes = reinterpret_cast<uint8_t*>(data); uint8_t buffer[sizeof(T)+1];
uint32_t length = sizeof(T); std::memcpy(buffer+1, data, sizeof(T));
// Set the first byte to 0x42 to mark it as a valid data point.
buffer[0] = 0x42;
uint32_t length = sizeof(T)+1;
// If the written data exceeds the remaining bytes in the page. // If the written data exceeds the remaining bytes in the page.
while (ptr_ + length >= W25QXX_PAGE_SIZE) while (ptr_ + length >= W25QXX_PAGE_SIZE)
@ -115,7 +119,7 @@ namespace sta
flash_->sectorErase(address_); flash_->sectorErase(address_);
} }
std::memcpy(buffer_ + ptr_, bytes, remaining); std::memcpy(buffer_ + ptr_, buffer, remaining);
flash_->pageProgram(address_, buffer_, W25QXX_PAGE_SIZE); flash_->pageProgram(address_, buffer_, W25QXX_PAGE_SIZE);
bytes += remaining; bytes += remaining;
@ -124,7 +128,7 @@ namespace sta
address_ += W25QXX_PAGE_SIZE; address_ += W25QXX_PAGE_SIZE;
} }
std::memcpy(buffer_ + ptr_, bytes, length); std::memcpy(buffer_ + ptr_, buffer, length);
ptr_ += length; ptr_ += length;
return true; return true;
@ -167,37 +171,37 @@ namespace sta
template <typename T> template <typename T>
size_t Logger<T>::count() size_t Logger<T>::count()
{ {
return ((address_ + ptr_) - start_ * W25QXX_SECTOR_SIZE) / sizeof(T); return ((address_ + ptr_) - start_ * W25QXX_SECTOR_SIZE) / (sizeof(T)+1);
} }
template <typename T> template <typename T>
size_t Logger<T>::remaining() size_t Logger<T>::remaining()
{ {
return (end_ * W25QXX_SECTOR_SIZE - (address_ + ptr_)) / sizeof(T); return (end_ * W25QXX_SECTOR_SIZE - (address_ + ptr_)) / (sizeof(T)+1);
} }
template <typename T> template <typename T>
size_t Logger<T>::capacity() size_t Logger<T>::capacity()
{ {
return (end_ - start_) * W25QXX_SECTOR_SIZE / sizeof(T); return (end_ - start_) * W25QXX_SECTOR_SIZE / (sizeof(T)+1);
} }
template <typename T> template <typename T>
T Logger<T>::get(std::size_t i) T Logger<T>::get(std::size_t i)
{ {
uint32_t address = start_ * W25QXX_SECTOR_SIZE + i * sizeof(T); uint32_t address = start_ * W25QXX_SECTOR_SIZE + i * (sizeof(T)+1);
// 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)
{ {
uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE; uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE + 1;
return *reinterpret_cast<T*>(ptr); return *reinterpret_cast<T*>(ptr);
} }
else else
{ {
uint8_t buffer[sizeof(T)]; uint8_t buffer[sizeof(T)+1];
flash_->readData(address, buffer, sizeof(T)); flash_->readData(address, buffer, sizeof(T)+1);
return *reinterpret_cast<T*>(buffer); return *reinterpret_cast<T*>(buffer+1);
} }
} }
} // namespace sta } // namespace sta