Compare commits
5 Commits
2215491c0b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| ceec87f921 | |||
| f9014aea6c | |||
| 6edb2e5a21 | |||
| 9245b49e4c | |||
| 2c66479a49 |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -2,3 +2,10 @@
|
|||||||
Release
|
Release
|
||||||
Debug
|
Debug
|
||||||
*.componentinfo.xml
|
*.componentinfo.xml
|
||||||
|
*.elf
|
||||||
|
*.o
|
||||||
|
*.hex
|
||||||
|
*.srec
|
||||||
|
*.eeprom
|
||||||
|
*.lss
|
||||||
|
*.map
|
||||||
|
|||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2020 BlackMark
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is furnished
|
||||||
|
to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice (including the next
|
||||||
|
paragraph) shall be included in all copies or substantial portions of the
|
||||||
|
Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
||||||
|
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
||||||
|
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
107
flash.hpp
107
flash.hpp
@@ -1,8 +1,18 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
#include <avr/pgmspace.h>
|
#include <avr/pgmspace.h>
|
||||||
|
|
||||||
|
#include "../util/util.hpp"
|
||||||
|
|
||||||
#define F(str) (reinterpret_cast<const ::detail::FlashString *>(PSTR(str)))
|
#define F(str) (reinterpret_cast<const ::detail::FlashString *>(PSTR(str)))
|
||||||
|
#define GF(name, str) \
|
||||||
|
const char __##name[] PROGMEM = str; \
|
||||||
|
const auto *name = reinterpret_cast<const ::detail::FlashString *>(__##name)
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
@@ -10,3 +20,100 @@ namespace detail {
|
|||||||
struct FlashString;
|
struct FlashString;
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
|
namespace flash {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static inline T load(const T &object)
|
||||||
|
{
|
||||||
|
T buffer;
|
||||||
|
auto byteBuffer = reinterpret_cast<std::byte *>(&buffer);
|
||||||
|
for (auto i = std::size_t{0}; i < sizeof(T); ++i) {
|
||||||
|
byteBuffer[i] = static_cast<std::byte>(pgm_read_byte(&reinterpret_cast<const std::byte *>(&object)[i]));
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct [[gnu::progmem]] Wrapper
|
||||||
|
{
|
||||||
|
using value_type = T;
|
||||||
|
|
||||||
|
inline T operator*() const
|
||||||
|
{
|
||||||
|
return load(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::byte operator[](const std::size_t idx) const
|
||||||
|
{
|
||||||
|
const auto bytePtr = reinterpret_cast<const std::byte *>(&value);
|
||||||
|
return load(bytePtr[idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const T value;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct RamWrapper {
|
||||||
|
using value_type = T;
|
||||||
|
|
||||||
|
inline constexpr T &operator*()
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline constexpr const T &operator*() const
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::byte &operator[](const std::size_t idx)
|
||||||
|
{
|
||||||
|
const auto bytePtr = reinterpret_cast<std::byte *>(&value);
|
||||||
|
return bytePtr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const std::byte &operator[](const std::size_t idx) const
|
||||||
|
{
|
||||||
|
const auto bytePtr = reinterpret_cast<const std::byte *>(&value);
|
||||||
|
return bytePtr[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
T value;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename WrapperT>
|
||||||
|
struct is_flash_wrapper : std::false_type {
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_flash_wrapper<Wrapper<T>> : std::true_type {
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename WrapperT>
|
||||||
|
static inline constexpr auto is_flash_wrapper_v = is_flash_wrapper<WrapperT>::value;
|
||||||
|
|
||||||
|
template <typename WrapperT>
|
||||||
|
struct is_ram_wrapper : std::false_type {
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_ram_wrapper<RamWrapper<T>> : std::true_type {
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename WrapperT>
|
||||||
|
static inline constexpr auto is_ram_wrapper_v = is_ram_wrapper<WrapperT>::value;
|
||||||
|
|
||||||
|
template <typename WrapperT, typename T>
|
||||||
|
static inline decltype(auto) loadLike(const T &object)
|
||||||
|
{
|
||||||
|
if constexpr (is_ram_wrapper_v<std::remove_cvref_t<WrapperT>>) {
|
||||||
|
return object;
|
||||||
|
} else if constexpr (is_flash_wrapper_v<std::remove_cvref_t<WrapperT>>) {
|
||||||
|
return load(object);
|
||||||
|
} else {
|
||||||
|
static_assert(util::always_false_v<T>, "Invalid wrapper type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace flash
|
||||||
|
|||||||
Reference in New Issue
Block a user