Reformat project using clang-format

This commit is contained in:
Lorow
2026-01-06 22:51:24 +01:00
parent 567d3ebd75
commit 555e290d71
70 changed files with 3282 additions and 3428 deletions

View File

@@ -2,166 +2,163 @@
#ifndef PROJECT_CONFIG_MODELS_HPP
#define PROJECT_CONFIG_MODELS_HPP
#include <Preferences.hpp>
#include <helpers.hpp>
#include <string>
#include <utility>
#include <vector>
#include <helpers.hpp>
#include "sdkconfig.h"
#include <Preferences.hpp>
#include "esp_log.h"
#include "sdkconfig.h"
struct BaseConfigModel
{
BaseConfigModel(Preferences *pref) : pref(pref) {}
BaseConfigModel(Preferences* pref) : pref(pref) {}
void load();
void save();
std::string toRepresentation();
void load();
void save();
std::string toRepresentation();
Preferences *pref;
Preferences* pref;
};
enum class StreamingMode
{
SETUP,
UVC,
WIFI,
SETUP,
UVC,
WIFI,
};
struct DeviceMode_t : BaseConfigModel
{
StreamingMode mode;
explicit DeviceMode_t(Preferences *pref) : BaseConfigModel(pref), mode(StreamingMode::SETUP) {}
StreamingMode mode;
explicit DeviceMode_t(Preferences* pref) : BaseConfigModel(pref), mode(StreamingMode::SETUP) {}
void load()
{
// Default mode can be controlled via sdkconfig:
// - If CONFIG_START_IN_UVC_MODE is enabled, default to UVC
// - Otherwise default to SETUP
int default_mode =
void load()
{
// Default mode can be controlled via sdkconfig:
// - If CONFIG_START_IN_UVC_MODE is enabled, default to UVC
// - Otherwise default to SETUP
int default_mode =
#if CONFIG_START_IN_UVC_MODE
static_cast<int>(StreamingMode::UVC);
static_cast<int>(StreamingMode::UVC);
#else
static_cast<int>(StreamingMode::SETUP);
static_cast<int>(StreamingMode::SETUP);
#endif
int stored_mode = this->pref->getInt("mode", default_mode);
this->mode = static_cast<StreamingMode>(stored_mode);
ESP_LOGI("DeviceMode", "Loaded device mode: %d", stored_mode);
}
int stored_mode = this->pref->getInt("mode", default_mode);
this->mode = static_cast<StreamingMode>(stored_mode);
ESP_LOGI("DeviceMode", "Loaded device mode: %d", stored_mode);
}
void save() const
{
this->pref->putInt("mode", static_cast<int>(this->mode));
ESP_LOGI("DeviceMode", "Saved device mode: %d", static_cast<int>(this->mode));
}
void save() const
{
this->pref->putInt("mode", static_cast<int>(this->mode));
ESP_LOGI("DeviceMode", "Saved device mode: %d", static_cast<int>(this->mode));
}
};
struct DeviceConfig_t : BaseConfigModel
{
DeviceConfig_t(Preferences *pref) : BaseConfigModel(pref) {}
DeviceConfig_t(Preferences* pref) : BaseConfigModel(pref) {}
std::string OTALogin;
std::string OTAPassword;
int led_external_pwm_duty_cycle;
int OTAPort;
std::string OTALogin;
std::string OTAPassword;
int led_external_pwm_duty_cycle;
int OTAPort;
void load()
{
this->OTALogin = this->pref->getString("OTALogin", "openiris");
this->OTAPassword = this->pref->getString("OTAPassword", "openiris");
this->OTAPort = this->pref->getInt("OTAPort", 3232);
void load()
{
this->OTALogin = this->pref->getString("OTALogin", "openiris");
this->OTAPassword = this->pref->getString("OTAPassword", "openiris");
this->OTAPort = this->pref->getInt("OTAPort", 3232);
#if CONFIG_LED_EXTERNAL_PWM_DUTY_CYCLE
this->led_external_pwm_duty_cycle = this->pref->getInt("led_ext_pwm", CONFIG_LED_EXTERNAL_PWM_DUTY_CYCLE);
this->led_external_pwm_duty_cycle = this->pref->getInt("led_ext_pwm", CONFIG_LED_EXTERNAL_PWM_DUTY_CYCLE);
#else
this->led_external_pwm_duty_cycle = this->pref->getInt("led_ext_pwm", 100);
this->led_external_pwm_duty_cycle = this->pref->getInt("led_ext_pwm", 100);
#endif
};
};
void save() const
{
this->pref->putString("OTALogin", this->OTALogin.c_str());
this->pref->putString("OTAPassword", this->OTAPassword.c_str());
this->pref->putInt("OTAPort", this->OTAPort);
this->pref->putInt("led_ext_pwm", this->led_external_pwm_duty_cycle);
};
void save() const
{
this->pref->putString("OTALogin", this->OTALogin.c_str());
this->pref->putString("OTAPassword", this->OTAPassword.c_str());
this->pref->putInt("OTAPort", this->OTAPort);
this->pref->putInt("led_ext_pwm", this->led_external_pwm_duty_cycle);
};
std::string toRepresentation() const
{
return Helpers::format_string(
"\"device_config\": {\"OTALogin\": \"%s\", \"OTAPassword\": \"%s\", "
"\"OTAPort\": %u, \"led_external_pwm_duty_cycle\": %u}",
this->OTALogin.c_str(), this->OTAPassword.c_str(), this->OTAPort, this->led_external_pwm_duty_cycle);
};
std::string toRepresentation() const
{
return Helpers::format_string(
"\"device_config\": {\"OTALogin\": \"%s\", \"OTAPassword\": \"%s\", "
"\"OTAPort\": %u, \"led_external_pwm_duty_cycle\": %u}",
this->OTALogin.c_str(), this->OTAPassword.c_str(), this->OTAPort, this->led_external_pwm_duty_cycle);
};
};
struct MDNSConfig_t : BaseConfigModel
{
MDNSConfig_t(Preferences *pref) : BaseConfigModel(pref) {}
MDNSConfig_t(Preferences* pref) : BaseConfigModel(pref) {}
std::string hostname;
std::string hostname;
void load()
{
// Default hostname comes from GENERAL_ADVERTISED_NAME (unified advertised name)
std::string default_hostname = CONFIG_GENERAL_ADVERTISED_NAME;
if (default_hostname.empty())
void load()
{
default_hostname = "openiristracker";
}
// Default hostname comes from GENERAL_ADVERTISED_NAME (unified advertised name)
std::string default_hostname = CONFIG_GENERAL_ADVERTISED_NAME;
if (default_hostname.empty())
{
default_hostname = "openiristracker";
}
this->hostname = this->pref->getString("hostname", default_hostname);
};
this->hostname = this->pref->getString("hostname", default_hostname);
};
void save() const
{
this->pref->putString("hostname", this->hostname.c_str());
};
void save() const
{
this->pref->putString("hostname", this->hostname.c_str());
};
std::string toRepresentation()
{
return Helpers::format_string(
"\"mdns_config\": {\"hostname\": \"%s\"}",
this->hostname.c_str());
};
std::string toRepresentation()
{
return Helpers::format_string("\"mdns_config\": {\"hostname\": \"%s\"}", this->hostname.c_str());
};
};
struct CameraConfig_t : BaseConfigModel
{
CameraConfig_t(Preferences *pref) : BaseConfigModel(pref) {}
CameraConfig_t(Preferences* pref) : BaseConfigModel(pref) {}
uint8_t vflip;
uint8_t href;
uint8_t framesize;
uint8_t quality;
uint8_t brightness;
uint8_t vflip;
uint8_t href;
uint8_t framesize;
uint8_t quality;
uint8_t brightness;
void load()
{
this->vflip = this->pref->getInt("vflip", 0);
this->href = this->pref->getInt("href", 0);
this->framesize = this->pref->getInt("framesize", 5);
this->quality = this->pref->getInt("quality", 7);
this->brightness = this->pref->getInt("brightness", 2);
};
void load()
{
this->vflip = this->pref->getInt("vflip", 0);
this->href = this->pref->getInt("href", 0);
this->framesize = this->pref->getInt("framesize", 5);
this->quality = this->pref->getInt("quality", 7);
this->brightness = this->pref->getInt("brightness", 2);
};
void save() const
{
this->pref->putInt("vflip", this->vflip);
this->pref->putInt("href", this->href);
this->pref->putInt("framesize", this->framesize);
this->pref->putInt("quality", this->quality);
this->pref->putInt("brightness", this->brightness);
};
void save() const
{
this->pref->putInt("vflip", this->vflip);
this->pref->putInt("href", this->href);
this->pref->putInt("framesize", this->framesize);
this->pref->putInt("quality", this->quality);
this->pref->putInt("brightness", this->brightness);
};
std::string toRepresentation()
{
return Helpers::format_string(
"\"camera_config\": {\"vflip\": %d,\"framesize\": %d,\"href\": "
"%d,\"quality\": %d,\"brightness\": %d}",
this->vflip, this->framesize, this->href, this->quality,
this->brightness);
};
std::string toRepresentation()
{
return Helpers::format_string(
"\"camera_config\": {\"vflip\": %d,\"framesize\": %d,\"href\": "
"%d,\"quality\": %d,\"brightness\": %d}",
this->vflip, this->framesize, this->href, this->quality, this->brightness);
};
};
// with wifi, we have to work a bit differently
@@ -170,177 +167,154 @@ struct CameraConfig_t : BaseConfigModel
// save them under an indexed name and load them as such.
struct WiFiConfig_t : BaseConfigModel
{
// default constructor used for loading
WiFiConfig_t(Preferences *pref) : BaseConfigModel(pref) {}
// default constructor used for loading
WiFiConfig_t(Preferences* pref) : BaseConfigModel(pref) {}
WiFiConfig_t(
Preferences *pref,
const uint8_t index,
std::string name,
std::string ssid,
std::string password,
const uint8_t channel,
const uint8_t power)
: BaseConfigModel(pref),
index(index),
name(std::move(name)),
ssid(std::move(ssid)),
password(std::move(password)),
channel(channel),
power(power) {}
WiFiConfig_t(Preferences* pref, const uint8_t index, std::string name, std::string ssid, std::string password, const uint8_t channel, const uint8_t power)
: BaseConfigModel(pref), index(index), name(std::move(name)), ssid(std::move(ssid)), password(std::move(password)), channel(channel), power(power)
{
}
uint8_t index;
std::string name;
std::string ssid;
std::string password;
uint8_t channel;
uint8_t power;
uint8_t index;
std::string name;
std::string ssid;
std::string password;
uint8_t channel;
uint8_t power;
void load(const uint8_t index)
{
this->index = index;
char buffer[2];
void load(const uint8_t index)
{
this->index = index;
char buffer[2];
auto const iter_str = std::string(Helpers::itoa(index, buffer, 10));
this->name = this->pref->getString(("name" + iter_str).c_str(), "");
this->ssid = this->pref->getString(("ssid" + iter_str).c_str(), "");
this->password = this->pref->getString(("password" + iter_str).c_str(), "");
this->channel = this->pref->getUInt(("channel" + iter_str).c_str());
this->power = this->pref->getUInt(("power" + iter_str).c_str());
auto const iter_str = std::string(Helpers::itoa(index, buffer, 10));
this->name = this->pref->getString(("name" + iter_str).c_str(), "");
this->ssid = this->pref->getString(("ssid" + iter_str).c_str(), "");
this->password = this->pref->getString(("password" + iter_str).c_str(), "");
this->channel = this->pref->getUInt(("channel" + iter_str).c_str());
this->power = this->pref->getUInt(("power" + iter_str).c_str());
ESP_LOGI("WiFiConfig", "Loaded network %d: name=%s, ssid=%s, channel=%d",
index, this->name.c_str(), this->ssid.c_str(), this->channel);
};
ESP_LOGI("WiFiConfig", "Loaded network %d: name=%s, ssid=%s, channel=%d", index, this->name.c_str(), this->ssid.c_str(), this->channel);
};
void save() const
{
char buffer[2];
auto const iter_str = std::string(Helpers::itoa(this->index, buffer, 10));
void save() const
{
char buffer[2];
auto const iter_str = std::string(Helpers::itoa(this->index, buffer, 10));
this->pref->putString(("name" + iter_str).c_str(), this->name.c_str());
this->pref->putString(("ssid" + iter_str).c_str(), this->ssid.c_str());
this->pref->putString(("password" + iter_str).c_str(), this->password.c_str());
this->pref->putUInt(("channel" + iter_str).c_str(), this->channel);
this->pref->putUInt(("power" + iter_str).c_str(), this->power);
this->pref->putString(("name" + iter_str).c_str(), this->name.c_str());
this->pref->putString(("ssid" + iter_str).c_str(), this->ssid.c_str());
this->pref->putString(("password" + iter_str).c_str(), this->password.c_str());
this->pref->putUInt(("channel" + iter_str).c_str(), this->channel);
this->pref->putUInt(("power" + iter_str).c_str(), this->power);
ESP_LOGI("WiFiConfig", "Saved network %d: name=%s, ssid=%s, channel=%d",
this->index, this->name.c_str(), this->ssid.c_str(), this->channel);
};
ESP_LOGI("WiFiConfig", "Saved network %d: name=%s, ssid=%s, channel=%d", this->index, this->name.c_str(), this->ssid.c_str(), this->channel);
};
std::string toRepresentation()
{
return Helpers::format_string(
"{\"name\": \"%s\", \"ssid\": \"%s\", \"password\": \"%s\", \"channel\": %u, \"power\": %u}",
this->name.c_str(), this->ssid.c_str(), this->password.c_str(),
this->channel, this->power);
};
std::string toRepresentation()
{
return Helpers::format_string("{\"name\": \"%s\", \"ssid\": \"%s\", \"password\": \"%s\", \"channel\": %u, \"power\": %u}", this->name.c_str(),
this->ssid.c_str(), this->password.c_str(), this->channel, this->power);
};
};
struct AP_WiFiConfig_t : BaseConfigModel
{
AP_WiFiConfig_t(Preferences *pref) : BaseConfigModel(pref) {}
AP_WiFiConfig_t(Preferences* pref) : BaseConfigModel(pref) {}
std::string ssid;
std::string password;
uint8_t channel;
std::string ssid;
std::string password;
uint8_t channel;
void load()
{
this->ssid = this->pref->getString("apSSID", CONFIG_WIFI_AP_SSID);
this->password = this->pref->getString("apPassword", CONFIG_WIFI_AP_PASSWORD);
};
void load()
{
this->ssid = this->pref->getString("apSSID", CONFIG_WIFI_AP_SSID);
this->password = this->pref->getString("apPassword", CONFIG_WIFI_AP_PASSWORD);
};
void save() const
{
this->pref->putString("apSSID", this->ssid.c_str());
this->pref->putString("apPass", this->password.c_str());
this->pref->putUInt("apChannel", this->channel);
};
void save() const
{
this->pref->putString("apSSID", this->ssid.c_str());
this->pref->putString("apPass", this->password.c_str());
this->pref->putUInt("apChannel", this->channel);
};
std::string toRepresentation()
{
return Helpers::format_string(
"\"ap_wifi_config\": {\"ssid\": \"%s\", \"password\": \"%s\", "
"\"channel\": %u}",
this->ssid.c_str(), this->password.c_str(), this->channel);
};
std::string toRepresentation()
{
return Helpers::format_string(
"\"ap_wifi_config\": {\"ssid\": \"%s\", \"password\": \"%s\", "
"\"channel\": %u}",
this->ssid.c_str(), this->password.c_str(), this->channel);
};
};
struct WiFiTxPower_t : BaseConfigModel
{
WiFiTxPower_t(Preferences *pref) : BaseConfigModel(pref) {}
WiFiTxPower_t(Preferences* pref) : BaseConfigModel(pref) {}
uint8_t power;
uint8_t power;
void load()
{
this->power = this->pref->getUInt("txpower", 52);
};
void load()
{
this->power = this->pref->getUInt("txpower", 52);
};
void save() const
{
this->pref->putUInt("txpower", this->power);
};
void save() const
{
this->pref->putUInt("txpower", this->power);
};
std::string toRepresentation()
{
return Helpers::format_string("\"wifi_tx_power\": {\"power\": %u}", this->power);
};
std::string toRepresentation()
{
return Helpers::format_string("\"wifi_tx_power\": {\"power\": %u}", this->power);
};
};
class TrackerConfig_t
{
public:
DeviceConfig_t device;
DeviceMode_t device_mode;
CameraConfig_t camera;
std::vector<WiFiConfig_t> networks;
AP_WiFiConfig_t ap_network;
MDNSConfig_t mdns;
WiFiTxPower_t txpower;
public:
DeviceConfig_t device;
DeviceMode_t device_mode;
CameraConfig_t camera;
std::vector<WiFiConfig_t> networks;
AP_WiFiConfig_t ap_network;
MDNSConfig_t mdns;
WiFiTxPower_t txpower;
TrackerConfig_t(
DeviceConfig_t device,
DeviceMode_t device_mode,
CameraConfig_t camera,
std::vector<WiFiConfig_t> networks,
AP_WiFiConfig_t ap_network,
MDNSConfig_t mdns,
WiFiTxPower_t txpower) : device(std::move(device)),
device_mode(std::move(device_mode)),
camera(std::move(camera)),
networks(std::move(networks)),
ap_network(std::move(ap_network)),
mdns(std::move(mdns)),
txpower(std::move(txpower)) {}
std::string toRepresentation()
{
std::string WifiConfigRepresentation;
// we need a valid json representation, so we can't have a dangling comma at the end
if (!this->networks.empty())
TrackerConfig_t(DeviceConfig_t device, DeviceMode_t device_mode, CameraConfig_t camera, std::vector<WiFiConfig_t> networks, AP_WiFiConfig_t ap_network,
MDNSConfig_t mdns, WiFiTxPower_t txpower)
: device(std::move(device)),
device_mode(std::move(device_mode)),
camera(std::move(camera)),
networks(std::move(networks)),
ap_network(std::move(ap_network)),
mdns(std::move(mdns)),
txpower(std::move(txpower))
{
if (this->networks.size() > 1)
{
for (auto i = 0; i < this->networks.size() - 1; i++)
{
WifiConfigRepresentation += Helpers::format_string("%s, ", this->networks[i].toRepresentation().c_str());
}
}
WifiConfigRepresentation += Helpers::format_string("%s", this->networks[networks.size() - 1].toRepresentation().c_str());
}
return Helpers::format_string(
"{%s, %s, %s, \"networks\": [%s], %s, %s}",
this->device.toRepresentation().c_str(),
this->mdns.toRepresentation().c_str(),
this->camera.toRepresentation().c_str(),
WifiConfigRepresentation.c_str(),
this->ap_network.toRepresentation().c_str(),
this->txpower.toRepresentation().c_str());
}
std::string toRepresentation()
{
std::string WifiConfigRepresentation;
// we need a valid json representation, so we can't have a dangling comma at the end
if (!this->networks.empty())
{
if (this->networks.size() > 1)
{
for (auto i = 0; i < this->networks.size() - 1; i++)
{
WifiConfigRepresentation += Helpers::format_string("%s, ", this->networks[i].toRepresentation().c_str());
}
}
WifiConfigRepresentation += Helpers::format_string("%s", this->networks[networks.size() - 1].toRepresentation().c_str());
}
return Helpers::format_string("{%s, %s, %s, \"networks\": [%s], %s, %s}", this->device.toRepresentation().c_str(),
this->mdns.toRepresentation().c_str(), this->camera.toRepresentation().c_str(), WifiConfigRepresentation.c_str(),
this->ap_network.toRepresentation().c_str(), this->txpower.toRepresentation().c_str());
}
};
#endif

