Skip to content

Commit 4d9bed8

Browse files
committed
[SD] Refactor sd header and clean it up
1 parent 52a8bd7 commit 4d9bed8

File tree

6 files changed

+81
-74
lines changed

6 files changed

+81
-74
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ void Wippersnapper_V2::connectV2() {
11881188
// If we are running in offline mode, we skip the network setup
11891189
// and MQTT connection process and jump to the offline device config process
11901190
// NOTE: After this, bail out of this function and run the app loop!!!
1191-
if (WsV2._sdCardV2->mode_offline == true) {
1191+
if (WsV2._sdCardV2->isModeOffline() == true) {
11921192
WS_DEBUG_PRINTLN("[Offline] Running device configuration...");
11931193
// Wait for incoming JSON string from Serial
11941194
#ifdef OFFLINE_MODE_DEBUG
@@ -1199,14 +1199,13 @@ void Wippersnapper_V2::connectV2() {
11991199
if (!WsV2._sdCardV2->parseConfigFile())
12001200
haltErrorV2("Failed to parse incoming JSON file");
12011201
WS_DEBUG_PRINT("[Offline] Attempting to configure hardware...");
1202-
// Mock the "check in" process
1203-
// Configure GPIO classes based on checkin response message
1204-
// TODO: Add checkin pins/data to the JSON string, we are hardcoding here
1205-
WsV2.digital_io_controller->SetMaxDigitalPins(10);
1206-
WsV2.analogio_controller->SetRefVoltage(3.3);
1207-
WsV2.analogio_controller->SetTotalAnalogPins(4);
1208-
// Call the signal decoder to parse the incoming JSON string
1209-
// TODO: We need a way to repeat this call better
1202+
#ifndef OFFLINE_MODE_DEBUG
1203+
// Create a new file to store the json log
1204+
if (!WsV2._sdCardV2->CreateNewLogFile()) {
1205+
haltErrorV2("Unable to create log file");
1206+
}
1207+
#endif
1208+
// Call the TL signal decoder to parse the incoming JSON data
12101209
callDecodeB2D();
12111210
WS_DEBUG_PRINTLN(
12121211
"[Offline] Hardware configured, skipping network setup...");
@@ -1269,7 +1268,7 @@ void Wippersnapper_V2::connectV2() {
12691268
/**************************************************************************/
12701269
ws_status_t Wippersnapper_V2::runV2() {
12711270
WsV2.feedWDTV2();
1272-
if (!WsV2._sdCardV2->mode_offline) {
1271+
if (!WsV2._sdCardV2->isModeOffline()) {
12731272
// Handle networking functions
12741273
runNetFSMV2();
12751274
pingBrokerV2();

src/components/analogIO/controller.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ bool AnalogIOController::EncodePublishPinEvent(
185185
}
186186

187187
// Publish the AnalogIO message to the broker
188-
if (!WsV2._sdCardV2->mode_offline) {
188+
if (!WsV2._sdCardV2->isModeOffline()) {
189189
WS_DEBUG_PRINTLN("Publishing AnalogIOEvent message to broker...");
190190
if (!WsV2.PublishSignal(
191191
wippersnapper_signal_DeviceToBroker_analogio_event_tag,
@@ -225,7 +225,7 @@ bool AnalogIOController::EncodePublishPinEvent(
225225
/***************************************************************************/
226226
bool AnalogIOController::EncodePublishPinValue(uint8_t pin, uint16_t value) {
227227

228-
if (WsV2._sdCardV2->mode_offline) {
228+
if (WsV2._sdCardV2->isModeOffline()) {
229229
return WsV2._sdCardV2->LogGPIOSensorEventToSD(
230230
pin, value, wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW);
231231
} else {
@@ -246,7 +246,7 @@ bool AnalogIOController::EncodePublishPinValue(uint8_t pin, uint16_t value) {
246246
*/
247247
/***************************************************************************/
248248
bool AnalogIOController::EncodePublishPinVoltage(uint8_t pin, float value) {
249-
if (WsV2._sdCardV2->mode_offline) {
249+
if (WsV2._sdCardV2->isModeOffline()) {
250250
return WsV2._sdCardV2->LogGPIOSensorEventToSD(
251251
pin, value, wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE);
252252
} else {

src/components/digitalIO/controller.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ bool DigitalIOController::IsPinTimerExpired(DigitalIOPin *pin, ulong cur_time) {
240240
*/
241241
/***********************************************************************/
242242
void DigitalIOController::PrintPinValue(DigitalIOPin *pin) {
243-
if (WsV2._sdCardV2->mode_offline)
243+
if (WsV2._sdCardV2->isModeOffline())
244244
return;
245245
WS_DEBUG_PRINT("[digitalio] DIO Pin D");
246246
WS_DEBUG_PRINT(pin->pin_name);
@@ -308,7 +308,7 @@ bool DigitalIOController::EncodePublishPinEvent(uint8_t pin_name,
308308
sprintf(c_pin_name, "D%d", pin_name);
309309

310310
// If we are in ONLINE mode, publish the event to the broker
311-
if (!WsV2._sdCardV2->mode_offline) {
311+
if (!WsV2._sdCardV2->isModeOffline()) {
312312
WS_DEBUG_PRINT(
313313
"[digitalio] Publishing DigitalIOEvent message to broker for pin: ");
314314
WS_DEBUG_PRINTLN(c_pin_name);

src/components/ds18x20/controller.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
104104

105105
// If we're not in offline mode, publish a Ds18x20Added message back to the
106106
// broker
107-
if (!WsV2._sdCardV2->mode_offline) {
107+
if (!WsV2._sdCardV2->isModeOffline()) {
108108
// Encode and publish a Ds18x20Added message back to the broker
109109
if (!_DS18X20_model->EncodeDS18x20Added(
110110
_DS18X20_model->GetDS18x20AddMsg()->onewire_pin, is_initialized)) {
@@ -227,7 +227,7 @@ void DS18X20Controller::update() {
227227
_DS18X20_model->GetDS18x20EventMsg();
228228
pb_size_t event_count = event_msg->sensor_events_count;
229229

230-
if (!WsV2._sdCardV2->mode_offline) {
230+
if (!WsV2._sdCardV2->isModeOffline()) {
231231
// Encode the Ds18x20Event message
232232
if (!_DS18X20_model->EncodeDs18x20Event()) {
233233
WS_DEBUG_PRINTLN(

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
*/
2121
/**************************************************************************/
2222
ws_sdcard::ws_sdcard() {
23-
mode_offline = false;
24-
_wokwi_runner = false;
23+
is_mode_offline = false;
24+
_is_using_wokwi = false;
2525
_use_test_data = false;
26+
_sz_log_file = 0;
2627
}
2728

2829
/**************************************************************************/
@@ -33,20 +34,20 @@ ws_sdcard::ws_sdcard() {
3334
ws_sdcard::~ws_sdcard() {
3435
// TODO: Close any open files
3536
// Then, end the SD card (ends SPI transaction)
36-
if (mode_offline) {
37+
if (is_mode_offline) {
3738
_sd.end();
38-
mode_offline = false;
39+
is_mode_offline = false;
3940
}
4041
}
4142

4243
bool ws_sdcard::InitSDCard() {
4344
#ifdef SD_CS_PIN
4445
// Attempt to initialize the SD card
4546
if (_sd.begin(SD_CS_PIN)) {
46-
mode_offline = true;
47+
is_mode_offline = true;
4748
}
4849
#endif
49-
return mode_offline;
50+
return is_mode_offline;
5051
}
5152

5253
/**************************************************************************/
@@ -346,6 +347,25 @@ bool ws_sdcard::PushSignalToSharedBuffer(
346347
return true;
347348
}
348349

350+
/**************************************************************************/
351+
/*!
352+
@brief Creates a new logging file on the SD card using the RTC's
353+
timestamp and sets the current log file path to reflect this
354+
file.
355+
@returns True if a log file was successfully created, False otherwise.
356+
*/
357+
/**************************************************************************/
358+
bool ws_sdcard::CreateNewLogFile() {
359+
File32 file;
360+
String logFilename = "log_" + String(GetTimestamp()) + ".json";
361+
_log_filename = logFilename.c_str();
362+
if (!file.open(_log_filename, FILE_WRITE))
363+
return false;
364+
WS_DEBUG_PRINT("[SD] Created new log file on SD card: ");
365+
WS_DEBUG_PRINTLN(_log_filename);
366+
return true;
367+
}
368+
349369
/**************************************************************************/
350370
/*!
351371
@brief Searches for and parses the JSON configuration file and sets up
@@ -395,7 +415,7 @@ bool ws_sdcard::parseConfigFile() {
395415
// this!
396416
const char *exportedBy = doc["exportedBy"];
397417
if (strcmp(exportedBy, "wokwi") == 0) {
398-
_wokwi_runner = true;
418+
_is_using_wokwi = true;
399419
}
400420

401421
// Parse the exportedFromDevice array
@@ -425,23 +445,6 @@ bool ws_sdcard::parseConfigFile() {
425445
return false;
426446
}
427447

428-
// Create new logging file on device from the RTC's timestamp
429-
#ifndef OFFLINE_MODE_DEBUG
430-
// TODO: Refactor this out into a func
431-
// TODO: Implement a counter within the log funcs to track # of lines in the
432-
// file and implement a MAX_LINE cutoff
433-
String logFilename = "log_" + String(GetTimestamp()) + ".json";
434-
_log_filename = logFilename.c_str();
435-
File32 file;
436-
if (!file.open(_log_filename, FILE_WRITE)) {
437-
WS_DEBUG_PRINTLN(
438-
"[SD] FATAL - Failed to create initial logging file on SD card!");
439-
return false;
440-
}
441-
WS_DEBUG_PRINT("[SD] Created new log file on SD card: ");
442-
WS_DEBUG_PRINTLN(_log_filename);
443-
#endif
444-
445448
// Parse the "components" array into a JsonObject
446449
JsonArray components_ar = doc["components"].as<JsonArray>();
447450
if (components_ar.isNull()) {
@@ -548,7 +551,7 @@ uint32_t ws_sdcard::GetTimestamp() {
548551
now = _rtc_soft->now();
549552
}
550553

551-
if (_wokwi_runner)
554+
if (_is_using_wokwi)
552555
return 0;
553556

554557
return now.unixtime();

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "Wippersnapper_V2.h"
2121

2222
#define SD_FAT_TYPE 3
23+
#define MAX_LOG_FILE_SZ 500 ///< Maximum log file size of 500 bytes
2324

2425
// forward decl.
2526
class Wippersnapper_V2;
@@ -35,13 +36,32 @@ class ws_sdcard {
3536
ws_sdcard();
3637
~ws_sdcard();
3738
bool InitSDCard();
39+
bool parseConfigFile();
40+
#ifdef OFFLINE_MODE_DEBUG
41+
bool waitForSerialConfig();
42+
#endif
43+
bool CreateNewLogFile();
44+
bool isModeOffline() { return is_mode_offline; }
45+
46+
bool LogGPIOSensorEventToSD(uint8_t pin, float value,
47+
wippersnapper_sensor_SensorType read_type);
48+
bool LogGPIOSensorEventToSD(uint8_t pin, bool value,
49+
wippersnapper_sensor_SensorType read_type);
50+
bool LogGPIOSensorEventToSD(uint8_t pin, uint16_t value,
51+
wippersnapper_sensor_SensorType read_type);
52+
bool LogDS18xSensorEventToSD(wippersnapper_ds18x20_Ds18x20Event *event_msg);
53+
54+
private:
3855
bool ConfigureRTC(const char *rtc_type);
3956
uint32_t GetTimestamp();
40-
bool mode_offline; // TODO: Refactor to getter/setter
41-
bool
42-
PushSignalToSharedBuffer(wippersnapper_signal_BrokerToDevice &msg_signal);
57+
bool InitDS1307();
58+
bool InitDS3231();
59+
bool InitPCF8523();
60+
bool InitSoftRTC();
61+
bool validateJson(const char *input);
62+
void CheckIn(uint8_t max_digital_pins, uint8_t max_analog_pins,
63+
float ref_voltage);
4364

44-
bool parseConfigFile();
4565
wippersnapper_sensor_SensorType ParseSensorType(const char *sensor_type);
4666
bool ParseDigitalIOAdd(wippersnapper_digitalio_DigitalIOAdd &msg_DigitalIOAdd,
4767
const char *pin, float period, bool value,
@@ -60,36 +80,21 @@ class ws_sdcard {
6080
wippersnapper_sensor_SensorType read_type);
6181
void BuildJSONDoc(JsonDocument &doc, const char *pin, bool value,
6282
wippersnapper_sensor_SensorType read_type);
63-
bool LogGPIOSensorEventToSD(uint8_t pin, float value,
64-
wippersnapper_sensor_SensorType read_type);
65-
bool LogGPIOSensorEventToSD(uint8_t pin, bool value,
66-
wippersnapper_sensor_SensorType read_type);
67-
bool LogGPIOSensorEventToSD(uint8_t pin, uint16_t value,
68-
wippersnapper_sensor_SensorType read_type);
69-
bool LogDS18xSensorEventToSD(wippersnapper_ds18x20_Ds18x20Event *event_msg);
70-
71-
#ifdef OFFLINE_MODE_DEBUG
72-
bool waitForSerialConfig();
73-
bool validateJson(const char *input);
74-
#endif
75-
private:
76-
bool InitDS1307();
77-
bool InitDS3231();
78-
bool InitPCF8523();
79-
bool InitSoftRTC();
80-
void CheckIn(uint8_t max_digital_pins, uint8_t max_analog_pins,
81-
float ref_voltage);
82-
SdFat _sd; ///< SD object from Adafruit SDFat library
83-
String _serialInput; ///< Serial input buffer
84-
const char *json_test_data; ///< Json test data
85-
bool _use_test_data; ///< True if sample data is being used to test, instead
86-
///< of serial input, False otherwise.
87-
bool _wokwi_runner; ///< True if `exportedBy` key is "wokwi", otherwise False
88-
const char *_log_filename; ///< Path to the log file
89-
RTC_DS3231 *_rtc_ds3231 = nullptr; ///< DS3231 RTC object
90-
RTC_DS1307 *_rtc_ds1307 = nullptr; ///< DS1307 RTC object
83+
bool
84+
PushSignalToSharedBuffer(wippersnapper_signal_BrokerToDevice &msg_signal);
85+
SdFat _sd; ///< SD object from Adafruit SDFat library
86+
bool is_mode_offline; ///< True if offline mode is enabled, False otherwise
87+
String _serialInput; ///< Serial input buffer
88+
const char *json_test_data; ///< Json test data
89+
const char *_log_filename; ///< Path to the log file
90+
int _sz_log_file; ///< Size of the current log file, in Bytes
91+
RTC_DS3231 *_rtc_ds3231 = nullptr; ///< DS3231 RTC object
92+
RTC_DS1307 *_rtc_ds1307 = nullptr; ///< DS1307 RTC object
9193
RTC_PCF8523 *_rtc_pcf8523 = nullptr; ///< PCF8523 RTC object
9294
RTC_Millis *_rtc_soft = nullptr; ///< Software RTC object
95+
// Testing
96+
bool _use_test_data; ///< True if sample data is being used for testing
97+
bool _is_using_wokwi; ///< True if `exportedBy` key is "wokwi"
9398
};
9499
extern Wippersnapper_V2 WsV2;
95100
#endif // WS_SDCARD_H

0 commit comments

Comments
 (0)