Skip to content

Commit 75b117b

Browse files
committed
[SD] Fix Wokwi Testing, test_digital_input is having issues still
1 parent f10f755 commit 75b117b

File tree

6 files changed

+124
-17
lines changed

6 files changed

+124
-17
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,12 @@ void Wippersnapper_V2::provisionV2() {
106106
_fileSystemV2->GetSDCSPin();
107107
#elif defined(USE_LITTLEFS)
108108
_littleFSV2->GetSDCSPin();
109+
#elif defined(OFFLINE_MODE_WOKWI)
110+
WsV2.pin_sd_cs = 15;
109111
#endif
110112
WsV2._sdCardV2 = new ws_sdcard();
111113
if (WsV2._sdCardV2->isSDCardInitialized()) {
112-
//haltErrorV2("GOOD: passed sd cspin check", WS_LED_STATUS_KAT);
113114
return;
114-
} else {
115-
// We are hitting against this at 4:16pm 12/16/2021
116-
haltErrorV2("ERROR: failed sd cspin check", WS_LED_STATUS_MQTT_CONNECTING);
117115
}
118116

119117
#ifdef USE_DISPLAY
@@ -1200,11 +1198,10 @@ void Wippersnapper_V2::connectV2() {
12001198
// NOTE: After this, bail out of this function and run the app loop!!!
12011199
if (WsV2._sdCardV2->isModeOffline() == true) {
12021200
WS_DEBUG_PRINTLN("[Offline] Running device configuration...");
1203-
// Wait for incoming JSON string from Serial
1204-
#ifdef OFFLINE_MODE_DEBUG
1205-
if (!WsV2._sdCardV2->waitForSerialConfig())
1206-
haltErrorV2("Unable to validate incoming JSON file");
1207-
#endif
1201+
// If debug mode, wait for serial config
1202+
#ifdef OFFLINE_MODE_DEBUG
1203+
WsV2._sdCardV2->waitForSerialConfig();
1204+
#endif
12081205
// Parse the JSON file
12091206
if (!WsV2._sdCardV2->parseConfigFile())
12101207
haltErrorV2("Failed to parse incoming JSON file");

src/provisioning/littlefs/WipperSnapper_LittleFS_V2.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,12 @@ void WipperSnapper_LittleFS_V2::fsHalt(String msg,
162162
*/
163163
/**************************************************************************/
164164
void WipperSnapper_LittleFS_V2::GetSDCSPin() {
165-
DeserializationError error;
166165
// Attempt to open and deserialize the config.json file
167166
File file_cfg = LittleFS.open("/config.json");
168167
if (!file_cfg)
169168
WsV2.pin_sd_cs = 255;
170169

171-
error = deserializeJson(WsV2._config_doc, file_cfg);
170+
DeserializationError error = deserializeJson(WsV2._config_doc, file_cfg);
172171
if (error) {
173172
file_cfg.close();
174173
WsV2.pin_sd_cs = 255;

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,8 @@ bool ws_sdcard::parseConfigFile() {
529529
// It is not possible to continue running in offline mode without a valid
530530
// config file
531531
if (error) {
532-
WS_DEBUG_PRINTLN(
533-
"[SD] Runtime Error: Unable to deserialize config.json. Error Code: " +
534-
String(error.c_str()));
532+
WS_DEBUG_PRINT("[SD] Runtime Error: Unable to deserialize config.json");
533+
WS_DEBUG_PRINTLN("\nError Code: " + String(error.c_str()));
535534
return false;
536535
}
537536
WS_DEBUG_PRINTLN("[SD] Successfully deserialized JSON config file!");
@@ -923,4 +922,116 @@ bool ws_sdcard::LogDS18xSensorEventToSD(
923922
LogJSONDoc(doc);
924923
}
925924
return true;
926-
}
925+
}
926+
927+
#ifdef OFFLINE_MODE_DEBUG
928+
/**************************************************************************/
929+
/*!
930+
@brief Waits for a valid JSON string to be received via the hardware's
931+
serial input or from a hardcoded test JSON string.
932+
@returns True if a valid JSON string was received, False otherwise.
933+
*/
934+
/**************************************************************************/
935+
void ws_sdcard::waitForSerialConfig() {
936+
json_test_data = "{"
937+
"\"exportVersion\": \"1.0.0\","
938+
"\"exportedBy\": \"tester\","
939+
"\"exportedAt\": \"2024-10-28T18:58:23.976Z\","
940+
"\"exportedFromDevice\": {"
941+
"\"board\": \"metroesp32s3\","
942+
"\"firmwareVersion\": \"1.0.0-beta.93\","
943+
"\"referenceVoltage\": 2.6,"
944+
"\"totalGPIOPins\": 11,"
945+
"\"totalAnalogPins\": 6"
946+
"},"
947+
"\"components\": ["
948+
"{"
949+
"\"componentAPI\": \"analogio\","
950+
"\"name\": \"Analog Pin\","
951+
"\"pinName\": \"D14\","
952+
"\"type\": \"analog_pin\","
953+
"\"mode\": \"ANALOG\","
954+
"\"direction\": \"INPUT\","
955+
"\"sampleMode\": \"TIMER\","
956+
"\"analogReadMode\": \"PIN_VALUE\","
957+
"\"period\": 5,"
958+
"\"isPin\": true"
959+
"},"
960+
"{"
961+
"\"componentAPI\": \"analogio\","
962+
"\"name\": \"Analog Pin\","
963+
"\"pinName\": \"D27\","
964+
"\"type\": \"analog_pin\","
965+
"\"mode\": \"ANALOG\","
966+
"\"direction\": \"INPUT\","
967+
"\"sampleMode\": \"TIMER\","
968+
"\"analogReadMode\": \"PIN_VALUE\","
969+
"\"period\": 5,"
970+
"\"isPin\": true"
971+
"},"
972+
"{"
973+
"\"componentAPI\": \"digitalio\","
974+
"\"name\": \"Button (D4)\","
975+
"\"pinName\": \"D4\","
976+
"\"type\": \"push_button\","
977+
"\"mode\": \"DIGITAL\","
978+
"\"sampleMode\": \"EVENT\","
979+
"\"direction\": \"INPUT\","
980+
"\"period\": 5,"
981+
"\"pull\": \"UP\","
982+
"\"isPin\": true"
983+
"},"
984+
"{"
985+
"\"componentAPI\": \"ds18x20\","
986+
"\"name\": \"DS18B20: Temperature Sensor (°F)\","
987+
"\"sensorTypeCount\": 2,"
988+
"\"sensorType1\": \"object-temp-fahrenheit\","
989+
"\"sensorType2\": \"object-temp\","
990+
"\"pinName\": \"D12\","
991+
"\"sensorResolution\": 12,"
992+
"\"period\": 5"
993+
"},"
994+
"{"
995+
"\"componentAPI\": \"ds18x20\","
996+
"\"name\": \"DS18B20: Temperature Sensor (°F)\","
997+
"\"sensorTypeCount\": 2,"
998+
"\"sensorType1\": \"object-temp-fahrenheit\","
999+
"\"sensorType2\": \"object-temp\","
1000+
"\"pinName\": \"D25\","
1001+
"\"sensorResolution\": 12,"
1002+
"\"period\": 5"
1003+
"}"
1004+
"]"
1005+
"}\\n\r\n";
1006+
1007+
_serialInput = ""; // Clear the serial input buffer
1008+
if (!_use_test_data) {
1009+
WS_DEBUG_PRINTLN("[SD] Waiting for incoming JSON string...");
1010+
while (true) {
1011+
// Check if there is data available to read
1012+
if (Serial.available() > 0) {
1013+
char c = Serial.read();
1014+
_serialInput += c;
1015+
if (_serialInput.endsWith("\\n")) {
1016+
WS_DEBUG_PRINTLN("[SD] End of JSON string detected!");
1017+
break;
1018+
}
1019+
}
1020+
}
1021+
}
1022+
// Trim the newline
1023+
_serialInput.trim();
1024+
1025+
// Print out the received JSON string
1026+
// TODO: REMOVE this for the PR
1027+
WS_DEBUG_PRINT("[SD][Debug] JSON string received!");
1028+
if (_use_test_data) {
1029+
WS_DEBUG_PRINTLN("[from json test data]");
1030+
WS_DEBUG_PRINTLN(json_test_data);
1031+
} else {
1032+
WS_DEBUG_PRINTLN(_serialInput);
1033+
}
1034+
1035+
WS_DEBUG_PRINTLN("[SD] JSON string received!");
1036+
}
1037+
#endif

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ class ws_sdcard {
5353
bool parseConfigFile();
5454
bool CreateNewLogFile();
5555
bool isModeOffline() { return is_mode_offline; }
56+
void waitForSerialConfig();
5657
bool LogGPIOSensorEventToSD(uint8_t pin, float value,
5758
wippersnapper_sensor_SensorType read_type);
5859
bool LogGPIOSensorEventToSD(uint8_t pin, bool value,
5960
wippersnapper_sensor_SensorType read_type);
6061
bool LogGPIOSensorEventToSD(uint8_t pin, uint16_t value,
6162
wippersnapper_sensor_SensorType read_type);
6263
bool LogDS18xSensorEventToSD(wippersnapper_ds18x20_Ds18x20Event *event_msg);
63-
6464
private:
6565
void calculateFileLimits();
6666
bool ValidateChecksum(JsonDocument &doc);

tests/bin/offline/firmware.elf

-55.7 KB
Binary file not shown.

tests/scenarios/offline/test-invalid-json.scenario.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ steps:
66
- wait-serial: '[SD] Waiting for incoming JSON string...'
77
- write-serial: '{"exportVersion":"1.0.0",'
88
- write-serial: '\n'
9-
- wait-serial: '[SD] Invalid JSON string received!'
9+
- wait-serial: '[SD] Runtime Error: Unable to deserialize config.json'

0 commit comments

Comments
 (0)