Files
Blinds_XIAO/src/main.cpp

88 lines
2.3 KiB
C++
Raw Normal View History

2025-11-28 18:26:32 -06:00
#include <driver/gptimer.h>
#include "servo.hpp"
2025-11-28 18:26:32 -06:00
#include "defines.h"
#include "nvs_flash.h"
#include "NimBLEDevice.h"
#include "WiFi.hpp"
#include "setup.hpp"
2025-12-23 17:21:44 -06:00
#include "socketIO.hpp"
#include "encoder.hpp"
#include "calibration.hpp"
2026-01-10 02:45:26 -06:00
#include "esp_pm.h"
// Global encoder instances
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;
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();
// Initialize encoders
topEnc->init();
bottomEnc->init();
servoInit();
2026-01-10 02:45:26 -06:00
xTaskCreate(setupLoop, "Setup", 8192, NULL, 5, &setupTaskHandle);
2025-12-28 15:31:20 -06:00
2026-01-10 02:45:26 -06:00
// TOMORROW!!!
// statusResolved = false;
2025-12-23 17:21:44 -06:00
2026-01-10 02:45:26 -06:00
// // Main loop
// while (1) {
// // websocket disconnect/reconnect handling
// if (statusResolved) {
// if (!connected) {
// printf("Disconnected! Beginning setup loop.\n");
// stopSocketIO();
// setupLoop();
// }
// else printf("Reconnected!\n");
// statusResolved = false;
// }
2026-01-10 02:45:26 -06:00
// if (clearCalibFlag) {
// calib.clearCalibrated();
// emitCalibStatus(false);
// clearCalibFlag = false;
// }
// if (savePosFlag) {
// servoSavePos();
// savePosFlag = false;
2026-01-10 02:45:26 -06:00
// // Send position update to server
// uint8_t currentAppPos = calib.convertToAppPos(topEnc->getCount());
// emitPosHit(currentAppPos);
2026-01-10 02:45:26 -06:00
// printf("Sent pos_hit: position %d\n", currentAppPos);
// }
// vTaskDelay(pdMS_TO_TICKS(100));
// }
}
2026-01-10 02:45:26 -06:00
void pm_init() {
esp_pm_config_t pm_config = {
.max_freq_mhz = 160, // Max CPU frequency
.min_freq_mhz = 80, // Min CPU frequency (DFS)
.light_sleep_enable = true // ALLOW CPU to power down during idle
};
ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
}
extern "C" void app_main() {
2026-01-10 02:45:26 -06:00
pm_init();
mainApp();
2025-11-28 18:26:32 -06:00
}