forked from thoralt/esp8266wordclock
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.cpp
165 lines (152 loc) · 5.34 KB
/
config.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
// ESP8266 Wordclock
// Copyright (C) 2016 Thoralt Franz, https://github.com/thoralt
//
// This is the configuration module. It contains methods to load/save the
// configuration from/to the internal EEPROM (simulated EEPROM in flash).
// Data is loaded into this->eeprom_data[EEPROM_SIZE] which shares RAM with
// this->config. Configuration variables are copied to public class members
// ntpserver, heartbeat, ... where they can be used by other modules. Upon
// save, the public members are copied back to this->config/this->eeprom_data[]
// and then written to the EEPROM.
//
// 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/>.
#include <Arduino.h>
#include <EEPROM.h>
#include "config.h"
//---------------------------------------------------------------------------------------
// global instance
//---------------------------------------------------------------------------------------
ConfigClass Config = ConfigClass();
//---------------------------------------------------------------------------------------
// ConfigClass
//
// Constructor, loads default values
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
ConfigClass::ConfigClass()
{
this->reset();
}
//---------------------------------------------------------------------------------------
// ~ConfigClass
//
// destructor
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
ConfigClass::~ConfigClass()
{
}
//---------------------------------------------------------------------------------------
// begin
//
// Initializes the class and loads current configuration from EEPROM into class members.
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void ConfigClass::begin()
{
EEPROM.begin(EEPROM_SIZE);
this->load();
}
//---------------------------------------------------------------------------------------
// save
//
// Copies the current class member values to EEPROM buffer and writes it to the EEPROM.
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void ConfigClass::save()
{
this->config->bg = this->bg;
this->config->fg = this->fg;
this->config->s = this->s;
this->config->timeZone = this->timeZone;
this->config->heartbeat = this->heartbeat;
this->config->mode = (uint32_t) this->defaultMode;
for (int i = 0; i < 4; i++)
this->config->ntpserver[i] = this->ntpserver[i];
for (int i = 0; i < EEPROM_SIZE; i++)
EEPROM.write(i, this->eeprom_data[i]);
EEPROM.commit();
}
//---------------------------------------------------------------------------------------
// reset
//
// Sets default values in EEPROM buffer and member variables.
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void ConfigClass::reset()
{
this->config->magic = 0xDEADBEEF;
this->config->bg =
{ 0, 0, 0};
this->bg = this->config->bg;
this->config->fg =
{ 255, 255, 255};
this->fg = this->config->fg;
this->config->s =
{ 32, 0, 21};
this->s = this->config->s;
this->config->heartbeat = true;
this->heartbeat = this->config->heartbeat;
this->defaultMode = DisplayMode::plain;
this->config->mode = (uint32_t) this->defaultMode;
this->timeZone = 0;
this->config->ntpserver[0] = 129;
this->config->ntpserver[1] = 6;
this->config->ntpserver[2] = 15;
this->config->ntpserver[3] = 28;
this->ntpserver[0] = this->config->ntpserver[0];
this->ntpserver[1] = this->config->ntpserver[1];
this->ntpserver[2] = this->config->ntpserver[2];
this->ntpserver[3] = this->config->ntpserver[3];
}
//---------------------------------------------------------------------------------------
// load
//
// Reads the content of the EEPROM into the EEPROM buffer and copies the values to the
// public member variables. Resets (and saves) the values to their defaults if the
// EEPROM data is not initialized.
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void ConfigClass::load()
{
Serial.println("Reading EEPROM config");
for (int i = 0; i < EEPROM_SIZE; i++)
this->eeprom_data[i] = EEPROM.read(i);
if (this->config->magic != 0xDEADBEEF)
{
Serial.println("EEPROM config invalid, writing default values");
this->reset();
this->save();
}
this->bg = this->config->bg;
this->fg = this->config->fg;
this->s = this->config->s;
this->defaultMode = (DisplayMode) this->config->mode;
this->heartbeat = this->config->heartbeat;
this->timeZone = this->config->timeZone;
for (int i = 0; i < 4; i++)
this->ntpserver[i] = this->config->ntpserver[i];
}