FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
flash.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef FLASH_HH
22#define FLASH_HH
23
24#include "boards/io.h"
25#include <avr/pgmspace.h>
26
30namespace flash
31{
33 // Utility to handle PROGMEM storage for strings
34 class FlashStorage;
36
49 template<typename T = uint8_t>
50 T* read_flash(uint16_t address, T* buffer, uint8_t size)
51 {
52 uint8_t* ptr = (uint8_t*) buffer;
53 for (size_t i = 0; i < (size * sizeof(T)); ++i) *ptr++ = pgm_read_byte(address++);
54 return buffer;
55 }
56
67 template<typename T> T& read_flash(uint16_t address, T& item)
68 {
69 uint8_t* ptr = (uint8_t*) &item;
70 for (size_t i = 0; i < sizeof(T); ++i) *ptr++ = pgm_read_byte(address++);
71 return item;
72 }
73
100 template<typename T> T& read_flash(const T* address, T& item)
101 {
102 return read_flash((uint16_t) address, item);
103 }
104
110 template<typename T> class FlashReader
111 {
112 public:
118 explicit FlashReader(const T* flash_buffer) : address_{uint16_t(flash_buffer)} {}
119
126 {
127 T item;
128 read_flash(address_, item);
129 address_ += sizeof(T);
130 return item;
131 }
132
133 private:
134 uint16_t address_;
135 };
136}
137
150#define F(ptr) \
151 (__extension__({ \
152 static const char __c[] PROGMEM = (ptr); \
153 (const flash::FlashStorage*) &__c[0]; \
154 }))
155
156#endif /* FLASH_HH */
Functor reading items from an address in AVR Flash memory (PROGMEM).
Definition: flash.h:111
FlashReader(const T *flash_buffer)
Construct a FlashReader functor reading Flash from flash_buffer address.
Definition: flash.h:118
T operator()()
Get the enxt item read from memory.
Definition: flash.h:125
Defines API to handle flash memory storage.
Definition: flash.h:31
T * read_flash(uint16_t address, T *buffer, uint8_t size)
Read flash memory content at given address into buffer.
Definition: flash.h:50