Skip to content

Commit bb332f0

Browse files
smalllin0mac8005
authored andcommitted
使用原始字符串字面量代替转义,提高可读性 (78#861)
* 使用原始字符串字面量代替转义,提高可读性 * 使用原始字符串字面量代替转义,提高可读性 * 增加一个使用ESP-IDF Monitor作为输出显示内容的类 * 修改代码风格
1 parent 7ecce71 commit bb332f0

File tree

4 files changed

+112
-43
lines changed

4 files changed

+112
-43
lines changed

main/boards/common/board.cc

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -106,62 +106,57 @@ std::string Board::GetJson() {
106106
}
107107
}
108108
*/
109-
std::string json = "{";
110-
json += "\"version\":2,";
111-
json += "\"language\":\"" + std::string(Lang::CODE) + "\",";
112-
json += "\"flash_size\":" + std::to_string(SystemInfo::GetFlashSize()) + ",";
113-
json += "\"minimum_free_heap_size\":" + std::to_string(SystemInfo::GetMinimumFreeHeapSize()) + ",";
114-
json += "\"mac_address\":\"" + SystemInfo::GetMacAddress() + "\",";
115-
json += "\"uuid\":\"" + uuid_ + "\",";
116-
json += "\"chip_model_name\":\"" + SystemInfo::GetChipModelName() + "\",";
117-
json += "\"chip_info\":{";
109+
std::string json = R"({"version":2,"language":")" + std::string(Lang::CODE) + R"(",)";
110+
json += R"("flash_size":)" + std::to_string(SystemInfo::GetFlashSize()) + R"(,)";
111+
json += R"("minimum_free_heap_size":")" + std::to_string(SystemInfo::GetMinimumFreeHeapSize()) + R"(",)";
112+
json += R"("mac_address":")" + SystemInfo::GetMacAddress() + R"(",)";
113+
json += R"("uuid":")" + uuid_ + R"(",)";
114+
json += R"("chip_model_name":")" + SystemInfo::GetChipModelName() + R"(",)";
118115

119116
esp_chip_info_t chip_info;
120117
esp_chip_info(&chip_info);
121-
json += "\"model\":" + std::to_string(chip_info.model) + ",";
122-
json += "\"cores\":" + std::to_string(chip_info.cores) + ",";
123-
json += "\"revision\":" + std::to_string(chip_info.revision) + ",";
124-
json += "\"features\":" + std::to_string(chip_info.features);
125-
json += "},";
118+
json += R"("chip_info":{)";
119+
json += R"("model":)" + std::to_string(chip_info.model) + R"(,)";
120+
json += R"("cores":)" + std::to_string(chip_info.cores) + R"(,)";
121+
json += R"("revision":)" + std::to_string(chip_info.revision) + R"(,)";
122+
json += R"("features":)" + std::to_string(chip_info.features) + R"(},)";
126123

127-
json += "\"application\":{";
128124
auto app_desc = esp_app_get_description();
129-
json += "\"name\":\"" + std::string(app_desc->project_name) + "\",";
130-
json += "\"version\":\"" + std::string(app_desc->version) + "\",";
131-
json += "\"compile_time\":\"" + std::string(app_desc->date) + "T" + std::string(app_desc->time) + "Z\",";
132-
json += "\"idf_version\":\"" + std::string(app_desc->idf_ver) + "\",";
133-
125+
json += R"("application":{)";
126+
json += R"("name":")" + std::string(app_desc->project_name) + R"(",)";
127+
json += R"("version":")" + std::string(app_desc->version) + R"(",)";
128+
json += R"("compile_time":")" + std::string(app_desc->date) + R"(T)" + std::string(app_desc->time) + R"(Z",)";
129+
json += R"("idf_version":")" + std::string(app_desc->idf_ver) + R"(",)";
134130
char sha256_str[65];
135131
for (int i = 0; i < 32; i++) {
136132
snprintf(sha256_str + i * 2, sizeof(sha256_str) - i * 2, "%02x", app_desc->app_elf_sha256[i]);
137133
}
138-
json += "\"elf_sha256\":\"" + std::string(sha256_str) + "\"";
139-
json += "},";
134+
json += R"("elf_sha256":")" + std::string(sha256_str) + R"(")";
135+
json += R"(},)";
140136

141-
json += "\"partition_table\": [";
137+
json += R"("partition_table": [)";
142138
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
143139
while (it) {
144140
const esp_partition_t *partition = esp_partition_get(it);
145-
json += "{";
146-
json += "\"label\":\"" + std::string(partition->label) + "\",";
147-
json += "\"type\":" + std::to_string(partition->type) + ",";
148-
json += "\"subtype\":" + std::to_string(partition->subtype) + ",";
149-
json += "\"address\":" + std::to_string(partition->address) + ",";
150-
json += "\"size\":" + std::to_string(partition->size);
151-
json += "},";
141+
json += R"({)";
142+
json += R"("label":")" + std::string(partition->label) + R"(",)";
143+
json += R"("type":)" + std::to_string(partition->type) + R"(,)";
144+
json += R"("subtype":)" + std::to_string(partition->subtype) + R"(,)";
145+
json += R"("address":)" + std::to_string(partition->address) + R"(,)";
146+
json += R"("size":)" + std::to_string(partition->size) + R"(},)";;
152147
it = esp_partition_next(it);
153148
}
154149
json.pop_back(); // Remove the last comma
155-
json += "],";
150+
json += R"(],)";
156151

157-
json += "\"ota\":{";
152+
json += R"("ota":{)";
158153
auto ota_partition = esp_ota_get_running_partition();
159-
json += "\"label\":\"" + std::string(ota_partition->label) + "\"";
160-
json += "},";
154+
json += R"("label":")" + std::string(ota_partition->label) + R"(")";
155+
json += R"(},)";
161156

162-
json += "\"board\":" + GetBoardJson();
157+
json += R"("board":)" + GetBoardJson();
163158

164159
// Close the JSON object
165-
json += "}";
160+
json += R"(})";
166161
return json;
167162
}

