Skip to content

Commit f9e8a1d

Browse files
committed
code tweaks
1 parent 4d8131b commit f9e8a1d

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/ArduinoYaml.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct yaml_stream_handler_data_t
8080
};
8181

8282

83+
// stream reader callback
8384
int _yaml_stream_reader(void *data, unsigned char *buffer, size_t size, size_t *size_read)
8485
{
8586
yaml_stream_handler_data_t *shd = (yaml_stream_handler_data_t*)data;
@@ -93,7 +94,7 @@ int _yaml_stream_reader(void *data, unsigned char *buffer, size_t size, size_t *
9394
}
9495

9596

96-
// typedef int yaml_write_handler_t(void *data, unsigned char *buffer, size_t size);
97+
// stream writer callback
9798
int _yaml_stream_writer(void *data, unsigned char *buffer, size_t size)
9899
{
99100
yaml_stream_handler_data_t *shd = (yaml_stream_handler_data_t*)data;

src/ArduinoYaml.hpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,27 @@ extern "C" {
3636
#include "libyaml/yaml.h" // https://github.com/yaml/libyaml
3737
}
3838

39-
40-
#define USE_STREAM_TO_STREAM
41-
4239
#if defined ARDUINO_ARCH_SAMD || defined ARDUINO_ARCH_RP2040 || defined ESP8266
40+
// __has_include() macro only sees inside the sketch folder, so assume ArduinoJson as a default dependancy
4341
#include <Arduino.h>
44-
#include <ArduinoJson.h>
4542
#include <assert.h>
46-
#undef USE_STREAM_TO_STREAM
43+
#include <ArduinoJson.h>
44+
// also disable the "unstable" stream-to-stream function
4745
#define HAS_ARDUINOJSON
4846
#endif
4947

5048
#if !defined HAS_ARDUINOJSON && __has_include(<Arduino.h>)
49+
// esp32 __has_include() macro works outside the sketch folder, so it's possible to guess
5150
#define HAS_ARDUINOJSON
5251
#endif
5352

53+
#if defined ESP32
54+
// platform specific feature is unstable but recoverable with ESP32 devices family
55+
#define USE_STREAM_TO_STREAM
56+
#endif
57+
5458

59+
// provide a default String::Stream reader/writer for internals
5560
class StringStream : public Stream
5661
{
5762
public:
@@ -67,7 +72,7 @@ class StringStream : public Stream
6772
};
6873

6974

70-
75+
// the base class
7176
class YAMLParser
7277
{
7378
public:
@@ -100,6 +105,8 @@ class YAMLParser
100105

101106
#if defined HAS_ARDUINOJSON
102107

108+
// ArduinoJson friendly functions and derivated class
109+
103110
#include <ArduinoJson.h>
104111

105112
// deconstructors
@@ -184,6 +191,8 @@ class YAMLParser
184191

185192
#if __has_include(<cJSON.h>)
186193

194+
// cJSON friendly functions and derivated class
195+
187196
#include <cJSON.h> // built-in with esp32
188197

189198
// deconstructors
@@ -232,5 +241,18 @@ class YAMLParser
232241

233242
#endif
234243

235-
// this macro does not like to be defined early (especially before ArduinoJson.h is included)
236-
#define indent(indent_size) std::string(indent_size*2, ' ').c_str()
244+
#if defined ARDUINO_ARCH_SAMD
245+
// using slow copy instead of a macro, because std::string is incomplete with samd core
246+
static String _indent_str;
247+
static const char* indent( size_t size )
248+
{
249+
_indent_str = "";
250+
for( size_t i=0;i<size;i++ ) _indent_str += " ";
251+
return _indent_str.c_str();
252+
}
253+
#else
254+
// this macro does not like to be defined early (especially before ArduinoJson.h is included)
255+
#define indent(indent_size) (std::string(indent_size*2, ' ')).c_str()
256+
#endif
257+
258+

0 commit comments

Comments
 (0)