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) {
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);

View File

@@ -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) {

View File

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

View File

@@ -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

View File

@@ -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<int32_t> DownTicks;
std::atomic<int32_t> 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<int32_t> DownTicks;
static std::atomic<int32_t> UpTicks;
private:
std::atomic<bool> calibrated;
static std::atomic<bool> calibrated;
};
extern Calibration calib;
#endif

View File

@@ -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

View File

@@ -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);
}

View File

@@ -2,6 +2,7 @@
#define SETUP_H
extern TaskHandle_t setupTaskHandle;
extern SemaphoreHandle_t Setup_Complete_Semaphore;
void initialSetup();
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");
// 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;