rgbwifi/src/main.cpp

217 lines
4.2 KiB
C++

/*
* ESP8266 RGBW controller
* Copyright 2020 (c) Mark van Renswoude
*
* https://git.x2software.net/pub/RGBWifi
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ESPAsyncTCP.h>
#include <NeoPixelBus.h>
#include <LittleFS.h>
#include "./config.h"
#include "./debug.h"
#include "./global.h"
#include "./main.wifi.h"
#include "./main.led.h"
#include "./main.debug.h"
#include "./server/static.h"
#include "./server/settings.h"
#include "./server/firmware.h"
#include "./server/api.h"
// Forward declarations
void handleNotFound(AsyncWebServerRequest* request);
AsyncWebServer server(80);
NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod>* strip = NULL;
#define colorSaturation 128
RgbwColor red(colorSaturation, 0, 0, 0);
RgbwColor green(0, colorSaturation, 0, 0);
RgbwColor blue(0, 0, colorSaturation, 0);
RgbwColor white(colorSaturation);
RgbwColor black(0);
void setup()
{
_dinit();
_dln("Initializing LED strip");
strip = new NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod>(5);
strip->Begin();
strip->Show();
currentTime = millis();
if (!LittleFS.begin())
_dln("Setup :: failed to mount file system");
connectionSettings->read();
systemSettings->read();
/*
stepsSettings->read();
timeTriggerSettings->read();
motionTriggerSettings->read();
*/
pinMode(systemSettings->pinAPButton(), INPUT_PULLUP);
pinMode(systemSettings->pinLEDAP(), OUTPUT);
pinMode(systemSettings->pinLEDSTA(), OUTPUT);
_dln("Setup :: starting initialization sequence");
/*
uint8_t bottomStep = stepsSettings->count() - 1;
for (uint8_t i = 0; i < InitialisationBlinkCount; i++)
{
stairs->set(bottomStep, InitialisationBrightness, InitialisationFadeTime);
waitForTransition();
stairs->set(bottomStep, 0, InitialisationFadeTime);
waitForTransition();
}
*/
_dln("Setup :: initializing WiFi");
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
initDebug();
initWiFi();
_dln("Setup :: registering routes");
registerStaticRoutes(&server);
registerAPIRoutes(&server);
registerSettingsRoutes(&server);
registerFirmwareRoutes(&server);
_dln("Setup :: starting HTTP server");
server.onNotFound(handleNotFound);
server.begin();
}
void loop()
{
/*
_dln("Looping");
delay(1000);
// set the colors,
// if they don't match in order, you need to use NeoGrbFeature feature
strip->SetPixelColor(0, red);
strip->SetPixelColor(1, green);
strip->SetPixelColor(2, blue);
strip->SetPixelColor(3, white);
strip->SetPixelColor(4, black);
// the following line demonstrates rgbw color support
// if the NeoPixels are rgbw types the following line will compile
// if the NeoPixels are anything else, the following line will give an error
//strip->SetPixelColor(3, RgbwColor(colorSaturation));
strip->Show();
delay(1000);
// turn off the pixels
strip->SetPixelColor(0, black);
strip->SetPixelColor(1, black);
strip->SetPixelColor(2, black);
strip->SetPixelColor(3, black);
strip->SetPixelColor(4, black);
strip->Show();
delay(1000);
return;
*/
if (shouldReboot || systemSettingsChanged)
{
_dln("Loop :: reboot requested, so long and thanks for all the fish!");
delay(100);
ESP.restart();
}
currentTime = millis();
updateDebugStatus();
if (connectionSettingsChanged)
{
_dln("Loop :: connection settings changed");
initWiFi();
connectionSettingsChanged = false;
}
updateWiFi();
updateLED();
//updateNTPClient();
//checkTriggers();
//stairs->tick();
}
void handleNotFound(AsyncWebServerRequest *request)
{
_d("HTTP :: not found: "); _dln(request->url());
request->send(404);
}
/*
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <ESPAsyncWebServer.h>
#include <ESPAsyncTCP.h>
#include <TimeLib.h>
#include <ArduinoJson.h>
#include <EspSaveCrash.h>
extern "C" {
#include <user_interface.h>
}
#include "./components/PCA9685.h"
#include "./settings/connection.h"
#include "./main.triggers.h"
ADC_MODE(ADC_VCC);
inline void waitForTransition()
{
while (stairs->inTransition())
{
currentTime = millis();
stairs->tick();
delay(1);
}
}
*/