forked from pichenettes/avril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources_manager.h
executable file
·98 lines (82 loc) · 3.33 KB
/
resources_manager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2009 Olivier Gillet.
//
// Author: Olivier Gillet ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// Resources manager. Support for lookup of values/strings in tables. Since
// one might not want this functionality and just use the plain program memory
// read/write function, an alias for a stripped down version without string
// table lookup is provided (SimpleResourcesManager).
#ifndef AVRLIB_RESOURCES_MANAGER_H_
#define AVRLIB_RESOURCES_MANAGER_H_
#include "avrlib/base.h"
#include <string.h>
#include <avr/pgmspace.h>
namespace avrlib {
template<const prog_char** strings, const prog_uint16_t** lookup_tables>
struct ResourcesTables {
static inline const prog_char** string_table() { return strings; }
static inline const prog_uint16_t** lookup_table_table() {
return lookup_tables;
}
};
struct NoResourcesTables {
static inline const prog_char** string_table() { return NULL; }
static inline const prog_uint16_t** lookup_table_table() { return NULL; }
};
template<typename ResourceId = uint8_t, typename Tables = NoResourcesTables>
class ResourcesManager {
public:
static inline void LoadStringResource(ResourceId resource, char* buffer,
uint8_t buffer_size) {
if (!Tables::string_table()) {
return;
}
char* address = (char*)(pgm_read_word(&(Tables::string_table()[resource])));
strncpy_P(buffer, address, buffer_size);
}
template<typename ResultType, typename IndexType>
static inline ResultType Lookup(ResourceId resource, IndexType i) {
if (!Tables::lookup_table_table()) {
return 0;
};
uint16_t* address = (uint16_t*)(
pgm_read_word(&(Tables::lookup_table_table()[resource])));
return ResultType(pgm_read_word(address + i));
}
template<typename ResultType, typename IndexType>
static inline ResultType Lookup(const prog_char* p, IndexType i) {
return ResultType(pgm_read_byte(p + i));
}
template<typename ResultType, typename IndexType>
static inline ResultType Lookup(const prog_uint8_t* p, IndexType i) {
return ResultType(pgm_read_byte(p + i));
}
template<typename ResultType, typename IndexType>
static inline ResultType Lookup(const prog_uint16_t* p, IndexType i) {
return ResultType(pgm_read_word(p + i));
}
template<typename T>
static void Load(const prog_char* p, uint8_t i, T* destination) {
memcpy_P(destination, p + i * sizeof(T), sizeof(T));
}
template<typename T>
static void Load(const T* p, uint8_t* destination, uint16_t size) {
memcpy_P(destination, p, size);
}
};
typedef ResourcesManager<> SimpleResourcesManager;
} // namespace avrlib
#endif // AVRLIB_RESOURCES_MANAGER_H_