Use CharStream instead of StringStream

This commit is contained in:
fvanroie 2020-04-11 19:49:11 +02:00
parent f79a7cc231
commit 94a34daab9
4 changed files with 66 additions and 24 deletions

View File

@ -0,0 +1,43 @@
#ifndef _CHAR_STREAM_H_
#define _CHAR_STREAM_H_
#include <Stream.h>
#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_

View File

@ -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<JsonObject>(), savedPage);
}
dispatchJsonl(file);
file.close();
Log.notice(F("HASP: File %s loaded"), pages.c_str());
file.close();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -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<JsonObject>(), 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<JsonObject>(), savedPage);
}
CharStream stream(payload);
dispatchJsonl(stream);
}
void dispatchIdle(const char * state)

View File

@ -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);