2121/* *************************************************************************/
2222ws_sdcard::ws_sdcard () {
2323 mode_offline = false ;
24+ _wokwi_runner = false ;
2425#ifndef SD_CS_PIN
2526 return ;
2627#endif
@@ -129,7 +130,6 @@ bool ws_sdcard::parseConfigFile() {
129130 // Attempt to de-serialize the JSON document
130131 DeserializationError error;
131132#ifdef OFFLINE_MODE_DEBUG
132- _use_test_data = true ; // TODO: This should be global
133133 if (!_use_test_data) {
134134 // Read the config file from the serial input buffer
135135 WS_DEBUG_PRINTLN (" [SD] Reading JSON config file..." );
@@ -193,6 +193,12 @@ bool ws_sdcard::parseConfigFile() {
193193 return false ;
194194 }
195195
196+ // This is enabled for wokwi-cli testing only
197+ const char *exportedBy = doc[" exportedBy" ];
198+ if (strcmp (exportedBy, " wokwi" ) == 0 ) {
199+ _wokwi_runner = true ;
200+ }
201+
196202 // Determine the component type and parse it into a PB message
197203 if (strcmp (component_api_type, " digitalio" ) == 0 ) {
198204 WS_DEBUG_PRINTLN (
@@ -232,31 +238,17 @@ bool ws_sdcard::parseConfigFile() {
232238 String (component[" direction" ]));
233239 return false ;
234240 }
235-
236- // Print out the contents of the DigitalIOAdd message
237- WS_DEBUG_PRINTLN (" [SD] DigitalIOAdd message:" );
238- WS_DEBUG_PRINTLN (" \t Pin Name: " + String (msg_DigitalIOAdd.pin_name ));
239- WS_DEBUG_PRINTLN (" \t Direction: " +
240- String (msg_DigitalIOAdd.gpio_direction ));
241- WS_DEBUG_PRINTLN (" \t Sample Mode: " +
242- String (msg_DigitalIOAdd.sample_mode ));
243- WS_DEBUG_PRINTLN (" \t Period: " + String (msg_DigitalIOAdd.period ));
244- WS_DEBUG_PRINTLN (" \t Value: " + String (msg_DigitalIOAdd.value ));
245-
246- // Create a new signal message
241+
247242 msg_signal_b2d = wippersnapper_signal_BrokerToDevice_init_zero;
248243 msg_signal_b2d.which_payload =
249244 wippersnapper_signal_BrokerToDevice_digitalio_add_tag;
250245 msg_signal_b2d.payload .digitalio_add = msg_DigitalIOAdd;
251246 } else if (strcmp (component_api_type, " analogio" ) == 0 ) {
252247 WS_DEBUG_PRINTLN (" [SD] AnalogIO component found, decoding JSON to PB..." );
253- // Parse the AnalogIOAdd message
254248 wippersnapper_analogio_AnalogIOAdd msg_AnalogIOAdd =
255249 wippersnapper_analogio_AnalogIOAdd_init_default;
256- // Fill in the AnalogIOAdd message
257250 strcpy (msg_AnalogIOAdd.pin_name , component[" pinName" ]);
258251 msg_AnalogIOAdd.period = component[" period" ];
259- // Parse the analog pin's read mode
260252 if (strcmp (component[" analogReadMode" ], " PIN_VALUE" ) == 0 ) {
261253 msg_AnalogIOAdd.read_mode =
262254 wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW;
@@ -270,15 +262,7 @@ bool ws_sdcard::parseConfigFile() {
270262 return false ;
271263 }
272264
273- // Print out the contents of the AnalogIOAdd message
274- WS_DEBUG_PRINTLN (" [SD] AnalogIOAdd message:" );
275- WS_DEBUG_PRINTLN (" \t Pin Name: " + String (msg_AnalogIOAdd.pin_name ));
276- WS_DEBUG_PRINTLN (" \t Period: " + String (msg_AnalogIOAdd.period ));
277- WS_DEBUG_PRINTLN (" \t Read Mode: " + String (msg_AnalogIOAdd.read_mode ));
278-
279- // Create a new signal message
280265 msg_signal_b2d = wippersnapper_signal_BrokerToDevice_init_zero;
281- // . Fill the signal message with msg_AnalogIOAdd data
282266 msg_signal_b2d.which_payload =
283267 wippersnapper_signal_BrokerToDevice_analogio_add_tag;
284268 msg_signal_b2d.payload .analogio_add = msg_AnalogIOAdd;
@@ -432,7 +416,7 @@ bool ws_sdcard::waitForSerialConfig() {
432416 // 2. Provide a JSON string via the hardware's serial input
433417 // 3. Use a test JSON string - for debugging purposes ONLY
434418
435- _use_test_data = true ;
419+ _use_test_data = false ;
436420 json_test_data = " {"
437421 " \" exportVersion\" : \" 1.0.0\" ,"
438422 " \" exportedBy\" : \" tester\" ,"
@@ -505,31 +489,35 @@ bool ws_sdcard::waitForSerialConfig() {
505489 " }\\ n\r\n " ;
506490
507491 _serialInput = " " ; // Clear the serial input buffer
508- if (!_use_test_data) {
492+ if (!_use_test_data) {
509493 WS_DEBUG_PRINTLN (" [SD] Waiting for incoming JSON string..." );
510494 while (true ) {
511- // Check if there is data available to read
512- if (Serial.available () > 0 ) {
513- // Read and append to _serialInput
514- char c = Serial.read ();
515- _serialInput += c;
516- // Check for EoL or end of JSON string
517- // Read the TODO/Note below!
518- // NOTE: This is checking for a \n delimeter from the serial
519- // and that wont be present in non-serial application
520- // Parse JSON normally if not using serial and inspect this condition!
521- if (c == ' \n ' ) {
522- break ;
495+ // Check if there is data available to read
496+ if (Serial.available () > 0 ) {
497+ // Read and append to _serialInput
498+ char c = Serial.read ();
499+ _serialInput += c;
500+
501+ // DEBUG - Check JSON output as an Int and total output
502+ // WS_DEBUG_PRINT("[SD] Character read: ");
503+ // WS_DEBUG_PRINTLN((int)c);
504+ // WS_DEBUG_PRINTLN(_serialInput);
505+
506+ // Check for end of JSON string using \n sequence
507+ if (_serialInput.endsWith (" \\ n" )) {
508+ WS_DEBUG_PRINTLN (" [SD] End of JSON string detected!" );
509+ break ;
510+ }
523511 }
524- }
525512 }
526- }
513+ }
514+
527515
528516 // Strip the '\n' off the end of _serialInput
529517 _serialInput.trim ();
530518
531519 // Print out the received JSON string
532- WS_DEBUG_PRINT (" [SD][Debug] JSON string received: " );
520+ WS_DEBUG_PRINT (" [SD][Debug] JSON string received! " );
533521 if (_use_test_data) {
534522 WS_DEBUG_PRINTLN (" [from json test data]" );
535523 WS_DEBUG_PRINTLN (json_test_data);
@@ -572,6 +560,10 @@ uint32_t ws_sdcard::GetTimestamp() {
572560 else {
573561 // TODO! implement software millis() version of now() and unixtime()
574562 }
563+
564+ if (_wokwi_runner)
565+ return 0 ;
566+
575567 return now.unixtime ();
576568}
577569
0 commit comments