Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

set(PROJECT_VER "1.5.1")
set(PROJECT_VER "1.6.0")
add_definitions(-DFIRMWARE_VERSION=\"${PROJECT_VER}\")

# Add this line to disable the specific warning
Expand Down
3 changes: 3 additions & 0 deletions firmware/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ idf_component_register(SRCS ${SOURCES}
esp_pm
esp_psram
esp_netif
esp_http_client
mbedtls
json
esp_driver_gpio
esp_driver_uart
esp_driver_spi
Expand Down
1 change: 1 addition & 0 deletions firmware/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ config USE_AUDIO_DEBUGGER
Enable audio debugger, send audio data through UDP to the host machine

menu "WiFi Configuration Method"
depends on !BOARD_TYPE_M5STACK_STACK_CHAN
help
WiFi Configuration Method Selection
config USE_HOTSPOT_WIFI_PROVISIONING
Expand Down
5 changes: 4 additions & 1 deletion firmware/main/apps/app_launcher/app_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ void AppLauncher::onLauncherOpen()

LvglLockGuard lock;

if (!_startup_checked && !GetHAL().isAppConfiged()) {
if (!_startup_checked && GetHAL().consumeAppSetupRequest()) {
mclog::tagInfo(getAppInfo().name, "resume App setup after network/binding failure");
_startup_worker = std::make_unique<setup_workers::WifiSetupWorker>();
} else if (!_startup_checked && !GetHAL().isAppConfiged()) {
mclog::tagInfo(getAppInfo().name, "app not configured, start startup worker");
_startup_worker = std::make_unique<setup_workers::StartupWorker>();
} else {
Expand Down
2 changes: 1 addition & 1 deletion firmware/main/apps/app_launcher/app_launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AppLauncher : public mooncake::templates::AppLauncherBase {
private:
std::unique_ptr<view::LauncherView> _view;
std::unique_ptr<view::Screensaver> _screensaver;
std::unique_ptr<setup_workers::StartupWorker> _startup_worker;
std::unique_ptr<setup_workers::WorkerBase> _startup_worker;
uint32_t _screensaver_timecount = 0;
bool _startup_checked = false;

Expand Down
5 changes: 5 additions & 0 deletions firmware/main/apps/app_setup/app_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ void AppSetup::onOpen()
_destroy_menu = true;
_need_warm_reset = true;
_worker = std::make_unique<WifiSetupWorker>();
}},
{"Testing service",
[&]() {
_destroy_menu = true;
_worker = std::make_unique<NetworkTestWorker>();
}}},
},
{
Expand Down
99 changes: 99 additions & 0 deletions firmware/main/apps/app_setup/workers/network_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* SPDX-License-Identifier: MIT */
#include "workers.h"

using namespace setup_workers;
using namespace uitk::lvgl_cpp;

namespace {
std::string description(const char* name, const network_diagnostics::ServiceResult& result)
{
std::string text = std::string(name) + "\n" + result.detail;
if (result.http_status > 0)
text += "\nHTTP " + std::to_string(result.http_status) + " | " + std::to_string(result.elapsed_ms) + " ms";
return text;
}

void updateResult(Label& label, std::string& previous, const char* name,
const network_diagnostics::ServiceResult& result)
{
const auto text = description(name, result);
if (text == previous) return;
previous = text;
label.setText(text);
using network_diagnostics::Status;
const auto color = result.status == Status::Available ? 0x217A43
: result.status == Status::Reachable ? 0x916400
: result.status == Status::Failed ? 0xB3261E
: 0x26206A;
label.setTextColor(lv_color_hex(color));
}
} // namespace

NetworkTestWorker::NetworkTestWorker()
{
_panel = std::make_unique<Container>(lv_screen_active());
_panel->setSize(320, 240);
_panel->setBgColor(lv_color_hex(0xEDF4FF));
_panel->setBorderWidth(0);
_panel->setRadius(0);
_panel->setScrollDir(LV_DIR_VER);
_panel->setScrollbarMode(LV_SCROLLBAR_MODE_ACTIVE);
lv_obj_set_style_pad_top(_panel->get(), 38, 0);
lv_obj_set_style_pad_bottom(_panel->get(), 32, 0);
lv_obj_set_style_pad_left(_panel->get(), 16, 0);
lv_obj_set_style_pad_right(_panel->get(), 16, 0);
lv_obj_set_style_pad_row(_panel->get(), 18, 0);
lv_obj_set_flex_flow(_panel->get(), LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(_panel->get(), LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
auto createLabel = [&]() {
auto label = std::make_unique<Label>(_panel->get());
label->setWidth(280);
label->setTextFont(&lv_font_montserrat_16);
label->setTextColor(lv_color_hex(0x26206A));
label->setLongMode(LV_LABEL_LONG_MODE_WRAP);
return label;
};
_title = createLabel();
_title->setText("Testing service");
_title->setTextFont(&lv_font_montserrat_24);
_wifi = createLabel();
_xiaozhi = createLabel();
_m5stack = createLabel();
_retry = std::make_unique<Button>(_panel->get());
apply_button_common_style(*_retry);
_retry->setSize(280, 44);
_retry->label().setText("Test Again");
_retry->onClick().connect([this]() { _retry_clicked = true; });
_back = std::make_unique<Button>(_panel->get());
apply_button_common_style(*_back);
_back->setSize(280, 44);
_back->label().setText("Back");
_back->onClick().connect([this]() { _back_clicked = true; });
_diagnostics.start();
update();
}

NetworkTestWorker::~NetworkTestWorker() = default;

void NetworkTestWorker::update()
{
if (_back_clicked) {
_is_done = true;
return;
}
if (_retry_clicked) {
_retry_clicked = false;
_diagnostics.start();
}
const auto results = _diagnostics.snapshot();
if (results.wifi != _last_wifi) {
_last_wifi = results.wifi;
_wifi->setText(results.wifi);
}
updateResult(*_xiaozhi, _last_xiaozhi, "Xiaozhi", results.xiaozhi);
updateResult(*_m5stack, _last_m5stack, "M5Stack", results.m5stack);
if (results.running)
_retry->addState(LV_STATE_DISABLED);
else
_retry->removeState(LV_STATE_DISABLED);
}
17 changes: 17 additions & 0 deletions firmware/main/apps/app_setup/workers/workers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <smooth_lvgl.hpp>
#include <uitk/short_namespace.hpp>
#include <hal/hal.h>
#include <hal/utils/network_diagnostics/network_diagnostics.h>
#include <cstdint>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -36,6 +37,22 @@ class WorkerBase {
bool _is_done = false;
};

class NetworkTestWorker : public WorkerBase {
public:
NetworkTestWorker();
~NetworkTestWorker();
void update() override;

private:
network_diagnostics::NetworkDiagnostics _diagnostics;
std::unique_ptr<uitk::lvgl_cpp::Container> _panel;
std::unique_ptr<uitk::lvgl_cpp::Label> _title, _wifi, _xiaozhi, _m5stack;
std::unique_ptr<uitk::lvgl_cpp::Button> _retry, _back;
std::string _last_wifi, _last_xiaozhi, _last_m5stack;
bool _retry_clicked = false;
bool _back_clicked = false;
};

/**
* @brief
*
Expand Down
131 changes: 113 additions & 18 deletions firmware/main/hal/board/stackchan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
#include <esp_lcd_ili9341.h>
#include <esp_timer.h>
#include <algorithm>
#include <mutex>
#include "stackchan_camera.h"
#include "hal_bridge.h"
#include <hal/hal.h>

#define TAG "M5Stack-StackChan-Board"

Expand Down Expand Up @@ -159,15 +161,41 @@ class Pmic : public Axp2101 {
return true;
}

bool IsExternalPowerConnected()
esp_err_t TryReadPowerStatus(bool& has_external_power, bool& charging, bool& discharging)
{
const uint8_t power_status = ReadReg(0x01);
uint8_t power_status = 0;
esp_err_t err = TryReadRegs(0x01, &power_status, 1);
if (err != ESP_OK) {
return err;
}

const uint8_t current_direction = (power_status & 0b01100000) >> 5;
const bool is_charging_done = (power_status & 0b00000111) == 0b00000100;
if (current_direction == 3) {
return ESP_ERR_INVALID_RESPONSE;
}
const bool is_charging_done = (power_status & 0b00000111) == 0b00000100;

// Treat any non-discharging state as externally powered so a plugged-in cable
// still counts even after the battery is full.
return current_direction != 2 || is_charging_done;
has_external_power = current_direction != 2 || is_charging_done;
charging = current_direction == 1;
discharging = current_direction == 2;
return ESP_OK;
}

esp_err_t TryReadBatteryLevel(int& level)
{
uint8_t battery_level = 0;
esp_err_t err = TryReadRegs(0xA4, &battery_level, 1);
if (err != ESP_OK) {
return err;
}
if (battery_level > 100) {
return ESP_ERR_INVALID_RESPONSE;
}

level = battery_level;
return ESP_OK;
}
};

Expand Down Expand Up @@ -311,8 +339,18 @@ class Ft6336 : public I2cDevice {

class M5StackCoreS3Board : public WifiBoard {
private:
static constexpr int kPowerSaveSleepDelaySeconds = 300;
static constexpr int kPowerStatePollIntervalMs = 1000;
static constexpr int kPowerSaveSleepDelaySeconds = 300;
static constexpr int kPowerStatePollIntervalMs = 1000;
static constexpr int kPowerReadErrorLogIntervalMs = 10000;

struct PowerState {
bool has_external_power = false;
bool charging = false;
bool discharging = false;
int battery_level = 0;
bool status_valid = false;
bool battery_level_valid = false;
};

i2c_master_bus_handle_t i2c_bus_;
Pmic* pmic_;
Expand All @@ -323,8 +361,12 @@ class M5StackCoreS3Board : public WifiBoard {
esp_timer_handle_t touchpad_timer_;
PowerSaveTimer* power_save_timer_;
hal_bridge::XiaozhiConfig_t xiaozhi_config_;
bool last_power_save_enabled_ = false;
int64_t last_power_state_check_ms_ = 0;
std::mutex power_state_mutex_;
PowerState power_state_;
bool last_power_save_enabled_ = false;
int64_t last_power_state_check_ms_ = 0;
int64_t last_power_error_log_ms_ = 0;
uint32_t consecutive_power_read_failures_ = 0;

bool ShouldEnablePowerSave(bool has_external_power, bool is_discharging) const
{
Expand All @@ -345,15 +387,61 @@ class M5StackCoreS3Board : public WifiBoard {
last_power_save_enabled_ = should_enable_power_save;
}

void RefreshPowerState()
{
bool has_external_power = false;
bool charging = false;
bool discharging = false;
int battery_level = 0;
esp_err_t status_err = pmic_->TryReadPowerStatus(has_external_power, charging, discharging);
esp_err_t level_err = pmic_->TryReadBatteryLevel(battery_level);

{
std::lock_guard<std::mutex> lock(power_state_mutex_);
if (status_err == ESP_OK) {
power_state_.has_external_power = has_external_power;
power_state_.charging = charging;
power_state_.discharging = discharging;
power_state_.status_valid = true;
}
if (level_err == ESP_OK) {
power_state_.battery_level = battery_level;
power_state_.battery_level_valid = true;
}
}

if (status_err == ESP_OK) {
UpdatePowerSaveEnabled(has_external_power, discharging);
}

if (status_err != ESP_OK || level_err != ESP_OK) {
consecutive_power_read_failures_++;
const int64_t now_ms = esp_timer_get_time() / 1000;
if (last_power_error_log_ms_ == 0 || (now_ms - last_power_error_log_ms_) >= kPowerReadErrorLogIntervalMs) {
ESP_LOGW(TAG, "AXP2101 read failed: status=%s, level=%s; keeping previous values (%lu failure(s))",
esp_err_to_name(status_err), esp_err_to_name(level_err),
static_cast<unsigned long>(consecutive_power_read_failures_));
last_power_error_log_ms_ = now_ms;
}
return;
}

if (consecutive_power_read_failures_ != 0) {
ESP_LOGI(TAG, "AXP2101 reads recovered after %lu failure(s)",
static_cast<unsigned long>(consecutive_power_read_failures_));
consecutive_power_read_failures_ = 0;
last_power_error_log_ms_ = 0;
}
}

void PollPowerSaveState()
{
const int64_t now_ms = esp_timer_get_time() / 1000;
if (last_power_state_check_ms_ != 0 && (now_ms - last_power_state_check_ms_) < kPowerStatePollIntervalMs) {
return;
}
last_power_state_check_ms_ = now_ms;

UpdatePowerSaveEnabled(pmic_->IsExternalPowerConnected(), pmic_->IsDischarging());
RefreshPowerState();
}

void InitializePowerSaveTimer()
Expand All @@ -380,7 +468,7 @@ class M5StackCoreS3Board : public WifiBoard {
GetBacklight()->RestoreBrightness();
});
power_save_timer_->OnShutdownRequest([this]() { pmic_->PowerOff(); });
UpdatePowerSaveEnabled(pmic_->IsExternalPowerConnected(), pmic_->IsDischarging());
RefreshPowerState();
}

void InitializeI2c()
Expand Down Expand Up @@ -602,6 +690,14 @@ class M5StackCoreS3Board : public WifiBoard {
GetBacklight()->RestoreBrightness();
}

// All fallback paths (no saved SSID, timeout, explicit reconfiguration) use
// the same BLE App setup flow. Never start the upstream configuration AP.
void StartWifiConfigMode() override
{
ESP_LOGW(TAG, "WiFi setup required; opening StackChan App setup");
GetHAL().requestAppSetup();
}

virtual AudioCodec* GetAudioCodec() override
{
static CoreS3AudioCodec audio_codec(i2c_bus_, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
Expand All @@ -623,15 +719,14 @@ class M5StackCoreS3Board : public WifiBoard {

virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override
{
static bool last_discharging = false;
charging = pmic_->IsCharging();
discharging = pmic_->IsDischarging();
if (discharging != last_discharging) {
power_save_timer_->SetEnabled(discharging);
last_discharging = discharging;
std::lock_guard<std::mutex> lock(power_state_mutex_);
if (!power_state_.status_valid || !power_state_.battery_level_valid) {
return false;
}

level = pmic_->GetBatteryLevel();
level = power_state_.battery_level;
charging = power_state_.charging;
discharging = power_state_.discharging;
return true;
}

Expand Down
Loading