From 5dc9c4f9349fb6b642665b15ac5678811da38b49 Mon Sep 17 00:00:00 2001 From: Karai Csaba Date: Sun, 15 May 2016 20:09:12 +0200 Subject: [PATCH] Implemented user pages --- examples/arduino/EspLinkSample/UserPage.ino | 17 +++++++++++- examples/arduino/EspLinkSample/WebServer.cpp | 28 ++++++++++++++++++++ examples/arduino/EspLinkSample/WebServer.h | 2 ++ html/userpage.js | 9 ++++++- web-server/web-server.c | 2 +- 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/examples/arduino/EspLinkSample/UserPage.ino b/examples/arduino/EspLinkSample/UserPage.ino index 7b7c187..0d7dfa4 100644 --- a/examples/arduino/EspLinkSample/UserPage.ino +++ b/examples/arduino/EspLinkSample/UserPage.ino @@ -8,6 +8,9 @@ #define POS_MAGIC 0 #define POS_FIRST_NAME (POS_MAGIC + 2) #define POS_LAST_NAME (POS_FIRST_NAME + MAX_STR_LEN) +#define POS_AGE (POS_LAST_NAME + MAX_STR_LEN) +#define POS_GENDER (POS_AGE+1) +#define POS_NOTIFICATIONS (POS_GENDER+1) void userInit() { @@ -20,6 +23,9 @@ void userInit() EEPROM.put(POS_MAGIC, magic); EEPROM.update(POS_FIRST_NAME, 0); EEPROM.update(POS_LAST_NAME, 0); + EEPROM.update(POS_AGE, 0); + EEPROM.update(POS_GENDER, 'f'); + EEPROM.update(POS_NOTIFICATIONS, 0); } } @@ -51,15 +57,24 @@ void userHtmlCallback(WebServerCommand command, char * data, int dataLen) userWriteStr(webServer.getArgString(), POS_FIRST_NAME); if( strcmp_P(data, PSTR("last_name")) == 0 ) userWriteStr(webServer.getArgString(), POS_LAST_NAME); + if( strcmp_P(data, PSTR("age")) == 0 ) + EEPROM.update(POS_AGE, (uint8_t)webServer.getArgInt()); + if( strcmp_P(data, PSTR("gender")) == 0 ) + EEPROM.update(POS_GENDER, (strcmp_P(webServer.getArgString(), PSTR("male")) == 0 ? 'm' : 'f')); + if( strcmp_P(data, PSTR("notifications")) == 0 ) + EEPROM.update(POS_NOTIFICATIONS, (uint8_t)webServer.getArgBoolean()); break; case LOAD: { char buf[MAX_STR_LEN]; - webServer.setArgNum(2); + webServer.setArgNum(5); userReadStr( buf, POS_FIRST_NAME ); webServer.setArgString("first_name", buf); userReadStr( buf, POS_LAST_NAME ); webServer.setArgString("last_name", buf); + webServer.setArgInt("age", (uint8_t)EEPROM[POS_AGE]); + webServer.setArgStringP("gender", (EEPROM[POS_GENDER] == 'm') ? PSTR("male") : PSTR("female")); + webServer.setArgBoolean("notifications", EEPROM[POS_NOTIFICATIONS] != 0); } break; case REFRESH: diff --git a/examples/arduino/EspLinkSample/WebServer.cpp b/examples/arduino/EspLinkSample/WebServer.cpp index a72faa0..5b0e775 100644 --- a/examples/arduino/EspLinkSample/WebServer.cpp +++ b/examples/arduino/EspLinkSample/WebServer.cpp @@ -162,6 +162,21 @@ void WebServer::setArgStringP(const char * name, const char * value) args_to_send--; } +void WebServer::setArgBoolean(const char * name, uint8_t value) +{ + if( args_to_send <= 0 ) + return; + + uint8_t nlen = strlen(name); + char buf[nlen + 4]; + buf[0] = WEB_BOOLEAN; + strcpy(buf+1, name); + buf[2 + nlen] = value; + espLink.sendPacketArg(nlen+3, (uint8_t *)buf); + + args_to_send--; +} + void WebServer::setArgJson(const char * name, const char * value) { if( args_to_send <= 0 ) @@ -203,3 +218,16 @@ char * WebServer::getArgString() return value_ptr; } +uint8_t WebServer::getArgBoolean() +{ + if( strcmp_P(value_ptr, PSTR("on")) == 0 ) + return 1; + if( strcmp_P(value_ptr, PSTR("true")) == 0 ) + return 1; + if( strcmp_P(value_ptr, PSTR("yes")) == 0 ) + return 1; + if( strcmp_P(value_ptr, PSTR("1")) == 0 ) + return 1; + return 0; +} + diff --git a/examples/arduino/EspLinkSample/WebServer.h b/examples/arduino/EspLinkSample/WebServer.h index e1742b6..3b9a444 100644 --- a/examples/arduino/EspLinkSample/WebServer.h +++ b/examples/arduino/EspLinkSample/WebServer.h @@ -76,9 +76,11 @@ class WebServer void setArgJson(const char * name, const char * value); void setArgString(const char * name, const char * value); void setArgStringP(const char * name, const char * value); + void setArgBoolean(const char * name, uint8_t value); int32_t getArgInt(); char * getArgString(); + uint8_t getArgBoolean(); }; #endif /* WEB_SERVER_H */ diff --git a/html/userpage.js b/html/userpage.js index 74c7a7b..e9e7555 100644 --- a/html/userpage.js +++ b/html/userpage.js @@ -21,7 +21,14 @@ function notifyResponse( data ) } else if( el.type == "checkbox" ) { - el.checked = data[v] == "on"; + if( data[v] == "on" ) + el.checked = true; + else if( data[v] == "off" ) + el.checked = false; + else if( data[v] == true ) + el.checked = true; + else + el.checked = false; } else { diff --git a/web-server/web-server.c b/web-server/web-server.c index 330c436..85bef1f 100644 --- a/web-server/web-server.c +++ b/web-server/web-server.c @@ -310,7 +310,7 @@ int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData) } break; case WEB_BOOLEAN: - if( value ) { + if( *value ) { os_memcpy(jsonBuf + jsonPtr, "true", 4); jsonPtr += 4; } else {