Files
party-cam/src/settings_service.cpp
2026-01-12 00:53:54 +01:00

151 lines
5.3 KiB
C++

#include <WiFi.h>
#include <esp_camera.h>
#include <WiFiManager.h>
#include <ESPmDNS.h>
#include "settings_service.h"
const char *index_html = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Party Cam Config</title>
<style>
body { font-family: sans-serif; max-width: 400px; margin: 0 auto; padding: 20px; }
label { display: block; margin: 10px 0 5px; font-weight: bold; }
input[type=number], input[type=text] { width: 100%; padding: 8px; margin-bottom: 10px; }
input[type=submit] { background-color: #4CAF50; color: white; padding: 12px 20px; border: none; width: 100%; cursor: pointer; }
.group { border: 1px solid #ddd; padding: 15px; border-radius: 8px; margin-bottom: 20px; }
</style>
</head>
<body>
<h2>Party Cam Settings</h2>
<form action="/save" method="POST">
<div class="group">
<h3>Camera</h3>
<label>Contrast (-2 to 2):</label> <input type="number" name="contrast" value="%CONTRAST%">
<label>Brightness (-2 to 2):</label> <input type="number" name="brightness" value="%BRIGHTNESS%">
<label><input type="checkbox" name="vflip" %VFLIP%> Vertical Flip</label><br>
<label><input type="checkbox" name="hmirror" %HMIRROR%> Horizontal Mirror</label>
</div>
<div class="group">
<h3>Printer</h3>
<label>Heat Time (Density):</label> <input type="number" name="heat" value="%HEAT%">
</div>
<div class="group">
<h3>Upload (Webhook)</h3>
<label>Upload URL:</label> <input type="text" name="url" value="%URL%">
<label><input type="checkbox" name="upload" %UPLOAD%> Enable Upload</label>
</div>
<input type="submit" value="Save Settings">
</form>
</body>
</html>
)rawliteral";
SettingsService::SettingsService() : server(80) {}
void SettingsService::begin()
{
// 1. Load saved values
loadSettings();
WiFiManager wm;
wm.setConfigPortalTimeout(180); // 3 minutes
String apName = "PartyCam-Setup";
Serial.println("Connecting to WiFi...");
if (!wm.autoConnect(apName.c_str()))
{
Serial.println("WiFi failed to connect (Timeout). Starting in Offline Mode.");
}
else
{
Serial.println("WiFi Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("partycam"))
{
Serial.println("mDNS responder started: http://partycam.local");
}
}
// 3. Setup Web Server
setupRoutes();
server.begin();
}
void SettingsService::loadSettings()
{
prefs.begin("partycam", false); // Namespace "partycam"
currentSettings.contrast = prefs.getInt("contrast", 0);
currentSettings.brightness = prefs.getInt("bright", 0);
currentSettings.vFlip = prefs.getBool("vflip", false);
currentSettings.hMirror = prefs.getBool("hmirror", false);
currentSettings.heatTime = prefs.getInt("heat", 120);
currentSettings.uploadUrl = prefs.getString("url", "");
currentSettings.enableUpload = prefs.getBool("ena_upload", false);
prefs.end();
}
void SettingsService::save()
{
prefs.begin("partycam", false);
prefs.putInt("contrast", currentSettings.contrast);
prefs.putInt("bright", currentSettings.brightness);
prefs.putBool("vflip", currentSettings.vFlip);
prefs.putBool("hmirror", currentSettings.hMirror);
prefs.putInt("heat", currentSettings.heatTime);
prefs.putString("url", currentSettings.uploadUrl);
prefs.putBool("ena_upload", currentSettings.enableUpload);
prefs.end();
}
void SettingsService::setupRoutes()
{
server.on("/", HTTP_GET, [this]()
{
String html = index_html;
// Simple template replacement
html.replace("%CONTRAST%", String(currentSettings.contrast));
html.replace("%BRIGHTNESS%", String(currentSettings.brightness));
html.replace("%VFLIP%", currentSettings.vFlip ? "checked" : "");
html.replace("%HMIRROR%", currentSettings.hMirror ? "checked" : "");
html.replace("%HEAT%", String(currentSettings.heatTime));
html.replace("%URL%", currentSettings.uploadUrl);
html.replace("%UPLOAD%", currentSettings.enableUpload ? "checked" : "");
server.send(200, "text/html", html); });
server.on("/save", HTTP_POST, [this]()
{
if (server.hasArg("contrast")) currentSettings.contrast = server.arg("contrast").toInt();
if (server.hasArg("brightness")) currentSettings.brightness = server.arg("brightness").toInt();
if (server.hasArg("heat")) currentSettings.heatTime = server.arg("heat").toInt();
currentSettings.uploadUrl = server.arg("url");
currentSettings.vFlip = server.hasArg("vflip");
currentSettings.hMirror = server.hasArg("hmirror");
currentSettings.enableUpload = server.hasArg("upload");
save();
server.send(200, "text/html", "Settings Saved! <a href='/'>Go Back</a>");
// Apply immediate camera changes if needed
sensor_t *s = esp_camera_sensor_get();
if (s) {
s->set_contrast(s, currentSettings.contrast);
s->set_brightness(s, currentSettings.brightness);
s->set_vflip(s, currentSettings.vFlip ? 1 : 0);
s->set_hmirror(s, currentSettings.hMirror ? 1 : 0);
} });
}
void SettingsService::handle()
{
server.handleClient();
}