This repository was archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHiveStorage.cpp
145 lines (115 loc) · 3.19 KB
/
HiveStorage.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
#include "HiveSetup.h"
#include "SD.h"
#include "EEPROM.h"
#include "HiveStorage.h"
uint8_t StorageType = EEPROMStorage;
// Returns
// 0 if storage isn't available
// 1 if storage is available and empty
// 2 if storage is available and filled
uint8_t initSDStorage() {
uint8_t storageResult = 0;
File myFile;
useDevice(DeviceIdSD);
if (!SD.begin(DevicesCSPins[DeviceIdSD])) {
Serial.println(F("Card init failed, or not present"));
// don't do anything more:
return 0;
}
if (SD.exists(StorageFileName)) {
// DEBUG
Serial.println(F("Found settings file"));
myFile = SD.open(StorageFileName, FILE_READ);
if (myFile && (myFile.size() > 0)) {
// DEBUG
Serial.println(F("File size > 0"));
storageResult = 2;
} else {
// DEBUG
Serial.println(F("File is empty"));
storageResult = 1;
}
myFile.close();
} else {
// DEBUG
Serial.println(F("File is missing"));
myFile = SD.open(StorageFileName, FILE_WRITE);
myFile.close();
storageResult = 1;
}
return storageResult;
}
// Returns true if there are saved settings
// otherwise returns false
uint8_t initEEPROMStorage() {
byte checkByte;
checkByte = EEPROM.read(0);
if (checkByte == StorageCheckByte) {
return 1;
} else {
return 0;
}
}
uint8_t initStorage() {
uint8_t initResult = 0;
// DEBUG
Serial.print(F("Set up storage: "));
// Check if there's an SD card available
initResult = initSDStorage();
if (initResult == 0) {
// If no SD card then use EEPROM for storage
StorageType = EEPROMStorage;
// DEBUG
Serial.println(F("EEPROM"));
Serial.print(F("Init result: "));
Serial.println(initResult);
// Tell if we need to load settings
return initEEPROMStorage();
} else {
// Use SD card if available
StorageType = SDStorage;
// We're going to store all system settings in EEPROM,
// so there's no need for storage offset for SD card
SettingsOffset = 0;
// DEBUG
Serial.println(F("SD"));
Serial.print(F("Init result: "));
Serial.println(initResult);
// Tell if we need to load settings (0 or 1)
return initResult - 1;
}
}
uint8_t loadSystemSettings() {
// Check if there are some settings stored in EEPROM
if (initEEPROMStorage() > 0) {
// Store current storage type
// and override it temporarily to read settings from EEPROM
uint8_t oldStorageType = StorageType;
StorageType = EEPROMStorage;
// Skip the first "check" byte and read settings
// All settings are meant to be global variables
for (int i = 1; i < 4; i++) {
readStorage(i, hivemac[i+2]);
}
// TODO: Load modules count byte
// Restore original storage type
StorageType = oldStorageType;
return 1;
} else {
return 0;
}
}
void saveSystemSettings() {
uint8_t oldStorageType = StorageType;
StorageType = EEPROMStorage;
// Write a "check" byte first
// to indicate we have some settings stored in EEPROM
writeStorage(0, StorageCheckByte);
for (int i = 1; i < 4; i++) {
writeStorage(i, hivemac[i+2]);
// DEBUG
Serial.println("Save system settings");
}
// TODO: Save modules count byte
StorageType = oldStorageType;
}