Skip to content

Commit d505b3b

Browse files
ghbhahaguhaibo78
authored
【需求】双网络类型通过长按boot切换网络 (#520)
* 【需求】双网络类型通过长按boot切换网络 * Update Kconfig.projbuild --------- Co-authored-by: guhaibo <[email protected]> Co-authored-by: Xiaoxia <[email protected]>
1 parent 8e638e3 commit d505b3b

File tree

10 files changed

+457
-4
lines changed

10 files changed

+457
-4
lines changed

main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ elseif(CONFIG_BOARD_TYPE_XINGZHI_Cube_1_54TFT_WIFI)
145145
set(BOARD_TYPE "xingzhi-cube-1.54tft-wifi")
146146
elseif(CONFIG_BOARD_TYPE_XINGZHI_Cube_1_54TFT_ML307)
147147
set(BOARD_TYPE "xingzhi-cube-1.54tft-ml307")
148+
elseif(CONFIG_BOARD_TYPE_XINGZHI_Cube_1_54TFT_DUAL)
149+
set(BOARD_TYPE "xingzhi-cube-1.54tft-dual")
148150
elseif(CONFIG_BOARD_TYPE_SENSECAP_WATCHER)
149151
set(BOARD_TYPE "sensecap-watcher")
150152
elseif(CONFIG_BOARD_TYPE_DOIT_S3_AIBOX)

main/Kconfig.projbuild

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ choice BOARD_TYPE
140140
bool "无名科技星智1.54(WIFI)"
141141
config BOARD_TYPE_XINGZHI_Cube_1_54TFT_ML307
142142
bool "无名科技星智1.54(ML307)"
143+
config BOARD_TYPE_XINGZHI_Cube_1_54TFT_DUAL
144+
bool "无名科技星智1.54(DUAL)"
143145
config BOARD_TYPE_SENSECAP_WATCHER
144146
bool "SenseCAP Watcher"
145147
config BOARD_TYPE_DOIT_S3_AIBOX

main/boards/common/board.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Board {
1717
private:
1818
Board(const Board&) = delete; // 禁用拷贝构造函数
1919
Board& operator=(const Board&) = delete; // 禁用赋值操作
20-
virtual std::string GetBoardJson() = 0;
2120

2221
protected:
2322
Board();
@@ -48,6 +47,7 @@ class Board {
4847
virtual bool GetBatteryLevel(int &level, bool& charging, bool& discharging);
4948
virtual std::string GetJson();
5049
virtual void SetPowerSaveMode(bool enabled) = 0;
50+
virtual std::string GetBoardJson() = 0;
5151
};
5252

5353
#define DECLARE_BOARD(BOARD_CLASS_NAME) \
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "dual_network_board.h"
2+
#include "application.h"
3+
#include "display.h"
4+
#include "assets/lang_config.h"
5+
#include "settings.h"
6+
#include <esp_log.h>
7+
8+
static const char *TAG = "DualNetworkBoard";
9+
10+
DualNetworkBoard::DualNetworkBoard(gpio_num_t ml307_tx_pin, gpio_num_t ml307_rx_pin, size_t ml307_rx_buffer_size)
11+
: Board(),
12+
ml307_tx_pin_(ml307_tx_pin),
13+
ml307_rx_pin_(ml307_rx_pin),
14+
ml307_rx_buffer_size_(ml307_rx_buffer_size) {
15+
16+
// 从Settings加载网络类型
17+
network_type_ = LoadNetworkTypeFromSettings();
18+
19+
// 只初始化当前网络类型对应的板卡
20+
InitializeCurrentBoard();
21+
}
22+
23+
NetworkType DualNetworkBoard::LoadNetworkTypeFromSettings() {
24+
Settings settings("network", true);
25+
int network_type = settings.GetInt("type", 1); // 默认使用ML307 (1)
26+
27+
ESP_LOGI(TAG, "从Settings加载网络类型: %d", network_type);
28+
29+
return network_type == 1 ? NetworkType::ML307 : NetworkType::WIFI;
30+
}
31+
32+
void DualNetworkBoard::SaveNetworkTypeToSettings(NetworkType type) {
33+
Settings settings("network", true);
34+
int network_type = (type == NetworkType::ML307) ? 1 : 0;
35+
36+
ESP_LOGI(TAG, "保存网络类型到Settings: %d", network_type);
37+
38+
settings.SetInt("type", network_type);
39+
}
40+
41+
void DualNetworkBoard::InitializeCurrentBoard() {
42+
if (network_type_ == NetworkType::ML307) {
43+
ESP_LOGI(TAG, "初始化ML307板卡");
44+
current_board_ = std::make_unique<Ml307Board>(ml307_tx_pin_, ml307_rx_pin_, ml307_rx_buffer_size_);
45+
} else {
46+
ESP_LOGI(TAG, "初始化WiFi板卡");
47+
current_board_ = std::make_unique<WifiBoard>();
48+
}
49+
}
50+
51+
void DualNetworkBoard::SwitchNetType() {
52+
if (network_type_ == NetworkType::WIFI) {
53+
ESP_LOGI(TAG, "切换到ML307模式");
54+
SaveNetworkTypeToSettings(NetworkType::ML307);
55+
} else {
56+
ESP_LOGI(TAG, "切换到WiFi模式");
57+
SaveNetworkTypeToSettings(NetworkType::WIFI);
58+
}
59+
}
60+
61+
62+
std::string DualNetworkBoard::GetBoardType() {
63+
return current_board_->GetBoardType();
64+
}
65+
66+
void DualNetworkBoard::StartNetwork() {
67+
auto display = Board::GetInstance().GetDisplay();
68+
69+
if (network_type_ == NetworkType::WIFI) {
70+
display->SetStatus(Lang::Strings::CONNECTING);
71+
} else {
72+
display->SetStatus(Lang::Strings::DETECTING_MODULE);
73+
}
74+
current_board_->StartNetwork();
75+
}
76+
77+
Http* DualNetworkBoard::CreateHttp() {
78+
return current_board_->CreateHttp();
79+
}
80+
81+
WebSocket* DualNetworkBoard::CreateWebSocket() {
82+
return current_board_->CreateWebSocket();
83+
}
84+
85+
Mqtt* DualNetworkBoard::CreateMqtt() {
86+
return current_board_->CreateMqtt();
87+
}
88+
89+
Udp* DualNetworkBoard::CreateUdp() {
90+
return current_board_->CreateUdp();
91+
}
92+
93+
const char* DualNetworkBoard::GetNetworkStateIcon() {
94+
return current_board_->GetNetworkStateIcon();
95+
}
96+
97+
void DualNetworkBoard::SetPowerSaveMode(bool enabled) {
98+
current_board_->SetPowerSaveMode(enabled);
99+
}
100+
101+
std::string DualNetworkBoard::GetBoardJson() {
102+
return current_board_->GetBoardJson();
103+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#ifndef DUAL_NETWORK_BOARD_H
2+
#define DUAL_NETWORK_BOARD_H
3+
4+
#include "board.h"
5+
#include "wifi_board.h"
6+
#include "ml307_board.h"
7+
#include <memory>
8+
9+
//enum NetworkType
10+
enum class NetworkType {
11+
WIFI,
12+
ML307
13+
};
14+
15+
// 双网络板卡类,可以在WiFi和ML307之间切换
16+
class DualNetworkBoard : public Board {
17+
private:
18+
// 使用基类指针存储当前活动的板卡
19+
std::unique_ptr<Board> current_board_;
20+
NetworkType network_type_ = NetworkType::ML307; // Default to ML307
21+
22+
// ML307的引脚配置
23+
gpio_num_t ml307_tx_pin_;
24+
gpio_num_t ml307_rx_pin_;
25+
size_t ml307_rx_buffer_size_;
26+
27+
// 从Settings加载网络类型
28+
NetworkType LoadNetworkTypeFromSettings();
29+
30+
// 保存网络类型到Settings
31+
void SaveNetworkTypeToSettings(NetworkType type);
32+
33+
// 初始化当前网络类型对应的板卡
34+
void InitializeCurrentBoard();
35+
36+
public:
37+
DualNetworkBoard(gpio_num_t ml307_tx_pin, gpio_num_t ml307_rx_pin, size_t ml307_rx_buffer_size = 4096);
38+
virtual ~DualNetworkBoard() = default;
39+
40+
// 切换网络类型
41+
void SwitchNetType();
42+
43+
// 获取当前网络类型
44+
NetworkType GetNetworkType() const { return network_type_; }
45+
46+
// 重写Board接口
47+
virtual std::string GetBoardType() override;
48+
virtual void StartNetwork() override;
49+
virtual Http* CreateHttp() override;
50+
virtual WebSocket* CreateWebSocket() override;
51+
virtual Mqtt* CreateMqtt() override;
52+
virtual Udp* CreateUdp() override;
53+
virtual const char* GetNetworkStateIcon() override;
54+
virtual void SetPowerSaveMode(bool enabled) override;
55+
virtual std::string GetBoardJson() override;
56+
57+
};
58+
59+
#endif // DUAL_NETWORK_BOARD_H

main/boards/common/ml307_board.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
class Ml307Board : public Board {
88
protected:
99
Ml307AtModem modem_;
10-
1110
virtual std::string GetBoardJson() override;
1211
void WaitForNetworkReady();
1312

@@ -21,6 +20,7 @@ class Ml307Board : public Board {
2120
virtual Udp* CreateUdp() override;
2221
virtual const char* GetNetworkStateIcon() override;
2322
virtual void SetPowerSaveMode(bool enabled) override;
23+
virtual AudioCodec* GetAudioCodec() override { return nullptr; }
2424
};
2525

2626
#endif // ML307_BOARD_H

main/boards/common/wifi_board.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
class WifiBoard : public Board {
77
protected:
88
bool wifi_config_mode_ = false;
9-
10-
WifiBoard();
119
void EnterWifiConfigMode();
1210
virtual std::string GetBoardJson() override;
1311

1412
public:
13+
WifiBoard();
1514
virtual std::string GetBoardType() override;
1615
virtual void StartNetwork() override;
1716
virtual Http* CreateHttp() override;
@@ -21,6 +20,7 @@ class WifiBoard : public Board {
2120
virtual const char* GetNetworkStateIcon() override;
2221
virtual void SetPowerSaveMode(bool enabled) override;
2322
virtual void ResetWifiConfiguration();
23+
virtual AudioCodec* GetAudioCodec() override { return nullptr; }
2424
};
2525

2626
#endif // WIFI_BOARD_H
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
#ifndef _BOARD_CONFIG_H_
3+
#define _BOARD_CONFIG_H_
4+
5+
#include <driver/gpio.h>
6+
7+
#define AUDIO_INPUT_SAMPLE_RATE 16000
8+
#define AUDIO_OUTPUT_SAMPLE_RATE 24000
9+
10+
#define AUDIO_I2S_MIC_GPIO_WS GPIO_NUM_4
11+
#define AUDIO_I2S_MIC_GPIO_SCK GPIO_NUM_5
12+
#define AUDIO_I2S_MIC_GPIO_DIN GPIO_NUM_6
13+
#define AUDIO_I2S_SPK_GPIO_DOUT GPIO_NUM_7
14+
#define AUDIO_I2S_SPK_GPIO_BCLK GPIO_NUM_15
15+
#define AUDIO_I2S_SPK_GPIO_LRCK GPIO_NUM_16
16+
17+
#define BOOT_BUTTON_GPIO GPIO_NUM_0
18+
#define VOLUME_UP_BUTTON_GPIO GPIO_NUM_40
19+
#define VOLUME_DOWN_BUTTON_GPIO GPIO_NUM_39
20+
21+
#define DISPLAY_SDA GPIO_NUM_10
22+
#define DISPLAY_SCL GPIO_NUM_9
23+
#define DISPLAY_DC GPIO_NUM_8
24+
#define DISPLAY_CS GPIO_NUM_14
25+
#define DISPLAY_RES GPIO_NUM_18
26+
#define DISPLAY_WIDTH 240
27+
#define DISPLAY_HEIGHT 240
28+
#define DISPLAY_SWAP_XY false
29+
#define DISPLAY_MIRROR_X false
30+
#define DISPLAY_MIRROR_Y false
31+
#define BACKLIGHT_INVERT false
32+
#define DISPLAY_OFFSET_X 0
33+
#define DISPLAY_OFFSET_Y 0
34+
#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_13
35+
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false
36+
37+
#define ML307_RX_PIN GPIO_NUM_11
38+
#define ML307_TX_PIN GPIO_NUM_12
39+
40+
#endif // _BOARD_CONFIG_H_
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"target": "esp32s3",
3+
"builds": [
4+
{
5+
"name": "xingzhi-cube-1.54tft-dual",
6+
"sdkconfig_append": []
7+
},
8+
{
9+
"name": "xingzhi-cube-1.54tft-dual-wechatui",
10+
"sdkconfig_append": [
11+
"CONFIG_USE_WECHAT_MESSAGE_STYLE=y"
12+
]
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)