Skip to content

Commit 55c6b53

Browse files
committed
Bring over SD card review changes
1 parent c362d0b commit 55c6b53

File tree

6 files changed

+47
-90
lines changed

6 files changed

+47
-90
lines changed

generate_json_checksum.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
import json
44

55
# load json file from provided path
6-
with open(sys.argv[1], 'r') as f:
7-
json_data = json.load(f)
6+
try:
7+
with open(sys.argv[1], 'r') as f:
8+
json_data = json.load(f)
9+
except FileNotFoundError as e:
10+
print("Error: JSON file not found, exiting...")
11+
exit(1)
12+
813
if not json_data:
914
print("Error: JSON file did not contain any data or path didn't match exactly, exiting...")
1015
exit(1)
@@ -24,4 +29,4 @@
2429
json_data['checksum'] = calculated_checksum
2530
with open(sys.argv[1], 'w') as f:
2631
json.dump(json_data, f)
27-
print('SUCCESS: Checksum written to JSON file!')
32+
print('SUCCESS: Checksum written to JSON file!')

src/Wippersnapper_V2.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
#define WS_DEBUG_PRINT(...) \
3535
{ WS_PRINTER.print(__VA_ARGS__); } /**< Print debug message to serial */
3636
#define WS_DEBUG_PRINTLN(...) \
37-
{ WS_PRINTER.println(__VA_ARGS__); } /**< Print debug message with newline \
38-
*/
37+
{ \
38+
WS_PRINTER.println(__VA_ARGS__); \
39+
} /**< Print debug message with newline \
40+
*/
3941
#define WS_DEBUG_PRINTHEX(...) \
4042
{ \
4143
WS_PRINTER.print(__VA_ARGS__, HEX); \
@@ -251,7 +253,7 @@ class Wippersnapper_V2 {
251253

252254
// TODO: does this really need to be global?
253255
uint8_t _macAddrV2[6]; /*!< Unique network iface identifier */
254-
char sUIDV2[13]; /*!< Unique network iface identifier */
256+
char sUIDV2[13]; /*!< Unique hardware identifier */
255257
const char *_boardIdV2; /*!< Adafruit IO+ board string */
256258
Adafruit_MQTT *_mqttV2; /*!< Reference to Adafruit_MQTT, _mqtt. */
257259

src/Wippersnapper_demo.ino.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/components/analogIO/controller.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* please support Adafruit and open-source hardware by purchasing
88
* products from Adafruit!
99
*
10-
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
10+
* Copyright (c) Brent Rubell 2024-2025 for Adafruit Industries.
1111
*
1212
* BSD license, all text here must be included in any redistribution.
1313
*
@@ -185,21 +185,15 @@ bool AnalogIOController::EncodePublishPinEvent(
185185
}
186186

187187
// Publish the AnalogIO message to the broker
188-
if (!WsV2._sdCardV2->isModeOffline()) {
189-
WS_DEBUG_PRINTLN("Publishing AnalogIOEvent message to broker...");
190-
if (!WsV2.PublishSignal(
191-
wippersnapper_signal_DeviceToBroker_analogio_event_tag,
192-
_analogio_model->GetAnalogIOEvent())) {
193-
WS_DEBUG_PRINTLN(
194-
"ERROR: Unable to publish analogio voltage event message, "
195-
"moving onto the next pin!");
196-
return false;
197-
}
198-
WS_DEBUG_PRINTLN("Published AnalogIOEvent message to broker!")
199-
} else {
200-
201-
// TODO: Log out this data by calling a logging function in sdcard class
188+
WS_DEBUG_PRINTLN("Publishing AnalogIOEvent message to broker...");
189+
if (!WsV2.PublishSignal(
190+
wippersnapper_signal_DeviceToBroker_analogio_event_tag,
191+
_analogio_model->GetAnalogIOEvent())) {
192+
WS_DEBUG_PRINTLN("ERROR: Unable to publish analogio voltage event message, "
193+
"moving onto the next pin!");
194+
return false;
202195
}
196+
WS_DEBUG_PRINTLN("Published AnalogIOEvent message to broker!")
203197

204198
return true;
205199
}

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ ws_sdcard::ws_sdcard()
4949
/**************************************************************************/
5050
ws_sdcard::~ws_sdcard() {
5151
if (is_mode_offline) {
52-
_sd.end(); // Close the SD card
52+
_sd.end(); // Close the SD card interface
5353
}
5454
is_mode_offline = false;
5555
}
@@ -181,6 +181,7 @@ bool ws_sdcard::InitPCF8523() {
181181
*/
182182
/**************************************************************************/
183183
bool ws_sdcard::InitSoftRTC() {
184+
// NOTE: RTC_Soft always returns void
184185
_rtc_soft->begin(DateTime(F(__DATE__), F(__TIME__)));
185186
return true;
186187
}
@@ -360,7 +361,7 @@ bool ws_sdcard::ParseDigitalIOAdd(
360361
strcpy(msg_DigitalIOAdd.pin_name, pin);
361362

362363
if (period == 0.0) {
363-
WS_DEBUG_PRINTLN("[SD] Parsing Error: Digital pin period not found!");
364+
WS_DEBUG_PRINTLN("[SD] Parsing Error: Invalid pin period!");
364365
return false;
365366
}
366367
msg_DigitalIOAdd.period = period;
@@ -662,24 +663,17 @@ bool ws_sdcard::ValidateChecksum(JsonDocument &doc) {
662663
bool ws_sdcard::parseConfigFile() {
663664
DeserializationError error;
664665
JsonDocument doc;
665-
666666
delay(5000);
667-
// TODO: THIS IS JUST FOR DEBUG Testing, remove for PR review
668-
WS_DEBUG_PRINT("SD card capacity: ");
669-
WS_DEBUG_PRINTLN(_sd_capacity);
670-
WS_DEBUG_PRINT("Maximum number of log files: ");
671-
WS_DEBUG_PRINTLN(_sd_max_num_log_files);
672-
WS_DEBUG_PRINT("Maximum size of log file: ");
673-
WS_DEBUG_PRINTLN(_max_sz_log_file);
674667

668+
// Parse configuration data
675669
#ifndef OFFLINE_MODE_DEBUG
676670
WS_DEBUG_PRINTLN("[SD] Parsing config.json...");
677-
doc = WsV2._config_doc; // Use the config document from the filesystem
671+
doc = WsV2._config_doc;
678672
#else
679673
// Use test data rather than data from the filesystem
680674
if (!_use_test_data) {
681675
WS_DEBUG_PRINTLN("[SD] Parsing Serial Input...");
682-
WS_DEBUG_PRINT(_serialInput);
676+
WS_DEBUG_PRINTLN(_serialInput);
683677
error = deserializeJson(doc, _serialInput.c_str(), MAX_LEN_CFG_JSON);
684678
} else {
685679
WS_DEBUG_PRINTLN("[SD] Parsing Test Data...");
@@ -700,7 +694,7 @@ bool ws_sdcard::parseConfigFile() {
700694
WS_DEBUG_PRINTLN("[SD] Checksum mismatch, file has been modified from its "
701695
"original state!");
702696
}
703-
WS_DEBUG_PRINTLN("[SD] JSON checksum OK!");
697+
WS_DEBUG_PRINTLN("[SD] Checksum OK!");
704698

705699
// Begin parsing the JSON document
706700
JsonObject exportedFromDevice = doc["exportedFromDevice"];
@@ -722,7 +716,7 @@ bool ws_sdcard::parseConfigFile() {
722716

723717
WS_DEBUG_PRINTLN("Parsing exportedFromDevice object...");
724718

725-
// We don't talk to IO here, perform an "offline" device check-in
719+
// We don't talk to IO here, mock an "offline" device check-in
726720
CheckIn(exportedFromDevice["totalGPIOPins"] | 0,
727721
exportedFromDevice["totalAnalogPins"] | 0,
728722
exportedFromDevice["referenceVoltage"] | 0.0);
@@ -733,7 +727,6 @@ bool ws_sdcard::parseConfigFile() {
733727
setStatusLEDBrightness(exportedFromDevice["statusLEDBrightness"] | 0.3);
734728

735729
WS_DEBUG_PRINTLN("Configuring RTC...");
736-
737730
#ifndef OFFLINE_MODE_WOKWI
738731
const char *json_rtc = exportedFromDevice["rtc"] | "SOFT_RTC";
739732
WS_DEBUG_PRINT("RTC Type: ");
@@ -742,8 +735,6 @@ bool ws_sdcard::parseConfigFile() {
742735
WS_DEBUG_PRINTLN("[SD] Runtime Error: Failed to to configure RTC!");
743736
return false;
744737
}
745-
#else
746-
WS_DEBUG_PRINTLN("[SD] Did not configure RTC for Wokwi...");
747738
#endif
748739

749740
WS_DEBUG_PRINTLN("Parsing components array...");
@@ -873,7 +864,7 @@ uint32_t ws_sdcard::GetTimestamp() {
873864
@returns A string representation of the SensorType enum.
874865
*/
875866
/**************************************************************************/
876-
const char *SensorTypeToString(wippersnapper_sensor_SensorType sensorType) {
867+
const char *SensorTypeToSIUnit(wippersnapper_sensor_SensorType sensorType) {
877868
switch (sensorType) {
878869
case wippersnapper_sensor_SensorType_SENSOR_TYPE_UNSPECIFIED:
879870
return "UNSPECIFIED";
@@ -969,12 +960,10 @@ const char *SensorTypeToString(wippersnapper_sensor_SensorType sensorType) {
969960
/**************************************************************************/
970961
void ws_sdcard::BuildJSONDoc(JsonDocument &doc, uint8_t pin, float value,
971962
wippersnapper_sensor_SensorType read_type) {
972-
char pin_name[12];
973-
sprintf(pin_name, "A%d", pin);
974963
doc["timestamp"] = GetTimestamp();
975-
doc["pin"] = pin_name;
964+
doc["pin"] = "A" + String(pin);
976965
doc["value"] = value;
977-
doc["si_unit"] = SensorTypeToString(read_type);
966+
doc["si_unit"] = SensorTypeToSIUnit(read_type);
978967
}
979968

980969
/**************************************************************************/
@@ -992,12 +981,10 @@ void ws_sdcard::BuildJSONDoc(JsonDocument &doc, uint8_t pin, float value,
992981
/**************************************************************************/
993982
void ws_sdcard::BuildJSONDoc(JsonDocument &doc, uint8_t pin, uint16_t value,
994983
wippersnapper_sensor_SensorType read_type) {
995-
char pin_name[12];
996-
sprintf(pin_name, "A%d", pin);
997984
doc["timestamp"] = GetTimestamp();
998-
doc["pin"] = pin_name;
985+
doc["pin"] = "A" + String(pin);
999986
doc["value"] = value;
1000-
doc["si_unit"] = SensorTypeToString(read_type);
987+
doc["si_unit"] = SensorTypeToSIUnit(read_type);
1001988
}
1002989

1003990
/**************************************************************************/
@@ -1015,12 +1002,10 @@ void ws_sdcard::BuildJSONDoc(JsonDocument &doc, uint8_t pin, uint16_t value,
10151002
/**************************************************************************/
10161003
void ws_sdcard::BuildJSONDoc(JsonDocument &doc, uint8_t pin, bool value,
10171004
wippersnapper_sensor_SensorType read_type) {
1018-
char pin_name[12];
1019-
sprintf(pin_name, "D%d", pin);
10201005
doc["timestamp"] = GetTimestamp();
1021-
doc["pin"] = pin_name;
1006+
doc["pin"] = "D" + String(pin);
10221007
doc["value"] = value;
1023-
doc["si_unit"] = SensorTypeToString(read_type);
1008+
doc["si_unit"] = SensorTypeToSIUnit(read_type);
10241009
}
10251010

10261011
/**************************************************************************/
@@ -1154,7 +1139,7 @@ bool ws_sdcard::LogDS18xSensorEventToSD(
11541139
doc["timestamp"] = timestamp;
11551140
doc["pin"] = event_msg->onewire_pin;
11561141
doc["value"] = event_msg->sensor_events[i].value.float_value;
1157-
doc["si_unit"] = SensorTypeToString(event_msg->sensor_events[i].type);
1142+
doc["si_unit"] = SensorTypeToSIUnit(event_msg->sensor_events[i].type);
11581143
LogJSONDoc(doc);
11591144
}
11601145
return true;
@@ -1190,7 +1175,7 @@ bool ws_sdcard::LogI2cDeviceEvent(
11901175
doc["timestamp"] = GetTimestamp();
11911176
doc["value"] = msg_device_event->i2c_device_events[i].value.float_value;
11921177
doc["si_unit"] =
1193-
SensorTypeToString(msg_device_event->i2c_device_events[i].type);
1178+
SensorTypeToSIUnit(msg_device_event->i2c_device_events[i].type);
11941179
if (!LogJSONDoc(doc))
11951180
return false;
11961181
}
@@ -1286,25 +1271,13 @@ void ws_sdcard::waitForSerialConfig() {
12861271
char c = Serial.read();
12871272
_serialInput += c;
12881273
if (_serialInput.endsWith("\\n")) {
1289-
WS_DEBUG_PRINTLN("[SD] End of JSON string detected!");
12901274
break;
12911275
}
12921276
}
12931277
}
12941278
}
1295-
// Trim the newline
1279+
// Remove the newline
12961280
_serialInput.trim();
1297-
1298-
// Print out the received JSON string
1299-
// TODO: REMOVE this for the PR
1300-
WS_DEBUG_PRINT("[SD][Debug] JSON string received!");
1301-
if (_use_test_data) {
1302-
WS_DEBUG_PRINTLN("[from json test data]");
1303-
WS_DEBUG_PRINTLN(json_test_data);
1304-
} else {
1305-
WS_DEBUG_PRINTLN(_serialInput);
1306-
}
1307-
13081281
WS_DEBUG_PRINTLN("[SD] JSON string received!");
13091282
}
13101283
#endif

src/provisioning/tinyusb/Wippersnapper_FS.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ Adafruit_USBD_MSC usb_msc_v2; /*!< USB mass storage object */
6767
FRESULT format_fs_fat12(void) {
6868
FATFS elmchamFatfs_v2;
6969
uint8_t workbuf_v2[4096];
70+
FRESULT r;
7071

7172
// make filesystem
72-
FRESULT r = f_mkfs("", FM_FAT | FM_SFD, 0, workbuf_v2, sizeof(workbuf_v2));
73+
r = f_mkfs("", FM_FAT | FM_SFD, 0, workbuf_v2, sizeof(workbuf_v2));
74+
if (r != FR_OK)
75+
return r;
7376

7477
// mount to set disk label
7578
r = f_mount(&elmchamFatfs_v2, "0:", 1);
@@ -82,7 +85,9 @@ FRESULT format_fs_fat12(void) {
8285
return r;
8386

8487
// unmount fs
85-
f_unmount("0:");
88+
r = f_unmount("0:");
89+
if (r != FR_OK)
90+
return r;
8691

8792
// sync to make sure all data is written to flash
8893
flash_v2.syncBlocks();

0 commit comments

Comments
 (0)