diff --git a/ESP/lib/src/data/utilities/helpers.cpp b/ESP/lib/src/data/utilities/helpers.cpp index 52af448..bf62d48 100644 --- a/ESP/lib/src/data/utilities/helpers.cpp +++ b/ESP/lib/src/data/utilities/helpers.cpp @@ -1,99 +1,93 @@ #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; - } +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++ = + "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxy" + "z"[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 &tokens) -{ - /* Store the original string in the array, so we can loop the rest - * of the algorithm. */ - tokens.emplace_back(str); +void split(const std::string& str, + const std::string& splitBy, + std::vector& 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))); + // 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 Helpers::split(const std::string &s, char delimiter) -{ - std::vector parts; - std::string part; - std::istringstream tokenStream(s); - while (std::getline(tokenStream, part, delimiter)) - { - parts.push_back(part); - } - return parts; +std::vector Helpers::split(const std::string& s, char delimiter) { + std::vector parts; + std::string part; + std::istringstream tokenStream(s); + while (std::getline(tokenStream, part, delimiter)) { + parts.push_back(part); + } + return parts; } -void Helpers::update_progress_bar(int progress, int total) -{ - int barWidth = 70; +void Helpers::update_progress_bar(int progress, int total) { + int barWidth = 70; - std::cout << "\r["; - int pos = barWidth * progress / total; - for (int i = 0; i < barWidth; ++i) - { - if (i < pos) - std::cout << "="; - else if (i == pos) - std::cout << ">"; - else - std::cout << " "; - } - std::cout << "] " << int(progress * 100.0 / total) << " %\r"; - std::cout.flush(); + std::cout << "\r["; + int pos = barWidth * progress / total; + for (int i = 0; i < barWidth; ++i) { + if (i < pos) + std::cout << "="; + else if (i == pos) + std::cout << ">"; + else + std::cout << " "; + } + std::cout << "] " << int(progress * 100.0 / total) << " %\r"; + std::cout.flush(); } \ No newline at end of file diff --git a/ESP/lib/src/data/utilities/helpers.hpp b/ESP/lib/src/data/utilities/helpers.hpp index 0f1d0a4..35cab0f 100644 --- a/ESP/lib/src/data/utilities/helpers.hpp +++ b/ESP/lib/src/data/utilities/helpers.hpp @@ -1,37 +1,41 @@ #ifndef HELPERS_HPP #define HELPERS_HPP -#include -#include -#include #include #include +#include +#include +#include -namespace Helpers -{ - char *itoa(int value, char *result, int base); - void split(std::string str, std::string splitBy, std::vector &tokens); - std::vector split(const std::string &s, char delimiter); - void update_progress_bar(int progress, int total); +const unsigned char ETVR_HEADER[] = {0xFF, 0xA0, 0xFF, 0xA1, 0x01}; +const char* const ETVR_HEADER_BYTES = "\xff\xa0\xff\xa1"; - /// @brief - /// @tparam ...Args - /// @param format - /// @param ...args - /// @return - template - 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_s); - std::unique_ptr 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 +namespace Helpers { + char* itoa(int value, char* result, int base); + void split(std::string str, + std::string splitBy, + std::vector& tokens); + std::vector split(const std::string& s, char delimiter); + void update_progress_bar(int progress, int total); + + /// @brief + /// @tparam ...Args + /// @param format + /// @param ...args + /// @return + template + 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_s); + std::unique_ptr 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 + } +} // namespace Helpers -#endif // HELPERS_HPP +#endif // HELPERS_HPP diff --git a/ESP/lib/src/io/LEDManager/LEDManager.cpp b/ESP/lib/src/io/LEDManager/LEDManager.cpp index 2ff76cf..20e3dc5 100644 --- a/ESP/lib/src/io/LEDManager/LEDManager.cpp +++ b/ESP/lib/src/io/LEDManager/LEDManager.cpp @@ -9,6 +9,7 @@ *between blinks. */ +// TODO rethink this LEDManager::ledStateMap_t LEDManager::ledStateMap = { {LEDStates_e::_LedStateNone, {{0, 500}}}, {LEDStates_e::_Improv_Error, @@ -58,7 +59,7 @@ LEDManager::~LEDManager() {} void LEDManager::begin() { pinMode(_ledPin, OUTPUT); - // the defualt state is _LedStateNone so we're fine + // the default state is _LedStateNone so we're fine this->currentState = ledStateManager.getCurrentState(); this->currentPatternIndex = 0; BlinkPatterns_t pattern = @@ -66,7 +67,12 @@ void LEDManager::begin() { this->toggleLED(pattern.state); this->nextStateChangeMillis = pattern.delayTime; - log_d("begin %d", this->currentPatternIndex); + log_d("Led manager began with: %d", this->currentPatternIndex); + +#ifdef CONFIG_CAMERA_MODULE_SWROOM_BABBLE_S3 + log_d("Setting up LED for the Babble board"); + this->setupBabbeLed(); +#endif } /** @@ -120,6 +126,19 @@ void LEDManager::handleLED() { log_d("updated stage %d", this->currentPatternIndex); } +#ifdef CONFIG_CAMERA_MODULE_SWROOM_BABBLE_S3 +void LEDManager::setupBabbeLed() { + // Set IR emitter strength to 100%. + const int ledPin = 1; // Replace this with a command endpoint eventually. + const int freq = 5000; + const int ledChannel = 0; + const int resolution = 8; + const int dutyCycle = 255; + ledcSetup(ledChannel, freq, resolution); + ledcAttachPin(ledPin, ledChannel); + ledcWrite(ledChannel, dutyCycle); +} +#endif /** * @brief Turn the LED on or off * diff --git a/ESP/lib/src/io/LEDManager/LEDManager.hpp b/ESP/lib/src/io/LEDManager/LEDManager.hpp index f8772a3..b1f5533 100644 --- a/ESP/lib/src/io/LEDManager/LEDManager.hpp +++ b/ESP/lib/src/io/LEDManager/LEDManager.hpp @@ -1,36 +1,38 @@ #ifndef LEDMANAGER_HPP #define LEDMANAGER_HPP -#include #include #include #include +#include -class LEDManager -{ -public: - LEDManager(byte pin); - virtual ~LEDManager(); +class LEDManager { + public: + LEDManager(byte pin); + virtual ~LEDManager(); - void begin(); - void handleLED(); - void toggleLED(bool state) const; + void begin(); + void handleLED(); + void toggleLED(bool state) const; +#ifdef CONFIG_CAMERA_MODULE_SWROOM_BABBLE_S3 + void setupBabbeLed(); +#endif -private: - byte _ledPin; - unsigned long nextStateChangeMillis = 0; - bool _ledState; + private: + byte _ledPin; + unsigned long nextStateChangeMillis = 0; + bool _ledState; - struct BlinkPatterns_t - { - int state; - int delayTime; - }; + struct BlinkPatterns_t { + int state; + int delayTime; + }; - typedef std::unordered_map> ledStateMap_t; - static ledStateMap_t ledStateMap; - static std::vector keepAliveStates; - LEDStates_e currentState; - unsigned int currentPatternIndex = 0; + typedef std::unordered_map> + ledStateMap_t; + static ledStateMap_t ledStateMap; + static std::vector keepAliveStates; + LEDStates_e currentState; + unsigned int currentPatternIndex = 0; }; -#endif // LEDMANAGER_HPP +#endif // LEDMANAGER_HPP diff --git a/ESP/lib/src/network/api/baseAPI/Hash.h b/ESP/lib/src/network/api/baseAPI/Hash.h deleted file mode 100644 index d98e562..0000000 --- a/ESP/lib/src/network/api/baseAPI/Hash.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef HASH_H_ -#define HASH_H_ - -#include -#include - -// #define DEBUG_SHA1 - -void sha1(const uint8_t* data, uint32_t size, uint8_t hash[20]); -void sha1(const char* data, uint32_t size, uint8_t hash[20]); -void sha1(const std::string& data, uint8_t hash[20]); - -std::string sha1(const uint8_t* data, uint32_t size); -std::string sha1(const char* data, uint32_t size); -std::string sha1(const std::string& data); - -#endif /* HASH_H_ */ diff --git a/ESP/lib/src/network/api/baseAPI/baseAPI.cpp b/ESP/lib/src/network/api/baseAPI/baseAPI.cpp index d3ce808..ec74227 100644 --- a/ESP/lib/src/network/api/baseAPI/baseAPI.cpp +++ b/ESP/lib/src/network/api/baseAPI/baseAPI.cpp @@ -102,17 +102,6 @@ void BaseAPI::setWiFi(AsyncWebServerRequest* request) { projectConfig.setWifiConfig(networkName, ssid, password, channel, power, adhoc, true); - /* if (WiFiStateManager->getCurrentState() == - WiFiState_e::WiFiState_ADHOC) - { - projectConfig.setAPWifiConfig(ssid, password, &channel, adhoc, - true); - } - else - { - - } */ - request->send(200, MIMETYPE_JSON, "{\"msg\":\"Done. Wifi Creds have been set.\"}"); break; diff --git a/ESP/lib/src/network/api/baseAPI/baseAPI.hpp b/ESP/lib/src/network/api/baseAPI/baseAPI.hpp index 4b6a544..35d5fbc 100644 --- a/ESP/lib/src/network/api/baseAPI/baseAPI.hpp +++ b/ESP/lib/src/network/api/baseAPI/baseAPI.hpp @@ -3,7 +3,6 @@ //! Warning do not format this file with clang-format or it will break the code -#include #include #include @@ -31,7 +30,6 @@ constexpr int HTTP_ANY = 0b01111111; #include #include -#include "Hash.h" #include "data/utilities/network_utilities.hpp" #include "tasks/tasks.hpp" @@ -100,33 +98,34 @@ class BaseAPI { typedef std::unordered_map networkMethodsMap_t; - ProjectConfig &projectConfig; - /// @brief Local instance of the AsyncWebServer - so that we dont need to use new and delete - AsyncWebServer server; + ProjectConfig& projectConfig; + /// @brief Local instance of the AsyncWebServer - so that we dont need to use + /// new and delete + AsyncWebServer server; #ifndef SIM_ENABLED - CameraHandler &camera; + CameraHandler& camera; #endif // SIM_ENABLED -public : - BaseAPI(ProjectConfig& projectConfig, + public: + BaseAPI(ProjectConfig& projectConfig, #ifndef SIM_ENABLED - CameraHandler& camera, + CameraHandler& camera, #endif // SIM_ENABLED - const std::string& api_url, + const std::string& api_url, #ifndef SIM_ENABLED - int port = 81 + int port = 81 #else - int port = 80 + int port = 80 #endif ); - virtual ~BaseAPI(); - virtual void begin(); - void checkAuthentication(AsyncWebServerRequest* request, - const char* login, - const char* password); - void beginOTA(); - void notFound(AsyncWebServerRequest* request) const; + virtual ~BaseAPI(); + virtual void begin(); + void checkAuthentication(AsyncWebServerRequest* request, + const char* login, + const char* password); + void beginOTA(); + void notFound(AsyncWebServerRequest* request) const; }; #endif // BASEAPI_HPP diff --git a/ESP/lib/src/network/mDNS/MDNSManager.cpp b/ESP/lib/src/network/mDNS/MDNSManager.cpp index e0e4d29..cff4632 100644 --- a/ESP/lib/src/network/mDNS/MDNSManager.cpp +++ b/ESP/lib/src/network/mDNS/MDNSManager.cpp @@ -6,7 +6,8 @@ MDNSHandler::MDNSHandler(ProjectConfig& configManager) bool MDNSHandler::startMDNS() { const std::string service = "_openiristracker"; auto mdnsConfig = configManager.getMDNSConfig(); - if (!MDNS.begin(mdnsConfig.hostname.c_str())) // lowercase only - as this will be the url + if (!MDNS.begin(mdnsConfig.hostname + .c_str())) // lowercase only - as this will be the url { mdnsStateManager.setState(MDNSState_e::MDNSState_Error); log_e("Error initializing MDNS"); diff --git a/ESP/lib/src/network/wifihandler/wifihandler.cpp b/ESP/lib/src/network/wifihandler/wifihandler.cpp index 8693eb2..f8d3626 100644 --- a/ESP/lib/src/network/wifihandler/wifihandler.cpp +++ b/ESP/lib/src/network/wifihandler/wifihandler.cpp @@ -1,7 +1,4 @@ #include "wifihandler.hpp" -#include -#include "data/StateManager/StateManager.hpp" -#include "data/utilities/helpers.hpp" WiFiHandler::WiFiHandler(ProjectConfig& configManager, const std::string& ssid, @@ -18,10 +15,11 @@ WiFiHandler::WiFiHandler(ProjectConfig& configManager, WiFiHandler::~WiFiHandler() {} void WiFiHandler::begin() { - - // just to be sure, we reeset everything before we do anything, some boards were having problems otherwise + // just to be sure, we reeset everything before we do anything, some boards + // were having problems otherwise WiFi.disconnect(); - // we purposefully set the lowest min required security level, some boards have problems connecting otherwise + // we purposefully set the lowest min required security level, some boards + // have problems connecting otherwise // https://github.com/espressif/arduino-esp32/issues/8770 WiFi.setMinSecurity(WIFI_AUTH_WEP); @@ -33,7 +31,9 @@ void WiFiHandler::begin() { return; } - log_d("ADHOC is disabled, setting up STA network and checking transmission power \n\r"); + log_d( + "ADHOC is disabled, setting up STA network and checking transmission " + "power \n\r"); auto txpower = configManager.getWiFiTxPowerConfig(); log_d("Setting Wifi Power to: %d", txpower.power); log_d("Setting WiFi sleep mode to NONE \n\r"); @@ -46,14 +46,9 @@ void WiFiHandler::begin() { if (networks.empty()) { log_i("No networks found in config, trying the default one \n\r"); - - if (this->iniSTA( - this->ssid, - this->password, - this->channel, - (wifi_power_t)txpower.power - ) - ) { + + if (this->iniSTA(this->ssid, this->password, this->channel, + (wifi_power_t)txpower.power)) { return; } @@ -139,12 +134,11 @@ bool WiFiHandler::iniSTA(const std::string& ssid, const std::string& password, uint8_t channel, wifi_power_t power) { - - // since networks may not have a password, we only need to check if we have an ssid - // bail if we don't - if (ssid == ""){ + // since networks may not have a password, we only need to check if we have an + // ssid bail if we don't + if (ssid == "") { log_d("ssid missing, bailing"); - return false; + return false; } unsigned long currentMillis = millis(); @@ -159,8 +153,9 @@ bool WiFiHandler::iniSTA(const std::string& ssid, INADDR_NONE); // need to call before setting hostname log_d("Setting hostname %s \n\r"); WiFi.setHostname(mdnsConfig.hostname.c_str()); - log_i("Setting TX power to: %d \n\r", (uint8_t)power); - WiFi.setTxPower(power); // https://github.com/espressif/arduino-esp32/issues/5698 + log_i("Setting TX power to: %d \n\r", (uint8_t)power); + WiFi.setTxPower( + power); // https://github.com/espressif/arduino-esp32/issues/5698 WiFi.begin(ssid.c_str(), password.c_str(), channel); log_d("Waiting for WiFi to connect... \n\r"); diff --git a/ESP/lib/src/network/wifihandler/wifihandler.hpp b/ESP/lib/src/network/wifihandler/wifihandler.hpp index fd187d7..acae52e 100644 --- a/ESP/lib/src/network/wifihandler/wifihandler.hpp +++ b/ESP/lib/src/network/wifihandler/wifihandler.hpp @@ -1,8 +1,12 @@ #pragma once #ifndef WIFIHANDLER_HPP #define WIFIHANDLER_HPP +#include #include +#include "data/StateManager/StateManager.hpp" #include "data/config/project_config.hpp" +#include "data/utilities/helpers.hpp" + #include "data/utilities/Observer.hpp" class WiFiHandler : public IObserver { @@ -29,7 +33,6 @@ class WiFiHandler : public IObserver { ProjectConfig& configManager; - bool _enable_adhoc; std::string ssid; std::string password; diff --git a/ESP/src/main.cpp b/ESP/src/main.cpp index db05ca3..3badeec 100644 --- a/ESP/src/main.cpp +++ b/ESP/src/main.cpp @@ -6,18 +6,16 @@ * @param mdnsName The mDNS hostname to use */ ProjectConfig deviceConfig("openiris", MDNS_HOSTNAME); -CommandManager commandManager(&deviceConfig); +CommandManager commandManager(deviceConfig); SerialManager serialManager(&commandManager); #ifdef CONFIG_CAMERA_MODULE_ESP32S3_XIAO_SENSE LEDManager ledManager(LED_BUILTIN); - #elif CONFIG_CAMERA_MODULE_SWROOM_BABBLE_S3 LEDManager ledManager(38); - #else LEDManager ledManager(33); -#endif // ESP32S3_XIAO_SENSE +#endif #ifndef SIM_ENABLED CameraHandler cameraHandler(deviceConfig); @@ -30,10 +28,10 @@ WiFiHandler wifiHandler(deviceConfig, WIFI_CHANNEL, ENABLE_ADHOC); MDNSHandler mdnsHandler(deviceConfig); -#ifdef SIM_ENABLED -APIServer apiServer(deviceConfig, wifiStateManager, "/control"); -#else + APIServer apiServer(deviceConfig, cameraHandler, "/control"); + +#ifndef SIM_ENABLED StreamServer streamServer; #endif // SIM_ENABLED @@ -45,37 +43,22 @@ void etvr_eye_tracker_web_init() { log_d("[SETUP]: Starting MDNS Handler"); mdnsHandler.startMDNS(); - switch (wifiStateManager.getCurrentState()) { - case WiFiState_e::WiFiState_Disconnected: { - //! TODO: Implement - break; - } - case WiFiState_e::WiFiState_ADHOC: { + auto wifiState = wifiStateManager.getCurrentState(); #ifndef SIM_ENABLED + if (wifiState == WiFiState_e::WiFiState_Connected || + wifiState == WiFiState_e::WiFiState_ADHOC) { + { log_d("[SETUP]: Starting Stream Server"); - streamServer.startStreamServer(); -#endif // SIM_ENABLED + auto result = streamServer.startTCPStreamServer(); + // streamServer.startStreamServer(); + // auto result = streamServer.startUDPStreamServer(); + + log_d("[SETUP]: Stream Server state: %s", + result ? "Connected" : "Failed to connect"); log_d("[SETUP]: Starting API Server"); apiServer.setup(); - break; } - case WiFiState_e::WiFiState_Connected: { -#ifndef SIM_ENABLED - log_d("[SETUP]: Starting Stream Server"); - streamServer.startStreamServer(); #endif // SIM_ENABLED - log_d("[SETUP]: Starting API Server"); - apiServer.setup(); - break; - } - case WiFiState_e::WiFiState_Connecting: { - //! TODO: Implement - break; - } - case WiFiState_e::WiFiState_Error: { - //! TODO: Implement - break; - } } } #endif // ETVR_EYE_TRACKER_WEB_API @@ -86,22 +69,10 @@ void setup() { Logo::printASCII(); ledManager.begin(); - #ifdef CONFIG_CAMERA_MODULE_SWROOM_BABBLE_S3 // Set IR emitter strength to 100%. - const int ledPin = 1; // Replace this with a command endpoint eventually. - const int freq = 5000; - const int ledChannel = 0; - const int resolution = 8; - const int dutyCycle = 255; - ledcSetup(ledChannel, freq, resolution); - ledcAttachPin(1, ledChannel); - ledcWrite(ledChannel, dutyCycle); - #endif - #ifndef SIM_ENABLED deviceConfig.attach(cameraHandler); #endif // SIM_ENABLED deviceConfig.load(); - serialManager.init(); #ifndef ETVR_EYE_TRACKER_USB_API @@ -112,6 +83,10 @@ void setup() { } void loop() { +#ifndef ETVR_EYE_TRACKER_USB_API + streamServer.sendTCPFrame(); +#endif + // streamServer.sendUDPFrame(); ledManager.handleLED(); serialManager.run(); }