View File

@@ -2,90 +2,88 @@
static auto CONFIGURATION_TAG = "[CONFIGURATION]";
int getNetworkCount(Preferences *pref)
int getNetworkCount(Preferences* pref)
{
return pref->getInt("networkcount", 0);
return pref->getInt("networkcount", 0);
}
void saveNetworkCount(Preferences *pref, const int count)
void saveNetworkCount(Preferences* pref, const int count)
{
pref->putInt("networkcount", count);
pref->putInt("networkcount", count);
}
ProjectConfig::ProjectConfig(Preferences *pref) : pref(pref),
_already_loaded(false),
config(DeviceConfig_t(pref),
DeviceMode_t(pref),
CameraConfig_t(pref),
std::vector<WiFiConfig_t>{},
AP_WiFiConfig_t(pref),
MDNSConfig_t(pref),
WiFiTxPower_t(pref)) {}
ProjectConfig::ProjectConfig(Preferences* pref)
: pref(pref),
_already_loaded(false),
config(DeviceConfig_t(pref), DeviceMode_t(pref), CameraConfig_t(pref), std::vector<WiFiConfig_t>{}, AP_WiFiConfig_t(pref), MDNSConfig_t(pref),
WiFiTxPower_t(pref))
{
}
ProjectConfig::~ProjectConfig() = default;
void ProjectConfig::save() const
{
ESP_LOGD(CONFIGURATION_TAG, "Saving project config");
this->config.device.save();
this->config.device_mode.save();
this->config.camera.save();
this->config.mdns.save();
this->config.txpower.save();
this->config.ap_network.save();
ESP_LOGD(CONFIGURATION_TAG, "Saving project config");
this->config.device.save();
this->config.device_mode.save();
this->config.camera.save();
this->config.mdns.save();
this->config.txpower.save();
this->config.ap_network.save();
auto const networks_count = static_cast<int>(this->config.networks.size());
for (int i = 0; i < networks_count; i++)
{
this->config.networks[i].save();
}
auto const networks_count = static_cast<int>(this->config.networks.size());
for (int i = 0; i < networks_count; i++)
{
this->config.networks[i].save();
}
saveNetworkCount(this->pref, networks_count);
this->pref->end(); // we call end() here to close the connection to the NVS partition, we
// only do this because we call ESP.restart() next.
saveNetworkCount(this->pref, networks_count);
this->pref->end(); // we call end() here to close the connection to the NVS partition, we
// only do this because we call ESP.restart() next.
// TODO add the restart task
// https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/freertos_idf.html
// OpenIrisTasks::ScheduleRestart(2000);
// TODO add the restart task
// https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/freertos_idf.html
// OpenIrisTasks::ScheduleRestart(2000);
}
void ProjectConfig::load()
{
ESP_LOGD(CONFIGURATION_TAG, "Loading project config");
if (this->_already_loaded)
{
ESP_LOGW(CONFIGURATION_TAG, "Project config already loaded");
return;
}
ESP_LOGD(CONFIGURATION_TAG, "Loading project config");
if (this->_already_loaded)
{
ESP_LOGW(CONFIGURATION_TAG, "Project config already loaded");
return;
}
const bool success = this->pref->begin("openiris");
const bool success = this->pref->begin("openiris");
ESP_LOGI(CONFIGURATION_TAG, "Config name: openiris");
ESP_LOGI(CONFIGURATION_TAG, "Config loaded: %s", success ? "true" : "false");
ESP_LOGI(CONFIGURATION_TAG, "Config name: openiris");
ESP_LOGI(CONFIGURATION_TAG, "Config loaded: %s", success ? "true" : "false");
this->config.device.load();
this->config.device_mode.load();
this->config.camera.load();
this->config.mdns.load();
this->config.txpower.load();
this->config.ap_network.load();
this->config.device.load();
this->config.device_mode.load();
this->config.camera.load();
this->config.mdns.load();
this->config.txpower.load();
this->config.ap_network.load();
const auto networks_count = getNetworkCount(this->pref);
ESP_LOGD(CONFIGURATION_TAG, "Loading networks: %d", networks_count);
for (int i = 0; i < getNetworkCount(this->pref); i++)
{
auto networkConfig = WiFiConfig_t(this->pref);
networkConfig.load(i);
this->config.networks.push_back(networkConfig);
}
const auto networks_count = getNetworkCount(this->pref);
ESP_LOGD(CONFIGURATION_TAG, "Loading networks: %d", networks_count);
for (int i = 0; i < getNetworkCount(this->pref); i++)
{
auto networkConfig = WiFiConfig_t(this->pref);
networkConfig.load(i);
this->config.networks.push_back(networkConfig);
}
this->_already_loaded = true;
this->_already_loaded = true;
}
bool ProjectConfig::reset()
{
ESP_LOGW(CONFIGURATION_TAG, "Resetting project config");
return this->pref->clear();
ESP_LOGW(CONFIGURATION_TAG, "Resetting project config");
return this->pref->clear();
}
//**********************************************************************************************************************
@@ -93,148 +91,128 @@ bool ProjectConfig::reset()
//! DeviceConfig
//*
//**********************************************************************************************************************
void ProjectConfig::setOTAConfig(const std::string &OTALogin,
const std::string &OTAPassword,
const int OTAPort)
void ProjectConfig::setOTAConfig(const std::string& OTALogin, const std::string& OTAPassword, const int OTAPort)
{
ESP_LOGD(CONFIGURATION_TAG, "Updating device config");
this->config.device.OTALogin.assign(OTALogin);
this->config.device.OTAPassword.assign(OTAPassword);
this->config.device.OTAPort = OTAPort;
this->config.device.save();
ESP_LOGD(CONFIGURATION_TAG, "Updating device config");
this->config.device.OTALogin.assign(OTALogin);
this->config.device.OTAPassword.assign(OTAPassword);
this->config.device.OTAPort = OTAPort;
this->config.device.save();
}
void ProjectConfig::setLEDDUtyCycleConfig(int led_external_pwm_duty_cycle)
{
this->config.device.led_external_pwm_duty_cycle = led_external_pwm_duty_cycle;
ESP_LOGI(CONFIGURATION_TAG, "Setting duty cycle to %d", led_external_pwm_duty_cycle);
this->config.device.save();
this->config.device.led_external_pwm_duty_cycle = led_external_pwm_duty_cycle;
ESP_LOGI(CONFIGURATION_TAG, "Setting duty cycle to %d", led_external_pwm_duty_cycle);
this->config.device.save();
}
void ProjectConfig::setMDNSConfig(const std::string &hostname)
void ProjectConfig::setMDNSConfig(const std::string& hostname)
{
ESP_LOGD(CONFIGURATION_TAG, "Updating MDNS config");
this->config.mdns.hostname.assign(hostname);
this->config.mdns.save();
ESP_LOGD(CONFIGURATION_TAG, "Updating MDNS config");
this->config.mdns.hostname.assign(hostname);
this->config.mdns.save();
}
void ProjectConfig::setCameraConfig(const uint8_t vflip,
const uint8_t framesize,
const uint8_t href,
const uint8_t quality,
const uint8_t brightness)
void ProjectConfig::setCameraConfig(const uint8_t vflip, const uint8_t framesize, const uint8_t href, const uint8_t quality, const uint8_t brightness)
{
ESP_LOGD(CONFIGURATION_TAG, "Updating camera config");
this->config.camera.vflip = vflip;
this->config.camera.href = href;
this->config.camera.framesize = framesize;
this->config.camera.quality = quality;
this->config.camera.brightness = brightness;
this->config.camera.save();
ESP_LOGD(CONFIGURATION_TAG, "Updating camera config");
this->config.camera.vflip = vflip;
this->config.camera.href = href;
this->config.camera.framesize = framesize;
this->config.camera.quality = quality;
this->config.camera.brightness = brightness;
this->config.camera.save();
ESP_LOGD(CONFIGURATION_TAG, "Updating Camera config");
ESP_LOGD(CONFIGURATION_TAG, "Updating Camera config");
}
void ProjectConfig::setWifiConfig(const std::string &networkName,
const std::string &ssid,
const std::string &password,
uint8_t channel,
uint8_t power)
void ProjectConfig::setWifiConfig(const std::string& networkName, const std::string& ssid, const std::string& password, uint8_t channel, uint8_t power)
{
const auto size = this->config.networks.size();
const auto size = this->config.networks.size();
const auto it = std::ranges::find_if(this->config.networks,
[&](const WiFiConfig_t &network)
{ return network.name == networkName; });
const auto it = std::ranges::find_if(this->config.networks, [&](const WiFiConfig_t& network) { return network.name == networkName; });
if (it != this->config.networks.end())
{
if (it != this->config.networks.end())
{
ESP_LOGI(CONFIGURATION_TAG, "Found network %s, updating it ...", it->name.c_str());
ESP_LOGI(CONFIGURATION_TAG, "Found network %s, updating it ...",
it->name.c_str());
it->name = networkName;
it->ssid = ssid;
it->password = password;
it->channel = channel;
it->power = power;
// Save the updated network immediately
it->save();
return;
}
it->name = networkName;
it->ssid = ssid;
it->password = password;
it->channel = channel;
it->power = power;
// Save the updated network immediately
it->save();
return;
}
if (size == 0)
{
ESP_LOGI(CONFIGURATION_TAG, "No networks, We're adding a new network");
this->config.networks.emplace_back(this->pref, static_cast<uint8_t>(0), networkName, ssid, password, channel, power);
// Save the new network immediately
this->config.networks.back().save();
saveNetworkCount(this->pref, 1);
return;
}
if (size == 0)
{
ESP_LOGI(CONFIGURATION_TAG, "No networks, We're adding a new network");
this->config.networks.emplace_back(this->pref, static_cast<uint8_t>(0), networkName, ssid, password, channel,
power);
// Save the new network immediately
this->config.networks.back().save();
saveNetworkCount(this->pref, 1);
return;
}
// we're allowing to store up to three additional networks
if (size < 3)
{
ESP_LOGI(CONFIGURATION_TAG, "We're adding a new network");
// we don't have that network yet, we can add it as we still have some
// space we're using emplace_back as push_back will create a copy of it,
// we want to avoid that
uint8_t last_index = getNetworkCount(this->pref);
this->config.networks.emplace_back(this->pref, last_index, networkName, ssid, password, channel,
power);
// Save the new network immediately
this->config.networks.back().save();
saveNetworkCount(this->pref, static_cast<int>(this->config.networks.size()));
}
else
{
ESP_LOGE(CONFIGURATION_TAG, "No more space for additional networks");
}
// we're allowing to store up to three additional networks
if (size < 3)
{
ESP_LOGI(CONFIGURATION_TAG, "We're adding a new network");
// we don't have that network yet, we can add it as we still have some
// space we're using emplace_back as push_back will create a copy of it,
// we want to avoid that
uint8_t last_index = getNetworkCount(this->pref);
this->config.networks.emplace_back(this->pref, last_index, networkName, ssid, password, channel, power);
// Save the new network immediately
this->config.networks.back().save();
saveNetworkCount(this->pref, static_cast<int>(this->config.networks.size()));
}
else
{
ESP_LOGE(CONFIGURATION_TAG, "No more space for additional networks");
}
}
void ProjectConfig::deleteWifiConfig(const std::string &networkName)
void ProjectConfig::deleteWifiConfig(const std::string& networkName)
{
if (const auto size = this->config.networks.size(); size == 0)
{
ESP_LOGI(CONFIGURATION_TAG, "No networks, nothing to delete");
}
if (const auto size = this->config.networks.size(); size == 0)
{
ESP_LOGI(CONFIGURATION_TAG, "No networks, nothing to delete");
}
const auto it = std::ranges::find_if(this->config.networks,
[&](const WiFiConfig_t &network)
{ return network.name == networkName; });
const auto it = std::ranges::find_if(this->config.networks, [&](const WiFiConfig_t& network) { return network.name == networkName; });
if (it != this->config.networks.end())
{
ESP_LOGI(CONFIGURATION_TAG, "Found network %s", it->name.c_str());
this->config.networks.erase(it);
ESP_LOGI(CONFIGURATION_TAG, "Deleted network %s", networkName.c_str());
}
if (it != this->config.networks.end())
{
ESP_LOGI(CONFIGURATION_TAG, "Found network %s", it->name.c_str());
this->config.networks.erase(it);
ESP_LOGI(CONFIGURATION_TAG, "Deleted network %s", networkName.c_str());
}
}
void ProjectConfig::setWiFiTxPower(uint8_t power)
{
this->config.txpower.power = power;
this->config.txpower.save();
ESP_LOGD(CONFIGURATION_TAG, "Updating wifi tx power");
this->config.txpower.power = power;
this->config.txpower.save();
ESP_LOGD(CONFIGURATION_TAG, "Updating wifi tx power");
}
void ProjectConfig::setAPWifiConfig(const std::string &ssid,
const std::string &password,
const uint8_t channel)
void ProjectConfig::setAPWifiConfig(const std::string& ssid, const std::string& password, const uint8_t channel)
{
this->config.ap_network.ssid.assign(ssid);
this->config.ap_network.password.assign(password);
this->config.ap_network.channel = channel;
this->config.ap_network.save();
ESP_LOGD(CONFIGURATION_TAG, "Updating access point config");
this->config.ap_network.ssid.assign(ssid);
this->config.ap_network.password.assign(password);
this->config.ap_network.channel = channel;
this->config.ap_network.save();
ESP_LOGD(CONFIGURATION_TAG, "Updating access point config");
}
void ProjectConfig::setDeviceMode(const StreamingMode deviceMode)
{
this->config.device_mode.mode = deviceMode;
this->config.device_mode.save(); // Save immediately
this->config.device_mode.mode = deviceMode;
this->config.device_mode.save(); // Save immediately
}
//**********************************************************************************************************************
@@ -243,41 +221,41 @@ void ProjectConfig::setDeviceMode(const StreamingMode deviceMode)
//*
//**********************************************************************************************************************
DeviceConfig_t &ProjectConfig::getDeviceConfig()
DeviceConfig_t& ProjectConfig::getDeviceConfig()
{
return this->config.device;
return this->config.device;
}
CameraConfig_t &ProjectConfig::getCameraConfig()
CameraConfig_t& ProjectConfig::getCameraConfig()
{
return this->config.camera;
return this->config.camera;
}
std::vector<WiFiConfig_t> &ProjectConfig::getWifiConfigs()
std::vector<WiFiConfig_t>& ProjectConfig::getWifiConfigs()
{
return this->config.networks;
return this->config.networks;
}
AP_WiFiConfig_t &ProjectConfig::getAPWifiConfig()
AP_WiFiConfig_t& ProjectConfig::getAPWifiConfig()
{
return this->config.ap_network;
return this->config.ap_network;
}
MDNSConfig_t &ProjectConfig::getMDNSConfig()
MDNSConfig_t& ProjectConfig::getMDNSConfig()
{
return this->config.mdns;
return this->config.mdns;
}
WiFiTxPower_t &ProjectConfig::getWiFiTxPowerConfig()
WiFiTxPower_t& ProjectConfig::getWiFiTxPowerConfig()
{
return this->config.txpower;
return this->config.txpower;
}
TrackerConfig_t &ProjectConfig::getTrackerConfig()
TrackerConfig_t& ProjectConfig::getTrackerConfig()
{
return this->config;
return this->config;
}
DeviceMode_t &ProjectConfig::getDeviceModeConfig()
DeviceMode_t& ProjectConfig::getDeviceModeConfig()
{
return this->config.device_mode;
return this->config.device_mode;
}
StreamingMode ProjectConfig::getDeviceMode()
{
return this->config.device_mode.mode;
return this->config.device_mode.mode;
}

