From d2764573d2bfc51efd8f612cd89a62cf8a91e1be Mon Sep 17 00:00:00 2001 From: Karai Csaba Date: Sat, 14 May 2016 21:15:23 +0200 Subject: [PATCH] Implemented: led pages --- .../arduino/EspLinkSample/EspLinkSample.ino | 99 +-------- examples/arduino/EspLinkSample/LedPage.ino | 192 ++++++++++++++++++ examples/arduino/EspLinkSample/Pages.h | 10 + examples/arduino/EspLinkSample/WebServer.cpp | 16 ++ examples/arduino/EspLinkSample/WebServer.h | 1 + 5 files changed, 222 insertions(+), 96 deletions(-) create mode 100644 examples/arduino/EspLinkSample/LedPage.ino create mode 100644 examples/arduino/EspLinkSample/Pages.h diff --git a/examples/arduino/EspLinkSample/EspLinkSample.ino b/examples/arduino/EspLinkSample/EspLinkSample.ino index c776994..9f443bf 100644 --- a/examples/arduino/EspLinkSample/EspLinkSample.ino +++ b/examples/arduino/EspLinkSample/EspLinkSample.ino @@ -1,16 +1,6 @@ #include "WebServer.h" - -#define LED_PIN 13 - -int8_t blinking = 0; -int8_t frequency = 10; -uint8_t pattern = 2; -uint16_t elapse = 100; -uint16_t elapse_delta = 200; -uint32_t next_ts = 0; - -void ledHtmlCallback(WebServerCommand command, char * data, int dataLen); +#include "Pages.h" const char ledURL[] PROGMEM = "/LED.html.json"; @@ -26,96 +16,13 @@ void setup() Serial.begin(57600); webServer.init(); - pinMode(LED_PIN, OUTPUT); - digitalWrite(LED_PIN, false); + ledInit(); } void loop() { webServer.loop(); - - if( blinking ) - { - if( next_ts <= millis() ) - { - digitalWrite(LED_PIN, !digitalRead(LED_PIN)); - next_ts += elapse; - elapse = elapse_delta - elapse; - } - } -} - -void ledHtmlCallback(WebServerCommand command, char * data, int dataLen) -{ - switch(command) - { - case BUTTON_PRESS: - if( strcmp_P(data, PSTR("btn_on") ) == 0 ) - { - blinking = 0; - digitalWrite(LED_PIN, true); - } else if( strcmp_P(data, PSTR("btn_off") ) == 0 ) - { - blinking = 0; - digitalWrite(LED_PIN, false); - } else if( strcmp_P(data, PSTR("btn_blink") ) == 0 ) - { - blinking = 1; - next_ts = millis() + elapse; - } - break; - case SET_FIELD: - if( strcmp_P(data, PSTR("frequency") ) == 0 ) - { - frequency = webServer.getArgInt(); - digitalWrite(LED_PIN, false); - elapse_delta = 2000 / frequency; - elapse = pattern * elapse_delta / 4; - } - else if( strcmp_P(data, PSTR("pattern") ) == 0 ) - { - char * arg = webServer.getArgString(); - - if( strcmp_P(arg, PSTR("25_75")) == 0 ) - pattern = 1; - else if( strcmp_P(arg, PSTR("50_50")) == 0 ) - pattern = 2; - else if( strcmp_P(arg, PSTR("75_25")) == 0 ) - pattern = 3; - - digitalWrite(LED_PIN, false); - elapse = pattern * elapse_delta / 4; - } - break; - case LOAD: - webServer.setArgNum(3); - webServer.setArgInt("frequency", frequency); - - switch(pattern) - { - case 1: - webServer.setArgStringP("pattern", PSTR("25_75")); - break; - case 2: - webServer.setArgStringP("pattern", PSTR("50_50")); - break; - case 3: - webServer.setArgStringP("pattern", PSTR("75_25")); - break; - } - case REFRESH: - if( command == REFRESH ) - webServer.setArgNum(1); - - if( blinking ) - webServer.setArgStringP("text", PSTR("LED is blinking")); - else - webServer.setArgStringP("text", digitalRead(LED_PIN) ? PSTR("LED is turned on") : PSTR("LED is turned off")); - break; - default: - break; - } + ledLoop(); } - diff --git a/examples/arduino/EspLinkSample/LedPage.ino b/examples/arduino/EspLinkSample/LedPage.ino new file mode 100644 index 0000000..768520b --- /dev/null +++ b/examples/arduino/EspLinkSample/LedPage.ino @@ -0,0 +1,192 @@ +#include "WebServer.h" + +#define LED_PIN 13 + +int8_t blinking = 0; +int8_t frequency = 10; +uint8_t pattern = 2; +uint16_t elapse = 100; +uint16_t elapse_delta = 200; +uint32_t next_ts = 0; + +#define MAX_LOGS 5 +uint32_t log_ts[MAX_LOGS]; +uint8_t log_msg[MAX_LOGS]; +uint8_t log_ptr = 0; + +void ledInit() +{ + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, false); +} + +void ledLoop() +{ + if( blinking ) + { + if( next_ts <= millis() ) + { + digitalWrite(LED_PIN, !digitalRead(LED_PIN)); + next_ts += elapse; + elapse = elapse_delta - elapse; + } + } +} + +void ledAddLog(uint8_t msg) +{ + if( log_ptr >= MAX_LOGS ) + log_ptr = MAX_LOGS - 1; + + for(int8_t i=log_ptr-1; i >= 0; i--) + { + log_ts[i+1] = log_ts[i]; + log_msg[i+1] = log_msg[i]; + } + log_msg[0] = msg; + log_ts[0] = millis(); + log_ptr++; +} + +void ledHistoryToLog(char * buf) +{ + buf[0] = 0; + strcat(buf, "["); + for(uint8_t i=0; i < log_ptr; i++) + { + if( i != 0 ) + strcat(buf, ","); + + char bf[20]; + sprintf(bf, "\"%lds: ", log_ts[i] / 1000); + strcat(buf, bf); + + uint8_t msg = log_msg[i]; + if( msg == 0xE1 ) + { + strcat_P(buf, PSTR("set pattern to 25%-75%")); + } + else if( msg == 0xE2 ) + { + strcat_P(buf, PSTR("set pattern to 50%-50%")); + } + else if( msg == 0xE3 ) + { + strcat_P(buf, PSTR("set pattern to 75%-25%")); + } + else if( msg == 0xF0 ) + { + strcat_P(buf, PSTR("set led on")); + } + else if( msg == 0xF1 ) + { + strcat_P(buf, PSTR("set led blinking")); + } + else if( msg == 0xF2 ) + { + strcat_P(buf, PSTR("set led off")); + } + else + { + strcat_P(buf, PSTR("set frequency to ")); + sprintf(bf, "%d Hz", msg); + strcat(buf, bf); + } + strcat(buf, "\""); + } + strcat(buf, "]"); +} + + +void ledHtmlCallback(WebServerCommand command, char * data, int dataLen) +{ + switch(command) + { + case BUTTON_PRESS: + if( strcmp_P(data, PSTR("btn_on") ) == 0 ) + { + if( blinking || digitalRead(LED_PIN) == false ) + ledAddLog(0xF0); + blinking = 0; + digitalWrite(LED_PIN, true); + } else if( strcmp_P(data, PSTR("btn_off") ) == 0 ) + { + if( blinking || digitalRead(LED_PIN) == true ) + ledAddLog(0xF2); + blinking = 0; + digitalWrite(LED_PIN, false); + } else if( strcmp_P(data, PSTR("btn_blink") ) == 0 ) + { + if( !blinking ) + ledAddLog(0xF1); + blinking = 1; + next_ts = millis() + elapse; + } + break; + case SET_FIELD: + if( strcmp_P(data, PSTR("frequency") ) == 0 ) + { + int8_t oldf = frequency; + frequency = webServer.getArgInt(); + digitalWrite(LED_PIN, false); + elapse_delta = 2000 / frequency; + elapse = pattern * elapse_delta / 4; + if( oldf != frequency ) + ledAddLog(frequency); + } + else if( strcmp_P(data, PSTR("pattern") ) == 0 ) + { + int8_t oldp = pattern; + char * arg = webServer.getArgString(); + + if( strcmp_P(arg, PSTR("25_75")) == 0 ) + pattern = 1; + else if( strcmp_P(arg, PSTR("50_50")) == 0 ) + pattern = 2; + else if( strcmp_P(arg, PSTR("75_25")) == 0 ) + pattern = 3; + + digitalWrite(LED_PIN, false); + elapse = pattern * elapse_delta / 4; + + if( oldp != pattern ) + ledAddLog(0xE0 + pattern); + } + break; + case LOAD: + webServer.setArgNum(4); + webServer.setArgInt("frequency", frequency); + + switch(pattern) + { + case 1: + webServer.setArgStringP("pattern", PSTR("25_75")); + break; + case 2: + webServer.setArgStringP("pattern", PSTR("50_50")); + break; + case 3: + webServer.setArgStringP("pattern", PSTR("75_25")); + break; + } + case REFRESH: + { + if( command == REFRESH ) + webServer.setArgNum(2); + + if( blinking ) + webServer.setArgStringP("text", PSTR("LED is blinking")); + else + webServer.setArgStringP("text", digitalRead(LED_PIN) ? PSTR("LED is turned on") : PSTR("LED is turned off")); + + char buf[255]; + ledHistoryToLog(buf); + webServer.setArgJson("led_history", buf); + } + break; + default: + break; + } +} + + diff --git a/examples/arduino/EspLinkSample/Pages.h b/examples/arduino/EspLinkSample/Pages.h new file mode 100644 index 0000000..efa277c --- /dev/null +++ b/examples/arduino/EspLinkSample/Pages.h @@ -0,0 +1,10 @@ +#ifndef PAGES_H +#define PAGES_H + +void ledHtmlCallback(WebServerCommand command, char * data, int dataLen); +void ledLoop(); +void ledInit(); + +#endif /* PAGES_H */ + + diff --git a/examples/arduino/EspLinkSample/WebServer.cpp b/examples/arduino/EspLinkSample/WebServer.cpp index 7aab354..a72faa0 100644 --- a/examples/arduino/EspLinkSample/WebServer.cpp +++ b/examples/arduino/EspLinkSample/WebServer.cpp @@ -162,6 +162,22 @@ void WebServer::setArgStringP(const char * name, const char * value) args_to_send--; } +void WebServer::setArgJson(const char * name, const char * value) +{ + if( args_to_send <= 0 ) + return; + + uint8_t nlen = strlen(name); + uint8_t vlen = strlen(value); + char buf[nlen + vlen + 3]; + buf[0] = WEB_JSON; + strcpy(buf+1, name); + strcpy(buf+2+nlen, value); + espLink.sendPacketArg(nlen+vlen+2, (uint8_t *)buf); + + args_to_send--; +} + void WebServer::setArgInt(const char * name, int32_t value) { if( args_to_send <= 0 ) diff --git a/examples/arduino/EspLinkSample/WebServer.h b/examples/arduino/EspLinkSample/WebServer.h index 28365be..e1742b6 100644 --- a/examples/arduino/EspLinkSample/WebServer.h +++ b/examples/arduino/EspLinkSample/WebServer.h @@ -73,6 +73,7 @@ class WebServer void setArgNum(uint8_t num); void setArgInt(const char * name, int32_t value); + void setArgJson(const char * name, const char * value); void setArgString(const char * name, const char * value); void setArgStringP(const char * name, const char * value);