Task-Driven, powersaving trial written, must test.
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "calibration.hpp"
|
||||
#include "defines.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "socketIO.hpp"
|
||||
#include <limits.h>
|
||||
|
||||
// Static member definitions
|
||||
std::atomic<bool> Calibration::calibrated{false};
|
||||
std::atomic<int32_t> Calibration::UpTicks{0};
|
||||
std::atomic<int32_t> Calibration::DownTicks{0};
|
||||
TaskHandle_t calibTaskHandle = NULL;
|
||||
|
||||
void Calibration::init() {
|
||||
nvs_handle_t calibHandle;
|
||||
@@ -109,4 +119,46 @@ uint8_t Calibration::convertToAppPos(int32_t ticks) {
|
||||
// appPos between 0 and 10, convert to target encoder ticks.
|
||||
int8_t retVal = (ticks - DownTicks) * 10 / (UpTicks - DownTicks);
|
||||
return (retVal < 0) ? 0 : ((retVal > 10) ? 10 : retVal);
|
||||
}
|
||||
|
||||
bool calibrate() {
|
||||
calibTaskHandle = xTaskGetCurrentTaskHandle();
|
||||
printf("Connecting to Socket.IO server for calibration...\n");
|
||||
initSocketIO();
|
||||
|
||||
// Wait for device_init message from server with timeout
|
||||
int timeout_count = 0;
|
||||
const int MAX_TIMEOUT = 60; // seconds
|
||||
|
||||
uint32_t status;
|
||||
// Wait for notification with timeout
|
||||
if (xTaskNotifyWait(0, ULONG_MAX, &status, pdMS_TO_TICKS(MAX_TIMEOUT * 1000)) == pdTRUE) {
|
||||
// Notification received within timeout
|
||||
if (status) {
|
||||
printf("Connected successfully, awaiting destroy command\n");
|
||||
xTaskNotifyWait(0, ULONG_MAX, &status, portMAX_DELAY);
|
||||
calibTaskHandle = NULL;
|
||||
if (status == 2) { // calibration complete
|
||||
printf("Calibration process complete\n");
|
||||
stopSocketIO();
|
||||
return true;
|
||||
}
|
||||
else { // unexpected disconnect
|
||||
printf("Disconnected unexpectedly!\n");
|
||||
stopSocketIO();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
calibTaskHandle = NULL;
|
||||
printf("Connection failed! Returning to setup.\n");
|
||||
stopSocketIO();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// Timeout reached
|
||||
calibTaskHandle = NULL;
|
||||
printf("Timeout waiting for device_init - connection failed\n");
|
||||
stopSocketIO();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user