From 159f075e06efac41a254f223ce29df99bf39f5b1 Mon Sep 17 00:00:00 2001 From: Karai Csaba Date: Sun, 15 May 2016 10:17:12 +0200 Subject: [PATCH] User page draft --- .../arduino/EspLinkSample/EspLinkSample.ino | 4 +- examples/arduino/EspLinkSample/Pages.h | 3 + examples/arduino/EspLinkSample/UserPage.ino | 70 +++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 examples/arduino/EspLinkSample/UserPage.ino diff --git a/examples/arduino/EspLinkSample/EspLinkSample.ino b/examples/arduino/EspLinkSample/EspLinkSample.ino index 9f443bf..57d7e14 100644 --- a/examples/arduino/EspLinkSample/EspLinkSample.ino +++ b/examples/arduino/EspLinkSample/EspLinkSample.ino @@ -1,11 +1,12 @@ - #include "WebServer.h" #include "Pages.h" const char ledURL[] PROGMEM = "/LED.html.json"; +const char userURL[] PROGMEM = "/User.html.json"; const WebMethod PROGMEM methods[] = { { ledURL, ledHtmlCallback }, + { userURL, userHtmlCallback }, { NULL, NULL }, }; @@ -17,6 +18,7 @@ void setup() webServer.init(); ledInit(); + userInit(); } void loop() diff --git a/examples/arduino/EspLinkSample/Pages.h b/examples/arduino/EspLinkSample/Pages.h index efa277c..6142f36 100644 --- a/examples/arduino/EspLinkSample/Pages.h +++ b/examples/arduino/EspLinkSample/Pages.h @@ -5,6 +5,9 @@ void ledHtmlCallback(WebServerCommand command, char * data, int dataLen); void ledLoop(); void ledInit(); +void userHtmlCallback(WebServerCommand command, char * data, int dataLen); +void userInit(); + #endif /* PAGES_H */ diff --git a/examples/arduino/EspLinkSample/UserPage.ino b/examples/arduino/EspLinkSample/UserPage.ino new file mode 100644 index 0000000..7b7c187 --- /dev/null +++ b/examples/arduino/EspLinkSample/UserPage.ino @@ -0,0 +1,70 @@ +#include +#include "WebServer.h" + +#define MAGIC 0xABEF + +#define MAX_STR_LEN 32 + +#define POS_MAGIC 0 +#define POS_FIRST_NAME (POS_MAGIC + 2) +#define POS_LAST_NAME (POS_FIRST_NAME + MAX_STR_LEN) + +void userInit() +{ + uint16_t magic; + EEPROM.get(POS_MAGIC, magic); + + if( magic != MAGIC ) + { + magic = MAGIC; + EEPROM.put(POS_MAGIC, magic); + EEPROM.update(POS_FIRST_NAME, 0); + EEPROM.update(POS_LAST_NAME, 0); + } +} + +void userWriteStr(char * str, int ndx) +{ + for(uint8_t i=0; i < MAX_STR_LEN-1; i++) + { + EEPROM.update(ndx + i, str[i]); + if( str[i] == 0 ) + break; + } + EEPROM.update(ndx + MAX_STR_LEN - 1, 0); +} + +void userReadStr(char * str, int ndx) +{ + for(uint8_t i=0; i < MAX_STR_LEN; i++) + { + str[i] = EEPROM[ndx + i]; + } +} + +void userHtmlCallback(WebServerCommand command, char * data, int dataLen) +{ + switch(command) + { + case SET_FIELD: + if( strcmp_P(data, PSTR("first_name")) == 0 ) + userWriteStr(webServer.getArgString(), POS_FIRST_NAME); + if( strcmp_P(data, PSTR("last_name")) == 0 ) + userWriteStr(webServer.getArgString(), POS_LAST_NAME); + break; + case LOAD: + { + char buf[MAX_STR_LEN]; + webServer.setArgNum(2); + userReadStr( buf, POS_FIRST_NAME ); + webServer.setArgString("first_name", buf); + userReadStr( buf, POS_LAST_NAME ); + webServer.setArgString("last_name", buf); + } + break; + case REFRESH: + // do nothing + break; + } +} +