Files
Bionic_sphere/Bionic_Core/OTAClass/OTAClass.cpp
T
Misaki 4985fee7c2 1. 完整封装并拓展了SD卡文件管理类,支持基本文件管理功能
2. 简单封装了LVGL渲染类,已经封装好了gif渲染功能
3. 修复了硬件厂商提供的驱动的Bug
4. 初步定义了宠物基类的抽象信息
2025-09-09 03:40:24 +08:00

149 lines
4.3 KiB
C++

//
// Created by misaki on 2025/9/2.
//
#include "OTAClass.h"
#include "esp_log.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <memory>
#include <sstream>
#include <pthread.h>
#include <esp_pthread.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
using namespace std::chrono;
const auto sleep_time = seconds{
5
};
// 下面的这个函数可以放在任何线程中,自动打印出对应线程的信息
void print_thread_info(const char *extra = nullptr)
{
std::stringstream ss;
if (extra) {
ss << extra;
}
ss << "Core id: " << xPortGetCoreID()
<< ", prio: " << uxTaskPriorityGet(nullptr)
<< ", minimum free stack: " << uxTaskGetStackHighWaterMark(nullptr) << " bytes.";
ESP_LOGI(pcTaskGetName(nullptr), "%s", ss.str().c_str());
}
void thread_func_inherited()
{
while (true) {
print_thread_info("我是 普普通通的inherited 线程");
std::this_thread::sleep_for(sleep_time);
}
}
void thread_func_any_core()
{
while (true) {
print_thread_info("我是一个会跑在任意一个核的任务~");
std::this_thread::sleep_for(sleep_time);
}
}
esp_pthread_cfg_t create_config(const char *name, int core_id, int stack, int prio)
{
auto cfg = esp_pthread_get_default_config();
cfg.thread_name = name;
cfg.pin_to_core = core_id;
cfg.stack_size = stack;
cfg.prio = prio;
return cfg;
}
#include "ThreadManager.h"
#include "WifiConnectors.h"
#include <string>
#include "LVGLRender.h"
#include "SDFileManager.h"
void OTAClass::Init() {
ESP_LOGI("OTA", "Init");
ESP_LOGI("OTAClass::Init", "当前固件版本 1.0.1");
// 列出当前目录内容
std::string listing = SDFileManager::getInstance()->lsCommand(".", false, true);
ESP_LOGI("SD", "%s", listing.c_str());
LVGLRender::getInstance()->RenderGif("sequence01.gif");
// 1. 创建普通函数一个可以运行在任意核上的线程
ThreadConfig config1;
config1.name = "NormalThread";
// config1.core_id = 0; // 不指定运行在哪个核,使其自动选择
config1.stack_size = 3072;
config1.priority = 5; // 优先级
std::thread normal_thread = ThreadManager::createThread(config1, thread_func_any_core);
ThreadConfig config2;
config2.name = "Thread2";
config2.core_id = 1; // 指定运行在核1
config2.stack_size = 3072;
config2.priority = 5;
std::thread thread2 = ThreadManager::createThread(config2, thread_func_inherited);
// 配置Wifi连接线程参数
ThreadConfig wifi_config;
wifi_config.name = "WifiConnector"; // 线程名称
wifi_config.core_id = 1; // 绑定到核心1(避免与主线程冲突)
wifi_config.stack_size = 4096; // 设置稍大的栈空间(Wifi连接可能需要)
wifi_config.priority = 6; // 设置较高优先级(确保连接及时)
// 使用单例方式创建线程,调用connectWifi成员函数
std::thread wifi_thread = ThreadManager::createSingletonThread<WifiConnectors>(
wifi_config,
&WifiConnectors::connectWifi,
"Misaki-2.4G", // SSID
"88888888", // 密码
5 // 最大重试次数
);
ThreadConfig ota_config;
ota_config.name = "OTA";
ota_config.stack_size = 4096;
ota_config.priority = 6;
ota_config.core_id = 0;
std::thread ota_thread = ThreadManager::createMemberThread<OTAClass>(
ota_config,
this,
&OTAClass::Update
);
while (true) {
std::stringstream ss;
ss << "core id: " << xPortGetCoreID()
<< ", prio: " << uxTaskPriorityGet(nullptr)
<< ", minimum free stack: " << uxTaskGetStackHighWaterMark(nullptr) << " bytes.";
ESP_LOGI(pcTaskGetName(nullptr), "%s", ss.str().c_str());
std::this_thread::sleep_for(sleep_time);
}
}
#include "ota_ws.h"
// 启动OTA更新线程,前提是已经连接WiFi
void OTAClass::Update() {
// 测试OTA服务器192.168.1.11
while (true) {
if (WifiConnectors::getInstance()->isWifiConnect()) { // 如果Wifi已连接
ota_ws_start("192.168.1.11", 8080);
// 启动完就退出,删除自身
break;
}else {
std::this_thread::sleep_for(sleep_time);
}
}
vTaskDelete(nullptr);
}