Refactored code to improve readability (merge dev PR)

Refactored code to improve readability
Fixed non-2-char indentations
This commit is contained in:
Aircoookie 2018-11-09 17:02:58 +01:00 committed by GitHub
commit a3bef49124
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 1127 additions and 1115 deletions

View File

@ -10,6 +10,7 @@
//uncomment this if red and green are swapped
//#define SWAPRG
//automatically uses the right driver method for each platform
#ifdef ARDUINO_ARCH_ESP32
#ifdef WORKAROUND_ESP32_BITBANG
@ -32,11 +33,11 @@
//handle swapping Red and Green automatically
#ifdef SWAPRG
#define PIXELFEATURE3 NeoRgbFeature
#define PIXELFEATURE4 NeoRgbwFeature
#define PIXELFEATURE3 NeoRgbFeature
#define PIXELFEATURE4 NeoRgbwFeature
#else
#define PIXELFEATURE3 NeoGrbFeature
#define PIXELFEATURE4 NeoGrbwFeature
#define PIXELFEATURE3 NeoGrbFeature
#define PIXELFEATURE4 NeoGrbwFeature
#endif
#include <NeoPixelBrightnessBus.h>
@ -71,8 +72,8 @@ public:
cleanup();
_type = type;
switch (_type) {
switch (_type)
{
case NeoPixelType_Grb:
_pGrb = new NeoPixelBrightnessBus<PIXELFEATURE3,PIXELMETHOD>(countPixels, LEDPIN);
_pGrb->Begin();
@ -94,7 +95,8 @@ public:
#endif
#endif
switch (_type) {
switch (_type)
{
case NeoPixelType_Grb: _pGrb->Show(); break;
case NeoPixelType_Grbw: _pGrbw->Show(); break;
}
@ -105,6 +107,7 @@ public:
#endif
#endif
}
bool CanShow() const
{
switch (_type) {
@ -146,8 +149,8 @@ public:
return 0;
}
// NOTE: Due to feature differences, some support RGBW but the method name
// here needs to be unique, thus GetPixeColorRgbw
// NOTE: Due to feature differences, some support RGBW but the method name
// here needs to be unique, thus GetPixeColorRgbw
RgbwColor GetPixelColorRgbw(uint16_t indexPixel) const
{
switch (_type) {

View File

@ -67,9 +67,11 @@ void WS2812FX::service() {
if(_running || _triggered) {
unsigned long now = millis(); // Be aware, millis() rolls over every 49 days
bool doShow = false;
for(uint8_t i=0; i < _num_segments; i++) {
for(uint8_t i=0; i < _num_segments; i++)
{
_segment_index = i;
if(now > SEGMENT_RUNTIME.next_time || _triggered) {
if(now > SEGMENT_RUNTIME.next_time || _triggered)
{
doShow = true;
handle_palette();
uint16_t delay = (this->*_mode[SEGMENT.mode])();

View File

@ -40,6 +40,8 @@
#define WS2812FX_h
#include "NpbWrapper.h"
#define FASTLED_INTERNAL //remove annoying pragma messages
#include "FastLED.h"
#define DEFAULT_BRIGHTNESS (uint8_t)50

View File

@ -30,29 +30,29 @@
//library inclusions
#include <Arduino.h>
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#include <ESPmDNS.h>
#include "src/dependencies/webserver/WebServer.h"
#include <HTTPClient.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include "src/dependencies/webserver/WebServer.h"
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#endif
#include <EEPROM.h>
#include <WiFiUDP.h>
#include <DNSServer.h>
#ifndef WLED_DISABLE_OTA
#include <ArduinoOTA.h>
#include "src/dependencies/webserver/ESP8266HTTPUpdateServer.h"
#include <ArduinoOTA.h>
#include "src/dependencies/webserver/ESP8266HTTPUpdateServer.h"
#endif
#include "src/dependencies/time/Time.h"
#include "src/dependencies/time/TimeLib.h"
#include "src/dependencies/timezone/Timezone.h"
#ifndef WLED_DISABLE_BLYNK
#include "src/dependencies/blynk/BlynkSimpleEsp.h"
#include "src/dependencies/blynk/BlynkSimpleEsp.h"
#endif
#include "src/dependencies/e131/E131.h"
#include "src/dependencies/pubsubclient/PubSubClient.h"
@ -63,7 +63,7 @@
//version code in format yymmddb (b = daily build)
#define VERSION 1811071
#define VERSION 1811091
char versionString[] = "0.8.1";
@ -357,6 +357,7 @@ bool auxActive = false, auxActiveBefore = false;
//alexa udp
WiFiUDP alexaUDP;
bool alexaUdpConnected = false;
IPAddress ipMulti(239, 255, 255, 250);
unsigned int portMulti = 1900;
String escapedMac;
@ -381,16 +382,16 @@ uint16_t olen = 0;
//server library objects
#ifdef ARDUINO_ARCH_ESP32
WebServer server(80);
WebServer server(80);
#else
ESP8266WebServer server(80);
ESP8266WebServer server(80);
#endif
HTTPClient* hueClient = NULL;
WiFiClient* mqttTCPClient = NULL;
PubSubClient* mqtt = NULL;
#ifndef WLED_DISABLE_OTA
ESP8266HTTPUpdateServer httpUpdater;
ESP8266HTTPUpdateServer httpUpdater;
#endif
//udp interface objects
@ -417,8 +418,8 @@ WS2812FX strip = WS2812FX();
//filesystem
#ifdef USEFS
#include <FS.h>;
File fsUploadFile;
#include <FS.h>;
File fsUploadFile;
#endif
//gamma 2.4 lookup table used for color correction
@ -503,7 +504,6 @@ void loop() {
yield();
if (!realtimeActive) //block stuff if WARLS/Adalight is enabled
{
if (dnsActive) dnsServer.processNextRequest();

View File

@ -2,17 +2,12 @@
* Sending XML status files to client
*/
//build XML response to HTTP /win API request
void XML_response(bool isHTTP)
{
olen = 0;
oappend("<?xml version = \"1.0\" ?><vs><ac>");
if (nightlightActive && nightlightFade)
{
oappendi(briT);
} else
{
oappendi(bri);
}
oappendi((nightlightActive && nightlightFade) ? briT : bri);
oappend("</ac>");
for (int i = 0; i < 3; i++)
@ -21,6 +16,7 @@ void XML_response(bool isHTTP)
oappendi(col[i]);
oappend("</cl>");
}
oappend("<ns>");
oappendi(notifyDirect);
oappend("</ns><nr>");
@ -55,7 +51,8 @@ void XML_response(bool isHTTP)
if (isHTTP) server.send(200, "text/xml", obuf);
}
void sappend(char stype, char* key, int val) //append a setting to string buffer
//append a numeric setting to string buffer
void sappend(char stype, char* key, int val)
{
char ds[] = "d.Sf.";
@ -85,7 +82,8 @@ void sappend(char stype, char* key, int val) //append a setting to string buffer
}
}
void sappends(char stype, char* key, char* val) //append a string setting
//append a string setting to buffer
void sappends(char stype, char* key, char* val)
{
switch(stype)
{
@ -106,7 +104,8 @@ void sappends(char stype, char* key, char* val) //append a string setting
}
}
void getSettingsJS(byte subPage) //get values for settings form in javascript
//get values for settings form in javascript
void getSettingsJS(byte subPage)
{
//0: menu 1: wifi 2: leds 3: ui 4: sync 5: time 6: sec
DEBUG_PRINT("settings resp");

View File

@ -61,7 +61,9 @@ void handleSettingsSet(byte subPage)
#endif
useRGBW = server.hasArg("EW");
autoRGBtoRGBW = server.hasArg("AW");
if (server.hasArg("IS")) //ignore settings and save current brightness, colors and fx as default
//ignore settings and save current brightness, colors and fx as default
if (server.hasArg("IS"))
{
colS[0] = col[0];
colS[1] = col[1];
@ -170,7 +172,9 @@ void handleSettingsSet(byte subPage)
strcpy(alexaInvocationName, server.arg("AI").c_str());
notifyAlexa = server.hasArg("SA");
if (server.hasArg("BK") && !server.arg("BK").equals("Hidden")) {strcpy(blynkApiKey,server.arg("BK").c_str()); initBlynk(blynkApiKey);}
if (server.hasArg("BK") && !server.arg("BK").equals("Hidden")) {
strcpy(blynkApiKey,server.arg("BK").c_str()); initBlynk(blynkApiKey);
}
strcpy(mqttServer, server.arg("MS").c_str());
strcpy(mqttDeviceTopic, server.arg("MD").c_str());
@ -209,7 +213,9 @@ void handleSettingsSet(byte subPage)
useAMPM = !server.hasArg("CF");
currentTimezone = server.arg("TZ").toInt();
utcOffsetSecs = server.arg("UO").toInt();
if (ntpEnabled && WiFi.status() == WL_CONNECTED && !ntpConnected) ntpConnected = ntpUdp.begin(ntpLocalPort); //start if not already connected
//start ntp if not already connected
if (ntpEnabled && WiFi.status() == WL_CONNECTED && !ntpConnected) ntpConnected = ntpUdp.begin(ntpLocalPort);
if (server.hasArg("OL")){
overlayDefault = server.arg("OL").toInt();
@ -690,6 +696,7 @@ bool handleSet(String req)
presetApplyFx = true;
if (req.indexOf("PX=0") > 0) presetApplyFx = false;
}
pos = req.indexOf("PS="); //saves current in preset
if (pos > 0) {
savePreset(req.substring(pos + 3).toInt());
@ -724,6 +731,7 @@ bool handleSet(String req)
if (overlayCurrent == 3) strip.setCronixieBacklight(cronixieBacklight);
overlayRefreshedTime = 0;
}
pos = req.indexOf("U0="); //user var 0
if (pos > 0) {
userVar0 = req.substring(pos + 3).toInt();

View File

@ -3,18 +3,20 @@
*/
void handleSerial()
{
if (Serial.available() > 0) //support for Adalight protocol to high-speed control LEDs over serial (gamma correction done by PC)
{
if (Serial.find("Ada"))
if (Serial.available() > 0) //support for Adalight protocol to high-speed control LEDs over serial
{
if (!Serial.find("Ada")) return;
if (!realtimeActive && bri == 0) strip.setBrightness(briLast);
arlsLock(realtimeTimeoutMs);
delay(1);
byte hi = Serial.read();
byte ledc = Serial.read();
byte chk = Serial.read();
if(chk != (hi ^ ledc ^ 0x55)) return;
if (ledCount < ledc) ledc = ledCount;
byte sc[3]; int t =-1; int to = 0;
for (int i=0; i < ledc; i++)
{
@ -33,7 +35,6 @@ void handleSerial()
}
strip.show();
}
}
}
#ifdef USEFS

View File

@ -139,7 +139,9 @@ void initStrip()
if (bootPreset>0) applyPreset(bootPreset, turnOnAtBoot, true, true);
colorUpdated(0);
if(digitalRead(buttonPin) == LOW) buttonEnabled = false; //disable button if it is "pressed" unintentionally
//disable button if it is "pressed" unintentionally
if(digitalRead(buttonPin) == LOW) buttonEnabled = false;
}
@ -173,7 +175,8 @@ void initCon()
WiFi.softAPdisconnect(true);
}
int fail_count = 0;
if (strlen(clientSSID) <1 || strcmp(clientSSID,"Your_Network") == 0) fail_count = apWaitTimeSecs*2; //instantly go to ap mode
if (strlen(clientSSID) <1 || strcmp(clientSSID,"Your_Network") == 0)
fail_count = apWaitTimeSecs*2; //instantly go to ap mode
#ifndef ARDUINO_ARCH_ESP32
WiFi.hostname(serverDescription);
#endif
@ -211,9 +214,9 @@ void initCon()
}
//fill string buffer with build info
void getBuildInfo()
{
//fill string buffer with build info
olen = 0;
oappend("hard-coded build info:\r\n\n");
#ifdef ARDUINO_ARCH_ESP32

View File

@ -118,7 +118,8 @@ void handleNotifications()
}
//receive UDP notifications
if(udpConnected && (receiveNotifications || receiveDirect)){
if (!udpConnected || !(receiveNotifications || receiveDirect)) return;
uint16_t packetSize = notifierUdp.parsePacket();
//hyperion / raw RGB
@ -142,6 +143,7 @@ void handleNotifications()
return;
}
//notifier and UDP realtime
if (packetSize > UDP_IN_MAXSIZE) return;
if(packetSize && notifierUdp.remoteIP() != WiFi.localIP()) //don't process broadcasts we send ourselves
{
@ -248,7 +250,6 @@ void handleNotifications()
}
}
}
}
}
void setRealtimePixel(uint16_t i, byte r, byte g, byte b, byte w)

View File

@ -195,7 +195,7 @@ void handleNightlight()
float nper = (millis() - nightlightStartTime)/((float)nightlightDelayMs);
if (nightlightFade)
{
bri = briNlT+((nightlightTargetBri - briNlT)*nper);
bri = briNlT + ((nightlightTargetBri - briNlT)*nper);
colorUpdated(5);
}
if (nper >= 1)

View File

@ -16,25 +16,23 @@ void alexaInit()
{
if (alexaEnabled && WiFi.status() == WL_CONNECTED)
{
udpConnected = connectUDP();
alexaUdpConnected = connectUDP();
if (udpConnected) alexaInitPages();
if (alexaUdpConnected) alexaInitPages();
}
}
void handleAlexa()
{
if (alexaEnabled && WiFi.status() == WL_CONNECTED)
{
if(udpConnected){
// if theres data available, read a packet
if (!alexaEnabled || WiFi.status() != WL_CONNECTED || !alexaUdpConnected) return;
// if there's data available, read a packet
int packetSize = alexaUDP.parsePacket();
if(packetSize>0) {
if(packetSize < 1) return;
IPAddress remote = alexaUDP.remoteIP();
int len = alexaUDP.read(obuf, 254);
if (len > 0) {
obuf[len] = 0;
}
if (len > 0) obuf[len] = 0;
if(strstr(obuf,"M-SEARCH") > 0) {
if(strstr(obuf,"upnp:rootdevice") > 0 || strstr(obuf,"device:basic:1") > 0) {
@ -42,9 +40,6 @@ void handleAlexa()
respondToSearch();
}
}
}
}
}
}
void alexaOn()
@ -212,7 +207,6 @@ void alexaInitPages() {
statrespone[0] = '1';
}
server.send(200, "text/plain", statrespone);
});
}
@ -288,9 +282,10 @@ bool connectUDP(){
return state;
}
#else
void alexaInit(){}
void handleAlexa(){}
void alexaInitPages(){}
bool handleAlexaApiCall(String req, String body){return false;}
void alexaInit(){}
void handleAlexa(){}
void alexaInitPages(){}
bool handleAlexaApiCall(String req, String body){return false;}
#endif

View File

@ -25,29 +25,21 @@ void colorHStoRGB(uint16_t hue, byte sat, byte* rgb) //hue, sat to rgb
void colorCTtoRGB(uint16_t mired, byte* rgb) //white spectrum to rgb
{
//this is only an approximation using WS2812B with gamma correction enabled
if (mired > 475)
{
if (mired > 475) {
rgb[0]=255;rgb[1]=199;rgb[2]=92;//500
} else if (mired > 425)
{
} else if (mired > 425) {
rgb[0]=255;rgb[1]=213;rgb[2]=118;//450
} else if (mired > 375)
{
} else if (mired > 375) {
rgb[0]=255;rgb[1]=216;rgb[2]=118;//400
} else if (mired > 325)
{
} else if (mired > 325) {
rgb[0]=255;rgb[1]=234;rgb[2]=140;//350
} else if (mired > 275)
{
} else if (mired > 275) {
rgb[0]=255;rgb[1]=243;rgb[2]=160;//300
} else if (mired > 225)
{
} else if (mired > 225) {
rgb[0]=250;rgb[1]=255;rgb[2]=188;//250
} else if (mired > 175)
{
} else if (mired > 175) {
rgb[0]=247;rgb[1]=255;rgb[2]=215;//200
} else
{
} else {
rgb[0]=237;rgb[1]=255;rgb[2]=239;//150
}
}

View File

@ -14,6 +14,7 @@ void parseMQTTBriPayload(char* payload)
}
}
void callbackMQTT(char* topic, byte* payload, unsigned int length) {
DEBUG_PRINT("MQTT callb rec: ");
@ -37,6 +38,7 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length) {
}
}
void publishMQTT()
{
if (mqtt == NULL) return;
@ -64,6 +66,7 @@ void publishMQTT()
mqtt->publish(subuf, obuf);*/
}
bool reconnectMQTT()
{
if (mqtt->connect(escapedMac.c_str()))
@ -99,6 +102,7 @@ bool reconnectMQTT()
return mqtt->connected();
}
bool initMQTT()
{
if (WiFi.status() != WL_CONNECTED) return false;
@ -116,6 +120,7 @@ bool initMQTT()
return true;
}
void handleMQTT()
{
if (WiFi.status() != WL_CONNECTED || !mqttInit) return;

View File

@ -259,8 +259,12 @@ void serveIndex()
if (uiConfiguration == 0) serveMobile = checkClientIsMobile(server.header("User-Agent"));
else if (uiConfiguration == 2) serveMobile = true;
if (!realtimeActive || enableRealtimeUI) //do not serve while receiving realtime
if (realtimeActive && !enableRealtimeUI) //do not serve while receiving realtime
{
serveRealtimeError(false);
return;
}
if (serveMobile)
{
server.setContentLength(strlen_P(PAGE_indexM));
@ -276,9 +280,6 @@ void serveIndex()
server.sendContent_P(PAGE_index2);
server.sendContent_P(PAGE_index3);
}
} else {
serveRealtimeError(false);
}
}
void serveMessage(int code, String headl, String subl="", int optionType)
@ -314,15 +315,18 @@ void serveMessage(int code, String headl, String subl="", int optionType)
void serveSettings(byte subPage)
{
//0: menu 1: wifi 2: leds 3: ui 4: sync 5: time 6: sec 255: welcomepage
if (!realtimeActive || enableRealtimeUI) //do not serve while receiving realtime
if (realtimeActive && !enableRealtimeUI) //do not serve while receiving realtime
{
serveRealtimeError(true);
return;
}
#ifdef WLED_DISABLE_MOBILE_UI //disable welcome page if not enough storage
if (subPage == 255) {serveIndex(); return;}
#endif
int pl0, pl1;
switch (subPage)
switch (subPage) //0: menu 1: wifi 2: leds 3: ui 4: sync 5: time 6: sec 255: welcomepage
{
case 1: pl0 = strlen_P(PAGE_settings_wifi0); pl1 = strlen_P(PAGE_settings_wifi1); break;
case 2: pl0 = strlen_P(PAGE_settings_leds0); pl1 = strlen_P(PAGE_settings_leds1); break;
@ -365,7 +369,4 @@ void serveSettings(byte subPage)
case 255: server.sendContent_P(PAGE_welcome1); break;
default: server.sendContent_P(PAGE_settings1);
}
} else {
serveRealtimeError(true);
}
}