Stairs/src/main.cpp

153 lines
3.0 KiB
C++
Raw Normal View History

/*
* Stairs lighting
* Copyright 2017 (c) Mark van Renswoude
*
* https://git.x2software.net/pub/Stairs
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
//#include <ESPAsyncTCP.h>
#include <WiFiUDP.h>
#include <ESPAsyncWebServer.h>
#include <ESPAsyncTCP.h>
2018-01-05 20:15:04 +00:00
#include <TimeLib.h>
#include <ArduinoJson.h>
extern "C" {
#include <user_interface.h>
}
#include "./config.h"
#include "./debug.h"
#include "./global.h"
#include "./components/PCA9685.h"
#include "./settings/connection.h"
#include "./server/static.h"
#include "./server/settings.h"
#include "./server/firmware.h"
#include "./server/api.h"
#include "./main.wifi.h"
#include "./main.debug.h"
#include "./main.led.h"
#include "./main.triggers.h"
ADC_MODE(ADC_VCC);
// Forward declarations
void handleNotFound(AsyncWebServerRequest* request);
AsyncWebServer server(80);
PCA9685* pwmDriver;
void setup()
{
_dinit();
currentTime = millis();
if (!SPIFFS.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);
initMotionPins();
_dln("Setup :: initializing PCA9685");
pwmDriver = new PCA9685();
pwmDriver->setAddress(systemSettings->pwmDriverAddress(), systemSettings->pinPWMDriverSDA(), systemSettings->pinPWMDriverSCL());
pwmDriver->setPWMFrequency(systemSettings->pwmDriverFrequency());
pwmDriver->setAll(0);
_dln("Setup :: initializing Stairs");
stairs = new Stairs();
stairs->init(pwmDriver);
_dln("Setup :: starting initialization sequence");
stairs->set(0, 255);
delay(300);
uint8_t stepCount = stepsSettings->count();
for (int step = 1; step < stepCount; step++)
{
stairs->set(step - 1, 0);
stairs->set(step, 255);
delay(300);
}
stairs->set(stepCount - 1, 0);
_dln("Setup :: initializing WiFi");
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
initDebug();
initWiFi();
_dln("Setup :: registering routes");
registerStaticRoutes(&server);
registerSettingsRoutes(&server);
registerFirmwareRoutes(&server);
registerAPIRoutes(&server);
_dln("Setup :: starting HTTP server");
server.onNotFound(handleNotFound);
server.begin();
}
void loop()
{
if (shouldReboot || systemSettingsChanged)
{
_dln("Loop :: reboot requested, so long and thanks for all the fish!");
delay(100);
ESP.restart();
}
if (motionTriggerSettingsChanged)
{
initMotionPins();
motionTriggerSettingsChanged = false;
}
currentTime = millis();
2018-01-05 20:15:04 +00:00
updateDebugStatus();
if (connectionSettingsChanged)
{
_dln("Loop :: connection settings changed");
initWiFi();
connectionSettingsChanged = false;
}
updateWiFi();
updateLED();
2018-01-05 20:15:04 +00:00
updateNTPClient();
checkTriggers();
stairs->tick();
}
void handleNotFound(AsyncWebServerRequest *request)
{
_d("HTTP :: not found: "); _dln(request->url());
request->send(404);
}