2019-07-27 13:34:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-06-02 21:37:58 +02:00
|
|
|
#include <cstddef>
|
|
|
|
|
2019-07-27 13:34:07 +02:00
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
|
2019-07-28 17:30:52 +02:00
|
|
|
#define F(str) (reinterpret_cast<const ::detail::FlashString *>(PSTR(str)))
|
2020-04-01 03:52:49 +02:00
|
|
|
#define GF(name, str) \
|
|
|
|
const char __##name[] PROGMEM = str; \
|
|
|
|
const auto *name = reinterpret_cast<const ::detail::FlashString *>(__##name)
|
2019-07-27 13:34:07 +02:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
// Only used for C++ type safety, because otherwise a PSTR would be indistinguishable from a normal c-string
|
|
|
|
struct FlashString;
|
|
|
|
|
|
|
|
} // namespace detail
|
2022-06-02 21:37:58 +02:00
|
|
|
|
|
|
|
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
|