From e068f5d6a0d54b5f8b870139027d9e1a3cc80291 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Sun, 24 Feb 2019 03:10:01 +0900 Subject: [PATCH] Changed JSON parameter loading to the function Changed JSON parameter loading to the function --- examples/HelloWorld/Data/style.json | 2 +- examples/HelloWorld/HelloWorld.ino | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/examples/HelloWorld/Data/style.json b/examples/HelloWorld/Data/style.json index 6c6030c..6d2efd3 100644 --- a/examples/HelloWorld/Data/style.json +++ b/examples/HelloWorld/Data/style.json @@ -1,5 +1,5 @@ { "name" : "Caption", "type" : "ACText", - "style": "text-align:center;font-family:'Avenir','Corbel','Osaka';color:Green;" + "style": "text-align:center;font-size:24px;font-family:'Impact','Futura',sans-serif;color:tomato;" } diff --git a/examples/HelloWorld/HelloWorld.ino b/examples/HelloWorld/HelloWorld.ino index 0ab0b81..df4bf7e 100644 --- a/examples/HelloWorld/HelloWorld.ino +++ b/examples/HelloWorld/HelloWorld.ino @@ -28,11 +28,17 @@ typedef WebServer WEBServer; #define HELLO_URI "/hello" #define PARAM_STYLE "/style.json" -// Declare AutoConnectElements and AutoConnectAux for the custom Web page. +// Declare AutoConnectText with only a value. +// Qualify the Caption by reading style attributes from the SPIFFS style.json file. ACText(Caption, "Hello, world"); + +//AutoConnectAux for the custom Web page. AutoConnectAux helloPage(HELLO_URI, "Hello", true, { Caption }); AutoConnect portal; +// JSON document loading buffer +String ElementJson; + // Redirects from root to the hello page. void onRoot() { WEBServer& webServer = portal.host(); @@ -44,17 +50,26 @@ void onRoot() { // Load the attribute of the element to modify at runtime from external. String onHello(AutoConnectAux& aux, PageArgument& args) { - File param = SPIFFS.open(PARAM_STYLE, "r"); - aux.loadElement(param); - param.close(); + aux.loadElement(ElementJson); return String(); } +// Load the element from specified file in SPIFFS. +void loadParam(const char* fileName) { + SPIFFS.begin(); + File param = SPIFFS.open(fileName, "r"); + if (param) { + ElementJson = param.readString(); + param.close(); + } + SPIFFS.end(); +} + void setup() { delay(1000); Serial.begin(115200); - SPIFFS.begin(); // Prepare SPIFFS + loadParam(PARAM_STYLE); // Pre-load the element from JSON. helloPage.on(onHello); // Register the attribute overwrite handler. portal.join(helloPage); // Join the hello page. portal.begin();