From 94a34daab9a25a2397d383b2662537501fe583ce Mon Sep 17 00:00:00 2001 From: fvanroie Date: Sat, 11 Apr 2020 19:49:11 +0200 Subject: [PATCH] Use CharStream instead of StringStream --- lib/CharStream/CharStream.h | 43 +++++++++++++++++++++++++++++++++++++ src/hasp.cpp | 17 +++++---------- src/hasp_dispatch.cpp | 29 ++++++++++++++----------- src/hasp_dispatch.h | 1 + 4 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 lib/CharStream/CharStream.h diff --git a/lib/CharStream/CharStream.h b/lib/CharStream/CharStream.h new file mode 100644 index 00000000..92faad25 --- /dev/null +++ b/lib/CharStream/CharStream.h @@ -0,0 +1,43 @@ +#ifndef _CHAR_STREAM_H_ +#define _CHAR_STREAM_H_ + +#include +#include "Arduino.h" + +class CharStream : public Stream { + public: + CharStream(char * s) : string(s), position(0) + {} + + // Stream methods + virtual int available() + { + return strlen(string) - position; + } + virtual int read() + { + return position < strlen(string) ? string[position++] : -1; + } + virtual int peek() + { + return position < strlen(string) ? string[position] : -1; + } + virtual void flush(){}; + // Print methods + virtual size_t write(uint8_t c) + { + /* char buf[2]; + buf[0] = c; + buf[1] = '\0'; + strncat((char *)string, buf, 1); + return 1;*/ + return 0; + }; + + private: + char * string; + unsigned int length; + unsigned int position; +}; + +#endif // _CHAR_STREAM_H_ \ No newline at end of file diff --git a/src/hasp.cpp b/src/hasp.cpp index eb2280e9..54aae96b 100644 --- a/src/hasp.cpp +++ b/src/hasp.cpp @@ -743,8 +743,9 @@ void haspNewObject(const JsonObject & config, uint8_t & saved_page_id) switch(objid) { /* ----- Basic Objects ------ */ case LV_HASP_BUTTON: { - obj = lv_btn_create(parent_obj, NULL); - lv_obj_t * label = lv_label_create(obj, NULL); + obj = lv_btn_create(parent_obj, NULL); + /* lv_obj_t * label ; */ + lv_label_create(obj, NULL); // haspSetOpacity(obj, LV_OPA_COVER); lv_obj_set_event_cb(obj, btn_event_handler); break; @@ -913,18 +914,10 @@ void haspLoadPage(String pages) Log.notice(F("HASP: Loading file %s"), pages.c_str()); File file = SPIFFS.open(pages, "r"); - // ReadBufferingStream bufferingStream(file, 256); - DynamicJsonDocument config(256); - - uint8_t savedPage = current_page; - while(deserializeJson(config, file) == DeserializationError::Ok) { - // serializeJson(config, Serial); - // Serial.println(); - haspNewObject(config.as(), savedPage); - } + dispatchJsonl(file); + file.close(); Log.notice(F("HASP: File %s loaded"), pages.c_str()); - file.close(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/hasp_dispatch.cpp b/src/hasp_dispatch.cpp index 9b5849e7..e694ce0c 100644 --- a/src/hasp_dispatch.cpp +++ b/src/hasp_dispatch.cpp @@ -1,6 +1,7 @@ #include "ArduinoJson.h" #include "ArduinoLog.h" #include "StringStream.h" +#include "CharStream.h" #include "hasp_dispatch.h" #include "hasp_config.h" @@ -249,20 +250,24 @@ void dispatchJson(char * payload) } } +void dispatchJsonl(Stream & stream) +{ + DynamicJsonDocument jsonl(3 * 128u); + uint8_t savedPage = haspGetPage(); + + Log.notice(F("DISPATCH: jsonl")); + + while(deserializeJson(jsonl, stream) == DeserializationError::Ok) { + serializeJson(jsonl, Serial); + Serial.println(); + haspNewObject(jsonl.as(), savedPage); + } +} + void dispatchJsonl(char * payload) { - uint8_t savedPage = 0; - DynamicJsonDocument config(3 * 128u); - String output((char *)0); - StringStream stream((String &)output); - output.reserve(3 * 128u); - - stream.print(payload); - while(deserializeJson(config, stream) == DeserializationError::Ok) { - serializeJson(config, Serial); - Serial.println(); - haspNewObject(config.as(), savedPage); - } + CharStream stream(payload); + dispatchJsonl(stream); } void dispatchIdle(const char * state) diff --git a/src/hasp_dispatch.h b/src/hasp_dispatch.h index 5f55f15c..8b16d138 100644 --- a/src/hasp_dispatch.h +++ b/src/hasp_dispatch.h @@ -10,6 +10,7 @@ void dispatchAttribute(String strTopic, const char * strPayload); void dispatchCommand(String cmnd); void dispatchJson(char * strPayload); void dispatchJsonl(char * strPayload); +void dispatchJsonl(Stream & stream); void dispatchPage(String strPageid); void dispatchClearPage(String strPageid);