Skip to content

Commit 0a29dc7

Browse files
committed
[SD] Finish refactoring to allow config.json to sit on WIPPER drive, add dynamic SD CS pin
1 parent 2d7139c commit 0a29dc7

File tree

8 files changed

+36
-44
lines changed

8 files changed

+36
-44
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,14 @@ void Wippersnapper_V2::provisionV2() {
104104
_littleFSV2 = new WipperSnapper_LittleFS();
105105
#endif
106106

107-
// Determines if app is in SDLogger mode
107+
// Determine if app is in SDLogger mode
108+
uint8_t pin_cs;
108109
#ifdef USE_TINYUSB
109-
_fileSystemV2->GetSDCSPin();
110+
pin_cs = _fileSystemV2->GetSDCSPin();
110111
#else
111-
_littleFSV2->GetSDCSPin();
112+
pin_cs = _littleFSV2->GetSDCSPin();
112113
#endif
113-
if (WsV2._sdCardV2->InitSDCard())
114+
if (WsV2._sdCardV2->InitSDCard(pin_cs))
114115
return;
115116

116117
#ifdef USE_DISPLAY

src/Wippersnapper_V2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class Wippersnapper_V2 {
257257
_sharedConfigBuffers; ///< Shared JSON config buffers for offline mode
258258

259259

260-
uint8_t pin_sd_cs; ///< SD card chip select pin
260+
JsonDocument _config_doc;
261261
private:
262262
void _initV2();
263263

src/provisioning/littlefs/WipperSnapper_LittleFS_V2.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,27 +161,24 @@ void WipperSnapper_LittleFS_V2::fsHalt(String msg) {
161161
config.json file.
162162
*/
163163
/**************************************************************************/
164-
void WipperSnapper_LittleFS_V2::GetSDCSPin() {
164+
uint8_t WipperSnapper_LittleFS_V2::GetSDCSPin() {
165165
File32 file_cfg;
166-
JsonDocument doc;
167166
DeserializationError error;
168167
// Attempt to open and deserialize the config.json file
169168
file_cfg = LittleFS.open("/config.json");
170-
if (!file_cfg) {
171-
WsV2.pin_sd_cs = 255;
172-
return;
173-
}
174-
error = deserializeJson(doc, file_cfg);
169+
if (!file_cfg)
170+
return 255;
171+
error = deserializeJson(WsV2._config_doc, file_cfg);
175172
if (error) {
176173
file_cfg.close();
177-
WsV2.pin_sd_cs = 255;
178-
return;
174+
return 255;
179175
}
180176

181177
// Parse config.json and save the SD CS pin
182-
JsonObject exportedFromDevice = doc["exportedFromDevice"];
183-
WsV2.pin_sd_cs = exportedFromDevice["sd_cs_pin"] | 255;
178+
JsonObject exportedFromDevice = WsV2._config_doc["exportedFromDevice"];
179+
uint8_t pin_cs = exportedFromDevice["sd_cs_pin"] | 255;
184180
file_cfg.close();
181+
return pin_cs;
185182
}
186183

187184
#endif

src/provisioning/littlefs/WipperSnapper_LittleFS_V2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class WipperSnapper_LittleFS_V2 {
3434
~WipperSnapper_LittleFS_V2();
3535
void parseSecrets();
3636
void fsHalt(String msg);
37-
void GetSDCSPin();
37+
uint8_t GetSDCSPin();
3838
};
3939
extern Wippersnapper_V2 WsV2;
4040
#endif // WIPPERSNAPPER_LITTLEFS_H

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
/**************************************************************************/
2222
ws_sdcard::ws_sdcard() {
23+
is_mode_offline = false;
2324
_use_test_data = false;
2425
_sz_log_file = 0;
2526
}
@@ -43,14 +44,16 @@ ws_sdcard::~ws_sdcard() {
4344
otherwise.
4445
*/
4546
/**************************************************************************/
46-
bool ws_sdcard::InitSDCard() {
47-
// Check if GetSDCSPin() threw error
48-
if (WsV2.pin_sd_cs == 255)
47+
bool ws_sdcard::InitSDCard(uint8_t pin_cs) {
48+
/* if (pin_cs == 255)
49+
return false; */
50+
if (_sd.begin(pin_cs)) {
51+
is_mode_offline = true;
52+
return is_mode_offline;
53+
}
4954
return false;
5055

51-
if (!_sd.begin(WsV2.pin_sd_cs))
52-
return false;
53-
return true;
56+
5457
}
5558

5659
/**************************************************************************/
@@ -475,14 +478,8 @@ bool ws_sdcard::parseConfigFile() {
475478
DeserializationError error;
476479
JsonDocument doc;
477480
#ifndef OFFLINE_MODE_DEBUG
478-
WS_DEBUG_PRINTLN("[SD] Parsing config.json from SD card...");
479-
if (!_sd.exists("config.json")) {
480-
WS_DEBUG_PRINTLN(
481-
"[SD] FATAL Error - config.json file not found on SD Card!");
482-
return false;
483-
}
484-
file_config = _sd.open("config.json", O_RDONLY);
485-
error = deserializeJson(doc, file_config);
481+
WS_DEBUG_PRINTLN("[SD] Parsing config.json...");
482+
doc = WsV2._config_doc;
486483
#else
487484
// Test Mode - do not use the SD card, use test data instead!
488485
if (!_use_test_data) {

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ws_sdcard {
3636
public:
3737
ws_sdcard();
3838
~ws_sdcard();
39-
bool InitSDCard();
39+
bool InitSDCard(uint8_t pin_cs);
4040
bool parseConfigFile();
4141
#ifdef OFFLINE_MODE_DEBUG
4242
bool waitForSerialConfig();

src/provisioning/tinyusb/Wippersnapper_FS_V2.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,24 @@ Wippersnapper_FS_V2::~Wippersnapper_FS_V2() {
168168
config.json file.
169169
*/
170170
/**************************************************************************/
171-
void Wippersnapper_FS_V2::GetSDCSPin() {
171+
uint8_t Wippersnapper_FS_V2::GetSDCSPin() {
172172
File32 file_cfg;
173-
JsonDocument doc;
174173
DeserializationError error;
175174
// Attempt to open and deserialize the config.json file
176175
file_cfg = wipperFatFs_v2.open("/config.json");
177-
if (!file_cfg) {
178-
WsV2.pin_sd_cs = 255;
179-
return;
180-
}
181-
deserializeJson(doc, file_cfg);
176+
if (!file_cfg)
177+
return 255;
178+
error = deserializeJson(WsV2._config_doc, file_cfg);
182179
if (error) {
183180
file_cfg.close();
184-
WsV2.pin_sd_cs = 255;
185-
return;
181+
return 255;
186182
}
187183

188184
// Parse config.json and save the SD CS pin
189-
JsonObject exportedFromDevice = doc["exportedFromDevice"];
190-
WsV2.pin_sd_cs = exportedFromDevice["sd_cs_pin"] | 255;
185+
JsonObject exportedFromDevice = WsV2._config_doc["exportedFromDevice"];
186+
uint8_t pin_cs = exportedFromDevice["sd_cs_pin"] | 255;
191187
file_cfg.close();
188+
return pin_cs;
192189
}
193190

194191
/**************************************************************************/

src/provisioning/tinyusb/Wippersnapper_FS_V2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Wippersnapper_FS_V2 {
5959
bool getSecretsFile();
6060
void parseSecrets();
6161

62-
void GetSDCSPin();
62+
uint8_t GetSDCSPin();
6363

6464
#ifdef ARDUINO_FUNHOUSE_ESP32S2
6565
void parseDisplayConfig(displayConfig &displayFile);

0 commit comments

Comments
 (0)