-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
111 lines (85 loc) · 2.47 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
//
// Created by S. Jansen.
//
#include "config.h"
void Config::setup() {
EEPROM.begin(EEPROM_SIZE);
}
void EEPROMupdate(const int address, const char value) {
if (EEPROM.read(address) == (uint8_t) value) {
return;
}
EEPROM.write(address, value);
EEPROM.commit();
}
void Config::wifiSsidSet(const char *input, uint8_t length) {
// Debug
Serial.print("[Config] Set Wifi Ssid = ");
Serial.write(input);
Serial.print(" [");
Serial.print(length);
Serial.println("]");
EEPROMupdate(EEPROM_WIFI_SSID_ADDRESS, length);
for (int i = 0;
i < length && (EEPROM_WIFI_SSID_ADDRESS + 1 + i) < EEPROM_WIFI_PASSWORD_ADDRESS;
i++) {
EEPROMupdate(EEPROM_WIFI_SSID_ADDRESS + 1 + i, input[i]);
}
}
const char *Config::wifiSsidGet() {
uint8_t length = EEPROM.read(EEPROM_WIFI_SSID_ADDRESS);
// If value hasn't been initialized in the EEPROM
if (length > 64) {
length = 0;
}
char *output = (char *) malloc(length + 1);
for (int i = 0; i < length; i++) {
output[i] = EEPROM.read(EEPROM_WIFI_SSID_ADDRESS + 1 + i);
}
output[length] = '\0';
// Debug
Serial.print("[Config] Get Wifi Ssid = ");
Serial.write(output);
Serial.print(" [");
Serial.print(length);
Serial.println("]");
return output;
}
void Config::wifiPasswordSet(const char *input, const uint8_t length) {
// Debug
Serial.print("[Config] Set Wifi password = ");
Serial.write(input);
Serial.print(" [");
Serial.print(length);
Serial.println("]");
EEPROMupdate(EEPROM_WIFI_PASSWORD_ADDRESS, length);
for (int i = 0; i < length; i++) {
EEPROMupdate(EEPROM_WIFI_PASSWORD_ADDRESS + 1 + i, input[i]);
}
}
const char *Config::wifiPasswordGet() {
uint8_t length = EEPROM.read(EEPROM_WIFI_PASSWORD_ADDRESS);
// If value hasn't been initialized in the EEPROM
if (length > 64) {
length = 0;
}
char *output = (char *) malloc(length + 1);
for (int i = 0; i < length; i++) {
output[i] = EEPROM.read(EEPROM_WIFI_PASSWORD_ADDRESS + 1 + i);
}
output[length] = '\0';
// Debug
Serial.print("[Config] Get Wifi password = ");
Serial.write(output);
Serial.print(" [");
Serial.print(length);
Serial.println("]");
return output;
}
boolean bootIntoConfig = false;
void Config::bootIntoConfigSet(boolean value) {
bootIntoConfig = value;
}
boolean Config::bootIntoConfigGet() {
return bootIntoConfig;
}