From f9014aea6cbbb1ea76f30148a64e6c3fc3152dcd Mon Sep 17 00:00:00 2001 From: BlackMark Date: Thu, 2 Jun 2022 21:37:58 +0200 Subject: [PATCH] Add function to load arbitrary type from flash --- flash.hpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/flash.hpp b/flash.hpp index 9721229..3e49f77 100644 --- a/flash.hpp +++ b/flash.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #define F(str) (reinterpret_cast(PSTR(str))) @@ -13,3 +15,18 @@ namespace detail { struct FlashString; } // namespace detail + +namespace flash { + +template +static T load(const T &object) +{ + auto buffer = T{}; + auto rawBuffer = reinterpret_cast(&buffer); + for (auto i = std::size_t{0}; i < sizeof(T); ++i) { + rawBuffer[i] = static_cast(pgm_read_byte(&reinterpret_cast(&object)[i])); + } + return buffer; +} + +} // namespace flash