add bed cooler project.
This commit is contained in:
68
src/core/device.cpp
Normal file
68
src/core/device.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "app.h"
|
||||
|
||||
#include <ESPmDNS.h>
|
||||
#include <LittleFS.h>
|
||||
|
||||
void applyLed() {
|
||||
uint8_t pct = constrain(ledBrightness, 0, 100);
|
||||
uint8_t duty = map(pct, 0, 100, 0, 255);
|
||||
if (ledInverted) duty = 255 - duty;
|
||||
analogWrite(LED_BUILTIN, duty);
|
||||
}
|
||||
|
||||
void factoryReset() {
|
||||
prefs.clear();
|
||||
LittleFS.remove(LOG_FILE_PATH);
|
||||
appendLog(LOG_SECURITY_AUDIT, "factory reset requested");
|
||||
}
|
||||
|
||||
void checkFactoryResetPin() {
|
||||
pinMode(FACTORY_RESET_PIN, INPUT_PULLUP);
|
||||
if (digitalRead(FACTORY_RESET_PIN) != FACTORY_RESET_ACTIVE_LEVEL) return;
|
||||
uint32_t start = millis();
|
||||
while (millis() - start < FACTORY_RESET_HOLD_MS) {
|
||||
delay(100);
|
||||
if (digitalRead(FACTORY_RESET_PIN) != FACTORY_RESET_ACTIVE_LEVEL) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
factoryReset();
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
bool connectWifi() {
|
||||
String ssid = prefString("wifiSsid", "");
|
||||
String pass = prefString("wifiPass", "");
|
||||
if (!ssid.length()) return false;
|
||||
String name = deviceName();
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setHostname(name.c_str());
|
||||
if (!networkDhcp) {
|
||||
IPAddress ip;
|
||||
IPAddress gateway;
|
||||
IPAddress subnet;
|
||||
IPAddress dns1;
|
||||
IPAddress dns2;
|
||||
bool ok = ip.fromString(networkIp) && gateway.fromString(networkGateway) && subnet.fromString(networkSubnet);
|
||||
if (ok) {
|
||||
if (!networkDns1.length() || !dns1.fromString(networkDns1)) dns1 = gateway;
|
||||
if (!networkDns2.length() || !dns2.fromString(networkDns2)) dns2 = IPAddress(0, 0, 0, 0);
|
||||
if (!WiFi.config(ip, gateway, subnet, dns1, dns2)) appendLog(LOG_WARN, "static IP configuration failed; continuing with DHCP");
|
||||
} else {
|
||||
appendLog(LOG_WARN, "static IP configuration is incomplete or invalid; continuing with DHCP");
|
||||
}
|
||||
}
|
||||
WiFi.begin(ssid.c_str(), pass.c_str());
|
||||
appendLog(LOG_INFO, "connecting to WiFi " + ssid);
|
||||
uint32_t start = millis();
|
||||
while (WiFi.status() != WL_CONNECTED && millis() - start < 20000) {
|
||||
delay(250);
|
||||
}
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
MDNS.begin(name.c_str());
|
||||
appendLog(LOG_INFO, "WiFi connected " + WiFi.localIP().toString());
|
||||
return true;
|
||||
}
|
||||
appendLog(LOG_WARN, "WiFi connection failed; entering setup AP");
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user