Skip to content

Commit d97e430

Browse files
committed
[SD] Add offline analog input scenario
1 parent 5b5463c commit d97e430

File tree

5 files changed

+80
-44
lines changed

5 files changed

+80
-44
lines changed

src/components/analogIO/controller.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
9292

9393
// Print out the pin's details
9494
WS_DEBUG_PRINTLN("[analogio] Added new pin:");
95-
WS_DEBUG_PRINT("\tPin Name: ");
95+
WS_DEBUG_PRINT("Pin Name: ");
9696
WS_DEBUG_PRINTLN(new_pin.name);
97-
WS_DEBUG_PRINT("\tPeriod: ");
97+
WS_DEBUG_PRINT("Period: ");
9898
WS_DEBUG_PRINTLN(new_pin.period);
99-
WS_DEBUG_PRINT("\tRead Mode: ");
99+
WS_DEBUG_PRINT("Read Mode: ");
100100
WS_DEBUG_PRINTLN(new_pin.read_mode);
101101

102102
// Add the new pin to the vector

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/**************************************************************************/
2222
ws_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("\tPin Name: " + String(msg_DigitalIOAdd.pin_name));
239-
WS_DEBUG_PRINTLN("\tDirection: " +
240-
String(msg_DigitalIOAdd.gpio_direction));
241-
WS_DEBUG_PRINTLN("\tSample Mode: " +
242-
String(msg_DigitalIOAdd.sample_mode));
243-
WS_DEBUG_PRINTLN("\tPeriod: " + String(msg_DigitalIOAdd.period));
244-
WS_DEBUG_PRINTLN("\tValue: " + 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("\tPin Name: " + String(msg_AnalogIOAdd.pin_name));
276-
WS_DEBUG_PRINTLN("\tPeriod: " + String(msg_AnalogIOAdd.period));
277-
WS_DEBUG_PRINTLN("\tRead 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

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ws_sdcard {
5959
const char *json_test_data; ///< Json test data
6060
bool _use_test_data; ///< True if sample data is being used to test, instead
6161
///< of serial input, False otherwise.
62+
bool _wokwi_runner; ///< True if `exportedBy` key is "wokwi", otherwise False
6263
RTC_DS3231 *_rtc_ds3231 = nullptr; ///< DS3231 RTC object
6364
RTC_DS1307 *_rtc_ds1307 = nullptr; ///< DS1307 RTC object
6465
RTC_PCF8523 *_rtc_pcf8523 = nullptr; ///< PCF8523 RTC object
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Offline Mode - Analog Input
2+
version: 1
3+
author: Brent Rubell for Adafruit Industries
4+
5+
steps:
6+
- wait-serial: '[SD] Waiting for incoming JSON string...'
7+
- write-serial: '{"exportVersion":"1.0.0","exportedBy":"wokwi","exportedAt":"2024-10-28T18:58:23.976Z","exportedFromDevice":{"board":"metroesp32s3","firmwareVersion":"1.0.0-beta.93","referenceVoltage":2.6,"totalGPIOPins":11,"totalAnalogPins":6},"components":[{"componentAPI":"analogio","name":"Analog Pin","pinName":"D14","type":"analog_pin","mode":"ANALOG","direction":"INPUT","sampleMode":"TIMER","analogReadMode":"PIN_VALUE","period":5,"isPin":true}]}'
8+
- write-serial: '\n'
9+
- wait-serial: '[SD] Valid JSON string received!'
10+
- wait-serial: '[analogio] Added new pin:'
11+
- wait-serial: 'Pin Name: 14'
12+
- wait-serial: 'Period: 5000.00'
13+
- wait-serial: 'Read Mode: 18'
14+
- delay: 5s
15+
- wait-serial: '{"timestamp":0,"pin":"A14","value":0,"si_unit":"RAW"}'
16+
- set-control:
17+
part-id: pot1
18+
control: position
19+
value: 0.5
20+
- delay: 3s
21+
- wait-serial: '{"timestamp":0,"pin":"A14","value":16384,"si_unit":"RAW"}'

tests/tests/test_sd_read_json.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Offline Mode - Analog Input
2+
version: 1
3+
author: Brent Rubell for Adafruit Industries
4+
5+
steps:
6+
- wait-serial: '[SD] Waiting for incoming JSON string...'
7+
- write-serial: '{"exportVersion":"1.0.0","exportedBy":"wokwi","exportedAt":"2024-10-28T18:58:23.976Z","exportedFromDevice":{"board":"metroesp32s3","firmwareVersion":"1.0.0-beta.93","referenceVoltage":2.6,"totalGPIOPins":11,"totalAnalogPins":6},"components":[{"componentAPI":"analogio","name":"Analog Pin","pinName":"D14","type":"analog_pin","mode":"ANALOG","direction":"INPUT","sampleMode":"TIMER","analogReadMode":"PIN_VALUE","period":5,"isPin":true}]}'
8+
- write-serial: '\n'
9+
- wait-serial: '[SD] Valid JSON string received!'
10+
- wait-serial: '[analogio] Added new pin:'
11+
- wait-serial: 'Pin Name: 14'
12+
- wait-serial: 'Period: 5000.00'
13+
- wait-serial: 'Read Mode: 18'
14+
- delay: 5s
15+
- wait-serial: '{"timestamp":0,"pin":"A14","value":0,"si_unit":"RAW"}'
16+
- set-control:
17+
part-id: pot1
18+
control: position
19+
value: 0.5
20+
- delay: 5s
21+
- wait-serial: '{"timestamp":0,"pin":"A14","value":0,"si_unit":"RAW"}'
22+

0 commit comments

Comments
 (0)