revert to non-task-driven, no tangible difference in performance. May change back later.

This commit is contained in:
2026-01-08 18:14:36 -06:00
parent e437c32c28
commit 03ca64080c
15 changed files with 240 additions and 435 deletions

View File

@@ -9,58 +9,67 @@
#include "encoder.hpp"
#include "calibration.hpp"
// Global synchronization primitives
EventGroupHandle_t g_system_events = NULL;
QueueHandle_t g_servo_command_queue = NULL;
QueueHandle_t g_encoder_event_queue = NULL;
SemaphoreHandle_t g_calibration_mutex = NULL;
// Global encoder instances
Encoder* topEnc = new Encoder(ENCODER_PIN_A, ENCODER_PIN_B);
Encoder* bottomEnc = new Encoder(InputEnc_PIN_A, InputEnc_PIN_B);
void mainTask(void* arg) {
EventBits_t bits;
// Global encoder pointers (used by servo.cpp)
// Global calibration instance
Calibration calib;
void mainApp() {
esp_err_t ret = nvs_flash_init(); // change to secure init logic soon!!
// 2. If NVS is full or corrupt (common after flashing new code), erase and retry
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
bmWiFi.init();
calib.init();
printf("Main task started - event-driven mode\n");
// Initialize encoders
topEnc->init();
bottomEnc->init();
servoInit();
setupLoop();
statusResolved = false;
int32_t prevCount = topEnc->getCount();
// Main loop
while (1) {
// Block waiting for ANY event (no polling!)
bits = xEventGroupWaitBits(
g_system_events,
EVENT_SOCKETIO_DISCONNECTED | EVENT_CLEAR_CALIB | EVENT_SAVE_POSITION,
pdTRUE, // Clear bits on exit
pdFALSE, // Wait for ANY bit (not all)
portMAX_DELAY // Block forever - no polling!
);
if (bits & EVENT_SOCKETIO_DISCONNECTED) {
// websocket disconnect/reconnect handling
if (statusResolved) {
if (!connected) {
printf("Disconnected! Beginning setup loop.\n");
stopSocketIO();
setupLoop();
}
else {
printf("Reconnected!\n");
}
else printf("Reconnected!\n");
statusResolved = false;
}
if (bits & EVENT_CLEAR_CALIB) {
xSemaphoreTake(g_calibration_mutex, portMAX_DELAY);
Calibration::clearCalibrated();
xSemaphoreGive(g_calibration_mutex);
if (clearCalibFlag) {
calib.clearCalibrated();
emitCalibStatus(false);
clearCalibFlag = false;
}
if (bits & EVENT_SAVE_POSITION) {
if (savePosFlag) {
servoSavePos();
savePosFlag = false;
// Send position update to server
uint8_t currentAppPos = Calibration::convertToAppPos(topEnc->getCount());
uint8_t currentAppPos = calib.convertToAppPos(topEnc->getCount());
emitPosHit(currentAppPos);
printf("Sent pos_hit: position %d\n", currentAppPos);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
@@ -82,44 +91,6 @@ void encoderTest() {
}
extern "C" void app_main() {
// Initialize NVS first
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// Create synchronization primitives
g_system_events = xEventGroupCreate();
g_servo_command_queue = xQueueCreate(10, sizeof(servo_cmd_msg_t));
g_encoder_event_queue = xQueueCreate(50, sizeof(encoder_event_t));
g_calibration_mutex = xSemaphoreCreateMutex();
if (g_system_events == NULL || g_servo_command_queue == NULL ||
g_encoder_event_queue == NULL || g_calibration_mutex == NULL) {
printf("ERROR: Failed to create synchronization primitives\n");
return;
}
// Initialize hardware
WiFi::init();
Calibration::init();
// Initialize encoders
topEnc->init();
bottomEnc->init();
servoInit();
// Create servo control task (highest priority - real-time)
xTaskCreate(servoControlTask, "servo_ctrl", 4096, NULL, SERVO_TASK_PRIORITY, NULL);
// Create main task (lower priority)
xTaskCreate(mainTask, "main", 4096, NULL, MAIN_TASK_PRIORITY, NULL);
// Run setup loop (this will handle WiFi/SocketIO connection)
setupLoop();
// app_main returns, but tasks continue running
printf("app_main complete - tasks running\n");
mainApp();
// encoderTest();
}