-
Notifications
You must be signed in to change notification settings - Fork 1
/
save_restore.h
127 lines (112 loc) · 4.09 KB
/
save_restore.h
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
#pragma once // Please format this file with clang before check-in to GitHub
/*
File: save_restore.h
Software: Barry Hansen, K7BWH, barry@k7bwh.com, Seattle, WA
Hardware: John Vanderbeck, KM7O, Seattle, WA
Purpose: This module saves configuration data to/from SDRAM.
QSPI Flash chip on the Feather M4 Express breakout board has 2 MB capacity.
Typical Save C++ Object:
void saveConfig() {
SaveRestore config(CONFIG_FILE_NAME, CONFIG_VERSION_STRING);
config.writeConfig((byte *)&buffer, sizeof(buffer));
}
Typical Restore C++ Object:
void restoreConfig() {
SaveRestore config(CONFIG_FILE_NAME, CONFIG_VERSION_STRING);
int tempValue;
int result = config.readConfig((byte *)&tempValue, sizeof(tempValue));
if (result) {
Serial.print("Loaded my value from NVR: ");
Serial.println(myValue);
} else {
Serial.println("Failed to load settings, re-initializing config file");
saveConfig();
}
}
*/
#include <SdFat.h> // SDRAM File Allocation Table filesystem
#include "logger.h" // conditional printing to Serial port
// ========== extern ===========================================
extern Logger logger; // Griduino.ino
// ========== class SaveRestore =========================
class SaveRestore {
public:
char fqFilename[64]; // fully qualified filename, e.g. "/Settings/volume.cfg" (strictly 8.3 names)
char sVersion[32]; // ID string, detects if settings are actually written
public:
/*
* Constructor
*/
SaveRestore(const char *vFilename, const char *vVersion) {
// vFilename MUST follow 8.3 naming conventions
if (strlen(vFilename) < sizeof(fqFilename)) {
strcpy(fqFilename, vFilename);
} else {
Serial.println("Error! Filename exceeds buffer length");
}
if (strlen(vVersion) < sizeof(sVersion)) {
strcpy(sVersion, vVersion);
} else {
Serial.println("Error! Version string exceeds buffer length");
}
}
~SaveRestore() {}
// ========== load configuration ======================
/*
* Load our class data from SDRAM
*/
int readConfig(byte *pBuffer, const unsigned int sizeBuffer);
// ========== save configuration ======================
/*
* Save our class data to SDRAM
*/
int writeConfig(const byte *pBuffer, const unsigned int sizeBuffer);
// ========== file management ======================
// Very basic file management is provided to report on the files
// stored in SDRAM. This provides an easier way to see where and how
// the SDRAM file system is being used via USB port, compared to the
// awkward process of restarting in CircuitPy mode.
/*
* List files in SDRAM
*/
int listFiles(const char *dirname);
/*
* Delete file
*/
int deleteFile(const char *vFilename);
int typeFile();
protected:
int openFlash(); // helper
void showFile(const char *indent, const int count, const char *filename, const int filesize);
void showDirectory(const char *dirname);
};
// ========== line-by-line string functions ============
/*
* For null-terminated strings to SDRAM
* Returns 1=success, 0=failure
*/
class SaveRestoreStrings : public SaveRestore {
public:
/*
* Constructor
*/
SaveRestoreStrings(const char *vFilename, const char *vVersion)
: SaveRestore{vFilename, vVersion} {
}
~SaveRestoreStrings() {}
int open(const char *filename, const char *mode); // https://cplusplus.com/reference/cstdio/fopen/
int writeLine(const char *pBuffer); // https://cplusplus.com/reference/cstdio/snprintf/
int readLine(char *pBuffer, int bufflen); // https://cplusplus.com/reference/cstdio/gets/
uint8_t getError() {
return handle.getError();
}
void close();
protected:
#if defined(ARDUINO_ADAFRUIT_FEATHER_RP2040)
// Adafruit Feather RP2040, https://www.adafruit.com/product/4884
File32 handle;
#else
// Adafruit Feather M4, https://www.adafruit.com/product/3857
File32 handle; // contains result of gFatfs.open()
#endif
};