Skip to content

Commit d1ac920

Browse files
committed
[SD] Address error cases/states for sd_cs handling
1 parent 33d5089 commit d1ac920

File tree

8 files changed

+36
-189
lines changed

8 files changed

+36
-189
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,12 @@ void Wippersnapper_V2::provisionV2() {
105105
#endif
106106

107107
// Determine if app is in SDLogger mode
108-
uint8_t pin_cs;
109108
#ifdef USE_TINYUSB
110-
pin_cs = _fileSystemV2->GetSDCSPin();
109+
_fileSystemV2->GetSDCSPin();
111110
#else
112-
pin_cs = _littleFSV2->GetSDCSPin();
111+
_littleFSV2->GetSDCSPin();
113112
#endif
114-
if (WsV2._sdCardV2->InitSDCard(pin_cs))
113+
if (WsV2._sdCardV2->InitSDCard())
115114
return;
116115

117116
#ifdef USE_DISPLAY

src/Wippersnapper_V2.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,8 @@ class Wippersnapper_V2 {
255255
///< otherwise.
256256
std::vector<std::vector<uint8_t>>
257257
_sharedConfigBuffers; ///< Shared JSON config buffers for offline mode
258-
259-
260258
JsonDocument _config_doc;
259+
uint8_t pin_sd_cs; ///< SD card chip select pin
261260
private:
262261
void _initV2();
263262

src/provisioning/littlefs/WipperSnapper_LittleFS_V2.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,24 +161,23 @@ void WipperSnapper_LittleFS_V2::fsHalt(String msg) {
161161
config.json file.
162162
*/
163163
/**************************************************************************/
164-
uint8_t WipperSnapper_LittleFS_V2::GetSDCSPin() {
164+
void WipperSnapper_LittleFS_V2::GetSDCSPin() {
165165
File32 file_cfg;
166166
DeserializationError error;
167167
// Attempt to open and deserialize the config.json file
168168
file_cfg = LittleFS.open("/config.json");
169169
if (!file_cfg)
170-
return 255;
170+
WsV2.pin_sd_cs = 255;
171171
error = deserializeJson(WsV2._config_doc, file_cfg);
172172
if (error) {
173173
file_cfg.close();
174-
return 255;
174+
WsV2.pin_sd_cs = 255;
175175
}
176176

177177
// Parse config.json and save the SD CS pin
178178
JsonObject exportedFromDevice = WsV2._config_doc["exportedFromDevice"];
179-
uint8_t pin_cs = exportedFromDevice["sd_cs_pin"] | 255;
179+
WsV2.pin_sd_cs = exportedFromDevice["sd_cs_pin"] | 255;
180180
file_cfg.close();
181-
return pin_cs;
182181
}
183182

184183
#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-
uint8_t GetSDCSPin();
37+
void GetSDCSPin();
3838
};
3939
extern Wippersnapper_V2 WsV2;
4040
#endif // WIPPERSNAPPER_LITTLEFS_H

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 11 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,18 @@ ws_sdcard::~ws_sdcard() {
4444
otherwise.
4545
*/
4646
/**************************************************************************/
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;
47+
bool ws_sdcard::InitSDCard() {
48+
if (WsV2.pin_sd_cs == PIN_SD_CS_ERROR) {
49+
is_mode_offline = false;
5250
return is_mode_offline;
5351
}
54-
return false;
52+
53+
if (_sd.begin(WsV2.pin_sd_cs)) {
54+
is_mode_offline = true;
55+
} else {
56+
is_mode_offline = false;
57+
}
58+
return is_mode_offline;
5559
}
5660

5761
/**************************************************************************/
@@ -881,162 +885,4 @@ bool ws_sdcard::LogDS18xSensorEventToSD(
881885
LogJSONDoc(doc);
882886
}
883887
return true;
884-
}
885-
886-
// TODO: Do we even need to use this function anymore now that we're running
887-
// wokwi unit tests?
888-
#ifdef OFFLINE_MODE_DEBUG
889-
/**************************************************************************/
890-
/*!
891-
@brief Validates a JSON string.
892-
@param input
893-
A JSON string to validate.
894-
@returns True if the provided JSON string is valid, False otherwise.
895-
*/
896-
/**************************************************************************/
897-
bool ws_sdcard::ValidateJSON(const char *input) {
898-
JsonDocument doc, filter;
899-
900-
DeserializationError error =
901-
deserializeJson(doc, input, DeserializationOption::Filter(filter));
902-
return error == DeserializationError::Ok;
903-
}
904-
905-
/**************************************************************************/
906-
/*!
907-
@brief Waits for a valid JSON string to be received via the hardware's
908-
serial input or from a hardcoded test JSON string.
909-
@returns True if a valid JSON string was received, False otherwise.
910-
*/
911-
/**************************************************************************/
912-
bool ws_sdcard::waitForSerialConfig() {
913-
914-
// We provide three ways to use this function:
915-
// 1. Use a SD card with a JSON config file
916-
// 2. Provide a JSON string via the hardware's serial input
917-
// 3. Use a test JSON string - for debugging purposes ONLY
918-
919-
json_test_data = "{"
920-
"\"exportVersion\": \"1.0.0\","
921-
"\"exportedBy\": \"tester\","
922-
"\"exportedAt\": \"2024-10-28T18:58:23.976Z\","
923-
"\"exportedFromDevice\": {"
924-
"\"board\": \"metroesp32s3\","
925-
"\"firmwareVersion\": \"1.0.0-beta.93\","
926-
"\"referenceVoltage\": 2.6,"
927-
"\"totalGPIOPins\": 11,"
928-
"\"totalAnalogPins\": 6"
929-
"},"
930-
"\"components\": ["
931-
"{"
932-
"\"componentAPI\": \"analogio\","
933-
"\"name\": \"Analog Pin\","
934-
"\"pinName\": \"D14\","
935-
"\"type\": \"analog_pin\","
936-
"\"mode\": \"ANALOG\","
937-
"\"direction\": \"INPUT\","
938-
"\"sampleMode\": \"TIMER\","
939-
"\"analogReadMode\": \"PIN_VALUE\","
940-
"\"period\": 5,"
941-
"\"isPin\": true"
942-
"},"
943-
"{"
944-
"\"componentAPI\": \"analogio\","
945-
"\"name\": \"Analog Pin\","
946-
"\"pinName\": \"D27\","
947-
"\"type\": \"analog_pin\","
948-
"\"mode\": \"ANALOG\","
949-
"\"direction\": \"INPUT\","
950-
"\"sampleMode\": \"TIMER\","
951-
"\"analogReadMode\": \"PIN_VALUE\","
952-
"\"period\": 5,"
953-
"\"isPin\": true"
954-
"},"
955-
"{"
956-
"\"componentAPI\": \"digitalio\","
957-
"\"name\": \"Button (D4)\","
958-
"\"pinName\": \"D4\","
959-
"\"type\": \"push_button\","
960-
"\"mode\": \"DIGITAL\","
961-
"\"sampleMode\": \"EVENT\","
962-
"\"direction\": \"INPUT\","
963-
"\"period\": 5,"
964-
"\"pull\": \"UP\","
965-
"\"isPin\": true"
966-
"},"
967-
"{"
968-
"\"componentAPI\": \"ds18x20\","
969-
"\"name\": \"DS18B20: Temperature Sensor (°F)\","
970-
"\"sensorTypeCount\": 2,"
971-
"\"sensorType1\": \"object-temp-fahrenheit\","
972-
"\"sensorType2\": \"object-temp\","
973-
"\"pinName\": \"D12\","
974-
"\"sensorResolution\": 12,"
975-
"\"period\": 5"
976-
"},"
977-
"{"
978-
"\"componentAPI\": \"ds18x20\","
979-
"\"name\": \"DS18B20: Temperature Sensor (°F)\","
980-
"\"sensorTypeCount\": 2,"
981-
"\"sensorType1\": \"object-temp-fahrenheit\","
982-
"\"sensorType2\": \"object-temp\","
983-
"\"pinName\": \"D25\","
984-
"\"sensorResolution\": 12,"
985-
"\"period\": 5"
986-
"}"
987-
"]"
988-
"}\\n\r\n";
989-
990-
_serialInput = ""; // Clear the serial input buffer
991-
if (!_use_test_data) {
992-
WS_DEBUG_PRINTLN("[SD] Waiting for incoming JSON string...");
993-
while (true) {
994-
// Check if there is data available to read
995-
if (Serial.available() > 0) {
996-
// Read and append to _serialInput
997-
char c = Serial.read();
998-
_serialInput += c;
999-
1000-
// DEBUG - Check JSON output as an Int and total output
1001-
// WS_DEBUG_PRINT("[SD] Character read: ");
1002-
// WS_DEBUG_PRINTLN((int)c);
1003-
// WS_DEBUG_PRINTLN(_serialInput);
1004-
1005-
// Check for end of JSON string using \n sequence
1006-
if (_serialInput.endsWith("\\n")) {
1007-
WS_DEBUG_PRINTLN("[SD] End of JSON string detected!");
1008-
break;
1009-
}
1010-
}
1011-
}
1012-
}
1013-
1014-
// Strip the '\n' off the end of _serialInput
1015-
_serialInput.trim();
1016-
1017-
// Print out the received JSON string
1018-
WS_DEBUG_PRINT("[SD][Debug] JSON string received!");
1019-
if (_use_test_data) {
1020-
WS_DEBUG_PRINTLN("[from json test data]");
1021-
WS_DEBUG_PRINTLN(json_test_data);
1022-
} else {
1023-
WS_DEBUG_PRINTLN(_serialInput);
1024-
}
1025-
1026-
// Attempt to validate the string as JSON
1027-
if (!_use_test_data) {
1028-
if (!ValidateJSON(_serialInput.c_str())) {
1029-
WS_DEBUG_PRINTLN("[SD] Invalid JSON string received!");
1030-
return false;
1031-
}
1032-
} else {
1033-
if (!ValidateJSON(json_test_data)) {
1034-
WS_DEBUG_PRINTLN("[SD] Invalid JSON string received!");
1035-
return false;
1036-
}
1037-
}
1038-
1039-
WS_DEBUG_PRINTLN("[SD] Valid JSON string received!");
1040-
return true;
1041-
}
1042-
#endif
888+
}

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#define SD_FAT_TYPE 3 ///< SdFat type (3 = SdFs)
2323
#define MAX_LOG_FILE_SZ 500 ///< Maximum log file size of 500 bytes
24+
#define PIN_SD_CS_ERROR 255 ///< Error code for invalid SD card CS pin
2425
#define UNKNOWN_VALUE "unknown" ///< Unknown JSON field value
2526

2627
// forward decl.
@@ -36,11 +37,8 @@ class ws_sdcard {
3637
public:
3738
ws_sdcard();
3839
~ws_sdcard();
39-
bool InitSDCard(uint8_t pin_cs);
40+
bool InitSDCard();
4041
bool parseConfigFile();
41-
#ifdef OFFLINE_MODE_DEBUG
42-
bool waitForSerialConfig();
43-
#endif
4442
bool CreateNewLogFile();
4543
bool isModeOffline() { return is_mode_offline; }
4644
bool LogGPIOSensorEventToSD(uint8_t pin, float value,
@@ -53,7 +51,6 @@ class ws_sdcard {
5351

5452
private:
5553
bool ValidateChecksum(JsonDocument &doc);
56-
bool ValidateJSON(const char *input);
5754
bool ValidateJSONKey(const char *key, const char *error_msg);
5855
void CheckIn(uint8_t max_digital_pins, uint8_t max_analog_pins,
5956
float ref_voltage);

src/provisioning/tinyusb/Wippersnapper_FS_V2.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,31 @@ Wippersnapper_FS_V2::~Wippersnapper_FS_V2() {
168168
config.json file.
169169
*/
170170
/**************************************************************************/
171-
uint8_t Wippersnapper_FS_V2::GetSDCSPin() {
171+
void Wippersnapper_FS_V2::GetSDCSPin() {
172172
File32 file_cfg;
173173
DeserializationError error;
174174
// Attempt to open and deserialize the config.json file
175175
file_cfg = wipperFatFs_v2.open("/config.json");
176-
if (!file_cfg)
177-
return 255;
176+
if (!file_cfg) {
177+
WsV2.pin_sd_cs = 255;
178+
return;
179+
}
180+
178181
error = deserializeJson(WsV2._config_doc, file_cfg);
179182
if (error) {
180183
file_cfg.close();
181-
return 255;
184+
WsV2.pin_sd_cs = 255;
185+
return;
182186
}
183187

184188
// Parse config.json and save the SD CS pin
185189
JsonObject exportedFromDevice = WsV2._config_doc["exportedFromDevice"];
186-
uint8_t pin_cs = exportedFromDevice["sd_cs_pin"] | 255;
190+
WsV2.pin_sd_cs = exportedFromDevice["sd_cs_pin"] | 255;
191+
if (WsV2.pin_sd_cs == 255) {
192+
file_cfg.close();
193+
fsHalt("ERROR: Could not find required sd_cs_pin in config.json!");
194+
}
187195
file_cfg.close();
188-
return pin_cs;
189196
}
190197

191198
/**************************************************************************/

src/provisioning/tinyusb/Wippersnapper_FS_V2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Wippersnapper_FS_V2 {
4646

4747
void initUSBMSC();
4848

49-
uint8_t GetSDCSPin();
49+
void GetSDCSPin();
5050

5151
bool writeFSContents();
5252
void fsHalt(String msg);

0 commit comments

Comments
 (0)