-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.cpp
99 lines (80 loc) · 2.04 KB
/
Settings.cpp
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
#include "Settings.h"
#include <Arduino.h>
#include <EEPROM.h>
Settings SETTINGS = Settings();
static const unsigned long MAGIC = 223456789;
SettingsParam::SettingsParam(const char* key, size_t size):
key(key),
size(size),
next(NULL) {
SETTINGS.addParam(this);
}
Settings::Settings() {
_prefixSize = sizeof(MAGIC);
}
bool Settings::begin() {
EEPROM.begin(_prefixSize + _dataSize);
unsigned long magic;
EEPROM.get(0, magic);
if( magic != MAGIC ) {
memset(&EEPROM[0], 0, EEPROM.length());
EEPROM.put(0, MAGIC);
EEPROM.commit();
return true;
}
return false;
}
void Settings::addParam(SettingsParam* param) {
param->next = params;
params = param;
param->index = _dataSize;
_dataSize += param->size;
}
int Settings::getInt(const char* key) {
return *((int*)getBytes(key));
}
bool Settings::getBool(const char* key) {
return *((bool*)getBytes(key));
}
const char* Settings::getString(const char* key) {
return (const char*)getBytes(key);
}
const byte* Settings::getBytes(const char* key) {
SettingsParam* param = getParam(key);
if( param == NULL ) {
return NULL;
}
return &EEPROM[_prefixSize + param->index];
}
bool Settings::setInt(const char* key, int value) {
return setBytes(key, (byte*)&value);
}
bool Settings::setBool(const char* key, bool value) {
return setBytes(key, (byte*)&value);
}
bool Settings::setString(const char* key, const char* value) {
SettingsParam* param = getParam(key);
if( param == NULL ) {
return false;
}
strncpy((char*)&EEPROM[_prefixSize + param->index], value, param->size);
EEPROM.commit();
return true;
}
bool Settings::setBytes(const char* key, const byte* value) {
SettingsParam* param = getParam(key);
if( param == NULL ) {
return false;
}
memcpy(&EEPROM[_prefixSize + param->index], value, param->size);
EEPROM.commit();
return true;
}
SettingsParam* Settings::getParam(const char* key) {
for( SettingsParam* p = params ; p != NULL ; p = p->next ) {
if( strcmp(p->key, key) == 0 ) {
return p;
}
}
return NULL;
}