diff --git a/ESP/lib/src/data/config/project_config.cpp b/ESP/lib/src/data/config/project_config.cpp index e2448ab..4430aad 100644 --- a/ESP/lib/src/data/config/project_config.cpp +++ b/ESP/lib/src/data/config/project_config.cpp @@ -1,8 +1,6 @@ #include "project_config.hpp" -Preferences preferences; - -ProjectConfig::ProjectConfig() : Config(&preferences, "config"), _already_loaded(false) {} +ProjectConfig::ProjectConfig() : _already_loaded(false) {} ProjectConfig::~ProjectConfig() {} @@ -12,9 +10,9 @@ ProjectConfig::~ProjectConfig() {} */ void ProjectConfig::initConfig() { - begin(); + begin("projectConf"); this->config.device = { - "EyeTrackVR", + "eyetrackvr", "", 3232, false, @@ -58,44 +56,25 @@ void ProjectConfig::load() return; } - bool device_name_success = this->read("device_name", this->config.device.name); - bool device_otapassword_success = this->read("ota_pass", this->config.device.OTAPassword); - bool device_otaport_success = this->read("ota_port", this->config.device.OTAPort); - - bool device_success = device_name_success && device_otapassword_success && device_otaport_success; - - bool camera_vflip_success = this->read("camera_vflip", this->config.camera.vflip); - bool camera_framesize_success = this->read("cameraFrmsz", this->config.camera.framesize); - bool camera_href_success = this->read("camera_href", this->config.camera.href); - bool camera_quality_success = this->read("camera_quality", this->config.camera.quality); - - bool camera_success = camera_vflip_success && camera_framesize_success && camera_href_success && camera_quality_success; - - bool network_info_success; - for (int i = 0; i < this->config.networks.size(); i++) + size_t configLen = getBytesLength("config"); + if (configLen == 0) { - char buff[25]; - snprintf(buff, sizeof(buff), "%d_name", i); - bool networks_name_success = this->read(buff, this->config.networks[i].name); - snprintf(buff, sizeof(buff), "%d_ssid", i); - bool networks_ssid_success = this->read(buff, this->config.networks[i].ssid); - snprintf(buff, sizeof(buff), "%d_password", i); - bool networks_password_success = this->read(buff, this->config.networks[i].password); - snprintf(buff, sizeof(buff), "%d_channel", i); - bool networks_channel_success = this->read(buff, this->config.networks[i].channel); - bool networks_adhoc_success = this->read(buff, this->config.networks[i].adhoc); - - network_info_success = networks_name_success && networks_ssid_success && networks_password_success && networks_channel_success && networks_adhoc_success; - } - - if (!device_success || !camera_success || !network_info_success) - { - log_e("Failed to load project config - Generating config and restarting"); + log_e("Project config not found - Generating config and restarting"); save(); delay(1000); ESP.restart(); return; } + else + { + log_d("Project config found - Config length: %d", configLen); + } + + char buff[configLen]; + getBytes("config", buff, configLen); + + for (int i = 0; i < configLen; i++) + Serial.printf("%02X ", buff[i]); this->_already_loaded = true; this->notify(ObserverEvent::configLoaded); @@ -105,28 +84,8 @@ void ProjectConfig::save() { log_d("Saving project config"); - this->write("device_name", this->config.device.name); - this->write("ota_pass", this->config.device.OTAPassword); - this->write("ota_port", this->config.device.OTAPort); - - this->write("camera_vflip", this->config.camera.vflip); - this->write("cameraFrmsz", this->config.camera.framesize); - this->write("camera_href", this->config.camera.href); - this->write("camera_quality", this->config.camera.quality); - - for (int i = 0; i < this->config.networks.size(); i++) - { - char buff[25]; - snprintf(buff, sizeof(buff), "%d_name", i); - this->write(buff, this->config.networks[i].name); - snprintf(buff, sizeof(buff), "%d_ssid", i); - this->write(buff, this->config.networks[i].ssid); - snprintf(buff, sizeof(buff), "%d_password", i); - this->write(buff, this->config.networks[i].password); - snprintf(buff, sizeof(buff), "%d_channel", i); - this->write(buff, this->config.networks[i].channel); - this->write(buff, this->config.networks[i].adhoc); - } + TrackerConfig_t *tracker_config = (TrackerConfig_t *)&this->config; + putBytes("config", tracker_config, 3 * sizeof(TrackerConfig_t)); log_i("Project config saved and system is rebooting"); delay(20000); diff --git a/ESP/lib/src/data/config/project_config.hpp b/ESP/lib/src/data/config/project_config.hpp index ce60c64..5f1e2de 100644 --- a/ESP/lib/src/data/config/project_config.hpp +++ b/ESP/lib/src/data/config/project_config.hpp @@ -2,13 +2,13 @@ #ifndef PROJECT_CONFIG_HPP #define PROJECT_CONFIG_HPP #include -#include +#include #include #include #include "data/utilities/Observer.hpp" -class ProjectConfig : public Config, public ISubject +class ProjectConfig : public Preferences, public ISubject { public: ProjectConfig(); diff --git a/ESP/lib/src/data/utilities/network_utilities.cpp b/ESP/lib/src/data/utilities/network_utilities.cpp index 7a55b8b..06d08fc 100644 --- a/ESP/lib/src/data/utilities/network_utilities.cpp +++ b/ESP/lib/src/data/utilities/network_utilities.cpp @@ -13,18 +13,16 @@ bool Network_Utilities::LoopWifiScan() { // WiFi.scanNetworks will return the number of networks found log_i("[INFO]: Beginning WiFi Scanner"); - int networks = WiFi.scanNetworks(); + int networks = WiFi.scanNetworks(true, true); log_i("[INFO]: scan done"); log_i("%d networks found", networks); for (int i = networks; i--;) { // Print SSID and RSSI for each network found - //! Add method here to interface with the API and forward the scanned networks to the API + //! TODO: Add method here to interface with the API and forward the scanned networks to the API log_i("%d: %s (%d) %s\n", i - 1, WiFi.SSID(i), WiFi.RSSI(i), (WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*"); my_delay(0.02L); // delay 20ms } - // Wait a bit before scanning again - delay(5000); return (networks > 0); } diff --git a/ESP/lib/src/io/camera/cameraHandler.cpp b/ESP/lib/src/io/camera/cameraHandler.cpp index a684d61..6be5975 100644 --- a/ESP/lib/src/io/camera/cameraHandler.cpp +++ b/ESP/lib/src/io/camera/cameraHandler.cpp @@ -6,6 +6,7 @@ int CameraHandler::setupCamera() config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; + config.grab_mode = CAMERA_GRAB_LATEST; config.pin_d0 = Y2_GPIO_NUM; config.pin_d1 = Y3_GPIO_NUM; config.pin_d2 = Y4_GPIO_NUM; @@ -22,7 +23,7 @@ int CameraHandler::setupCamera() config.pin_sscb_scl = SIOC_GPIO_NUM; config.pin_pwdn = PWDN_GPIO_NUM; config.pin_reset = RESET_GPIO_NUM; - config.xclk_freq_hz = 20000000; // 10000000 stable, + config.xclk_freq_hz = 16500000; // 10000000 stable, // 16500000 optimal, // 20000000 max fps config.pixel_format = PIXFORMAT_JPEG; diff --git a/ESP/lib/src/network/WifiHandler/wifiHandler.cpp b/ESP/lib/src/network/WifiHandler/wifiHandler.cpp index d3a389e..0382357 100644 --- a/ESP/lib/src/network/WifiHandler/wifiHandler.cpp +++ b/ESP/lib/src/network/WifiHandler/wifiHandler.cpp @@ -76,6 +76,7 @@ void WiFiHandler::setupWifi() void WiFiHandler::adhoc(const char *ssid, const char *password, uint8_t channel) { + stateManager->setState(WiFiState_e::WiFiState_ADHOC); log_i("\n[INFO]: Setting Access Point...\n"); log_i("\n[INFO]: Configuring access point...\n"); WiFi.mode(WIFI_AP); @@ -86,7 +87,6 @@ void WiFiHandler::adhoc(const char *ssid, const char *password, uint8_t channel) // You can remove the password parameter if you want the AP to be open. WiFi.softAP(ssid, password, channel); // AP mode with password WiFi.setTxPower(WIFI_POWER_11dBm); - stateManager->setState(WiFiState_e::WiFiState_ADHOC); } /* @@ -155,7 +155,6 @@ void WiFiHandler::iniSTA() this->setUpADHOC(); log_w("Setting up adhoc mode"); log_w("Please use adhoc mode and the app to set your WiFi credentials and reboot the device"); - stateManager->setState(WiFiState_e::WiFiState_ADHOC); return; } } diff --git a/ESP/lib/src/network/api/baseAPI/baseAPI.cpp b/ESP/lib/src/network/api/baseAPI/baseAPI.cpp index 6b9ced3..342727b 100644 --- a/ESP/lib/src/network/api/baseAPI/baseAPI.cpp +++ b/ESP/lib/src/network/api/baseAPI/baseAPI.cpp @@ -1,6 +1,14 @@ #include "baseAPI.hpp" -BaseAPI::BaseAPI() {} +BaseAPI::BaseAPI(int CONTROL_PORT, + WiFiHandler *network, + CameraHandler *camera, + StateManager *stateManager, + std::string api_url) : API_Utilities(CONTROL_PORT, + network, + camera, + stateManager, + api_url) {} BaseAPI::~BaseAPI() {} @@ -106,7 +114,7 @@ void BaseAPI::triggerWifiConfigWrite() } } -//! TODO: Add JSON handling to the POST request +//! TODO: ADD JSON handlers for the POST requests void BaseAPI::handleJson(AsyncWebServerRequest *request) { std::string type = request->pathArg(0).c_str(); diff --git a/ESP/lib/src/network/api/baseAPI/baseAPI.hpp b/ESP/lib/src/network/api/baseAPI/baseAPI.hpp index 538bdcf..dcefd7e 100644 --- a/ESP/lib/src/network/api/baseAPI/baseAPI.hpp +++ b/ESP/lib/src/network/api/baseAPI/baseAPI.hpp @@ -70,7 +70,11 @@ protected: route_map_t route_map; public: - BaseAPI(); + BaseAPI(int CONTROL_PORT, + WiFiHandler *network, + CameraHandler *camera, + StateManager *stateManager, + std::string api_url); virtual ~BaseAPI(); virtual void begin(); diff --git a/ESP/lib/src/network/api/utilities/apiUtilities.cpp b/ESP/lib/src/network/api/utilities/apiUtilities.cpp index 1287eca..2096577 100644 --- a/ESP/lib/src/network/api/utilities/apiUtilities.cpp +++ b/ESP/lib/src/network/api/utilities/apiUtilities.cpp @@ -18,7 +18,16 @@ bool API_Utilities::channel_write = false; //! API Utilities //********************************************************************************************* -API_Utilities::API_Utilities() : server(new AsyncWebServer(_control_port)) {} +API_Utilities::API_Utilities(int CONTROL_PORT, + WiFiHandler *network, + CameraHandler *camera, + StateManager *stateManager, + std::string api_url) : server(new AsyncWebServer(CONTROL_PORT)), + stateManager(stateManager), + network(network), + camera(camera), + api_url(api_url) {} + API_Utilities::~API_Utilities() {} std::string API_Utilities::shaEncoder(std::string data) diff --git a/ESP/lib/src/network/api/utilities/apiUtilities.hpp b/ESP/lib/src/network/api/utilities/apiUtilities.hpp index a39ccee..7565711 100644 --- a/ESP/lib/src/network/api/utilities/apiUtilities.hpp +++ b/ESP/lib/src/network/api/utilities/apiUtilities.hpp @@ -29,7 +29,11 @@ class API_Utilities { public: - API_Utilities(); + API_Utilities(int CONTROL_PORT, + WiFiHandler *network, + CameraHandler *camera, + StateManager *stateManager, + std::string api_url); virtual ~API_Utilities(); static void printASCII(); @@ -76,7 +80,6 @@ protected: protected: std::string api_url; - byte _control_port; static bool ssid_write; static bool pass_write; diff --git a/ESP/lib/src/network/api/webserverHandler.cpp b/ESP/lib/src/network/api/webserverHandler.cpp index d9d68a3..c9223d6 100644 --- a/ESP/lib/src/network/api/webserverHandler.cpp +++ b/ESP/lib/src/network/api/webserverHandler.cpp @@ -4,18 +4,15 @@ //! API Server //********************************************************************************************* -APIServer::APIServer(byte control_port, +APIServer::APIServer(int CONTROL_PORT, WiFiHandler *network, CameraHandler *camera, StateManager *stateManager, - std::string api_url) -{ - this->_control_port = control_port; - this->network = network; - this->camera = camera; - this->stateManager = stateManager; - this->api_url = api_url; -} + std::string api_url) : BaseAPI(CONTROL_PORT, + network, + camera, + stateManager, + api_url) {} APIServer::~APIServer() {} @@ -44,6 +41,7 @@ void APIServer::setupServer() routes.emplace("deleteRoute", &APIServer::deleteRoute); // Camera Routes + //! reserve enough memory for all routes - must be called after adding routes and before adding routes to route_map indexes.reserve(routes.size()); // this is done to avoid reallocation of memory and copying of data diff --git a/ESP/lib/src/network/api/webserverHandler.hpp b/ESP/lib/src/network/api/webserverHandler.hpp index 9cdb2a6..806f6b3 100644 --- a/ESP/lib/src/network/api/webserverHandler.hpp +++ b/ESP/lib/src/network/api/webserverHandler.hpp @@ -7,7 +7,7 @@ class APIServer : public BaseAPI { public: - APIServer(byte control_port, + APIServer(int CONTROL_PORT, WiFiHandler *network, CameraHandler *camera, StateManager *stateManager, diff --git a/ESP/lib/src/network/stream/streamServer.cpp b/ESP/lib/src/network/stream/streamServer.cpp index b038126..aa58a0f 100644 --- a/ESP/lib/src/network/stream/streamServer.cpp +++ b/ESP/lib/src/network/stream/streamServer.cpp @@ -15,7 +15,7 @@ esp_err_t StreamHelpers::stream(httpd_req_t *req) size_t _jpg_buf_len = 0; uint8_t *_jpg_buf = NULL; - char *part_buf[128]; + char *part_buf[256]; static int64_t last_frame = 0; if (!last_frame) @@ -89,6 +89,7 @@ int StreamServer::startStreamServer() config.max_uri_handlers = 1; config.server_port = this->STREAM_SERVER_PORT; config.ctrl_port = this->STREAM_SERVER_PORT; + config.stack_size = 20480; httpd_uri_t stream_page = { .uri = "/", diff --git a/ESP/src/main.cpp b/ESP/src/main.cpp index 9b9da25..501fa5d 100644 --- a/ESP/src/main.cpp +++ b/ESP/src/main.cpp @@ -53,6 +53,12 @@ void setup() break; } case WiFiState_e::WiFiState_ADHOC: + { + streamServer.startStreamServer(); + apiServer.begin(); + log_d("[SETUP]: Starting Stream Server"); + break; + } case WiFiState_e::WiFiState_Connected: { streamServer.startStreamServer();