增加4G扩展 (#653)

在不影响wifi原版功能的基础上,方便想增加4G功能的朋友通过内置的fpc座子连接4G模块;
默认开机是wifi模式,在联网成功前,双击主按钮,可以进行WIFI和4G网络切换;
若在wifi模式,在成功连接前,可单击主按钮可以进行重新配网;
This commit is contained in:
MakerM0
2025-05-21 22:19:25 +08:00
committed by GitHub
parent 0183830915
commit 9874a1b009
4 changed files with 59 additions and 17 deletions

View File

@@ -7,22 +7,22 @@
static const char *TAG = "DualNetworkBoard"; static const char *TAG = "DualNetworkBoard";
DualNetworkBoard::DualNetworkBoard(gpio_num_t ml307_tx_pin, gpio_num_t ml307_rx_pin, size_t ml307_rx_buffer_size) DualNetworkBoard::DualNetworkBoard(gpio_num_t ml307_tx_pin, gpio_num_t ml307_rx_pin, size_t ml307_rx_buffer_size, int32_t default_net_type)
: Board(), : Board(),
ml307_tx_pin_(ml307_tx_pin), ml307_tx_pin_(ml307_tx_pin),
ml307_rx_pin_(ml307_rx_pin), ml307_rx_pin_(ml307_rx_pin),
ml307_rx_buffer_size_(ml307_rx_buffer_size) { ml307_rx_buffer_size_(ml307_rx_buffer_size) {
// 从Settings加载网络类型 // 从Settings加载网络类型
network_type_ = LoadNetworkTypeFromSettings(); network_type_ = LoadNetworkTypeFromSettings(default_net_type);
// 只初始化当前网络类型对应的板卡 // 只初始化当前网络类型对应的板卡
InitializeCurrentBoard(); InitializeCurrentBoard();
} }
NetworkType DualNetworkBoard::LoadNetworkTypeFromSettings() { NetworkType DualNetworkBoard::LoadNetworkTypeFromSettings(int32_t default_net_type) {
Settings settings("network", true); Settings settings("network", true);
int network_type = settings.GetInt("type", 1); // 默认使用ML307 (1) int network_type = settings.GetInt("type", default_net_type); // 默认使用ML307 (1)
return network_type == 1 ? NetworkType::ML307 : NetworkType::WIFI; return network_type == 1 ? NetworkType::ML307 : NetworkType::WIFI;
} }

View File

@@ -25,7 +25,7 @@ private:
size_t ml307_rx_buffer_size_; size_t ml307_rx_buffer_size_;
// 从Settings加载网络类型 // 从Settings加载网络类型
NetworkType LoadNetworkTypeFromSettings(); NetworkType LoadNetworkTypeFromSettings(int32_t default_net_type);
// 保存网络类型到Settings // 保存网络类型到Settings
void SaveNetworkTypeToSettings(NetworkType type); void SaveNetworkTypeToSettings(NetworkType type);
@@ -34,7 +34,7 @@ private:
void InitializeCurrentBoard(); void InitializeCurrentBoard();
public: public:
DualNetworkBoard(gpio_num_t ml307_tx_pin, gpio_num_t ml307_rx_pin, size_t ml307_rx_buffer_size = 4096); DualNetworkBoard(gpio_num_t ml307_tx_pin, gpio_num_t ml307_rx_pin, size_t ml307_rx_buffer_size = 4096, int32_t default_net_type = 1);
virtual ~DualNetworkBoard() = default; virtual ~DualNetworkBoard() = default;
// 切换网络类型 // 切换网络类型

View File

@@ -47,4 +47,9 @@
#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_13 #define DISPLAY_BACKLIGHT_PIN GPIO_NUM_13
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT true #define DISPLAY_BACKLIGHT_OUTPUT_INVERT true
#define ML307_RX_PIN GPIO_NUM_42
#define ML307_TX_PIN GPIO_NUM_44
#define ML307_POWER_PIN GPIO_NUM_40
#define ML307_POWER_OUTPUT_INVERT false
#endif // _BOARD_CONFIG_H_ #endif // _BOARD_CONFIG_H_

View File

@@ -1,4 +1,4 @@
#include "wifi_board.h" #include "dual_network_board.h"
#include "display/lcd_display.h" #include "display/lcd_display.h"
#include "audio_codecs/es8311_audio_codec.h" #include "audio_codecs/es8311_audio_codec.h"
#include "application.h" #include "application.h"
@@ -20,6 +20,7 @@
#include "power_manager.h" #include "power_manager.h"
#include "power_save_timer.h" #include "power_save_timer.h"
#include "esp_wifi.h"
#define TAG "magiclick_2p5" #define TAG "magiclick_2p5"
@@ -73,7 +74,7 @@ static const gc9a01_lcd_init_cmd_t gc9107_lcd_init_cmds[] = {
{0xba, (uint8_t[]){0xFF, 0xFF}, 2, 0}, {0xba, (uint8_t[]){0xFF, 0xFF}, 2, 0},
}; };
class magiclick_2p5 : public WifiBoard { class magiclick_2p5 : public DualNetworkBoard {
private: private:
i2c_master_bus_handle_t codec_i2c_bus_; i2c_master_bus_handle_t codec_i2c_bus_;
Button main_button_; Button main_button_;
@@ -116,6 +117,19 @@ private:
power_save_timer_->SetEnabled(true); power_save_timer_->SetEnabled(true);
} }
void Enable4GModule() {
// enable the 4G module
gpio_reset_pin(ML307_POWER_PIN);
gpio_set_direction(ML307_POWER_PIN, GPIO_MODE_OUTPUT);
gpio_set_level(ML307_POWER_PIN, ML307_POWER_OUTPUT_INVERT ? 0 : 1);
}
void Disable4GModule() {
// enable the 4G module
gpio_reset_pin(ML307_POWER_PIN);
gpio_set_direction(ML307_POWER_PIN, GPIO_MODE_OUTPUT);
gpio_set_level(ML307_POWER_PIN, ML307_POWER_OUTPUT_INVERT ? 1 : 0);
}
void InitializeCodecI2c() { void InitializeCodecI2c() {
// Initialize I2C peripheral // Initialize I2C peripheral
i2c_master_bus_config_t i2c_bus_cfg = { i2c_master_bus_config_t i2c_bus_cfg = {
@@ -133,11 +147,37 @@ private:
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_)); ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
} }
void CheckNetType() {
if (GetNetworkType() == NetworkType::WIFI) {
Disable4GModule();
} else if (GetNetworkType() == NetworkType::ML307) {
Enable4GModule();
}
}
void InitializeButtons() { void InitializeButtons() {
main_button_.OnClick([this]() { main_button_.OnClick([this]() {
auto& app = Application::GetInstance(); auto& app = Application::GetInstance();
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) { if (GetNetworkType() == NetworkType::WIFI) {
ResetWifiConfiguration(); if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
// cast to WifiBoard
auto& wifi_board = static_cast<WifiBoard&>(GetCurrentBoard());
wifi_board.ResetWifiConfiguration();
Disable4GModule();
}
} else if(GetNetworkType() == NetworkType::ML307) {
Enable4GModule();
// stop WiFi
esp_wifi_stop();
}
});
main_button_.OnDoubleClick([this]() {
auto& app = Application::GetInstance();
if (app.GetDeviceState() == kDeviceStateStarting || app.GetDeviceState() == kDeviceStateWifiConfiguring) {
SwitchNetworkType();
} }
}); });
main_button_.OnPressDown([this]() { main_button_.OnPressDown([this]() {
@@ -149,11 +189,7 @@ private:
}); });
left_button_.OnClick([this]() { left_button_.OnClick([this]() {
power_save_timer_->WakeUp(); power_save_timer_->WakeUp();
auto& app = Application::GetInstance();
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
ResetWifiConfiguration();
}
auto codec = GetAudioCodec(); auto codec = GetAudioCodec();
auto volume = codec->output_volume() - 10; auto volume = codec->output_volume() - 10;
if (volume < 0) { if (volume < 0) {
@@ -253,11 +289,12 @@ private:
} }
public: public:
magiclick_2p5() : magiclick_2p5() : DualNetworkBoard(ML307_TX_PIN, ML307_RX_PIN, 4096, 0),
main_button_(MAIN_BUTTON_GPIO), main_button_(MAIN_BUTTON_GPIO),
left_button_(LEFT_BUTTON_GPIO), left_button_(LEFT_BUTTON_GPIO),
right_button_(RIGHT_BUTTON_GPIO) { right_button_(RIGHT_BUTTON_GPIO) {
InitializeLedPower(); InitializeLedPower();
CheckNetType();
InitializePowerManager(); InitializePowerManager();
InitializePowerSaveTimer(); InitializePowerSaveTimer();
InitializeCodecI2c(); InitializeCodecI2c();
@@ -305,7 +342,7 @@ public:
if (!enabled) { if (!enabled) {
power_save_timer_->WakeUp(); power_save_timer_->WakeUp();
} }
WifiBoard::SetPowerSaveMode(enabled); DualNetworkBoard::SetPowerSaveMode(enabled);
} }
}; };