From 046275592262d14798b630e429350d12d78bc861 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Sun, 22 Jul 2018 14:55:10 +0200 Subject: [PATCH] further optimization --- wled00/NpbWrapper.h | 2 +- wled00/wled00.ino | 6 +--- wled00/wled10_ntp.ino | 28 ++++++++-------- wled00/wled12_alexa.ino | 71 +++++++++++++++++++++++------------------ 4 files changed, 56 insertions(+), 51 deletions(-) diff --git a/wled00/NpbWrapper.h b/wled00/NpbWrapper.h index f8638c7f8..b97ae8e5e 100644 --- a/wled00/NpbWrapper.h +++ b/wled00/NpbWrapper.h @@ -1,6 +1,6 @@ //this code is a modified version of https://github.com/Makuna/NeoPixelBus/issues/103 -#define WORKAROUND_ESP32_BITBANG +//#define WORKAROUND_ESP32_BITBANG //see https://github.com/Aircoookie/WLED/issues/2 for flicker free ESP32 support //uncomment this if red and green are swapped diff --git a/wled00/wled00.ino b/wled00/wled00.ino index 2b6b03435..1477ab771 100644 --- a/wled00/wled00.ino +++ b/wled00/wled00.ino @@ -264,8 +264,7 @@ WiFiUDP notifierUdp, rgbUdp; WiFiUDP ntpUdp; IPAddress ntpServerIP; unsigned int ntpLocalPort = 2390; -const uint16_t NTP_PACKET_SIZE = 48; -byte ntpPacketBuffer[NTP_PACKET_SIZE]; +#define NTP_PACKET_SIZE 48 unsigned long ntpLastSyncTime = 999000000L; unsigned long ntpPacketSentTime = 999000000L; @@ -364,9 +363,6 @@ void loop() { handleBlynk(); if (briT) strip.service(); //do not update strip if off, prevents flicker on ESP32 } - /*#ifdef ARDUINO_ARCH_ESP32 - delay(1); - #endif*/ //DEBUG #ifdef DEBUG diff --git a/wled00/wled10_ntp.ino b/wled00/wled10_ntp.ino index 61e0c7f60..2656a1062 100644 --- a/wled00/wled10_ntp.ino +++ b/wled00/wled10_ntp.ino @@ -75,20 +75,20 @@ void sendNTPPacket() WiFi.hostByName(ntpServerName, ntpServerIP); DEBUG_PRINTLN("send NTP packet"); - memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE); + memset(obuf, 0, NTP_PACKET_SIZE); - ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode - ntpPacketBuffer[1] = 0; // Stratum, or type of clock - ntpPacketBuffer[2] = 6; // Polling Interval - ntpPacketBuffer[3] = 0xEC; // Peer Clock Precision + obuf[0] = 0b11100011; // LI, Version, Mode + obuf[1] = 0; // Stratum, or type of clock + obuf[2] = 6; // Polling Interval + obuf[3] = 0xEC; // Peer Clock Precision // 8 bytes of zero for Root Delay & Root Dispersion - ntpPacketBuffer[12] = 49; - ntpPacketBuffer[13] = 0x4E; - ntpPacketBuffer[14] = 49; - ntpPacketBuffer[15] = 52; + obuf[12] = 49; + obuf[13] = 0x4E; + obuf[14] = 49; + obuf[15] = 52; ntpUdp.beginPacket(ntpServerIP, 123); //NTP requests are to port 123 - ntpUdp.write(ntpPacketBuffer, NTP_PACKET_SIZE); + ntpUdp.write((byte*)obuf, NTP_PACKET_SIZE); ntpUdp.endPacket(); } @@ -96,13 +96,13 @@ bool checkNTPResponse() { int cb = ntpUdp.parsePacket(); if (cb) { - DEBUG_PRINT("packet received, length="); + DEBUG_PRINT("packet received, l="); DEBUG_PRINTLN(cb); - ntpUdp.read(ntpPacketBuffer, NTP_PACKET_SIZE); // read the packet into the buffer + ntpUdp.read(obuf, NTP_PACKET_SIZE); // read the packet into the buffer - unsigned long highWord = word(ntpPacketBuffer[40], ntpPacketBuffer[41]); - unsigned long lowWord = word(ntpPacketBuffer[42], ntpPacketBuffer[43]); + unsigned long highWord = word(obuf[40], obuf[41]); + unsigned long lowWord = word(obuf[42], obuf[43]); if (highWord == 0 && lowWord == 0) return false; unsigned long secsSince1900 = highWord << 16 | lowWord; diff --git a/wled00/wled12_alexa.ino b/wled00/wled12_alexa.ino index 8297edad1..7b48f5914 100644 --- a/wled00/wled12_alexa.ino +++ b/wled00/wled12_alexa.ino @@ -53,12 +53,7 @@ void alexaOn() applyMacro(macroAlexaOn); } - String body = "[{\"success\":{\"/lights/1/state/on\":true}}]"; - - server.send(200, "text/xml", body.c_str()); - - DEBUG_PRINT("Sending :"); - DEBUG_PRINTLN(body); + server.send(200, "application/json", "[{\"success\":{\"/lights/1/state/on\":true}}]"); } void alexaOff() @@ -71,19 +66,17 @@ void alexaOff() applyMacro(macroAlexaOff); } - String body = "[{\"success\":{\"/lights/1/state/on\":false}}]"; - - server.send(200, "application/json", body.c_str()); - - DEBUG_PRINT("Sending:"); - DEBUG_PRINTLN(body); + server.send(200, "application/json", "[{\"success\":{\"/lights/1/state/on\":false}}]"); } void alexaDim(byte briL) { - String body = "[{\"success\":{\"/lights/1/state/bri\":"+ String(briL) +"}}]"; + olen = 0; + oappend("[{\"success\":{\"/lights/1/state/bri\":"); + oappendi(briL); + oappend("}}]"); - server.send(200, "application/json", body.c_str()); + server.send(200, "application/json", obuf); String ct = (alexaNotify)?"win&IN&A=":"win&NN&IN&A="; if (briL < 255) @@ -113,22 +106,29 @@ void respondToSearch() { char s[16]; sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]); - String response = + olen = 0; + oappend( "HTTP/1.1 200 OK\r\n" "EXT:\r\n" "CACHE-CONTROL: max-age=100\r\n" // SSDP_INTERVAL - "LOCATION: http://"+ String(s) +":80/description.xml\r\n" + "LOCATION: http://"); + oappend(s); + oappend(":80/description.xml\r\n" "SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/1.17.0\r\n" // _modelName, _modelNumber - "hue-bridgeid: "+ escapedMac +"\r\n" + "hue-bridgeid: "); + oappend((char*)escapedMac.c_str()); + oappend("\r\n" "ST: urn:schemas-upnp-org:device:basic:1\r\n" // _deviceType - "USN: uuid:2f402f80-da50-11e1-9b23-"+ escapedMac +"::upnp:rootdevice\r\n" // _uuid::_deviceType - "\r\n"; + "USN: uuid:2f402f80-da50-11e1-9b23-"); + oappend((char*)escapedMac.c_str()); + oappend("::upnp:rootdevice\r\n" // _uuid::_deviceType + "\r\n"); UDP.beginPacket(UDP.remoteIP(), UDP.remotePort()); #ifdef ARDUINO_ARCH_ESP32 - UDP.write((byte*)response.c_str(), response.length()); + UDP.write((byte*)obuf, olen); #else - UDP.write(response.c_str()); + UDP.write(obuf); #endif UDP.endPacket(); @@ -143,22 +143,31 @@ void alexaInitPages() { IPAddress localIP = WiFi.localIP(); char s[16]; sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]); - - String setup_xml = "" + + olen = 0; + oappend("" "" "10" - "http://"+ String(s) +":80/" + "http://"); + oappend(s); + oappend(":80/" "" "urn:schemas-upnp-org:device:Basic:1" - "Philips hue ("+ String(s) +")" + "Philips hue ("); + oappend(s); + oappend(")" "Royal Philips Electronics" "http://www.philips.com" "Philips hue Personal Wireless Lighting" "Philips hue bridge 2012" "929000226503" "http://www.meethue.com" - ""+ escapedMac +"" - "uuid:2f402f80-da50-11e1-9b23-"+ escapedMac +"" + ""); + oappend((char*)escapedMac.c_str()); + oappend("" + "uuid:2f402f80-da50-11e1-9b23-"); + oappend((char*)escapedMac.c_str()); + oappend("" "index.html" "" " " @@ -177,9 +186,9 @@ void alexaInitPages() { " " "" "" - ""; + ""); - server.send(200, "text/xml", setup_xml.c_str()); + server.send(200, "text/xml", obuf); DEBUG_PRINT("Sending :"); DEBUG_PRINTLN(setup_xml); @@ -201,9 +210,9 @@ void alexaInitPages() { server.on("/status.html", HTTP_GET, [](){ DEBUG_PRINTLN("Got status request"); - String statrespone = "0"; + char statrespone[] = "0"; if (bri > 0) { - statrespone = "1"; + statrespone[0] = '1'; } server.send(200, "text/plain", statrespone);