Add function to load arbitrary type from flash

This commit is contained in:
BlackMark 2022-06-02 21:37:58 +02:00
parent 6edb2e5a21
commit f9014aea6c

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <cstddef>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#define F(str) (reinterpret_cast<const ::detail::FlashString *>(PSTR(str))) #define F(str) (reinterpret_cast<const ::detail::FlashString *>(PSTR(str)))
@ -13,3 +15,18 @@ namespace detail {
struct FlashString; struct FlashString;
} // namespace detail } // namespace detail
namespace flash {
template <typename T>
static T load(const T &object)
{
auto buffer = T{};
auto rawBuffer = reinterpret_cast<std::byte *>(&buffer);
for (auto i = std::size_t{0}; i < sizeof(T); ++i) {
rawBuffer[i] = static_cast<std::byte>(pgm_read_byte(&reinterpret_cast<const std::byte *>(&object)[i]));
}
return buffer;
}
} // namespace flash