diff --git a/include/BLE.cpp b/include/BLE.cpp index 627108a..30a3da0 100644 --- a/include/BLE.cpp +++ b/include/BLE.cpp @@ -266,7 +266,7 @@ void BLE_manager_task(void *pvParameters) { if (!scanBlock) { scanBlock = true; printf("Scanning WiFi...\n"); - bmWiFi.scanAndUpdateSSIDList(); + WiFi::scanAndUpdateSSIDList(); } else printf("Duplicate scan request\n"); } @@ -285,12 +285,12 @@ void BLE_manager_task(void *pvParameters) { else if (received_event_type == EVENT_SHUTDOWN) break; } } - xSemaphoreGive(BLE_Queue_Shutdown_Semaphore); + xSemaphoreGive(BLE_Queue_Shutdown_Semaphore); // this is null-safe vTaskDelete(NULL); } bool tokenCheck() { - if (!bmWiFi.isConnected()) { + if (!WiFi::isConnected()) { printf("ERROR: token given without WiFi connection\n"); notifyAuthStatus(false); return false; @@ -353,8 +353,8 @@ bool attemptUseWiFiCreds() { bool wifiConnect; if (tmpAUTH == WIFI_AUTH_WPA2_ENTERPRISE || tmpAUTH == WIFI_AUTH_WPA3_ENTERPRISE) - wifiConnect = bmWiFi.attemptConnect(tmpSSID.c_str(), tmpUNAME.c_str(), tmpPASS.c_str(), tmpAUTH); - else wifiConnect = bmWiFi.attemptConnect(tmpSSID.c_str(), tmpPASS.c_str(), tmpAUTH); + wifiConnect = WiFi::attemptConnect(tmpSSID.c_str(), tmpUNAME.c_str(), tmpPASS.c_str(), tmpAUTH); + else wifiConnect = WiFi::attemptConnect(tmpSSID.c_str(), tmpPASS.c_str(), tmpAUTH); if (!wifiConnect) { // notify errored notifyConnectionStatus(false); diff --git a/include/WiFi.cpp b/include/WiFi.cpp index 80ca08c..3cd133a 100644 --- a/include/WiFi.cpp +++ b/include/WiFi.cpp @@ -12,8 +12,6 @@ esp_event_handler_instance_t WiFi::instance_got_ip = NULL; #define WIFI_CONNECTED_BIT BIT0 #define WIFI_STARTED_BIT BIT1 -WiFi bmWiFi; - // The Event Handler (The engine room) void WiFi::event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { diff --git a/include/WiFi.hpp b/include/WiFi.hpp index 77bde73..335b3ed 100644 --- a/include/WiFi.hpp +++ b/include/WiFi.hpp @@ -27,6 +27,4 @@ class WiFi { static std::string getIP(); }; -extern WiFi bmWiFi; - #endif \ No newline at end of file diff --git a/include/bmEvents.hpp b/include/bmEvents.hpp index e1eff23..9a3ae9a 100644 --- a/include/bmEvents.hpp +++ b/include/bmEvents.hpp @@ -6,4 +6,20 @@ // void deinit_event_queue(QueueHandle_t& event_queue); +// // 1. Event Types: The "What" +// typedef enum { +// EVENT_BUTTON_PRESSED, +// EVENT_WIFI_CONNECTED, +// EVENT_WIFI_DISCONNECTED, +// EVENT_BLE_DATA_RECEIVED, +// EVENT_TIMER_TICK +// } event_type_t; + +// // 2. The Message Structure: The "Payload" +// typedef struct { +// event_type_t type; +// void *data; // Optional: Pointer to data (buffer, string, etc.) +// int data_len; // Optional: Length of data +// } app_event_t; + #endif \ No newline at end of file diff --git a/include/calibration.hpp b/include/calibration.hpp index e862e7e..ff3b0dc 100644 --- a/include/calibration.hpp +++ b/include/calibration.hpp @@ -5,20 +5,18 @@ class Calibration { public: - void init(); - bool beginDownwardCalib(Encoder& topEnc); - bool completeCalib(Encoder& topEnc); - int32_t convertToTicks(uint8_t appPos); - uint8_t convertToAppPos(int32_t ticks); - bool getCalibrated() {return calibrated;} - bool clearCalibrated(); - std::atomic DownTicks; - std::atomic UpTicks; + static void init(); + static bool beginDownwardCalib(Encoder& topEnc); + static bool completeCalib(Encoder& topEnc); + static int32_t convertToTicks(uint8_t appPos); + static uint8_t convertToAppPos(int32_t ticks); + static bool getCalibrated() {return calibrated;} + static bool clearCalibrated(); + static std::atomic DownTicks; + static std::atomic UpTicks; private: - std::atomic calibrated; + static std::atomic calibrated; }; -extern Calibration calib; - #endif \ No newline at end of file diff --git a/include/servo.cpp b/include/servo.cpp index b81d61f..49d4916 100644 --- a/include/servo.cpp +++ b/include/servo.cpp @@ -49,7 +49,7 @@ void servoInit() { gpio_set_level(debugLED, 0); // Start with LED off topEnc->count = servoReadPos(); - if (calib.getCalibrated()) initMainLoop(); + if (Calibration::getCalibrated()) initMainLoop(); debugLEDSwitch(1); } @@ -89,7 +89,7 @@ bool servoInitCalib() { bottomEnc->wandListen.store(false, std::memory_order_release); topEnc->wandListen.store(false, std::memory_order_release); topEnc->serverListen.store(false, std::memory_order_release); - if (!calib.clearCalibrated()) return false; + if (!Calibration::clearCalibrated()) return false; if (topEnc == nullptr || bottomEnc == nullptr) { printf("ERROR: CALIBRATION STARTED BEFORE SERVO INITIALIZATION\n"); return false; @@ -119,7 +119,7 @@ bool servoBeginDownwardCalib() { calibListen = false; servoOff(); vTaskDelay(pdMS_TO_TICKS(1000)); - if (!calib.beginDownwardCalib(*topEnc)) return false; + if (!Calibration::beginDownwardCalib(*topEnc)) return false; baseDiff = bottomEnc->getCount() - topEnc->getCount(); calibListen = true; return true; @@ -129,7 +129,7 @@ bool servoCompleteCalib() { calibListen = false; servoOff(); vTaskDelay(pdMS_TO_TICKS(1000)); - if (!calib.completeCalib(*topEnc)) return false; + if (!Calibration::completeCalib(*topEnc)) return false; initMainLoop(); return true; } @@ -205,8 +205,8 @@ void servoWandListen() { stopServerRun(); // freeze atomic values - int32_t upBound = calib.UpTicks; - int32_t downBound = calib.DownTicks; + int32_t upBound = Calibration::UpTicks; + int32_t downBound = Calibration::DownTicks; int32_t bottomCount = bottomEnc->getCount(); int32_t topCount = topEnc->getCount(); @@ -259,10 +259,10 @@ void servoServerListen() { void runToAppPos(uint8_t appPos) { // manual control takes precedence over remote control, always. // also do not begin operation if not calibrated; - if (runningManual || !calib.getCalibrated()) return; + if (runningManual || !Calibration::getCalibrated()) return; servoOff(); - target = calib.convertToTicks(appPos); // calculate target encoder position + target = Calibration::convertToTicks(appPos); // calculate target encoder position printf("runToAppPos Called, running to %d from %d", target.load(), topEnc->getCount()); // allow servo position to settle diff --git a/include/setup.cpp b/include/setup.cpp index a138aae..bd7ad4f 100644 --- a/include/setup.cpp +++ b/include/setup.cpp @@ -15,6 +15,7 @@ void initialSetup() { } void setupLoop(void *pvParameters) { + TaskHandle_t parent_handle = (TaskHandle_t)pvParameters; bool initSuccess = false; while(!initSuccess) { nvs_handle_t WiFiHandle; @@ -36,7 +37,7 @@ void setupLoop(void *pvParameters) { char pw[pwSize]; nvs_get_str(WiFiHandle, passTag, pw, &pwSize); nvs_close(WiFiHandle); - if (!bmWiFi.attemptConnect(ssid, pw, (wifi_auth_mode_t)authMode)) { + if (!WiFi::attemptConnect(ssid, pw, (wifi_auth_mode_t)authMode)) { // Make RGB LED certain color (Blue?) printf("Found credentials, failed to connect.\n"); initialSetup(); @@ -102,4 +103,6 @@ void setupLoop(void *pvParameters) { initialSetup(); } } + xTaskNotifyGive(parent_handle); + vTaskDelete(NULL); } \ No newline at end of file diff --git a/include/setup.hpp b/include/setup.hpp index f75f527..9c81b57 100644 --- a/include/setup.hpp +++ b/include/setup.hpp @@ -2,6 +2,7 @@ #define SETUP_H extern TaskHandle_t setupTaskHandle; +extern SemaphoreHandle_t Setup_Complete_Semaphore; void initialSetup(); void setupLoop(void *pvParameters); diff --git a/include/socketIO.cpp b/include/socketIO.cpp index 0bdb530..7829339 100644 --- a/include/socketIO.cpp +++ b/include/socketIO.cpp @@ -93,7 +93,7 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base, if (port != 1) printf("ERROR: NON-1 PORT RECEIVED\n"); // Report back actual calibration status from device else { - bool deviceCalibrated = calib.getCalibrated(); + bool deviceCalibrated = Calibration::getCalibrated(); emitCalibStatus(deviceCalibrated); printf(" Reported calibrated=%d for port %d\n", deviceCalibrated, port); runToAppPos(lastPos); @@ -106,7 +106,7 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base, statusResolved = true; } else { printf("Device authentication failed\n"); - calib.clearCalibrated(); + Calibration::clearCalibrated(); deleteWiFiAndTokenDetails(); connected = false; statusResolved = true; @@ -123,7 +123,7 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base, printf("Server message: %s\n", message->valuestring); } } - calib.clearCalibrated(); + Calibration::clearCalibrated(); deleteWiFiAndTokenDetails(); connected = false; statusResolved = true; diff --git a/src/main.cpp b/src/main.cpp index 5bd2bb7..4034111 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,8 +16,9 @@ Encoder* bottomEnc = new Encoder(InputEnc_PIN_A, InputEnc_PIN_B); // Global encoder pointers (used by servo.cpp) -// Global calibration instance -Calibration calib; +void MainTask(void *pvParameters) { + +} void mainApp() { esp_err_t ret = nvs_flash_init(); // change to secure init logic soon!! @@ -28,15 +29,16 @@ void mainApp() { } ESP_ERROR_CHECK(ret); - bmWiFi.init(); - calib.init(); + WiFi::init(); + Calibration::init(); // Initialize encoders topEnc->init(); bottomEnc->init(); servoInit(); - xTaskCreate(setupLoop, "Setup", 8192, NULL, 5, &setupTaskHandle); + xTaskCreate(setupLoop, "Setup", 8192, xTaskGetCurrentTaskHandle(), 5, &setupTaskHandle); + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // TOMORROW!!! // statusResolved = false;