mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-23 00:13:43 +02:00
Mostly port project config and helpers, clean up some stuff
This commit is contained in:
3
components/Helpers/CMakeLists.txt
Normal file
3
components/Helpers/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
idf_component_register(SRCS "Helpers/helpers.cpp"
|
||||||
|
INCLUDE_DIRS "helpers"
|
||||||
|
)
|
||||||
80
components/Helpers/Helpers/helpers.cpp
Normal file
80
components/Helpers/Helpers/helpers.cpp
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#include "helpers.hpp"
|
||||||
|
|
||||||
|
char *Helpers::itoa(int value, char *result, int base)
|
||||||
|
{
|
||||||
|
// check that the base if valid
|
||||||
|
if (base < 2 || base > 36)
|
||||||
|
{
|
||||||
|
*result = '\0';
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ptr = result, *ptr1 = result, tmp_char;
|
||||||
|
int tmp_value;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
tmp_value = value;
|
||||||
|
value /= base;
|
||||||
|
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
|
||||||
|
} while (value);
|
||||||
|
|
||||||
|
// Apply negative sign
|
||||||
|
if (tmp_value < 0)
|
||||||
|
*ptr++ = '-';
|
||||||
|
*ptr-- = '\0';
|
||||||
|
while (ptr1 < ptr)
|
||||||
|
{
|
||||||
|
tmp_char = *ptr;
|
||||||
|
*ptr-- = *ptr1;
|
||||||
|
*ptr1++ = tmp_char;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void split(const std::string &str, const std::string &splitBy, std::vector<std::string> &tokens)
|
||||||
|
{
|
||||||
|
/* Store the original string in the array, so we can loop the rest
|
||||||
|
* of the algorithm. */
|
||||||
|
tokens.emplace_back(str);
|
||||||
|
|
||||||
|
// Store the split index in a 'size_t' (unsigned integer) type.
|
||||||
|
size_t splitAt;
|
||||||
|
// Store the size of what we're splicing out.
|
||||||
|
size_t splitLen = splitBy.size();
|
||||||
|
// Create a string for temporarily storing the fragment we're processing.
|
||||||
|
std::string frag;
|
||||||
|
// Loop infinitely - break is internal.
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
/* Store the last string in the vector, which is the only logical
|
||||||
|
* candidate for processing. */
|
||||||
|
frag = tokens.back();
|
||||||
|
/* The index where the split is. */
|
||||||
|
splitAt = frag.find(splitBy);
|
||||||
|
// If we didn't find a new split point...
|
||||||
|
if (splitAt == std::string::npos)
|
||||||
|
{
|
||||||
|
// Break the loop and (implicitly) return.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* Put everything from the left side of the split where the string
|
||||||
|
* being processed used to be. */
|
||||||
|
tokens.back() = frag.substr(0, splitAt);
|
||||||
|
/* Push everything from the right side of the split to the next empty
|
||||||
|
* index in the vector. */
|
||||||
|
tokens.emplace_back(frag.substr(splitAt + splitLen, frag.size() - (splitAt + splitLen)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> Helpers::split(const std::string &s, char delimiter)
|
||||||
|
{
|
||||||
|
std::vector<std::string> parts;
|
||||||
|
std::string part;
|
||||||
|
std::istringstream tokenStream(s);
|
||||||
|
while (std::getline(tokenStream, part, delimiter))
|
||||||
|
{
|
||||||
|
parts.push_back(part);
|
||||||
|
}
|
||||||
|
return parts;
|
||||||
|
}
|
||||||
36
components/Helpers/Helpers/helpers.hpp
Normal file
36
components/Helpers/Helpers/helpers.hpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef HELPERS_HPP
|
||||||
|
#define HELPERS_HPP
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace Helpers
|
||||||
|
{
|
||||||
|
char *itoa(int value, char *result, int base);
|
||||||
|
void split(std::string str, std::string splitBy, std::vector<std::string> &tokens);
|
||||||
|
std::vector<std::string> split(const std::string &s, char delimiter);
|
||||||
|
|
||||||
|
/// @brief
|
||||||
|
/// @tparam ...Args
|
||||||
|
/// @param format
|
||||||
|
/// @param ...args
|
||||||
|
/// @return
|
||||||
|
template <typename... Args>
|
||||||
|
std::string format_string(const std::string &format, Args... args)
|
||||||
|
{
|
||||||
|
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
|
||||||
|
if (size_s <= 0)
|
||||||
|
{
|
||||||
|
std::cout << "Error during formatting.";
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
auto size = static_cast<size_t>(size_s);
|
||||||
|
std::unique_ptr<char[]> buf(new char[size]);
|
||||||
|
std::snprintf(buf.get(), size, format.c_str(), args...);
|
||||||
|
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // HELPERS_HPP
|
||||||
4
components/ProjectConfig/CMakeLists.txt
Normal file
4
components/ProjectConfig/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
idf_component_register(SRCS "ProjectConfig/ProjectConfig.cpp"
|
||||||
|
INCLUDE_DIRS "ProjectConfig"
|
||||||
|
REQUIRES Preferences Helpers
|
||||||
|
)
|
||||||
512
components/ProjectConfig/ProjectConfig/ProjectConfig.cpp
Normal file
512
components/ProjectConfig/ProjectConfig/ProjectConfig.cpp
Normal file
@@ -0,0 +1,512 @@
|
|||||||
|
#include "ProjectConfig.hpp"
|
||||||
|
|
||||||
|
ProjectConfig::ProjectConfig(const std::string &name,
|
||||||
|
const std::string &mdnsName)
|
||||||
|
: _name(std::move(name)),
|
||||||
|
_mdnsName(std::move(mdnsName)),
|
||||||
|
_already_loaded(false) {}
|
||||||
|
|
||||||
|
ProjectConfig::~ProjectConfig() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@brief Initializes the structures with blank data to prevent empty memory
|
||||||
|
*sectors and nullptr errors.
|
||||||
|
*@brief This is to be called in setup() before loading the config.
|
||||||
|
*/
|
||||||
|
void ProjectConfig::initConfig()
|
||||||
|
{
|
||||||
|
if (_name.empty())
|
||||||
|
{
|
||||||
|
ESP_LOGE(CONFIGURATION_TAG, "Config name is null\n");
|
||||||
|
_name = "openiris";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool success = begin(_name.c_str());
|
||||||
|
|
||||||
|
ESP_LOGI(CONFIGURATION_TAG, "Config name: %s", _name.c_str());
|
||||||
|
ESP_LOGI(CONFIGURATION_TAG, "Config loaded: %s", success ? "true" : "false");
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If the config is not loaded,
|
||||||
|
* we need to initialize the config with default data
|
||||||
|
! Do not initialize the WiFiConfig_t struct here,
|
||||||
|
! as it will create a blank network which breaks the WiFiManager
|
||||||
|
*/
|
||||||
|
// TODO add support for OTA
|
||||||
|
// this->config.device = {OTA_LOGIN, OTA_PASSWORD, 3232};
|
||||||
|
this->config.device = {"openiris", "openiris", 3232};
|
||||||
|
|
||||||
|
if (_mdnsName.empty())
|
||||||
|
{
|
||||||
|
ESP_LOGE(CONFIGURATION_TAG, "MDNS name is null\n Auto-assigning name to 'openiristracker'");
|
||||||
|
_mdnsName = "openiristracker";
|
||||||
|
}
|
||||||
|
this->config.mdns = {
|
||||||
|
_mdnsName,
|
||||||
|
"openiristracker",
|
||||||
|
};
|
||||||
|
|
||||||
|
ESP_LOGI(CONFIGURATION_TAG, "MDNS name: %s", _mdnsName.c_str());
|
||||||
|
|
||||||
|
this->config.ap_network = {
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
1,
|
||||||
|
false,
|
||||||
|
};
|
||||||
|
|
||||||
|
this->config.camera = {
|
||||||
|
.vflip = 0,
|
||||||
|
.href = 0,
|
||||||
|
.framesize = 4,
|
||||||
|
.quality = 7,
|
||||||
|
.brightness = 2,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::save()
|
||||||
|
{
|
||||||
|
ESP_LOGD(CONFIGURATION_TAG, "Saving project config");
|
||||||
|
deviceConfigSave();
|
||||||
|
mdnsConfigSave();
|
||||||
|
cameraConfigSave();
|
||||||
|
wifiConfigSave();
|
||||||
|
wifiTxPowerConfigSave();
|
||||||
|
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
|
||||||
|
// OpenIrisTasks::ScheduleRestart(2000);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::wifiConfigSave()
|
||||||
|
{
|
||||||
|
ESP_LOGI(CONFIGURATION_TAG, "Saving wifi config");
|
||||||
|
|
||||||
|
/* WiFi Config */
|
||||||
|
putInt("networkCount", this->config.networks.size());
|
||||||
|
|
||||||
|
std::string name = "name";
|
||||||
|
std::string ssid = "ssid";
|
||||||
|
std::string password = "pass";
|
||||||
|
std::string channel = "channel";
|
||||||
|
std::string power = "txpower";
|
||||||
|
for (int i = 0; i < this->config.networks.size(); i++)
|
||||||
|
{
|
||||||
|
char buffer[2];
|
||||||
|
std::string iter_str = Helpers::itoa(i, buffer, 10);
|
||||||
|
|
||||||
|
name.append(iter_str);
|
||||||
|
ssid.append(iter_str);
|
||||||
|
password.append(iter_str);
|
||||||
|
channel.append(iter_str);
|
||||||
|
power.append(iter_str);
|
||||||
|
|
||||||
|
putString(name.c_str(), this->config.networks[i].name.c_str());
|
||||||
|
putString(ssid.c_str(), this->config.networks[i].ssid.c_str());
|
||||||
|
putString(password.c_str(), this->config.networks[i].password.c_str());
|
||||||
|
putUInt(channel.c_str(), this->config.networks[i].channel);
|
||||||
|
putUInt(power.c_str(), this->config.networks[i].power);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* AP Config */
|
||||||
|
putString("apSSID", this->config.ap_network.ssid.c_str());
|
||||||
|
putString("apPass", this->config.ap_network.password.c_str());
|
||||||
|
putUInt("apChannel", this->config.ap_network.channel);
|
||||||
|
|
||||||
|
ESP_LOGI(CONFIGURATION_TAG, "[Project Config]: Wifi configs saved");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::deviceConfigSave()
|
||||||
|
{
|
||||||
|
/* Device Config */
|
||||||
|
putString("OTAPassword", this->config.device.OTAPassword.c_str());
|
||||||
|
putString("OTALogin", this->config.device.OTALogin.c_str());
|
||||||
|
putInt("OTAPort", this->config.device.OTAPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::mdnsConfigSave()
|
||||||
|
{
|
||||||
|
/* Device Config */
|
||||||
|
putString("hostname", this->config.mdns.hostname.c_str());
|
||||||
|
putString("service", this->config.mdns.service.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::wifiTxPowerConfigSave()
|
||||||
|
{
|
||||||
|
/* Device Config */
|
||||||
|
putInt("txpower", this->config.txpower.power);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::cameraConfigSave()
|
||||||
|
{
|
||||||
|
/* Camera Config */
|
||||||
|
putInt("vflip", this->config.camera.vflip);
|
||||||
|
putInt("href", this->config.camera.href);
|
||||||
|
putInt("framesize", this->config.camera.framesize);
|
||||||
|
putInt("quality", this->config.camera.quality);
|
||||||
|
putInt("brightness", this->config.camera.brightness);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ProjectConfig::reset()
|
||||||
|
{
|
||||||
|
ESP_LOGW(CONFIGURATION_TAG, "Resetting project config");
|
||||||
|
return clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::load()
|
||||||
|
{
|
||||||
|
ESP_LOGD(CONFIGURATION_TAG, "Loading project config");
|
||||||
|
if (this->_already_loaded)
|
||||||
|
{
|
||||||
|
ESP_LOGW(CONFIGURATION_TAG, "Project config already loaded");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
initConfig();
|
||||||
|
|
||||||
|
/* Device Config */
|
||||||
|
this->config.device.OTALogin = getString("OTALogin", "openiris").c_str();
|
||||||
|
this->config.device.OTAPassword =
|
||||||
|
getString("OTAPassword", "12345678").c_str();
|
||||||
|
this->config.device.OTAPort = getInt("OTAPort", 3232);
|
||||||
|
|
||||||
|
/* MDNS Config */
|
||||||
|
this->config.mdns.hostname = getString("hostname", _mdnsName.c_str()).c_str();
|
||||||
|
this->config.mdns.service = getString("service").c_str();
|
||||||
|
|
||||||
|
/* Wifi TX Power Config */
|
||||||
|
// 11dBm is the default value
|
||||||
|
this->config.txpower.power = getUInt("txpower", 52);
|
||||||
|
|
||||||
|
/* WiFi Config */
|
||||||
|
int networkCount = getInt("networkCount", 0);
|
||||||
|
std::string name = "name";
|
||||||
|
std::string ssid = "ssid";
|
||||||
|
std::string password = "pass";
|
||||||
|
std::string channel = "channel";
|
||||||
|
std::string power = "txpower";
|
||||||
|
for (int i = 0; i < networkCount; i++)
|
||||||
|
{
|
||||||
|
char buffer[2];
|
||||||
|
std::string iter_str = Helpers::itoa(i, buffer, 10);
|
||||||
|
|
||||||
|
name.append(iter_str);
|
||||||
|
ssid.append(iter_str);
|
||||||
|
password.append(iter_str);
|
||||||
|
channel.append(iter_str);
|
||||||
|
power.append(iter_str);
|
||||||
|
|
||||||
|
const std::string &temp_1 = getString(name.c_str()).c_str();
|
||||||
|
const std::string &temp_2 = getString(ssid.c_str()).c_str();
|
||||||
|
const std::string &temp_3 = getString(password.c_str()).c_str();
|
||||||
|
uint8_t temp_4 = getUInt(channel.c_str());
|
||||||
|
uint8_t temp_5 = getUInt(power.c_str());
|
||||||
|
|
||||||
|
//! push_back creates a copy of the object, so we need to use emplace_back
|
||||||
|
this->config.networks.emplace_back(
|
||||||
|
temp_1, temp_2, temp_3, temp_4, temp_5,
|
||||||
|
false); // false because the networks we store in the config are the
|
||||||
|
// ones we want the esp to connect to, rather than host as AP
|
||||||
|
}
|
||||||
|
|
||||||
|
/* AP Config */
|
||||||
|
this->config.ap_network.ssid = getString("apSSID").c_str();
|
||||||
|
this->config.ap_network.password = getString("apPass").c_str();
|
||||||
|
this->config.ap_network.channel = getUInt("apChannel");
|
||||||
|
|
||||||
|
/* Camera Config */
|
||||||
|
this->config.camera.vflip = getInt("vflip", 0);
|
||||||
|
this->config.camera.href = getInt("href", 0);
|
||||||
|
this->config.camera.framesize = getInt("framesize", 4);
|
||||||
|
this->config.camera.quality = getInt("quality", 7);
|
||||||
|
this->config.camera.brightness = getInt("brightness", 2);
|
||||||
|
|
||||||
|
this->_already_loaded = true;
|
||||||
|
// TODO add support for what's the pattern? Eh, the pattern
|
||||||
|
// this->notifyAll(ConfigState_e::configLoaded);
|
||||||
|
}
|
||||||
|
//**********************************************************************************************************************
|
||||||
|
//*
|
||||||
|
//! DeviceConfig
|
||||||
|
//*
|
||||||
|
//**********************************************************************************************************************
|
||||||
|
void ProjectConfig::setDeviceConfig(const std::string &OTALogin,
|
||||||
|
const std::string &OTAPassword,
|
||||||
|
int OTAPort,
|
||||||
|
bool shouldNotify)
|
||||||
|
{
|
||||||
|
ESP_LOGD(CONFIGURATION_TAG, "Updating device config");
|
||||||
|
this->config.device.OTALogin.assign(OTALogin);
|
||||||
|
this->config.device.OTAPassword.assign(OTAPassword);
|
||||||
|
this->config.device.OTAPort = OTAPort;
|
||||||
|
|
||||||
|
// TODO turn this on
|
||||||
|
// if (shouldNotify)
|
||||||
|
// this->notifyAll(ConfigState_e::deviceConfigUpdated);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::setMDNSConfig(const std::string &hostname,
|
||||||
|
const std::string &service,
|
||||||
|
bool shouldNotify)
|
||||||
|
{
|
||||||
|
ESP_LOGD(CONFIGURATION_TAG, "Updating MDNS config");
|
||||||
|
this->config.mdns.hostname.assign(hostname);
|
||||||
|
this->config.mdns.service.assign(service);
|
||||||
|
|
||||||
|
// TODO turn this on
|
||||||
|
// if (shouldNotify)
|
||||||
|
// this->notifyAll(ConfigState_e::mdnsConfigUpdated);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::setCameraConfig(uint8_t vflip,
|
||||||
|
uint8_t framesize,
|
||||||
|
uint8_t href,
|
||||||
|
uint8_t quality,
|
||||||
|
uint8_t brightness,
|
||||||
|
bool shouldNotify)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
|
ESP_LOGD(CONFIGURATION_TAG, "Updating Camera config");
|
||||||
|
// TODO turn this on
|
||||||
|
// if (shouldNotify)
|
||||||
|
// this->notifyAll(ConfigState_e::cameraConfigUpdated);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::setWifiConfig(const std::string &networkName,
|
||||||
|
const std::string &ssid,
|
||||||
|
const std::string &password,
|
||||||
|
uint8_t channel,
|
||||||
|
uint8_t power,
|
||||||
|
bool adhoc,
|
||||||
|
bool shouldNotify)
|
||||||
|
{
|
||||||
|
// we store the ADHOC flag as false because the networks we store in the
|
||||||
|
// config are the ones we want the esp to connect to, rather than host as AP,
|
||||||
|
// and here we're just updating them
|
||||||
|
size_t size = this->config.networks.size();
|
||||||
|
|
||||||
|
for (auto it = this->config.networks.begin();
|
||||||
|
it != this->config.networks.end();)
|
||||||
|
{
|
||||||
|
if (it->name == networkName)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
it->adhoc = false;
|
||||||
|
|
||||||
|
if (shouldNotify)
|
||||||
|
{
|
||||||
|
// TODO port state managers
|
||||||
|
// wifiStateManager.setState(WiFiState_e::WiFiState_Disconnected);
|
||||||
|
// WiFi.disconnect();
|
||||||
|
this->wifiConfigSave();
|
||||||
|
// TODO turn this on
|
||||||
|
// this->notifyAll(ConfigState_e::networksConfigUpdated);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size < 3 && size > 0)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
this->config.networks.emplace_back(networkName, ssid, password, channel,
|
||||||
|
power, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// we're allowing to store up to three additional networks
|
||||||
|
if (size == 0)
|
||||||
|
{
|
||||||
|
ESP_LOGI(CONFIGURATION_TAG, "No networks, We're adding a new network");
|
||||||
|
this->config.networks.emplace_back(networkName, ssid, password, channel,
|
||||||
|
power, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldNotify)
|
||||||
|
{
|
||||||
|
// TODO port state managers
|
||||||
|
// wifiStateManager.setState(WiFiState_e::WiFiState_None);
|
||||||
|
// WiFi.disconnect();
|
||||||
|
this->wifiConfigSave();
|
||||||
|
// TODO turn this on
|
||||||
|
// this->notifyAll(ConfigState_e::networksConfigUpdated);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::deleteWifiConfig(const std::string &networkName,
|
||||||
|
bool shouldNotify)
|
||||||
|
{
|
||||||
|
size_t size = this->config.networks.size();
|
||||||
|
if (size == 0)
|
||||||
|
{
|
||||||
|
ESP_LOGI(CONFIGURATION_TAG, "No networks, nothing to delete");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto it = this->config.networks.begin();
|
||||||
|
it != this->config.networks.end();)
|
||||||
|
{
|
||||||
|
if (it->name == networkName)
|
||||||
|
{
|
||||||
|
ESP_LOGI(CONFIGURATION_TAG, "Found network %s", it->name.c_str());
|
||||||
|
it = this->config.networks.erase(it);
|
||||||
|
ESP_LOGI(CONFIGURATION_TAG, "Deleted network %s", networkName.c_str());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldNotify)
|
||||||
|
{
|
||||||
|
this->wifiConfigSave();
|
||||||
|
// TODO turn this on
|
||||||
|
// this->notifyAll(ConfigState_e::networksConfigUpdated);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::setWiFiTxPower(uint8_t power, bool shouldNotify)
|
||||||
|
{
|
||||||
|
this->config.txpower.power = power;
|
||||||
|
ESP_LOGD(CONFIGURATION_TAG, "Updating wifi tx power");
|
||||||
|
// TODO turn this on
|
||||||
|
// if (shouldNotify)
|
||||||
|
// this->notifyAll(ConfigState_e::wifiTxPowerUpdated);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::setAPWifiConfig(const std::string &ssid,
|
||||||
|
const std::string &password,
|
||||||
|
uint8_t channel,
|
||||||
|
bool adhoc,
|
||||||
|
bool shouldNotify)
|
||||||
|
{
|
||||||
|
this->config.ap_network.ssid.assign(ssid);
|
||||||
|
this->config.ap_network.password.assign(password);
|
||||||
|
this->config.ap_network.channel = channel;
|
||||||
|
this->config.ap_network.adhoc = adhoc;
|
||||||
|
|
||||||
|
ESP_LOGD(CONFIGURATION_TAG, "Updating access point config");
|
||||||
|
if (shouldNotify)
|
||||||
|
{
|
||||||
|
// TODO port state managers
|
||||||
|
// wifiStateManager.setState(WiFiState_e::WiFiState_None);
|
||||||
|
// TODO Add some sort of signalling or IPC to tell the wifi manager to shut off
|
||||||
|
// WiFi.disconnect();
|
||||||
|
this->wifiConfigSave();
|
||||||
|
// TODO turn this on
|
||||||
|
// this->notifyAll(ConfigState_e::networksConfigUpdated);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//**********************************************************************************************************************
|
||||||
|
//*
|
||||||
|
//! Representation
|
||||||
|
//*
|
||||||
|
//**********************************************************************************************************************
|
||||||
|
|
||||||
|
std::string ProjectConfig::DeviceConfig_t::toRepresentation()
|
||||||
|
{
|
||||||
|
std::string json = Helpers::format_string(
|
||||||
|
"\"device_config\": {\"OTALogin\": \"%s\", \"OTAPassword\": \"%s\", "
|
||||||
|
"\"OTAPort\": %u}",
|
||||||
|
this->OTALogin.c_str(), this->OTAPassword.c_str(), this->OTAPort);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ProjectConfig::MDNSConfig_t::toRepresentation()
|
||||||
|
{
|
||||||
|
std::string json = Helpers::format_string(
|
||||||
|
"\"mdns_config\": {\"hostname\": \"%s\", \"service\": \"%s\"}",
|
||||||
|
this->hostname.c_str(), this->service.c_str());
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ProjectConfig::CameraConfig_t::toRepresentation()
|
||||||
|
{
|
||||||
|
std::string json = 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);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ProjectConfig::WiFiConfig_t::toRepresentation()
|
||||||
|
{
|
||||||
|
std::string json = Helpers::format_string(
|
||||||
|
"{\"name\": \"%s\", \"ssid\": \"%s\", \"password\": \"%s\", "
|
||||||
|
"\"channel\": "
|
||||||
|
"%u, \"power\": %u,\"adhoc\": %s}",
|
||||||
|
this->name.c_str(), this->ssid.c_str(), this->password.c_str(),
|
||||||
|
this->channel, this->power, this->adhoc ? "true" : "false");
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ProjectConfig::AP_WiFiConfig_t::toRepresentation()
|
||||||
|
{
|
||||||
|
std::string json = Helpers::format_string(
|
||||||
|
"\"ap_wifi_config\": {\"ssid\": \"%s\", \"password\": \"%s\", "
|
||||||
|
"\"channel\": %u, \"adhoc\": %s}",
|
||||||
|
this->ssid.c_str(), this->password.c_str(), this->channel,
|
||||||
|
this->adhoc ? "true" : "false");
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ProjectConfig::WiFiTxPower_t::toRepresentation()
|
||||||
|
{
|
||||||
|
std::string json =
|
||||||
|
Helpers::format_string("\"wifi_tx_power\": {\"power\": %u}", this->power);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
//**********************************************************************************************************************
|
||||||
|
//*
|
||||||
|
//! Get Methods
|
||||||
|
//*
|
||||||
|
//**********************************************************************************************************************
|
||||||
|
|
||||||
|
ProjectConfig::DeviceConfig_t &ProjectConfig::getDeviceConfig()
|
||||||
|
{
|
||||||
|
return this->config.device;
|
||||||
|
}
|
||||||
|
ProjectConfig::CameraConfig_t &ProjectConfig::getCameraConfig()
|
||||||
|
{
|
||||||
|
return this->config.camera;
|
||||||
|
}
|
||||||
|
std::vector<ProjectConfig::WiFiConfig_t> &ProjectConfig::getWifiConfigs()
|
||||||
|
{
|
||||||
|
return this->config.networks;
|
||||||
|
}
|
||||||
|
ProjectConfig::AP_WiFiConfig_t &ProjectConfig::getAPWifiConfig()
|
||||||
|
{
|
||||||
|
return this->config.ap_network;
|
||||||
|
}
|
||||||
|
ProjectConfig::MDNSConfig_t &ProjectConfig::getMDNSConfig()
|
||||||
|
{
|
||||||
|
return this->config.mdns;
|
||||||
|
}
|
||||||
|
ProjectConfig::WiFiTxPower_t &ProjectConfig::getWiFiTxPowerConfig()
|
||||||
|
{
|
||||||
|
return this->config.txpower;
|
||||||
|
}
|
||||||
150
components/ProjectConfig/ProjectConfig/ProjectConfig.hpp
Normal file
150
components/ProjectConfig/ProjectConfig/ProjectConfig.hpp
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _PROJECT_CONFIG_HPP_
|
||||||
|
#define _PROJECT_CONFIG_HPP_
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <helpers.hpp>
|
||||||
|
#include <Preferences.hpp>
|
||||||
|
|
||||||
|
static const char *CONFIGURATION_TAG = "[CONFIGURATION]";
|
||||||
|
|
||||||
|
class ProjectConfig : public Preferences
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
ProjectConfig(const std::string &name = std::string(), const std::string &mdnsName = std::string());
|
||||||
|
virtual ~ProjectConfig();
|
||||||
|
|
||||||
|
void load();
|
||||||
|
void save();
|
||||||
|
|
||||||
|
void wifiConfigSave();
|
||||||
|
void cameraConfigSave();
|
||||||
|
void deviceConfigSave();
|
||||||
|
void mdnsConfigSave();
|
||||||
|
void wifiTxPowerConfigSave();
|
||||||
|
bool reset();
|
||||||
|
void initConfig();
|
||||||
|
|
||||||
|
struct DeviceConfig_t
|
||||||
|
{
|
||||||
|
std::string OTALogin;
|
||||||
|
std::string OTAPassword;
|
||||||
|
int OTAPort;
|
||||||
|
std::string toRepresentation();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MDNSConfig_t
|
||||||
|
{
|
||||||
|
std::string hostname;
|
||||||
|
std::string service;
|
||||||
|
std::string toRepresentation();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CameraConfig_t
|
||||||
|
{
|
||||||
|
uint8_t vflip;
|
||||||
|
uint8_t href;
|
||||||
|
uint8_t framesize;
|
||||||
|
uint8_t quality;
|
||||||
|
uint8_t brightness;
|
||||||
|
|
||||||
|
std::string toRepresentation();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct WiFiConfig_t
|
||||||
|
{
|
||||||
|
//! Constructor for WiFiConfig_t - allows us to use emplace_back
|
||||||
|
WiFiConfig_t(const std::string &name,
|
||||||
|
const std::string &ssid,
|
||||||
|
const std::string &password,
|
||||||
|
uint8_t channel,
|
||||||
|
uint8_t power,
|
||||||
|
bool adhoc)
|
||||||
|
: name(std::move(name)),
|
||||||
|
ssid(std::move(ssid)),
|
||||||
|
password(std::move(password)),
|
||||||
|
channel(channel),
|
||||||
|
power(power),
|
||||||
|
adhoc(adhoc) {}
|
||||||
|
std::string name;
|
||||||
|
std::string ssid;
|
||||||
|
std::string password;
|
||||||
|
uint8_t channel;
|
||||||
|
uint8_t power;
|
||||||
|
bool adhoc;
|
||||||
|
|
||||||
|
std::string toRepresentation();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AP_WiFiConfig_t
|
||||||
|
{
|
||||||
|
std::string ssid;
|
||||||
|
std::string password;
|
||||||
|
uint8_t channel;
|
||||||
|
bool adhoc;
|
||||||
|
std::string toRepresentation();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct WiFiTxPower_t
|
||||||
|
{
|
||||||
|
uint8_t power;
|
||||||
|
std::string toRepresentation();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TrackerConfig_t
|
||||||
|
{
|
||||||
|
DeviceConfig_t device;
|
||||||
|
CameraConfig_t camera;
|
||||||
|
std::vector<WiFiConfig_t> networks;
|
||||||
|
AP_WiFiConfig_t ap_network;
|
||||||
|
MDNSConfig_t mdns;
|
||||||
|
WiFiTxPower_t txpower;
|
||||||
|
};
|
||||||
|
|
||||||
|
DeviceConfig_t &getDeviceConfig();
|
||||||
|
CameraConfig_t &getCameraConfig();
|
||||||
|
std::vector<WiFiConfig_t> &getWifiConfigs();
|
||||||
|
AP_WiFiConfig_t &getAPWifiConfig();
|
||||||
|
MDNSConfig_t &getMDNSConfig();
|
||||||
|
WiFiTxPower_t &getWiFiTxPowerConfig();
|
||||||
|
|
||||||
|
void setDeviceConfig(const std::string &OTALogin,
|
||||||
|
const std::string &OTAPassword,
|
||||||
|
int OTAPort,
|
||||||
|
bool shouldNotify);
|
||||||
|
void setMDNSConfig(const std::string &hostname,
|
||||||
|
const std::string &service,
|
||||||
|
bool shouldNotify);
|
||||||
|
void setCameraConfig(uint8_t vflip,
|
||||||
|
uint8_t framesize,
|
||||||
|
uint8_t href,
|
||||||
|
uint8_t quality,
|
||||||
|
uint8_t brightness,
|
||||||
|
bool shouldNotify);
|
||||||
|
void setWifiConfig(const std::string &networkName,
|
||||||
|
const std::string &ssid,
|
||||||
|
const std::string &password,
|
||||||
|
uint8_t channel,
|
||||||
|
uint8_t power,
|
||||||
|
bool adhoc,
|
||||||
|
bool shouldNotify);
|
||||||
|
void setAPWifiConfig(const std::string &ssid,
|
||||||
|
const std::string &password,
|
||||||
|
uint8_t channel,
|
||||||
|
bool adhoc,
|
||||||
|
bool shouldNotify);
|
||||||
|
void setWiFiTxPower(uint8_t power, bool shouldNotify);
|
||||||
|
|
||||||
|
void deleteWifiConfig(const std::string &networkName, bool shouldNotify);
|
||||||
|
|
||||||
|
private:
|
||||||
|
TrackerConfig_t config;
|
||||||
|
std::string _name;
|
||||||
|
std::string _mdnsName;
|
||||||
|
bool _already_loaded;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
idf_component_register(SRCS "customSomething/customSomething.hpp"
|
|
||||||
INCLUDE_DIRS "customSomething"
|
|
||||||
REQUIRES "driver"
|
|
||||||
)
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#include "esp_log.h"
|
|
||||||
|
|
||||||
void printTheInclude()
|
|
||||||
{
|
|
||||||
ESP_LOGI("customSomething", "test");
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
idf_component_register(SRCS "openiris_logo/openiris_logo.hpp"
|
idf_component_register(SRCS "openiris_logo/openiris_logo.hpp"
|
||||||
INCLUDE_DIRS "openiris_logo"
|
INCLUDE_DIRS "openiris_logo"
|
||||||
REQUIRES "driver"
|
|
||||||
)
|
)
|
||||||
@@ -20,11 +20,10 @@
|
|||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
#include <openiris_logo.hpp>
|
#include <openiris_logo.hpp>
|
||||||
#include <customSomething.hpp>
|
|
||||||
#include <wifiManager.hpp>
|
#include <wifiManager.hpp>
|
||||||
#include <Preferences.hpp>
|
#include <ProjectConfig.hpp>
|
||||||
|
|
||||||
static const char *TAG = "example";
|
static const char *TAG = "[MAIN]";
|
||||||
|
|
||||||
/* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
|
/* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
|
||||||
or you can edit the following line and set a number here.
|
or you can edit the following line and set a number here.
|
||||||
@@ -64,8 +63,8 @@ extern "C" void app_main(void)
|
|||||||
// port the wifi manager first. - worky!!!
|
// port the wifi manager first. - worky!!!
|
||||||
// get it connect to the network and setup an AP with hardcoded creds first -- connects. AP will be next
|
// get it connect to the network and setup an AP with hardcoded creds first -- connects. AP will be next
|
||||||
// port the logo - done
|
// port the logo - done
|
||||||
// port preferences lib - in progress; prolly temporary
|
// port preferences lib - DONE; prolly temporary
|
||||||
// then port the config - in progress
|
// then port the config - done, needs todos done
|
||||||
// then port the led manager as this will be fairly easy
|
// then port the led manager as this will be fairly easy
|
||||||
// then port the serial manager
|
// then port the serial manager
|
||||||
// then port the camera manager
|
// then port the camera manager
|
||||||
@@ -74,10 +73,15 @@ extern "C" void app_main(void)
|
|||||||
// then port the Elegant OTA stuff
|
// then port the Elegant OTA stuff
|
||||||
// then port the mdns stuff
|
// then port the mdns stuff
|
||||||
|
|
||||||
|
// TODO add this option
|
||||||
|
// ProjectConfig deviceConfig("openiris", MDNS_HOSTNAME);
|
||||||
|
ProjectConfig deviceConfig("openiris", "openiristracker");
|
||||||
|
WiFiManager wifiManager;
|
||||||
|
|
||||||
Logo::printASCII();
|
Logo::printASCII();
|
||||||
initNVSStorage();
|
initNVSStorage();
|
||||||
|
|
||||||
WiFiManager wifiManager;
|
deviceConfig.load();
|
||||||
wifiManager.Begin();
|
wifiManager.Begin();
|
||||||
|
|
||||||
/* Configure the peripheral according to the LED type */
|
/* Configure the peripheral according to the LED type */
|
||||||
@@ -85,7 +89,6 @@ extern "C" void app_main(void)
|
|||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
printTheInclude();
|
|
||||||
ESP_LOGI(TAG, "Turning the LED on pin %d %s!", BLINK_GPIO, s_led_state == true ? "ON" : "OFF");
|
ESP_LOGI(TAG, "Turning the LED on pin %d %s!", BLINK_GPIO, s_led_state == true ? "ON" : "OFF");
|
||||||
blink_led();
|
blink_led();
|
||||||
/* Toggle the LED state */
|
/* Toggle the LED state */
|
||||||
|
|||||||
Reference in New Issue
Block a user