-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
117 lines (97 loc) · 3.2 KB
/
config.cpp
File metadata and controls
117 lines (97 loc) · 3.2 KB
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
#include <Arduino.h>
#include <EEPROM.h>
#include "config.h"
//---------------------------------------------------------------------------------------
// global instance
//---------------------------------------------------------------------------------------
ConfigClass Config = ConfigClass();
// Constructor, loads default values
ConfigClass::ConfigClass()
{
this->reset();
}
// destructor
ConfigClass::~ConfigClass()
{
}
// Initializes the class and loads current configuration from EEPROM into class members.
void ConfigClass::begin()
{
EEPROM.begin(EEPROM_SIZE);
this->load();
}
// Copies the current class member values to EEPROM buffer and writes it to the EEPROM
void ConfigClass::saveDelayed()
{
this->delayedWriteTimer = 1000; // 10 seconds using 10 ms timer
}
// Copies the current class member values to EEPROM buffer and writes it to the EEPROM.
void ConfigClass::save()
{
this->delayedWriteFlag = false;
this->config->timeZone = this->timeZone;
this->config->delay = this->delay;
this->config->heartbeat = this->heartbeat;
this->config->animationType = (uint32_t)this->currentAnimation;
this->config->color = (uint32_t)this->currentColor;
this->config->text = this->currentText;
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();
}
// Sets default values in EEPROM buffer and member variables.
void ConfigClass::reset()
{
this->config->magic = 0xDEADBEEF;
this->config->delay = 40;
this->delay = this->config->delay;
this->config->heartbeat = true;
this->heartbeat = this->config->heartbeat;
this->config->animationType = (uint32_t)this->currentAnimation;
this->config->color = (uint32_t)this->currentColor;
this->config->text = this->currentText;
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];
}
void ConfigClass::formatEeprom()
{
Serial.println("Formating EEPROM");
// write a 0 to all 4096 bytes of the EEPROM
for (int i = 0; i < 4096; i++)
EEPROM.write(i, 0);
EEPROM.commit();
Serial.println("Done...");
}
// 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()
{
// this->formatEeprom();
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->currentAnimation = this->config->animationType;
this->currentColor = this->config->color;
this->currentText = this->config->text;
this->heartbeat = this->config->heartbeat;
this->delay = this->config->delay;
this->timeZone = this->config->timeZone;
for (int i = 0; i < 4; i++)
this->ntpserver[i] = this->config->ntpserver[i];
}