-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.cpp
148 lines (115 loc) · 4.28 KB
/
configuration.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
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
#include <Arduino.h>
#include "pgmString.h"
#include <EEPROM.h>
#include "configuration.h"
#include "debugInfo.h"
Configuration::Configuration(const int _iResetPin) :
m_bRequireStoring(false),
m_iResetPin(_iResetPin) {
// Setup the reset configuration pin
pinMode(m_iResetPin, INPUT_PULLUP);
memset(&g_configuration, 0, sizeof(g_configuration));
memcpy(&g_configuration.m_header.m_magic, HEADER_MAGIC, sizeof(HEADER_MAGIC) - 1);
g_configuration.m_header.m_size = sizeof(g_configuration);
}
Configuration::~Configuration() {
}
Configuration::SupportedDevices Configuration::convert(const String& _sDevice) {
if (_sDevice == getPgmString(STR_ON_OFF)) return E_DEVICE_ON_OFF;
else if (_sDevice == getPgmString(STR_TRIGGER)) return E_DEVICE_TRIGGER;
else if (_sDevice == getPgmString(STR_SENSOR)) return E_DEVICE_SENSOR;
else return E_DEVICE_UNKNOWN;
}
String Configuration::convert(const SupportedDevices _eDevice) {
switch (_eDevice) {
case E_DEVICE_ON_OFF:
return getPgmString(STR_ON_OFF);
break;
case E_DEVICE_TRIGGER:
return getPgmString(STR_TRIGGER);
break;
case E_DEVICE_SENSOR:
return getPgmString(STR_SENSOR);
break;
default:
return getPgmString(STR_UNKNOWN);
break;
}
return getPgmString(STR_UNKNOWN);
}
bool Configuration::restoreConfiguration() {
bool _bReturnValue = false;
// If the EEPROM has valid Magic and size, then there
// is a valid configuration in the EEPROM, read it.
// Otherwise, start from blank.
// Check the reset PIN, if it's set LOW, then the configuration
// will be erased
if (digitalRead(m_iResetPin)) {
// comparing EEPROM memory with the configuration header
byte* _pConfiguration = (byte*)&g_configuration;
unsigned int _uiIndex;
for (_uiIndex = 0; _uiIndex < sizeof(g_configuration.m_header); ++_uiIndex) {
if (_pConfiguration[_uiIndex] != EEPROM[_uiIndex]) {
break;
}
}
if (_uiIndex == sizeof(g_configuration.m_header))
{
for (_uiIndex = 0; _uiIndex < sizeof(g_configuration); ++_uiIndex)
_pConfiguration[_uiIndex] = EEPROM[_uiIndex];
_bReturnValue = true;
} else
m_bRequireStoring = true;
} else
m_bRequireStoring = true;
return _bReturnValue;
}
bool Configuration::storeConfiguration() {
bool _bReturnValue = false;
if (m_bRequireStoring) {
byte* _pConfiguration = (byte*)&g_configuration;
unsigned int _uiIndex;
for (_uiIndex = 0; _uiIndex < sizeof(g_configuration); ++_uiIndex)
EEPROM[_uiIndex] = _pConfiguration[_uiIndex];
m_bRequireStoring = false;
_bReturnValue = true;
}
return _bReturnValue;
}
byte Configuration::maxNumberOfDevices() {
return NUMBER_OF_DEVICES;
}
bool Configuration::getDeviceParams(const byte _deviceIndex,
SupportedDevices& _deviceId,
byte& _param1,
byte& _param2,
Configuration::PrivateData& _privateData) {
bool _bReturnValue = false;
if (_deviceIndex < maxNumberOfDevices()) {
_deviceId = g_configuration.m_devices[_deviceIndex].m_deviceId;
_param1 = g_configuration.m_devices[_deviceIndex].m_param1;
_param2 = g_configuration.m_devices[_deviceIndex].m_param2;
_privateData = g_configuration.m_devices[_deviceIndex].m_privateData;
_bReturnValue = true;
}
return _bReturnValue;
}
bool Configuration::setDeviceParams(const byte _deviceIndex,
const SupportedDevices _deviceId,
const byte _param1,
const byte _param2,
const Configuration::PrivateData& _privateData) {
bool _bReturnValue = false;
if (_deviceIndex >= 0 && _deviceIndex < maxNumberOfDevices()) {
g_configuration.m_devices[_deviceIndex].m_deviceId = _deviceId;
g_configuration.m_devices[_deviceIndex].m_param1 = _param1;
g_configuration.m_devices[_deviceIndex].m_param2 = _param2;
g_configuration.m_devices[_deviceIndex].m_privateData = _privateData;
m_bRequireStoring = true;
_bReturnValue = true;
}
return _bReturnValue;
}
void Configuration::dumpConfiguration(String& _sDump) {
dumpMemory(&g_configuration, sizeof(g_configuration), _sDump);
}