Skip to content

Commit

Permalink
Merge pull request #2 from tobozo/1.2.2
Browse files Browse the repository at this point in the history
1.2.2
  • Loading branch information
tobozo authored Oct 13, 2022
2 parents abeb59d + 435aac7 commit 7dc3c81
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 29 deletions.
8 changes: 7 additions & 1 deletion examples/test/test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ const char* json_example_str = R"_JSON_STRING_(
void setup()
{
Serial.begin(115200);
delay(1000);
delay(5000);
Serial.print("Welcome to the YAML Test sketch\nRam free: ");
Serial.print( HEAP_AVAILABLE() );
Serial.println(" bytes");

const size_t yaml_str_size = strlen(yaml_example_str);
const size_t json_str_size = strlen(json_example_str);
int test_number = 1;
Expand All @@ -82,6 +86,7 @@ void setup()

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

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


{
Expand Down
3 changes: 2 additions & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=YAMLDuino
version=1.2.1
version=1.2.2
author=tobozo <tobozo@noreply.github.com>
maintainer=tobozo <tobozo@noreply.github.com>
sentence=A simple and efficient YAML library for embedded C++
Expand All @@ -8,4 +8,5 @@ category=Data Processing
url=https://github.com/tobozo/YAMLDuino
architectures=esp32,esp8266,samd,rp2040,mbed_rp2040,mbed_nano
includes=ArduinoYaml.h,YAMLDuino.h
depends=ArduinoJson
license=MIT
3 changes: 2 additions & 1 deletion src/ArduinoYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct yaml_stream_handler_data_t
};


// stream reader callback
int _yaml_stream_reader(void *data, unsigned char *buffer, size_t size, size_t *size_read)
{
yaml_stream_handler_data_t *shd = (yaml_stream_handler_data_t*)data;
Expand All @@ -93,7 +94,7 @@ int _yaml_stream_reader(void *data, unsigned char *buffer, size_t size, size_t *
}


// typedef int yaml_write_handler_t(void *data, unsigned char *buffer, size_t size);
// stream writer callback
int _yaml_stream_writer(void *data, unsigned char *buffer, size_t size)
{
yaml_stream_handler_data_t *shd = (yaml_stream_handler_data_t*)data;
Expand Down
38 changes: 30 additions & 8 deletions src/ArduinoYaml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,27 @@ extern "C" {
#include "libyaml/yaml.h" // https://github.com/yaml/libyaml
}


#define USE_STREAM_TO_STREAM

#if defined ARDUINO_ARCH_SAMD || defined ARDUINO_ARCH_RP2040 || defined ESP8266
// __has_include() macro only sees inside the sketch folder, so assume ArduinoJson as a default dependancy
#include <Arduino.h>
#include <ArduinoJson.h>
#include <assert.h>
#undef USE_STREAM_TO_STREAM
#include <ArduinoJson.h>
// also disable the "unstable" stream-to-stream function
#define HAS_ARDUINOJSON
#endif

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

#if defined ESP32
// platform specific feature is unstable but recoverable with ESP32 devices family
#define USE_STREAM_TO_STREAM
#endif


// provide a default String::Stream reader/writer for internals
class StringStream : public Stream
{
public:
Expand All @@ -67,7 +72,7 @@ class StringStream : public Stream
};



// the base class
class YAMLParser
{
public:
Expand Down Expand Up @@ -100,6 +105,8 @@ class YAMLParser

#if defined HAS_ARDUINOJSON

// ArduinoJson friendly functions and derivated class

#include <ArduinoJson.h>

// deconstructors
Expand Down Expand Up @@ -184,6 +191,8 @@ class YAMLParser

#if __has_include(<cJSON.h>)

// cJSON friendly functions and derivated class

#include <cJSON.h> // built-in with esp32

// deconstructors
Expand Down Expand Up @@ -232,5 +241,18 @@ class YAMLParser

#endif

// this macro does not like to be defined early (especially before ArduinoJson.h is included)
#define indent(indent_size) std::string(indent_size*2, ' ').c_str()
#if defined ARDUINO_ARCH_SAMD
// using slow copy instead of a macro, because std::string is incomplete with samd core
static String _indent_str;
static const char* indent( size_t size )
{
_indent_str = "";
for( size_t i=0;i<size;i++ ) _indent_str += " ";
return _indent_str.c_str();
}
#else
// this macro does not like to be defined early (especially before ArduinoJson.h is included)
#define indent(indent_size) (std::string(indent_size*2, ' ')).c_str()
#endif


67 changes: 49 additions & 18 deletions src/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,48 @@
#define HEAP_AVAILABLE() ESP.getFreeHeap()
#define YAML_DEFAULT_LOG_LEVEL (LogLevel_t)ARDUHAL_LOG_LEVEL
#define YAML_PATHNAME pathToFileName
#else
#include <stdio.h>
#include <stdint.h>
#elif defined ESP8266
#include "Esp.h" // bring esp8266-arduino specifics to scope
#define HEAP_AVAILABLE() ESP.getFreeHeap()
#define LOG_PRINTF Serial.printf
#elif defined ARDUINO_ARCH_RP2040
#include <Arduino.h>
#define LOG_PRINTF Serial.printf
#define HEAP_AVAILABLE() rp2040.getFreeHeap()
#elif defined ARDUINO_ARCH_SAMD
#include <stdarg.h>
// declare macros and functions needed by the logger
#define LOG_PRINTF printf
#ifdef ESP8266
#include "Esp.h" // bring esp8266-arduino specifics to scope
#define HEAP_AVAILABLE() ESP.getFreeHeap()
#else
#define HEAP_AVAILABLE() getFreeRam()
static int getFreeRam()
{
// implement your own
return 0;
}
#endif
#define YAML_DEFAULT_LOG_LEVEL LogLevelWarning
#define YAML_PATHNAME _pathToFileName
#include <Arduino.h>
#define LOG_PRINTF Serial.printf
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
static int getFreeRam()
{
char top;
#ifdef __arm__
return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
return &top - __brkval;
#else // __arm__
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
}
#define HEAP_AVAILABLE() getFreeRam()
#else
static int getFreeRam()
{
// implement your own
return 0;
}
#define HEAP_AVAILABLE() getFreeRam()

#endif

#if !defined YAML_PATHNAME
#define YAML_PATHNAME _pathToFileName
static const char * _pathToFileName(const char * path)
{
size_t i = 0, pos = 0;
Expand All @@ -82,6 +104,15 @@
}
#endif

#if !defined LOG_PRINTF
#define LOG_PRINTF printf
#endif

#if !defined YAML_DEFAULT_LOG_LEVEL
#define YAML_DEFAULT_LOG_LEVEL LogLevelWarning
#endif



namespace YAML
{
Expand Down

0 comments on commit 7dc3c81

Please sign in to comment.