diff --git a/include/encoder.cpp b/include/encoder.cpp index 0551e0a..c4fec06 100644 --- a/include/encoder.cpp +++ b/include/encoder.cpp @@ -50,6 +50,7 @@ void IRAM_ATTR Encoder::isr_handler(void* arg) if (calibListen) servoCalibListen(); if (encoder->feedWDog) esp_timer_restart(encoder->watchdog_handle, 500000); if (encoder->wandListen) servoWandListen(); + if (encoder->serverListen) servoServerListen(); } else if (encoder->last_count_base < 0) { encoder->count -= 1; @@ -57,6 +58,7 @@ void IRAM_ATTR Encoder::isr_handler(void* arg) if (calibListen) servoCalibListen(); if (encoder->feedWDog) esp_timer_restart(encoder->watchdog_handle, 500000); if (encoder->wandListen) servoWandListen(); + if (encoder->serverListen) servoServerListen(); } encoder->last_state_a = current_a; diff --git a/include/servo.cpp b/include/servo.cpp index 76dcbc7..5c45610 100644 --- a/include/servo.cpp +++ b/include/servo.cpp @@ -10,16 +10,13 @@ std::atomic calibListen{false}; std::atomic baseDiff{0}; std::atomic target{0}; -Encoder* topEnc = nullptr; -Encoder* bottomEnc = nullptr; - std::atomic runningManual{false}; std::atomic runningServer{false}; std::atomic clearCalibFlag{false}; std::atomic savePosFlag{false}; std::atomic startLess{false}; -void servoInit(Encoder& bottom, Encoder& top) { +void servoInit() { // LEDC timer configuration (C++ aggregate initialization) ledc_timer_config_t ledc_timer = {}; ledc_timer.speed_mode = LEDC_LOW_SPEED_MODE; @@ -40,9 +37,6 @@ void servoInit(Encoder& bottom, Encoder& top) { ledc_channel.hpoint = 0; ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel)); - topEnc = ⊤ - bottomEnc = ⊥ - // Configure servo power switch pin as output gpio_set_direction(servoSwitch, GPIO_MODE_OUTPUT); gpio_set_level(servoSwitch, 0); // Start with servo power off @@ -203,12 +197,12 @@ void servoWandListen() { topEnc->wandListen = false; } else if (effDiff > 1) { - servoOn(CCW, manual); topEnc->wandListen = true; + servoOn(CCW, manual); } else if (effDiff < -1) { - servoOn(CW, manual); topEnc->wandListen = true; + servoOn(CW, manual); } else { servoOff(); diff --git a/include/servo.hpp b/include/servo.hpp index bb6ce0b..a738004 100644 --- a/include/servo.hpp +++ b/include/servo.hpp @@ -9,19 +9,14 @@ #define server 1 #define manual 0 -extern Encoder* topEnc; -extern Encoder* bottomEnc; extern std::atomic calibListen; -extern std::atomic runningManual; -extern std::atomic runningServer; extern std::atomic clearCalibFlag; extern std::atomic savePosFlag; -extern std::atomic startLess; -extern std::atomic baseDiff; -extern std::atomic target; +extern Encoder* topEnc; +extern Encoder* bottomEnc; -void servoInit(Encoder& bottom, Encoder& top); +void servoInit(); void servoOn(uint8_t dir, uint8_t manOrServer); void servoOff(); void servoMainSwitch(uint8_t onOff); @@ -37,7 +32,7 @@ void servoSavePos(); int32_t servoReadPos(); void stopServerRun(); void servoWandListen(); - +void servoServerListen(); void runToAppPos(uint8_t appPos); #endif \ No newline at end of file diff --git a/include/socketIO.cpp b/include/socketIO.cpp index 35c8ab6..3157fce 100644 --- a/include/socketIO.cpp +++ b/include/socketIO.cpp @@ -296,28 +296,28 @@ static void emitSocketEvent(const char* eventName, cJSON* data) { } // Function to emit 'calib_done' as expected by your server -void emitCalibDone(int port = 1) { +void emitCalibDone(int port) { cJSON *data = cJSON_CreateObject(); cJSON_AddNumberToObject(data, "port", port); emitSocketEvent("calib_done", data); } // Function to emit 'calib_stage1_ready' to notify server device is ready for tilt up -void emitCalibStage1Ready(int port = 1) { +void emitCalibStage1Ready(int port) { cJSON *data = cJSON_CreateObject(); cJSON_AddNumberToObject(data, "port", port); emitSocketEvent("calib_stage1_ready", data); } // Function to emit 'calib_stage2_ready' to notify server device is ready for tilt down -void emitCalibStage2Ready(int port = 1) { +void emitCalibStage2Ready(int port) { cJSON *data = cJSON_CreateObject(); cJSON_AddNumberToObject(data, "port", port); emitSocketEvent("calib_stage2_ready", data); } // Function to emit 'report_calib_status' to tell server device's actual calibration state -void emitCalibStatus(bool calibrated, int port = 1) { +void emitCalibStatus(bool calibrated, int port) { cJSON *data = cJSON_CreateObject(); cJSON_AddNumberToObject(data, "port", port); cJSON_AddBoolToObject(data, "calibrated", calibrated); @@ -325,7 +325,7 @@ void emitCalibStatus(bool calibrated, int port = 1) { } // Function to emit 'device_calib_error' to notify server of calibration failure -void emitCalibError(const char* errorMessage, int port = 1) { +void emitCalibError(const char* errorMessage, int port) { cJSON *data = cJSON_CreateObject(); cJSON_AddNumberToObject(data, "port", port); cJSON_AddStringToObject(data, "message", errorMessage); @@ -333,7 +333,7 @@ void emitCalibError(const char* errorMessage, int port = 1) { } // Function to emit 'pos_hit' to notify server of position change -void emitPosHit(int pos, int port = 1) { +void emitPosHit(int pos, int port) { cJSON *data = cJSON_CreateObject(); cJSON_AddNumberToObject(data, "port", port); cJSON_AddNumberToObject(data, "pos", pos); diff --git a/src/main.cpp b/src/main.cpp index 34593e9..a882b2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,8 +10,10 @@ #include "calibration.hpp" // Global encoder instances -Encoder topEnc(ENCODER_PIN_A, ENCODER_PIN_B); -Encoder bottomEnc(InputEnc_PIN_A, InputEnc_PIN_B); +Encoder* topEnc = new Encoder(ENCODER_PIN_A, ENCODER_PIN_B); +Encoder* bottomEnc = new Encoder(InputEnc_PIN_A, InputEnc_PIN_B); + +// Global encoder pointers (used by servo.cpp) // Global calibration instance Calibration calib; @@ -30,15 +32,15 @@ void mainApp() { calib.init(); // Initialize encoders - topEnc.init(); - bottomEnc.init(); - servoInit(bottomEnc, topEnc); + topEnc->init(); + bottomEnc->init(); + servoInit(); setupLoop(); statusResolved = false; - int32_t prevCount = topEnc.getCount(); + int32_t prevCount = topEnc->getCount(); // Main loop while (1) { @@ -63,7 +65,7 @@ void mainApp() { savePosFlag = false; // Send position update to server - uint8_t currentAppPos = calib.convertToAppPos(topEnc.getCount()); + uint8_t currentAppPos = calib.convertToAppPos(topEnc->getCount()); emitPosHit(currentAppPos); printf("Sent pos_hit: position %d\n", currentAppPos);