main/boards/common/wifi_board.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,17 @@ const char* WifiBoard::GetNetworkStateIcon() {
158158
std::string WifiBoard::GetBoardJson() {
159159
// Set the board type for OTA
160160
auto& wifi_station = WifiStation::GetInstance();
161-
std::string board_json = std::string("{\"type\":\"" BOARD_TYPE "\",");
162-
board_json += "\"name\":\"" BOARD_NAME "\",";
161+
std::string board_json = R"({)";
162+
board_json += R"("type":")" + std::string(BOARD_TYPE) + R"(",)";
163+
board_json += R"("name":")" + std::string(BOARD_NAME) + R"(",)";
163164
if (!wifi_config_mode_) {
164-
board_json += "\"ssid\":\"" + wifi_station.GetSsid() + "\",";
165-
board_json += "\"rssi\":" + std::to_string(wifi_station.GetRssi()) + ",";
166-
board_json += "\"channel\":" + std::to_string(wifi_station.GetChannel()) + ",";
167-
board_json += "\"ip\":\"" + wifi_station.GetIpAddress() + "\",";
165+
board_json += R"("ssid":")" + wifi_station.GetSsid() + R"(",)";
166+
board_json += R"("rssi":)" + std::to_string(wifi_station.GetRssi()) + R"(,)";
167+
board_json += R"("channel":)" + std::to_string(wifi_station.GetChannel()) + R"(,)";
168+
board_json += R"("ip":")" + wifi_station.GetIpAddress() + R"(",)";
168169
}
169-
board_json += "\"mac\":\"" + SystemInfo::GetMacAddress() + "\"}";
170+
board_json += R"("mac":")" + SystemInfo::GetMacAddress() + R"(")";
171+
board_json += R"(})";
170172
return board_json;
171173
}
172174

main/display/esplog_display.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "esplog_display.h"
2+
3+
#include "esp_log.h"
4+
5+
#define TAG "EspLogDisplay"
6+
7+
8+
EspLogDisplay::EspLogDisplay()
9+
{}
10+
11+
EspLogDisplay::~EspLogDisplay()
12+
{}
13+
14+
void EspLogDisplay::SetStatus(const char* status)
15+
{
16+
ESP_LOGW(TAG, "SetStatus: %s", status);
17+
}
18+
19+
void EspLogDisplay::ShowNotification(const char* notification, int duration_ms)
20+
{
21+
ESP_LOGW(TAG, "ShowNotification: %s", notification);
22+
}
23+
void EspLogDisplay::ShowNotification(const std::string &notification, int duration_ms)
24+
{
25+
ShowNotification(notification.c_str(), duration_ms);
26+
}
27+
28+
29+
void EspLogDisplay::SetEmotion(const char* emotion)
30+
{
31+
ESP_LOGW(TAG, "SetEmotion: %s", emotion);
32+
}
33+
34+
void EspLogDisplay::SetIcon(const char* icon)
35+
{
36+
ESP_LOGW(TAG, "SetIcon: %s", icon);
37+
}
38+
39+
void EspLogDisplay::SetChatMessage(const char* role, const char* content)
40+
{
41+
ESP_LOGW(TAG, "Role:%s", role);
42+
ESP_LOGW(TAG, " %s", content);
43+
}
44+

main/display/esplog_display.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef ESPLOG_DISPLAY_H_
2+
#define ESPLOG_DISPLAY_H_
3+
4+
#include "display.h"
5+
6+
#include <string>
7+
8+
class EspLogDisplay : public Display {
9+
public:
10+
EspLogDisplay();
11+
~EspLogDisplay();
12+
13+
virtual void SetStatus(const char* status);
14+
virtual void ShowNotification(const char* notification, int duration_ms = 3000);
15+
virtual void ShowNotification(const std::string &notification, int duration_ms = 3000);
16+
virtual void SetEmotion(const char* emotion) override;
17+
virtual void SetChatMessage(const char* role, const char* content) override;
18+
virtual void SetIcon(const char* icon) override;
19+
virtual inline void SetPreviewImage(const lv_img_dsc_t* image) override {}
20+
virtual inline void SetTheme(const std::string& theme_name) override {}
21+
virtual inline void UpdateStatusBAR(bool update_all = false) override {}
22+
23+
protected:
24+
virtual inline bool Lock(int timeout_ms = 0) override { return true; }
25+
virtual inline void Unlock() override {}
26+
};
27+
28+
#endif

0 commit comments

Comments
 (0)