Files
xiaozhi-esp32/main/boards/common/ml307_board.cc

122 lines
3.7 KiB
C++
Raw Normal View History

2024-11-05 20:15:00 +08:00
#include "ml307_board.h"
2024-12-06 11:08:49 +08:00
2024-11-05 20:15:00 +08:00
#include "application.h"
2024-12-06 11:08:49 +08:00
#include "display.h"
2024-11-18 06:17:39 +08:00
#include "font_awesome_symbols.h"
2025-02-19 23:54:59 +08:00
#include "assets/lang_config.h"
2024-10-29 00:22:29 +08:00
#include <esp_log.h>
#include <esp_timer.h>
2024-11-05 20:15:00 +08:00
#include <ml307_http.h>
#include <ml307_ssl_transport.h>
#include <web_socket.h>
2024-11-14 23:15:43 +08:00
#include <ml307_mqtt.h>
#include <ml307_udp.h>
2025-02-04 14:24:51 +08:00
#include <opus_encoder.h>
2024-10-29 00:22:29 +08:00
static const char *TAG = "Ml307Board";
2025-02-04 14:24:51 +08:00
Ml307Board::Ml307Board(gpio_num_t tx_pin, gpio_num_t rx_pin, size_t rx_buffer_size) : modem_(tx_pin, rx_pin, rx_buffer_size) {
2024-10-29 00:22:29 +08:00
}
2025-02-04 14:24:51 +08:00
std::string Ml307Board::GetBoardType() {
return "ml307";
2024-10-29 00:22:29 +08:00
}
2024-10-30 06:58:29 +08:00
void Ml307Board::StartNetwork() {
2024-11-06 06:18:56 +08:00
auto display = Board::GetInstance().GetDisplay();
2025-02-19 23:54:59 +08:00
display->SetStatus(Lang::Strings::DETECTING_MODULE);
2024-11-06 06:18:56 +08:00
modem_.SetDebug(false);
modem_.SetBaudRate(921600);
auto& application = Application::GetInstance();
// If low power, the material ready event will be triggered by the modem because of a reset
modem_.OnMaterialReady([this, &application]() {
ESP_LOGI(TAG, "ML307 material ready");
application.Schedule([this, &application]() {
2025-01-05 19:34:28 +08:00
application.SetDeviceState(kDeviceStateIdle);
2024-11-06 06:18:56 +08:00
WaitForNetworkReady();
});
});
WaitForNetworkReady();
}
void Ml307Board::WaitForNetworkReady() {
2024-10-29 00:22:29 +08:00
auto& application = Application::GetInstance();
2024-11-06 06:18:56 +08:00
auto display = Board::GetInstance().GetDisplay();
2025-02-19 23:54:59 +08:00
display->SetStatus(Lang::Strings::REGISTERING_NETWORK);
2024-10-30 06:58:29 +08:00
int result = modem_.WaitForNetworkReady();
if (result == -1) {
2025-02-19 23:54:59 +08:00
application.Alert(Lang::Strings::ERROR, Lang::Strings::PIN_ERROR, "sad", Lang::Sounds::P3_ERR_PIN);
return;
2024-10-30 06:58:29 +08:00
} else if (result == -2) {
2025-02-19 23:54:59 +08:00
application.Alert(Lang::Strings::ERROR, Lang::Strings::REG_ERROR, "sad", Lang::Sounds::P3_ERR_REG);
return;
2024-10-30 06:58:29 +08:00
}
2024-10-29 00:22:29 +08:00
// Print the ML307 modem information
std::string module_name = modem_.GetModuleName();
std::string imei = modem_.GetImei();
std::string iccid = modem_.GetIccid();
2024-10-30 06:58:29 +08:00
ESP_LOGI(TAG, "ML307 Module: %s", module_name.c_str());
2024-10-29 00:22:29 +08:00
ESP_LOGI(TAG, "ML307 IMEI: %s", imei.c_str());
ESP_LOGI(TAG, "ML307 ICCID: %s", iccid.c_str());
2024-12-06 11:08:49 +08:00
// Close all previous connections
modem_.ResetConnections();
2024-10-29 00:22:29 +08:00
}
Http* Ml307Board::CreateHttp() {
return new Ml307Http(modem_);
}
WebSocket* Ml307Board::CreateWebSocket() {
return new WebSocket(new Ml307SslTransport(modem_, 0));
}
2024-11-14 23:15:43 +08:00
Mqtt* Ml307Board::CreateMqtt() {
return new Ml307Mqtt(modem_, 0);
}
Udp* Ml307Board::CreateUdp() {
return new Ml307Udp(modem_, 0);
}
2024-11-18 06:17:39 +08:00
const char* Ml307Board::GetNetworkStateIcon() {
if (!modem_.network_ready()) {
return FONT_AWESOME_SIGNAL_OFF;
}
int csq = modem_.GetCsq();
if (csq == -1) {
return FONT_AWESOME_SIGNAL_OFF;
2025-02-01 23:08:41 +08:00
} else if (csq >= 0 && csq <= 14) {
2024-11-18 06:17:39 +08:00
return FONT_AWESOME_SIGNAL_1;
} else if (csq >= 15 && csq <= 19) {
2025-02-01 23:08:41 +08:00
return FONT_AWESOME_SIGNAL_2;
2024-11-18 06:17:39 +08:00
} else if (csq >= 20 && csq <= 24) {
2025-02-01 23:08:41 +08:00
return FONT_AWESOME_SIGNAL_3;
2024-11-18 06:17:39 +08:00
} else if (csq >= 25 && csq <= 31) {
2025-02-01 23:08:41 +08:00
return FONT_AWESOME_SIGNAL_4;
2024-11-18 06:17:39 +08:00
}
ESP_LOGW(TAG, "Invalid CSQ: %d", csq);
return FONT_AWESOME_SIGNAL_OFF;
}
2024-11-03 05:54:15 +08:00
std::string Ml307Board::GetBoardJson() {
2024-10-29 00:22:29 +08:00
// Set the board type for OTA
std::string board_json = std::string("{\"type\":\"" BOARD_TYPE "\",");
board_json += "\"name\":\"" BOARD_NAME "\",";
2024-11-14 23:15:43 +08:00
board_json += "\"revision\":\"" + modem_.GetModuleName() + "\",";
board_json += "\"carrier\":\"" + modem_.GetCarrierName() + "\",";
board_json += "\"csq\":\"" + std::to_string(modem_.GetCsq()) + "\",";
board_json += "\"imei\":\"" + modem_.GetImei() + "\",";
board_json += "\"iccid\":\"" + modem_.GetIccid() + "\"}";
2024-10-29 00:22:29 +08:00
return board_json;
}
2024-11-15 23:07:20 +08:00
void Ml307Board::SetPowerSaveMode(bool enabled) {
// TODO: Implement power save mode for ML307
}