View File

@@ -1,67 +1,55 @@
#pragma once
#ifndef PROJECT_CONFIG_HPP
#define PROJECT_CONFIG_HPP
#include "esp_log.h"
#include <algorithm>
#include <vector>
#include <string>
#include <helpers.hpp>
#include "Models.hpp"
#include <Preferences.hpp>
#include <algorithm>
#include <helpers.hpp>
#include <string>
#include <vector>
#include "Models.hpp"
#include "esp_log.h"
int getNetworkCount(Preferences *pref);
int getNetworkCount(Preferences* pref);
void saveNetworkCount(Preferences *pref, int count);
void saveNetworkCount(Preferences* pref, int count);
class ProjectConfig
{
public:
explicit ProjectConfig(Preferences *pref);
virtual ~ProjectConfig();
public:
explicit ProjectConfig(Preferences* pref);
virtual ~ProjectConfig();
void load();
void save() const;
void load();
void save() const;
bool reset();
bool reset();
DeviceConfig_t &getDeviceConfig();
DeviceMode_t &getDeviceModeConfig();
CameraConfig_t &getCameraConfig();
std::vector<WiFiConfig_t> &getWifiConfigs();
AP_WiFiConfig_t &getAPWifiConfig();
MDNSConfig_t &getMDNSConfig();
WiFiTxPower_t &getWiFiTxPowerConfig();
TrackerConfig_t &getTrackerConfig();
DeviceConfig_t& getDeviceConfig();
DeviceMode_t& getDeviceModeConfig();
CameraConfig_t& getCameraConfig();
std::vector<WiFiConfig_t>& getWifiConfigs();
AP_WiFiConfig_t& getAPWifiConfig();
MDNSConfig_t& getMDNSConfig();
WiFiTxPower_t& getWiFiTxPowerConfig();
TrackerConfig_t& getTrackerConfig();
void setOTAConfig(const std::string &OTALogin,
const std::string &OTAPassword,
int OTAPort);
void setLEDDUtyCycleConfig(int led_external_pwm_duty_cycle);
void setMDNSConfig(const std::string &hostname);
void setCameraConfig(uint8_t vflip,
uint8_t framesize,
uint8_t href,
uint8_t quality,
uint8_t brightness);
void setWifiConfig(const std::string &networkName,
const std::string &ssid,
const std::string &password,
uint8_t channel,
uint8_t power);
void setOTAConfig(const std::string& OTALogin, const std::string& OTAPassword, int OTAPort);
void setLEDDUtyCycleConfig(int led_external_pwm_duty_cycle);
void setMDNSConfig(const std::string& hostname);
void setCameraConfig(uint8_t vflip, uint8_t framesize, uint8_t href, uint8_t quality, uint8_t brightness);
void setWifiConfig(const std::string& networkName, const std::string& ssid, const std::string& password, uint8_t channel, uint8_t power);
void deleteWifiConfig(const std::string &networkName);
void deleteWifiConfig(const std::string& networkName);
void setAPWifiConfig(const std::string &ssid,
const std::string &password,
uint8_t channel);
void setWiFiTxPower(uint8_t power);
void setDeviceMode(StreamingMode deviceMode);
StreamingMode getDeviceMode();
void setAPWifiConfig(const std::string& ssid, const std::string& password, uint8_t channel);
void setWiFiTxPower(uint8_t power);
void setDeviceMode(StreamingMode deviceMode);
StreamingMode getDeviceMode();
private:
Preferences *pref;
bool _already_loaded;
TrackerConfig_t config;
private:
Preferences* pref;
bool _already_loaded;
TrackerConfig_t config;
};
#endif