-
Notifications
You must be signed in to change notification settings - Fork 46
/
valuelist.h
251 lines (197 loc) · 5.39 KB
/
valuelist.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
This software may be distributed and modified under the terms of the GNU
General Public License version 2 (GPL2) as published by the Free Software
Foundation and appearing in the file GPL2.TXT included in the packaging of
this file. Please note that GPL2 Section 2[b] requires that all works based
on this software must also be made publicly available under the terms of
the GPL2 ("Copyleft").
Contact information
-------------------
Circuits At Home, LTD
Web : http://www.circuitsathome.com
e-mail : [email protected]
*/
#if !defined(__VALUELIST_H__)
#define __VALUELIST_H__
#include <inttypes.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#if defined(ARDUINO) && ARDUINO >=100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
template <class ValueType, const uint8_t TitleSize>
struct ValueTitle
{
ValueType value;
const char title[TitleSize];
};
template <class ValueType, const uint16_t ListSize>
class ValueList
{
public:
uint16_t listSize;
ValueType valueList[ListSize];
ValueType currentValue;
ValueType GetNext()
{
for (uint16_t i=0; i<listSize; i++)
if (valueList[i] == currentValue)
return ((i+1 < listSize) ? valueList[i+1] : currentValue);
return currentValue;
};
ValueType GetPrev()
{
for (uint16_t i=0; i<listSize; i++)
if (valueList[i] == currentValue)
return ((i-1 >= 0) ? valueList[i-1] : currentValue);
return currentValue;
};
};
template <class ValueType, const uint8_t TitleSize>
const char* FindTitle(uint8_t size, const ValueTitle<ValueType, TitleSize> *p, ValueType val)
{
for (int i=0; i<size; i++)
{
switch (sizeof(ValueType))
{
case 1:
if (pgm_read_byte(&(p[i].value)) == val)
return (const char*)p[i].title;
break;
case 2:
if (pgm_read_word(&(p[i].value)) == val)
return (const char*)p[i].title;
break;
case 4:
if (pgm_read_dword(&(p[i].value)) == val)
return (const char*)p[i].title;
break;
}
}
return PSTR("N/A");
}
class EEPROMByteList
{
const uint16_t listOffset;
const uint8_t maxListSize;
uint8_t listSize;
uint16_t GetValueAddress(uint8_t val)
{
uint16_t tail = listOffset+listSize+2;
for (uint16_t i=listOffset+1; i<tail; i++)
if (eeprom_read_byte((uint8_t*)i) == val)
return i;
return 0xffff;
};
public:
EEPROMByteList(uint16_t list_offset, uint16_t max_size) : listOffset(list_offset), maxListSize(max_size), listSize(0)
{
};
uint16_t GetValueIndex(uint8_t val)
{
uint16_t addr = GetValueAddress(val);
return (addr == 0xffff) ? addr : addr - listOffset - 1;
};
void SetSize(uint8_t size)
{
listSize = (size < maxListSize) ? size : maxListSize;
if (eeprom_read_byte((uint8_t*) listOffset) != listSize)
eeprom_write_byte((uint8_t*) listOffset, listSize);
};
uint8_t GetSize()
{
return listSize;
};
uint8_t Get(uint8_t i)
{
return (eeprom_read_byte((uint8_t*)(listOffset + 1 + ((i < listSize) ? i : listOffset+listSize-1))));
};
void Set(uint8_t i, uint8_t val)
{
if (i < listSize)
{
uint16_t pos = listOffset + i + 1;
if (eeprom_read_byte((uint8_t*) pos) != val)
eeprom_write_byte((uint8_t*) pos, val);
}
};
uint8_t GetNext(uint8_t val, uint8_t di=1)
{
uint16_t addr = GetValueAddress(val);
uint16_t tail = listOffset+listSize;
if (addr == 0xffff)
return eeprom_read_byte((uint8_t*)tail);
addr += di;
return eeprom_read_byte((uint8_t*)((addr > tail) ? tail : addr));
};
uint8_t GetPrev(uint8_t val, uint8_t di=1)
{
uint16_t addr = GetValueAddress(val);
if (addr == 0xffff)
return eeprom_read_byte((uint8_t*)(listOffset+1));
addr -= di;
return eeprom_read_byte((uint8_t*)((addr <= listOffset) ? listOffset+1 : addr));
};
};
template <class VALUE_TYPE, const uint16_t MAX_LIST_SIZE>
class SRAMValueList
{
VALUE_TYPE theList[MAX_LIST_SIZE];
uint16_t listSize;
uint16_t GetValueAddress(VALUE_TYPE val)
{
for (uint16_t i=0; i<listSize; i++)
if (theList[i] == val)
return i;
return 0xffff;
};
public:
SRAMValueList() : listSize(0)
{
};
uint16_t GetValueIndex(VALUE_TYPE val)
{
return GetValueAddress(val);
};
void SetSize(uint16_t size)
{
listSize = (size <= MAX_LIST_SIZE) ? size : MAX_LIST_SIZE;
};
uint16_t GetSize()
{
return listSize;
};
VALUE_TYPE Get(uint16_t i)
{
return (theList[(i < listSize) ? i : listSize-1]);
};
void Set(uint16_t i, VALUE_TYPE val)
{
if (i < listSize)
theList[i] = val;
};
void Append(VALUE_TYPE val)
{
if (listSize < MAX_LIST_SIZE)
theList[listSize++] = val;
};
uint8_t GetNext(VALUE_TYPE val, uint8_t di=1)
{
uint16_t addr = GetValueAddress(val);
if (addr == 0xffff)
return theList[(addr < listSize) ? addr : listSize - 1];
addr += di;
return theList[(addr < listSize) ? addr : listSize-1];
};
uint8_t GetPrev(VALUE_TYPE val, uint8_t di=1)
{
uint16_t addr = GetValueAddress(val);
if (addr == 0xffff)
return theList[0];
addr -= di;
return theList[(addr < listSize) ? addr : 0];
};
};
#endif // __VALUELIST_H__