1. 完成了对音频播放类的完整C++封装,测试通过
2. 修复了LVGL渲染类当中的一些小bug 3. 增加了一些CPU资源占用的日志打印函数,运行在主线程当中 4. 完善了底层通信类的封装,基于websocket,尚未测试
This commit is contained in:
@@ -197,4 +197,113 @@ extern "C" void app_main(void)
|
||||
std::this_thread::sleep_for(sleep_time);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
```c++
|
||||
#include "WebSocketManager.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
// JSON数据回调示例
|
||||
void onJsonData(cJSON* json) {
|
||||
// 处理接收到的JSON数据
|
||||
cJSON* type = cJSON_GetObjectItem(json, "type");
|
||||
if (type && cJSON_IsString(type)) {
|
||||
ESP_LOGI("App", "Received message type: %s", type->valuestring);
|
||||
|
||||
if (strcmp(type->valuestring, "sensor_data") == 0) {
|
||||
cJSON* value = cJSON_GetObjectItem(json, "value");
|
||||
if (value && cJSON_IsNumber(value)) {
|
||||
ESP_LOGI("App", "Sensor value: %.2f", value->valuedouble);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
// 事件回调示例
|
||||
void onWebSocketEvent(WebSocketEvent event, const std::string& message) {
|
||||
switch (event) {
|
||||
case WebSocketEvent::CONNECTED:
|
||||
ESP_LOGI("App", "WebSocket connected: %s", message.c_str());
|
||||
break;
|
||||
case WebSocketEvent::DISCONNECTED:
|
||||
ESP_LOGI("App", "WebSocket disconnected: %s", message.c_str());
|
||||
break;
|
||||
case WebSocketEvent::DATA_RECEIVED:
|
||||
ESP_LOGI("App", "Received raw data: %s", message.c_str());
|
||||
break;
|
||||
case WebSocketEvent::ERROR:
|
||||
ESP_LOGE("App", "WebSocket error: %s", message.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void app_main() {
|
||||
// 初始化WiFi
|
||||
WifiConnectors* wifi = WifiConnectors::getInstance();
|
||||
|
||||
// 连接WiFi(在实际应用中应该在单独线程中进行)
|
||||
ThreadConfig wifi_config;
|
||||
wifi_config.name = "WiFi_Connector";
|
||||
|
||||
auto wifi_thread = ThreadManager::createSingletonThread<WifiConnectors>(
|
||||
wifi_config, &WifiConnectors::connectWifi, "Your_SSID", "Your_Password");
|
||||
|
||||
wifi_thread.join();
|
||||
|
||||
// 获取WebSocket管理器实例
|
||||
WebSocketManager* ws = WebSocketManager::getInstance();
|
||||
|
||||
// 配置WebSocket
|
||||
WebSocketConfig config;
|
||||
config.uri = "ws://your-websocket-server.com/ws";
|
||||
config.auto_reconnect = true;
|
||||
config.reconnect_interval = 5000;
|
||||
config.heartbeat_interval = 30000;
|
||||
config.max_reconnect_attempts = 10;
|
||||
|
||||
// 初始化WebSocket管理器
|
||||
if (!ws->initialize(config)) {
|
||||
ESP_LOGE("App", "Failed to initialize WebSocket manager");
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置回调
|
||||
ws->setJsonCallback(onJsonData);
|
||||
ws->setEventCallback(onWebSocketEvent);
|
||||
|
||||
// 连接WebSocket
|
||||
if (!ws->connect()) {
|
||||
ESP_LOGE("App", "Failed to connect WebSocket");
|
||||
return;
|
||||
}
|
||||
|
||||
// 等待连接建立
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
|
||||
// 发送JSON数据
|
||||
cJSON* json = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(json, "type", "greeting");
|
||||
cJSON_AddStringToObject(json, "message", "Hello from ESP32!");
|
||||
cJSON_AddNumberToObject(json, "timestamp", esp_log_timestamp());
|
||||
|
||||
ws->sendJson(json);
|
||||
|
||||
// 保持运行
|
||||
while (true) {
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
|
||||
// 可以在这里发送其他数据或处理业务逻辑
|
||||
if (ws->isConnected()) {
|
||||
cJSON* status = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(status, "type", "status");
|
||||
cJSON_AddNumberToObject(status, "free_heap", esp_get_free_heap_size());
|
||||
|
||||
ws->sendJson(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user