Skip to content

Commit b47ecf6

Browse files
committed
test sketch now compatible with platformio and arduino IDE
1 parent 7ad9da4 commit b47ecf6

File tree

8 files changed

+389
-269
lines changed

8 files changed

+389
-269
lines changed

examples/test/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.pio

examples/test/include/README

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

examples/test/lib/README

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

examples/test/platformio.ini

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[platformio]
2+
description = YAMLDuino test sketch for platformio
3+
default_envs = esp32_2_0_5
4+
src_dir = src
5+
6+
7+
[env]
8+
framework = arduino
9+
lib_deps =
10+
bblanchon/ArduinoJson @ ^6
11+
tobozo/YAMLDuino @ ^1.2
12+
lib_ldf_mode = deep
13+
14+
15+
[env:esp32_2_0_5]
16+
board = esp32dev
17+
; using tasmota platform instead of the official to ensure all latest patches are applied
18+
platform = https://github.com/tasmota/platform-espressif32
19+
; using package 2.0.5 as eth_* support is instable before this version
20+
platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32/releases/download/2.0.5/esp32-2.0.5.zip

examples/test/src/test.cpp

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
#include <ArduinoJson.h> // optional
2+
//#include <cJSON.h> // implicit with esp32, otherwise optional
3+
#include <YAMLDuino.h>
4+
5+
6+
// sorry about the notation, but it looks nicer than chunk-splitting+quoting
7+
const char* yaml_example_str = R"_YAML_STRING_(
8+
first: "true"
9+
blah:
10+
just_a_string: "I am a string"
11+
array_of_strings:
12+
# some unindented comments
13+
- oops
14+
# more indented comments
15+
- meh
16+
same_array_of_strings: [ oops, meh ]
17+
array_mixed: [1,2,3, "soleil!"]
18+
array_of_anonymous_objects:
19+
- prop1: wizz
20+
prop2: pop
21+
prop3: snap
22+
- prop1: foo
23+
prop3: bar
24+
prop2: baz
25+
prop4: wat
26+
integer: 12345
27+
float: 12.3323
28+
inline_json_for_the_haters: { "hello":"json", "nested":[3,2,"1","moon"] }
29+
whatever:
30+
nope: ["n","o","p","e"]
31+
last: "true"
32+
33+
)_YAML_STRING_";
34+
35+
36+
const char* json_example_str = R"_JSON_STRING_(
37+
{
38+
"first": "true",
39+
"blah": {
40+
"just_a_string": "I am a string",
41+
"array_of_strings": ["oops", "meh" ],
42+
"same_array_of_strings": [ "oops", "meh" ],
43+
"array_mixed": [ 1, 2, 3, "soleil!" ],
44+
"array_of_anonymous_objects": [
45+
{
46+
"prop1": "wizz",
47+
"prop2": "pop",
48+
"prop3": "snap"
49+
},
50+
{
51+
"prop1": "foo",
52+
"prop3": "bar",
53+
"prop2": "baz",
54+
"prop4": "wat"
55+
}
56+
],
57+
"integer": 12345,
58+
"float": 12.3323,
59+
"inline_json_for_the_haters": { "hello": "json", "nested": [ 3, 2, "1", "moon" ] }
60+
},
61+
"whatever": { "nope": [ "n", "o", "p", "e" ] },
62+
"last": "true"
63+
}
64+
)_JSON_STRING_";
65+
66+
67+
void setup()
68+
{
69+
Serial.begin(115200);
70+
delay(5000);
71+
Serial.print("Welcome to the YAML Test sketch\nRam free: ");
72+
Serial.print( HEAP_AVAILABLE() );
73+
Serial.println(" bytes");
74+
75+
const size_t yaml_str_size = strlen(yaml_example_str);
76+
const size_t json_str_size = strlen(json_example_str);
77+
int test_number = 1;
78+
79+
Serial.printf("[DEBUG] YAML (in):\n%s\n\n", yaml_example_str);
80+
81+
YAMLParser::setLogLevel( YAML::LogLevelDebug ); // override sketch debug level (otherwise inherited)
82+
83+
84+
#if defined HAS_ARDUINOJSON
85+
#pragma message "Enabling ArduinoJson tests"
86+
87+
YAML_LOG_n("YAML=>JSON and JSON=>YAML using ArduinoJson\n\n");
88+
89+
#if !defined ARDUINO_ARCH_SAMD // samd with 32kb ram can oom after this, so only enable for specific test
90+
{
91+
YAML_LOG_n( "[TEST #%d] YAML stream to JsonObject -> deserializeYml(json_doc, yaml_stream):", test_number++ );
92+
DynamicJsonDocument json_doc(yaml_str_size*2);
93+
String yaml_str = String( yaml_example_str );
94+
StringStream yaml_stream( yaml_str );
95+
auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument
96+
if( err ) {
97+
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
98+
return;
99+
}
100+
const size_t bytes_out = serializeJsonPretty( json_doc, Serial ); // print deserialized JsonObject
101+
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
102+
}
103+
#endif
104+
105+
106+
{
107+
YAML_LOG_n( "[TEST #%d] YAML string to JsonObject -> deserializeYml(json_doc, yaml_example_str):", test_number++ );
108+
DynamicJsonDocument json_doc(yaml_str_size*2);
109+
auto err = deserializeYml( json_doc, yaml_example_str ); // deserialize yaml string to JsonDocument
110+
if( err ) {
111+
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
112+
return;
113+
}
114+
JsonObject json_obj = json_doc.as<JsonObject>();
115+
const size_t bytes_out = serializeJsonPretty( json_obj, Serial ); // print deserialized JsonObject
116+
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
117+
}
118+
119+
120+
121+
122+
123+
{
124+
// Convert JsonObject to yaml
125+
YAML_LOG_n( "[TEST #%d] JsonObject to YAML stream -> serializeYml(json_obj, yaml_stream_out):", test_number++ );
126+
String str_yaml_out = ""; // YAML output string
127+
StringStream yaml_stream_out( str_yaml_out ); // Stream to str_yaml_out
128+
// create and populate a JsonObject
129+
DynamicJsonDocument doc(json_str_size*2);
130+
auto err = deserializeJson( doc, json_example_str );
131+
if( err ) {
132+
YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() );
133+
return;
134+
}
135+
JsonObject json_obj = doc.as<JsonObject>();
136+
const size_t bytes_out = serializeYml( json_obj, yaml_stream_out );
137+
Serial.println( str_yaml_out );
138+
YAML_LOG_n("[JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n\n", json_str_size, bytes_out );
139+
}
140+
141+
142+
{
143+
// Convert JsonObject to yaml
144+
YAML_LOG_n( "[TEST #%d] JsonObject to YAML stream -> serializeYml(json_obj, str_yaml_out):", test_number++ );
145+
String str_yaml_out = ""; // YAML output string
146+
//StringStream yaml_stream_out( str_yaml_out ); // Stream to str_yaml_out
147+
// create and populate a JsonObject
148+
DynamicJsonDocument doc(json_str_size*2);
149+
auto err = deserializeJson( doc, json_example_str );
150+
if( err ) {
151+
YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() );
152+
return;
153+
}
154+
JsonObject json_obj = doc.as<JsonObject>();
155+
const size_t bytes_out = serializeYml( json_obj, str_yaml_out );
156+
Serial.println( str_yaml_out );
157+
YAML_LOG_n("[JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n\n", json_str_size, bytes_out );
158+
}
159+
160+
161+
#if defined USE_STREAM_TO_STREAM // stream to stream unavailable on esp8266 (not enough memory)
162+
#pragma message "Enabling ArduinoJson stream<=>stream tests"
163+
{
164+
YAML_LOG_n( "[TEST #%d] JSON stream to JsonObject to YAML stream -> serializeYml(stream_in, Serial):", test_number++ );
165+
String str_json = String( json_example_str );
166+
StringStream stream_in( str_json );
167+
const size_t bytes_out = serializeYml( stream_in, Serial );
168+
YAML_LOG_n("[JSON=>JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n", json_str_size, bytes_out);
169+
}
170+
#endif
171+
172+
173+
YAML_LOG_n("ArduinoJson tests complete");
174+
175+
#endif
176+
177+
178+
179+
180+
#if __has_include(<cJSON.h>)
181+
#pragma message "Enabling cJSON tests"
182+
183+
184+
185+
YAML_LOG_n("\n\nYAML=>JSON and JSON=>YAML using cJSON:\n");
186+
187+
188+
{
189+
YAML_LOG_n( "YAML string to cJSON Object -> deserializeYml(cJSON_obj*, yaml_example_str):" );
190+
// deserialize YAML string into cJSON object
191+
cJSON* objPtr = (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
192+
int ret = deserializeYml( objPtr, yaml_example_str );
193+
if (!ret) {
194+
Serial.println("deserializeYml failed");
195+
return;
196+
}
197+
YAML_LOG_n("Printing json");
198+
char* json = cJSON_Print( objPtr );
199+
if( !json ) {
200+
YAML_LOG_e("emtpy output, aborting");
201+
return;
202+
}
203+
String str_json_out = String( json );
204+
free(json);
205+
Serial.print( str_json_out );
206+
cJSON_Delete( objPtr );
207+
YAML_LOG_n("[YAML=>cJsonObject] yaml bytes in=%d, json bytes out=%d\n", yaml_str_size, str_json_out.length() );
208+
}
209+
210+
211+
212+
{
213+
YAML_LOG_n( "YAML stream to cJSON Object -> deserializeYml(cJSON_obj*, yaml_stream):" );
214+
String yaml_str = String( yaml_example_str );
215+
StringStream yaml_stream( yaml_str );
216+
cJSON* objPtr = (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
217+
// deserialize YAML stream into cJSON object
218+
int ret = deserializeYml( objPtr, yaml_stream );
219+
if (!ret) {
220+
Serial.println("deserializeYml failed");
221+
return;
222+
}
223+
YAML_LOG_n("Printing json");
224+
char* json = cJSON_Print( objPtr );
225+
if( !json ) {
226+
YAML_LOG_e("emtpy output, aborting");
227+
return;
228+
}
229+
size_t bytes_out = strlen(json);
230+
Serial.print( json );
231+
free(json);
232+
cJSON_Delete( objPtr );
233+
YAML_LOG_n("[YAML=>cJsonObject] yaml bytes in=%d, json bytes out=%d\n", yaml_str.length(), bytes_out );
234+
}
235+
236+
237+
{
238+
YAML_LOG_n( "cJSON Object to YAML stream -> serializeYml( objPtr, Serial ):" );
239+
cJSON* objPtr = cJSON_Parse( json_example_str );
240+
size_t bytes_out = serializeYml( objPtr, Serial );
241+
cJSON_Delete( objPtr );
242+
YAML_LOG_n("[YAML=>cJsonObject=>YAML] yaml bytes in=%d, json bytes out=%d\n", json_str_size, bytes_out);
243+
}
244+
245+
246+
{
247+
YAML_LOG_n( "cJSON Object to YAML string -> serializeYml( objPtr, yaml_dest_str ):" );
248+
cJSON* objPtr = cJSON_Parse( json_example_str );
249+
String yaml_dest_str;
250+
size_t bytes_out = serializeYml( objPtr, yaml_dest_str );
251+
Serial.println( yaml_dest_str );
252+
cJSON_Delete( objPtr );
253+
YAML_LOG_n("[YAML=>cJsonObject=>YAML] yaml bytes in=%d, json bytes out=%d\n", json_str_size, bytes_out );
254+
}
255+
256+
257+
YAML_LOG_n("cJSON tests complete");
258+
259+
260+
#endif
261+
262+
263+
}
264+
265+
266+
void loop()
267+
{
268+
269+
}

0 commit comments

Comments
 (0)