Save progress: pre-main-task, post-setup task-driven

This commit is contained in:
2026-01-10 22:48:24 -06:00
parent 0fd4db453d
commit a31c00ba45
10 changed files with 54 additions and 38 deletions

View File

@@ -266,7 +266,7 @@ void BLE_manager_task(void *pvParameters) {
if (!scanBlock) { if (!scanBlock) {
scanBlock = true; scanBlock = true;
printf("Scanning WiFi...\n"); printf("Scanning WiFi...\n");
bmWiFi.scanAndUpdateSSIDList(); WiFi::scanAndUpdateSSIDList();
} }
else printf("Duplicate scan request\n"); else printf("Duplicate scan request\n");
} }
@@ -285,12 +285,12 @@ void BLE_manager_task(void *pvParameters) {
else if (received_event_type == EVENT_SHUTDOWN) break; else if (received_event_type == EVENT_SHUTDOWN) break;
} }
} }
xSemaphoreGive(BLE_Queue_Shutdown_Semaphore); xSemaphoreGive(BLE_Queue_Shutdown_Semaphore); // this is null-safe
vTaskDelete(NULL); vTaskDelete(NULL);
} }
bool tokenCheck() { bool tokenCheck() {
if (!bmWiFi.isConnected()) { if (!WiFi::isConnected()) {
printf("ERROR: token given without WiFi connection\n"); printf("ERROR: token given without WiFi connection\n");
notifyAuthStatus(false); notifyAuthStatus(false);
return false; return false;
@@ -353,8 +353,8 @@ bool attemptUseWiFiCreds() {
bool wifiConnect; bool wifiConnect;
if (tmpAUTH == WIFI_AUTH_WPA2_ENTERPRISE || tmpAUTH == WIFI_AUTH_WPA3_ENTERPRISE) if (tmpAUTH == WIFI_AUTH_WPA2_ENTERPRISE || tmpAUTH == WIFI_AUTH_WPA3_ENTERPRISE)
wifiConnect = bmWiFi.attemptConnect(tmpSSID.c_str(), tmpUNAME.c_str(), tmpPASS.c_str(), tmpAUTH); wifiConnect = WiFi::attemptConnect(tmpSSID.c_str(), tmpUNAME.c_str(), tmpPASS.c_str(), tmpAUTH);
else wifiConnect = bmWiFi.attemptConnect(tmpSSID.c_str(), tmpPASS.c_str(), tmpAUTH); else wifiConnect = WiFi::attemptConnect(tmpSSID.c_str(), tmpPASS.c_str(), tmpAUTH);
if (!wifiConnect) { if (!wifiConnect) {
// notify errored // notify errored
notifyConnectionStatus(false); notifyConnectionStatus(false);

View File

@@ -12,8 +12,6 @@ esp_event_handler_instance_t WiFi::instance_got_ip = NULL;
#define WIFI_CONNECTED_BIT BIT0 #define WIFI_CONNECTED_BIT BIT0
#define WIFI_STARTED_BIT BIT1 #define WIFI_STARTED_BIT BIT1
WiFi bmWiFi;
// The Event Handler (The engine room) // The Event Handler (The engine room)
void WiFi::event_handler(void* arg, esp_event_base_t event_base, void WiFi::event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data) { int32_t event_id, void* event_data) {

View File

@@ -27,6 +27,4 @@ class WiFi {
static std::string getIP(); static std::string getIP();
}; };
extern WiFi bmWiFi;
#endif #endif

View File

@@ -6,4 +6,20 @@
// void deinit_event_queue(QueueHandle_t& event_queue); // 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 #endif

View File

@@ -5,20 +5,18 @@
class Calibration { class Calibration {
public: public:
void init(); static void init();
bool beginDownwardCalib(Encoder& topEnc); static bool beginDownwardCalib(Encoder& topEnc);
bool completeCalib(Encoder& topEnc); static bool completeCalib(Encoder& topEnc);
int32_t convertToTicks(uint8_t appPos); static int32_t convertToTicks(uint8_t appPos);
uint8_t convertToAppPos(int32_t ticks); static uint8_t convertToAppPos(int32_t ticks);
bool getCalibrated() {return calibrated;} static bool getCalibrated() {return calibrated;}
bool clearCalibrated(); static bool clearCalibrated();
std::atomic<int32_t> DownTicks; static std::atomic<int32_t> DownTicks;
std::atomic<int32_t> UpTicks; static std::atomic<int32_t> UpTicks;
private: private:
std::atomic<bool> calibrated; static std::atomic<bool> calibrated;
}; };
extern Calibration calib;
#endif #endif

View File

@@ -49,7 +49,7 @@ void servoInit() {
gpio_set_level(debugLED, 0); // Start with LED off gpio_set_level(debugLED, 0); // Start with LED off
topEnc->count = servoReadPos(); topEnc->count = servoReadPos();
if (calib.getCalibrated()) initMainLoop(); if (Calibration::getCalibrated()) initMainLoop();
debugLEDSwitch(1); debugLEDSwitch(1);
} }
@@ -89,7 +89,7 @@ bool servoInitCalib() {
bottomEnc->wandListen.store(false, std::memory_order_release); bottomEnc->wandListen.store(false, std::memory_order_release);
topEnc->wandListen.store(false, std::memory_order_release); topEnc->wandListen.store(false, std::memory_order_release);
topEnc->serverListen.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) { if (topEnc == nullptr || bottomEnc == nullptr) {
printf("ERROR: CALIBRATION STARTED BEFORE SERVO INITIALIZATION\n"); printf("ERROR: CALIBRATION STARTED BEFORE SERVO INITIALIZATION\n");
return false; return false;
@@ -119,7 +119,7 @@ bool servoBeginDownwardCalib() {
calibListen = false; calibListen = false;
servoOff(); servoOff();
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
if (!calib.beginDownwardCalib(*topEnc)) return false; if (!Calibration::beginDownwardCalib(*topEnc)) return false;
baseDiff = bottomEnc->getCount() - topEnc->getCount(); baseDiff = bottomEnc->getCount() - topEnc->getCount();
calibListen = true; calibListen = true;
return true; return true;
@@ -129,7 +129,7 @@ bool servoCompleteCalib() {
calibListen = false; calibListen = false;
servoOff(); servoOff();
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
if (!calib.completeCalib(*topEnc)) return false; if (!Calibration::completeCalib(*topEnc)) return false;
initMainLoop(); initMainLoop();
return true; return true;
} }
@@ -205,8 +205,8 @@ void servoWandListen() {
stopServerRun(); stopServerRun();
// freeze atomic values // freeze atomic values
int32_t upBound = calib.UpTicks; int32_t upBound = Calibration::UpTicks;
int32_t downBound = calib.DownTicks; int32_t downBound = Calibration::DownTicks;
int32_t bottomCount = bottomEnc->getCount(); int32_t bottomCount = bottomEnc->getCount();
int32_t topCount = topEnc->getCount(); int32_t topCount = topEnc->getCount();
@@ -259,10 +259,10 @@ void servoServerListen() {
void runToAppPos(uint8_t appPos) { void runToAppPos(uint8_t appPos) {
// manual control takes precedence over remote control, always. // manual control takes precedence over remote control, always.
// also do not begin operation if not calibrated; // also do not begin operation if not calibrated;
if (runningManual || !calib.getCalibrated()) return; if (runningManual || !Calibration::getCalibrated()) return;
servoOff(); 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()); printf("runToAppPos Called, running to %d from %d", target.load(), topEnc->getCount());
// allow servo position to settle // allow servo position to settle

View File

@@ -15,6 +15,7 @@ void initialSetup() {
} }
void setupLoop(void *pvParameters) { void setupLoop(void *pvParameters) {
TaskHandle_t parent_handle = (TaskHandle_t)pvParameters;
bool initSuccess = false; bool initSuccess = false;
while(!initSuccess) { while(!initSuccess) {
nvs_handle_t WiFiHandle; nvs_handle_t WiFiHandle;
@@ -36,7 +37,7 @@ void setupLoop(void *pvParameters) {
char pw[pwSize]; char pw[pwSize];
nvs_get_str(WiFiHandle, passTag, pw, &pwSize); nvs_get_str(WiFiHandle, passTag, pw, &pwSize);
nvs_close(WiFiHandle); 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?) // Make RGB LED certain color (Blue?)
printf("Found credentials, failed to connect.\n"); printf("Found credentials, failed to connect.\n");
initialSetup(); initialSetup();
@@ -102,4 +103,6 @@ void setupLoop(void *pvParameters) {
initialSetup(); initialSetup();
} }
} }
xTaskNotifyGive(parent_handle);
vTaskDelete(NULL);
} }

View File

@@ -2,6 +2,7 @@
#define SETUP_H #define SETUP_H
extern TaskHandle_t setupTaskHandle; extern TaskHandle_t setupTaskHandle;
extern SemaphoreHandle_t Setup_Complete_Semaphore;
void initialSetup(); void initialSetup();
void setupLoop(void *pvParameters); void setupLoop(void *pvParameters);

View File

@@ -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"); if (port != 1) printf("ERROR: NON-1 PORT RECEIVED\n");
// Report back actual calibration status from device // Report back actual calibration status from device
else { else {
bool deviceCalibrated = calib.getCalibrated(); bool deviceCalibrated = Calibration::getCalibrated();
emitCalibStatus(deviceCalibrated); emitCalibStatus(deviceCalibrated);
printf(" Reported calibrated=%d for port %d\n", deviceCalibrated, port); printf(" Reported calibrated=%d for port %d\n", deviceCalibrated, port);
runToAppPos(lastPos); runToAppPos(lastPos);
@@ -106,7 +106,7 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base,
statusResolved = true; statusResolved = true;
} else { } else {
printf("Device authentication failed\n"); printf("Device authentication failed\n");
calib.clearCalibrated(); Calibration::clearCalibrated();
deleteWiFiAndTokenDetails(); deleteWiFiAndTokenDetails();
connected = false; connected = false;
statusResolved = true; 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); printf("Server message: %s\n", message->valuestring);
} }
} }
calib.clearCalibrated(); Calibration::clearCalibrated();
deleteWiFiAndTokenDetails(); deleteWiFiAndTokenDetails();
connected = false; connected = false;
statusResolved = true; statusResolved = true;

View File

@@ -16,8 +16,9 @@ Encoder* bottomEnc = new Encoder(InputEnc_PIN_A, InputEnc_PIN_B);
// Global encoder pointers (used by servo.cpp) // Global encoder pointers (used by servo.cpp)
// Global calibration instance void MainTask(void *pvParameters) {
Calibration calib;
}
void mainApp() { void mainApp() {
esp_err_t ret = nvs_flash_init(); // change to secure init logic soon!! esp_err_t ret = nvs_flash_init(); // change to secure init logic soon!!
@@ -28,15 +29,16 @@ void mainApp() {
} }
ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(ret);
bmWiFi.init(); WiFi::init();
calib.init(); Calibration::init();
// Initialize encoders // Initialize encoders
topEnc->init(); topEnc->init();
bottomEnc->init(); bottomEnc->init();
servoInit(); servoInit();
xTaskCreate(setupLoop, "Setup", 8192, NULL, 5, &setupTaskHandle); xTaskCreate(setupLoop, "Setup", 8192, xTaskGetCurrentTaskHandle(), 5, &setupTaskHandle);
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
// TOMORROW!!! // TOMORROW!!!
// statusResolved = false; // statusResolved = false;