Skip to content

Commit 7dc3c81

Browse files
authored
Merge pull request #2 from tobozo/1.2.2
1.2.2
2 parents abeb59d + 435aac7 commit 7dc3c81

File tree

5 files changed

+90
-29
lines changed

5 files changed

+90
-29
lines changed

examples/test/test.ino

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ const char* json_example_str = R"_JSON_STRING_(
6767
void setup()
6868
{
6969
Serial.begin(115200);
70-
delay(1000);
70+
delay(5000);
71+
Serial.print("Welcome to the YAML Test sketch\nRam free: ");
72+
Serial.print( HEAP_AVAILABLE() );
73+
Serial.println(" bytes");
74+
7175
const size_t yaml_str_size = strlen(yaml_example_str);
7276
const size_t json_str_size = strlen(json_example_str);
7377
int test_number = 1;
@@ -82,6 +86,7 @@ void setup()
8286

8387
YAML_LOG_n("YAML=>JSON and JSON=>YAML using ArduinoJson\n\n");
8488

89+
#if !defined ARDUINO_ARCH_SAMD // samd with 32kb ram can oom after this, so only enable for specific test
8590
{
8691
YAML_LOG_n( "[TEST #%d] YAML stream to JsonObject -> deserializeYml(json_doc, yaml_stream):", test_number++ );
8792
DynamicJsonDocument json_doc(yaml_str_size*2);
@@ -95,6 +100,7 @@ void setup()
95100
const size_t bytes_out = serializeJsonPretty( json_doc, Serial ); // print deserialized JsonObject
96101
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
97102
}
103+
#endif
98104

99105

100106
{

library.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=YAMLDuino
2-
version=1.2.1
2+
version=1.2.2
33
author=tobozo <[email protected]>
44
maintainer=tobozo <[email protected]>
55
sentence=A simple and efficient YAML library for embedded C++
@@ -8,4 +8,5 @@ category=Data Processing
88
url=https://github.com/tobozo/YAMLDuino
99
architectures=esp32,esp8266,samd,rp2040,mbed_rp2040,mbed_nano
1010
includes=ArduinoYaml.h,YAMLDuino.h
11+
depends=ArduinoJson
1112
license=MIT

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+

src/logger.hpp

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,48 @@
4747
#define HEAP_AVAILABLE() ESP.getFreeHeap()
4848
#define YAML_DEFAULT_LOG_LEVEL (LogLevel_t)ARDUHAL_LOG_LEVEL
4949
#define YAML_PATHNAME pathToFileName
50-
#else
51-
#include <stdio.h>
52-
#include <stdint.h>
50+
#elif defined ESP8266
51+
#include "Esp.h" // bring esp8266-arduino specifics to scope
52+
#define HEAP_AVAILABLE() ESP.getFreeHeap()
53+
#define LOG_PRINTF Serial.printf
54+
#elif defined ARDUINO_ARCH_RP2040
55+
#include <Arduino.h>
56+
#define LOG_PRINTF Serial.printf
57+
#define HEAP_AVAILABLE() rp2040.getFreeHeap()
58+
#elif defined ARDUINO_ARCH_SAMD
5359
#include <stdarg.h>
54-
// declare macros and functions needed by the logger
55-
#define LOG_PRINTF printf
56-
#ifdef ESP8266
57-
#include "Esp.h" // bring esp8266-arduino specifics to scope
58-
#define HEAP_AVAILABLE() ESP.getFreeHeap()
59-
#else
60-
#define HEAP_AVAILABLE() getFreeRam()
61-
static int getFreeRam()
62-
{
63-
// implement your own
64-
return 0;
65-
}
66-
#endif
67-
#define YAML_DEFAULT_LOG_LEVEL LogLevelWarning
68-
#define YAML_PATHNAME _pathToFileName
60+
#include <Arduino.h>
61+
#define LOG_PRINTF Serial.printf
62+
#ifdef __arm__
63+
// should use uinstd.h to define sbrk but Due causes a conflict
64+
extern "C" char* sbrk(int incr);
65+
#else // __ARM__
66+
extern char *__brkval;
67+
#endif // __arm__
68+
static int getFreeRam()
69+
{
70+
char top;
71+
#ifdef __arm__
72+
return &top - reinterpret_cast<char*>(sbrk(0));
73+
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
74+
return &top - __brkval;
75+
#else // __arm__
76+
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
77+
#endif // __arm__
78+
}
79+
#define HEAP_AVAILABLE() getFreeRam()
80+
#else
81+
static int getFreeRam()
82+
{
83+
// implement your own
84+
return 0;
85+
}
86+
#define HEAP_AVAILABLE() getFreeRam()
6987

88+
#endif
89+
90+
#if !defined YAML_PATHNAME
91+
#define YAML_PATHNAME _pathToFileName
7092
static const char * _pathToFileName(const char * path)
7193
{
7294
size_t i = 0, pos = 0;
@@ -82,6 +104,15 @@
82104
}
83105
#endif
84106

107+
#if !defined LOG_PRINTF
108+
#define LOG_PRINTF printf
109+
#endif
110+
111+
#if !defined YAML_DEFAULT_LOG_LEVEL
112+
#define YAML_DEFAULT_LOG_LEVEL LogLevelWarning
113+
#endif
114+
115+
85116

86117
namespace YAML
87118
{

0 commit comments

Comments
 (0)