1. 完成了ota功能的基本测试,测试通过
2. 封装了一个模板线程类,支持创建来自单例类的成员函数线程,普通类的线程,普通函数线程 3. 封装了一个Wifi模块类,支持Wifi的各种基本配置
This commit is contained in:
@@ -4,6 +4,141 @@
|
||||
#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>
|
||||
void OTAClass::Init() {
|
||||
ESP_LOGI("OTA", "Init");
|
||||
|
||||
ESP_LOGI("OTAClass::Init", "当前固件版本 1.0.1");
|
||||
|
||||
// 测试Wifi
|
||||
// WifiConnectors::getInstance()->log();
|
||||
//
|
||||
// WifiConnectors::getInstance()->connectWifi("Misaki-2.4G", "88888888");
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,18 +4,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
class OTAClass {
|
||||
public:
|
||||
static void Init(void);
|
||||
void Init(void);
|
||||
void Update(void);
|
||||
};
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user