-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathWidgetSettings.cpp
204 lines (159 loc) · 5.45 KB
/
WidgetSettings.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
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
/*
* 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 2 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 Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* WidgetSettings.cpp
* Copyright (C) 2011 Simon Newton
*
*
* EEPROM layout is as follows:
* magic number (2)
* dmx start address (2)
* esta ID (2)
* serial number (4)
* device label size (2)
* device label (32)
* device power cycles (4)
* sensor 0 recorded value (2)
* dmx personality (1)
*/
#include "EEPROM/EEPROM.h"
#include "WidgetSettings.h"
const int WidgetSettingsClass::MAGIC_NUMBER = 0x4f4d;
const long WidgetSettingsClass::DEFAULT_SERIAL_NUMBER = 1;
const char WidgetSettingsClass::DEFAULT_LABEL[] = "Default Label";
const byte WidgetSettingsClass::MAGIC_NUMBER_OFFSET = 0;
const byte WidgetSettingsClass::START_ADDRESS_OFFSET = 2;
const byte WidgetSettingsClass::ESTA_ID_OFFSET = 4;
const byte WidgetSettingsClass::SERIAL_NUMBER_OFFSET = 6;
const byte WidgetSettingsClass::DEVICE_LABEL_SIZE_OFFSET = 10;
const byte WidgetSettingsClass::DEVICE_LABEL_OFFSET = 12;
const byte WidgetSettingsClass::DEVICE_POWER_CYCLES_OFFSET = 44;
const byte WidgetSettingsClass::SENSOR_0_RECORDED_VALUE = 46;
const byte WidgetSettingsClass::DMX_PERSONALITY_VALUE = 48;
/**
* Check if the settings are valid and if not initialize them
*/
void WidgetSettingsClass::Init() {
int magic_number = ReadInt(MAGIC_NUMBER_OFFSET);
if (magic_number != MAGIC_NUMBER) {
// init the settings
WriteInt(MAGIC_NUMBER_OFFSET, MAGIC_NUMBER);
SetStartAddress(1);
SetEstaId(0x7a70);
SetSerialNumber(DEFAULT_SERIAL_NUMBER);
SetDeviceLabel(DEFAULT_LABEL, sizeof(DEFAULT_LABEL));
SetDevicePowerCycles(0);
SaveSensorValue(0);
SetPersonality(1);
} else {
m_start_address = ReadInt(START_ADDRESS_OFFSET);
m_personality = EEPROM.read(DMX_PERSONALITY_VALUE);
}
IncrementDevicePowerCycles();
}
void WidgetSettingsClass::SetStartAddress(unsigned int start_address) {
WriteInt(START_ADDRESS_OFFSET, start_address);
m_start_address = start_address;
}
int WidgetSettingsClass::EstaId() const {
return ReadInt(ESTA_ID_OFFSET);
}
void WidgetSettingsClass::SetEstaId(int esta_id) {
WriteInt(ESTA_ID_OFFSET, esta_id);
}
bool WidgetSettingsClass::MatchesEstaId(const byte *data) const {
bool match = true;
for (byte i = 0; i < sizeof(long); ++i) {
match &= EEPROM.read(ESTA_ID_OFFSET + i) == data[i];
}
return match;
}
long WidgetSettingsClass::SerialNumber() const {
return ReadLong(SERIAL_NUMBER_OFFSET);
}
void WidgetSettingsClass::SetSerialNumber(long serial_number) {
WriteLong(SERIAL_NUMBER_OFFSET, serial_number);
}
bool WidgetSettingsClass::MatchesSerialNumber(const byte *data) const {
bool match = true;
for (byte i = 0; i < sizeof(long); ++i) {
match &= EEPROM.read(6 + i) == data[i];
}
return match;
}
byte WidgetSettingsClass::DeviceLabel(char *label, byte length) const {
byte size = min(ReadInt(DEVICE_LABEL_SIZE_OFFSET), length);
byte i = 0;
for (; i < size; ++i) {
label[i] = EEPROM.read(DEVICE_LABEL_OFFSET + i);
if (!label[i])
break;
}
return i;
}
void WidgetSettingsClass::SetDeviceLabel(const char *new_label,
byte length) {
byte size = min(MAX_LABEL_LENGTH, length);
memcpy(m_label_buffer, new_label, size);
m_label_size = size;
m_label_pending = true;
}
unsigned long WidgetSettingsClass::DevicePowerCycles() const {
return ReadLong(DEVICE_POWER_CYCLES_OFFSET);
}
void WidgetSettingsClass::SetDevicePowerCycles(unsigned long count) {
WriteLong(DEVICE_POWER_CYCLES_OFFSET, count);
}
int WidgetSettingsClass::SensorValue() const {
return ReadInt(SENSOR_0_RECORDED_VALUE);
}
void WidgetSettingsClass::SaveSensorValue(int value) {
WriteInt(SENSOR_0_RECORDED_VALUE, value);
}
void WidgetSettingsClass::SetPersonality(byte value) {
EEPROM.write(DMX_PERSONALITY_VALUE, value);
m_personality = value;
}
bool WidgetSettingsClass::PerformWrite() {
if (!m_label_pending)
return false;
for (byte i = 0; i < m_label_size; ++i) {
EEPROM.write(DEVICE_LABEL_OFFSET + i, m_label_buffer[i]);
}
WriteInt(DEVICE_LABEL_SIZE_OFFSET, m_label_size);
m_label_pending = false;
return true;
}
void WidgetSettingsClass::IncrementDevicePowerCycles() {
SetDevicePowerCycles(DevicePowerCycles() + 1);
}
unsigned int WidgetSettingsClass::ReadInt(unsigned int offset) const {
return (EEPROM.read(offset) << 8) + EEPROM.read(offset + 1);
}
void WidgetSettingsClass::WriteInt(unsigned int offset, int data) {
EEPROM.write(offset, data >> 8);
EEPROM.write(offset + 1, data);
}
unsigned long WidgetSettingsClass::ReadLong(unsigned long offset) const {
unsigned long l = ReadInt(offset);
l = l << 16;
l += ReadInt(offset + 2);
return l;
}
void WidgetSettingsClass::WriteLong(unsigned long offset, long data) {
WriteInt(offset, data >> 16);
WriteInt(offset + 2, data);
}
WidgetSettingsClass WidgetSettings;