Skip to content

Commit 33d5089

Browse files
committed
clean up
1 parent 0a29dc7 commit 33d5089

File tree

3 files changed

+30
-43
lines changed

3 files changed

+30
-43
lines changed

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@ ws_sdcard::~ws_sdcard() {
4545
*/
4646
/**************************************************************************/
4747
bool ws_sdcard::InitSDCard(uint8_t pin_cs) {
48-
/* if (pin_cs == 255)
49-
return false; */
48+
/* if (pin_cs == 255)
49+
return false; */
5050
if (_sd.begin(pin_cs)) {
5151
is_mode_offline = true;
5252
return is_mode_offline;
5353
}
54-
return false;
55-
56-
54+
return false;
5755
}
5856

5957
/**************************************************************************/
@@ -390,7 +388,7 @@ bool ws_sdcard::ParseDS18X20Add(
390388
buffer, False otherwise.
391389
*/
392390
/**************************************************************************/
393-
bool ws_sdcard::PushSignalToSharedBuffer(
391+
bool ws_sdcard::AddSignalMessageToSharedBuffer(
394392
wippersnapper_signal_BrokerToDevice &msg_signal) {
395393
// Create a temporary buffer to hold the encoded signal message
396394
std::vector<uint8_t> tempBuf(512);
@@ -472,16 +470,14 @@ bool ws_sdcard::ValidateChecksum(JsonDocument &doc) {
472470
/**************************************************************************/
473471
bool ws_sdcard::parseConfigFile() {
474472
int max_json_len = 4096;
475-
476-
// Attempt to open and deserialize the JSON config file
477-
File32 file_config;
478473
DeserializationError error;
479474
JsonDocument doc;
475+
480476
#ifndef OFFLINE_MODE_DEBUG
481477
WS_DEBUG_PRINTLN("[SD] Parsing config.json...");
482-
doc = WsV2._config_doc;
478+
doc = WsV2._config_doc; // Use the config document from the filesystem
483479
#else
484-
// Test Mode - do not use the SD card, use test data instead!
480+
// Use test data rather than data from the filesystem
485481
if (!_use_test_data) {
486482
WS_DEBUG_PRINTLN("[SD] Parsing Serial Input...");
487483
WS_DEBUG_PRINT(_serialInput);
@@ -491,43 +487,43 @@ bool ws_sdcard::parseConfigFile() {
491487
error = deserializeJson(doc, json_test_data, max_json_len);
492488
}
493489
#endif
494-
// If the JSON document failed to deserialize - halt the running device and
495-
// print the error because it is not possible to continue running in offline
496-
// mode without a valid config file
490+
// It is not possible to continue running in offline mode without a valid
491+
// config file
497492
if (error) {
498493
WS_DEBUG_PRINTLN("[SD] Unable to deserialize config JSON, error code: " +
499494
String(error.c_str()));
500495
return false;
501496
}
502497
WS_DEBUG_PRINTLN("[SD] Successfully deserialized JSON config file!");
503498

504-
// Calculate the 8-bit checksum of the JSON file to ensure it is valid data
499+
// Check the file's integrity
505500
if (!ValidateChecksum(doc)) {
506501
WS_DEBUG_PRINTLN("[SD] Checksum mismatch, file has been modified from its "
507502
"original state!");
508503
}
509504
WS_DEBUG_PRINTLN("[SD] JSON checksum OK!");
510505

511-
// Parse the exportedFromDevice array
506+
// Begin parsing the JSON document
512507
JsonObject exportedFromDevice = doc["exportedFromDevice"];
513508
if (exportedFromDevice.isNull()) {
514509
WS_DEBUG_PRINTLN("[SD] FATAL Parsing error - No exportedFromDevice object "
515510
"found in JSON string! Unable to configure hardware!");
516511
return false;
517512
}
513+
JsonArray components_ar = doc["components"].as<JsonArray>();
514+
if (components_ar.isNull()) {
515+
WS_DEBUG_PRINTLN("[SD] FATAL Parsing error - No components array found in "
516+
"JSON string!");
517+
return false;
518+
}
518519

519-
// Mock the check-in process using the JSON's values
520-
WS_DEBUG_PRINTLN("[SD] Mocking check-in process...");
520+
// We don't talk to IO here, perform an "offline" device check-in
521521
CheckIn(exportedFromDevice["totalGPIOPins"] | 0,
522522
exportedFromDevice["totalAnalogPins"] | 0,
523523
exportedFromDevice["referenceVoltage"] | 0.0);
524-
525-
WS_DEBUG_PRINTLN("[SD] Configuring status LED...");
526524
setStatusLEDBrightness(exportedFromDevice["statusLEDBrightness"] | 0.3);
527525

528-
// Initialize and configure RTC
529526
#ifndef OFFLINE_MODE_WOKWI
530-
WS_DEBUG_PRINTLN("[SD] Configuring RTC...");
531527
const char *json_rtc = exportedFromDevice["rtc"] | "SOFT_RTC";
532528
if (!ConfigureRTC(json_rtc)) {
533529
WS_DEBUG_PRINTLN("[SD] Failed to to configure RTC!");
@@ -537,17 +533,6 @@ bool ws_sdcard::parseConfigFile() {
537533
WS_DEBUG_PRINTLN("[SD] Skipping RTC configuration for Wokwi Simulator...");
538534
#endif
539535

540-
// Parse the "components" array into a JsonObject
541-
WS_DEBUG_PRINTLN("[SD] Parsing out components array...");
542-
JsonArray components_ar = doc["components"].as<JsonArray>();
543-
if (components_ar.isNull()) {
544-
WS_DEBUG_PRINTLN("[SD] FATAL Parsing error - No components array found in "
545-
"JSON string!");
546-
return false;
547-
}
548-
int count = components_ar.size();
549-
WS_DEBUG_PRINTLN("[SD] Found " + String(count) + " components in JSON file!");
550-
551536
// Parse each component from JSON->PB and push into a shared buffer
552537
for (JsonObject component : doc["components"].as<JsonArray>()) {
553538
wippersnapper_signal_BrokerToDevice msg_signal_b2d =
@@ -576,14 +561,14 @@ bool ws_sdcard::parseConfigFile() {
576561
"DigitalIO component!");
577562
return false;
578563
}
564+
579565
msg_signal_b2d.which_payload =
580566
wippersnapper_signal_BrokerToDevice_digitalio_add_tag;
581567
msg_signal_b2d.payload.digitalio_add = msg_DigitalIOAdd;
582568
} else if (strcmp(component_api_type, "analogio") == 0) {
583569
WS_DEBUG_PRINTLN("[SD] AnalogIO component found, decoding JSON to PB...");
584570
wippersnapper_analogio_AnalogIOAdd msg_AnalogIOAdd =
585571
wippersnapper_analogio_AnalogIOAdd_init_default;
586-
// Parse: JSON->AnalogIOAdd
587572
if (!ParseAnalogIOAdd(msg_AnalogIOAdd,
588573
component["pinName"] | UNKNOWN_VALUE,
589574
component["period"] | 0.0,
@@ -592,14 +577,14 @@ bool ws_sdcard::parseConfigFile() {
592577
"[SD] FATAL Parsing error - Unable to parse AnalogIO component!");
593578
return false;
594579
}
580+
595581
msg_signal_b2d.which_payload =
596582
wippersnapper_signal_BrokerToDevice_analogio_add_tag;
597583
msg_signal_b2d.payload.analogio_add = msg_AnalogIOAdd;
598584
} else if (strcmp(component_api_type, "ds18x20") == 0) {
599585
WS_DEBUG_PRINTLN("[SD] Ds18x20 component found, decoding JSON to PB...");
600586
wippersnapper_ds18x20_Ds18x20Add msg_DS18X20Add =
601587
wippersnapper_ds18x20_Ds18x20Add_init_default;
602-
// Parse: JSON->DS18X20Add
603588
if (!ParseDS18X20Add(msg_DS18X20Add, component["pinName"] | UNKNOWN_VALUE,
604589
component["sensorResolution"] | 0,
605590
component["period"] | 0.0,
@@ -610,6 +595,7 @@ bool ws_sdcard::parseConfigFile() {
610595
"[SD] FATAL Parsing error - Unable to parse DS18X20 component!");
611596
return false;
612597
}
598+
613599
msg_signal_b2d.which_payload =
614600
wippersnapper_signal_BrokerToDevice_ds18x20_add_tag;
615601
msg_signal_b2d.payload.ds18x20_add = msg_DS18X20Add;
@@ -620,10 +606,10 @@ bool ws_sdcard::parseConfigFile() {
620606
return false;
621607
}
622608

623-
// Push the signal message into the shared buffer
624-
if (!PushSignalToSharedBuffer(msg_signal_b2d)) {
625-
WS_DEBUG_PRINTLN("[SD] FATAL Error - Unable to push signal message to "
626-
"shared buffer!");
609+
// App handles the signal messages, in-order
610+
if (!AddSignalMessageToSharedBuffer(msg_signal_b2d)) {
611+
WS_DEBUG_PRINTLN(
612+
"[SD] FATAL Error - Unable to add signal message to shared buffer!");
627613
return false;
628614
}
629615
}
@@ -897,6 +883,8 @@ bool ws_sdcard::LogDS18xSensorEventToSD(
897883
return true;
898884
}
899885

886+
// TODO: Do we even need to use this function anymore now that we're running
887+
// wokwi unit tests?
900888
#ifdef OFFLINE_MODE_DEBUG
901889
/**************************************************************************/
902890
/*!

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class ws_sdcard {
8282
wippersnapper_sensor_SensorType read_type);
8383
bool LogJSONDoc(JsonDocument &doc);
8484
bool
85-
PushSignalToSharedBuffer(wippersnapper_signal_BrokerToDevice &msg_signal);
85+
AddSignalMessageToSharedBuffer(wippersnapper_signal_BrokerToDevice &msg_signal);
8686
SdFat _sd; ///< SD object from Adafruit SDFat library
8787
bool is_mode_offline; ///< True if offline mode is enabled, False otherwise
8888
String _serialInput; ///< Serial input buffer

src/provisioning/tinyusb/Wippersnapper_FS_V2.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Wippersnapper_FS_V2 {
4646

4747
void initUSBMSC();
4848

49+
uint8_t GetSDCSPin();
50+
4951
bool writeFSContents();
5052
void fsHalt(String msg);
5153
void eraseCPFS();
@@ -58,9 +60,6 @@ class Wippersnapper_FS_V2 {
5860
void createSecretsFile();
5961
bool getSecretsFile();
6062
void parseSecrets();
61-
62-
uint8_t GetSDCSPin();
63-
6463
#ifdef ARDUINO_FUNHOUSE_ESP32S2
6564
void parseDisplayConfig(displayConfig &displayFile);
6665
void createDisplayConfig();

0 commit comments

Comments
 (0)