From ac60757dba2353a63a59c13204ebf5b1c282fe43 Mon Sep 17 00:00:00 2001 From: Forairaaaaa Date: Thu, 5 Nov 2026 13:37:53 +0800 Subject: [PATCH] feat(audio): backport quiet mode to original Cardputer Port the ADV quiet-mode controls to the original Cardputer's HAL and launcher. Q enables quiet mode on the boot screen and toggles it in the launcher, with a status icon and an NVS preference restored before boot audio. UI sounds respect the setting while recorded audio still plays. Based on the ADV implementation in fbfb429. Fixes #21 --- README.md | 6 ++ main/apps/launcher/launcher.cpp | 6 ++ main/apps/launcher/launcher.h | 3 + main/apps/launcher/port/port.cpp | 29 +++++++++- .../launcher/views/boot_anim/boot_anim.cpp | 3 + main/apps/launcher/views/menu/menu.cpp | 7 +++ .../launcher/views/system_bar/assets/quiet.h | 52 +++++++++++++++++ .../launcher/views/system_bar/system_bar.cpp | 7 +-- main/hal/hal.h | 6 +- main/hal/hal_cardputer.cpp | 56 +++++++++++++++++++ main/hal/hal_cardputer.h | 29 +++++++++- 11 files changed, 194 insertions(+), 10 deletions(-) create mode 100644 main/apps/launcher/views/system_bar/assets/quiet.h diff --git a/README.md b/README.md index 1c6e8f4c..f8a69189 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ # M5Cardputer-UserDemo M5Cardputer user demo for hardware evaluation. +Press **Q** on the startup screen to enable quiet mode, or press **Q** in the +launcher to toggle it. Quiet mode silences startup audio and UI/keyboard sounds, +shows a muted speaker icon in the status bar, and is remembered across restarts. +Recorded audio playback is unaffected. To silence the first startup sound, hold +**Q** while powering on. + > **Note: If you are using Cardputer ADV, please use the [CardputerADV branch](https://github.com/m5stack/M5Cardputer-UserDemo/tree/CardputerADV).** #### Tool Chains diff --git a/main/apps/launcher/launcher.cpp b/main/apps/launcher/launcher.cpp index 863e05e9..e5ce2a05 100644 --- a/main/apps/launcher/launcher.cpp +++ b/main/apps/launcher/launcher.cpp @@ -40,6 +40,12 @@ void Launcher::onCreate() startApp(); } +void Launcher::onResume() +{ + // A Q held while returning from an app must not toggle quiet mode. + _data.hal->keyboard()->updateKeyList(); + _data.quiet_key_was_pressed = _data.hal->keyboard()->isKeyPressing(_data.hal->keyboard()->getKeyNum({1, 1})); +} void Launcher::onRunning() { diff --git a/main/apps/launcher/launcher.h b/main/apps/launcher/launcher.h index acc1750d..d6318627 100644 --- a/main/apps/launcher/launcher.h +++ b/main/apps/launcher/launcher.h @@ -68,6 +68,7 @@ namespace MOONCAKE SMOOTH_MENU::Simple_Menu* menu = nullptr; SMOOTH_MENU::SimpleMenuCallback_t* menu_render_cb = nullptr; APP_BASE* _opened_app = nullptr; + bool quiet_key_was_pressed = false; // System bar uint32_t system_bar_update_preiod = 1000; @@ -98,12 +99,14 @@ namespace MOONCAKE bool _port_check_next_pressed(); bool _port_check_last_pressed(); bool _port_check_enter_pressed(); + bool _port_check_quiet_pressed(); void _port_update_keyboard_state(); void _port_update_system_state(); public: void onCreate() override; + void onResume() override; void onRunning() override; void onRunningBG() override; }; diff --git a/main/apps/launcher/port/port.cpp b/main/apps/launcher/port/port.cpp index 6cdc21bf..5ef6bca4 100644 --- a/main/apps/launcher/port/port.cpp +++ b/main/apps/launcher/port/port.cpp @@ -21,10 +21,14 @@ void Launcher::_port_wait_enter() { // delay(600); - _data.hal->Speaker()->setVolume(255); + _data.hal->Speaker()->setVolume(90); // bool shit = false; _data.hal->keyboard()->updateKeyList(); + const int quiet_key = _data.hal->keyboard()->getKeyNum({1, 1}); // Q + const bool quiet_pressed = _data.hal->keyboard()->isKeyPressing(quiet_key); + if (quiet_pressed) + _data.hal->setQuietMode(true); if (_data.hal->keyboard()->keyList().size()) { // _data.hal->Speaker()->playWav(boot_sound_1, sizeof(boot_sound_1)); @@ -33,24 +37,36 @@ void Launcher::_port_wait_enter() { // shit = true; #ifndef NO_BOOT_PLAY_SHIT - _data.hal->Speaker()->playWav(boot_sound_2, sizeof(boot_sound_2)); + if (!_data.hal->isQuietMode()) + _data.hal->Speaker()->playWav(boot_sound_2, sizeof(boot_sound_2)); #endif } // Hold till release while (_data.hal->keyboard()->keyList().size()) + { _data.hal->keyboard()->updateKeyList(); + delay(1); + } + + if (quiet_pressed) + return; while (1) { _data.hal->keyboard()->updateKeyList(); if (_data.hal->keyboard()->keyList().size()) { + if (_data.hal->keyboard()->isKeyPressing(quiet_key)) + _data.hal->setQuietMode(true); _data.hal->playNextSound(); // Hold till release while (_data.hal->keyboard()->keyList().size()) + { _data.hal->keyboard()->updateKeyList(); + delay(1); + } break; } @@ -64,6 +80,15 @@ void Launcher::_port_wait_enter() // _data.hal->Speaker()->playWav(boot_sound_1, sizeof(boot_sound_1)); } +bool Launcher::_port_check_quiet_pressed() +{ + _data.hal->keyboard()->updateKeyList(); + const bool pressed = _data.hal->keyboard()->isKeyPressing(_data.hal->keyboard()->getKeyNum({1, 1})); + const bool newly_pressed = pressed && !_data.quiet_key_was_pressed; + _data.quiet_key_was_pressed = pressed; + return newly_pressed; +} + bool Launcher::_port_check_next_pressed() { if (_data.hal->keyboard()->isKeyPressing(55) || _data.hal->keyboard()->isKeyPressing(54)) diff --git a/main/apps/launcher/views/boot_anim/boot_anim.cpp b/main/apps/launcher/views/boot_anim/boot_anim.cpp index 7ff77751..4d126e7c 100644 --- a/main/apps/launcher/views/boot_anim/boot_anim.cpp +++ b/main/apps/launcher/views/boot_anim/boot_anim.cpp @@ -25,5 +25,8 @@ void Launcher::_boot_anim() // Show logo _data.hal->display()->pushImage(0, 0, 240, 135, image_data_logo); + _data.hal->display()->setFont(&fonts::Font0); + _data.hal->display()->setTextColor((uint32_t)0x999999, (uint32_t)0xE6E6E6); + _data.hal->display()->drawString("Q=quiet", 6, 122); _port_wait_enter(); } diff --git a/main/apps/launcher/views/menu/menu.cpp b/main/apps/launcher/views/menu/menu.cpp index 2234156c..212dc443 100644 --- a/main/apps/launcher/views/menu/menu.cpp +++ b/main/apps/launcher/views/menu/menu.cpp @@ -92,6 +92,13 @@ void Launcher::_update_menu() { if ((millis() - _data.menu_update_count) > _data.menu_update_preiod) { + if (_port_check_quiet_pressed()) + { + _data.hal->setQuietMode(!_data.hal->isQuietMode()); + _data.system_bar_update_count = millis() - _data.system_bar_update_preiod - 1; + _update_system_bar(); + } + // Navigate if (_port_check_last_pressed()) _data.menu->goLast(); diff --git a/main/apps/launcher/views/system_bar/assets/quiet.h b/main/apps/launcher/views/system_bar/assets/quiet.h new file mode 100644 index 00000000..c1a924f1 --- /dev/null +++ b/main/apps/launcher/views/system_bar/assets/quiet.h @@ -0,0 +1,52 @@ + +/******************************************************************************* + * name: quiet + * + * preset name: Color R5G6B5 + * data block size: 16 bit(s), uint16_t + * RLE compression enabled: no + * conversion type: Color, not_used not_used + * split to rows: yes + * bits per pixel: 16 + * + * preprocess: + * main scan direction: top_to_bottom + * line scan direction: forward + * inverse: no + *******************************************************************************/ + +/* + typedef struct { + const uint16_t *data; + uint16_t width; + uint16_t height; + uint8_t dataSize; + } tImage; +*/ +#include + +static const uint16_t image_data_quiet[256] = { + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0x82e0, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0x82e0, 0x82e0, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0x0000, 0xe09f, 0x82e0, + 0x82e0, 0x82e0, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0x0000, 0x0000, 0x82e0, 0x82e0, 0x82e0, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0x0000, 0x0000, 0x0000, 0x82e0, 0x82e0, 0x82e0, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x82e0, 0x82e0, 0x82e0, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0x0000, 0x0000, 0x0000, 0x0000, 0x82e0, + 0x82e0, 0x82e0, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0x0000, + 0x0000, 0x0000, 0x82e0, 0x82e0, 0x82e0, 0x0000, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0x0000, 0x0000, 0x82e0, 0x82e0, 0x82e0, 0x0000, 0x0000, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0x82e0, 0x82e0, 0x82e0, 0x0000, + 0x0000, 0x0000, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0x82e0, + 0x82e0, 0x82e0, 0xe09f, 0xe09f, 0x0000, 0x0000, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0x82e0, 0x82e0, 0x82e0, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0x0000, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0x82e0, 0x82e0, 0x82e0, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, 0xe09f, + 0xe09f, 0xe09f, 0xe09f, 0xe09f}; +// const tImage quiet = { image_data_quiet, 16, 16, +// 16 }; diff --git a/main/apps/launcher/views/system_bar/system_bar.cpp b/main/apps/launcher/views/system_bar/system_bar.cpp index df60df41..9d730d49 100644 --- a/main/apps/launcher/views/system_bar/system_bar.cpp +++ b/main/apps/launcher/views/system_bar/system_bar.cpp @@ -22,6 +22,7 @@ #include "assets/wifi3.h" #include "assets/wifi4.h" #include "assets/wifi5.h" +#include "assets/quiet.h" using namespace MOONCAKE::APPS; @@ -79,10 +80,8 @@ void Launcher::_update_system_bar() else if (_data.system_state.wifi_state == 5) _data.hal->canvas_system_bar()->pushImage(x, y, 16, 16, image_data_wifi5); - - - - + if (_data.hal->isQuietMode()) + _data.hal->canvas_system_bar()->pushImage(x + 18, y, 16, 16, image_data_quiet); // Bat shit x = _data.hal->canvas_system_bar()->width() - 45; diff --git a/main/hal/hal.h b/main/hal/hal.h index 5dc13884..8110b472 100644 --- a/main/hal/hal.h +++ b/main/hal/hal.h @@ -39,12 +39,13 @@ namespace HAL SDCard* _sdcard; bool _sntp_adjusted; + bool _quiet_mode; public: Hal() : _display(nullptr), _canvas(nullptr), _canvas_system_bar(nullptr), _canvas_keyboard_bar(nullptr), _keyboard(nullptr), _mic(nullptr), _speaker(nullptr), _homeButton(nullptr), _sdcard(nullptr), - _sntp_adjusted(false) + _sntp_adjusted(false), _quiet_mode(false) { } @@ -62,6 +63,9 @@ namespace HAL inline void setSntpAdjusted(bool isAdjusted) { _sntp_adjusted = isAdjusted; } inline bool isSntpAdjusted(void) { return _sntp_adjusted; } + inline bool isQuietMode() const { return _quiet_mode; } + virtual void setQuietMode(bool quiet) { _quiet_mode = quiet; } + // Canvas inline void canvas_system_bar_update() { _canvas_system_bar->pushSprite(_canvas_keyboard_bar->width(), 0); } inline void canvas_keyboard_bar_update() { _canvas_keyboard_bar->pushSprite(0, 0); } diff --git a/main/hal/hal_cardputer.cpp b/main/hal/hal_cardputer.cpp index 9134ddb5..8e387024 100644 --- a/main/hal/hal_cardputer.cpp +++ b/main/hal/hal_cardputer.cpp @@ -13,6 +13,8 @@ #include #include "../apps/utils/common_define.h" #include "bat/adc_read.h" +#include "nvs.h" +#include "nvs_flash.h" using namespace HAL; @@ -91,6 +93,59 @@ void HalCardputer::_bat_init() { adc_read_init(); } void HalCardputer::_sdcard_init() { _sdcard = new SDCard; } +void HalCardputer::_load_quiet_mode() +{ + esp_err_t err = nvs_flash_init(); + if (err != ESP_OK) + { + spdlog::warn("cannot initialize settings: {}", esp_err_to_name(err)); + return; + } + + nvs_handle_t handle; + err = nvs_open("cardputer", NVS_READONLY, &handle); + if (err == ESP_ERR_NVS_NOT_FOUND) + return; + if (err != ESP_OK) + { + spdlog::warn("cannot open settings: {}", esp_err_to_name(err)); + return; + } + + uint8_t quiet = 0; + err = nvs_get_u8(handle, "quiet_mode", &quiet); + nvs_close(handle); + if (err == ESP_OK) + _quiet_mode = quiet != 0; + else if (err != ESP_ERR_NVS_NOT_FOUND) + spdlog::warn("cannot load quiet mode: {}", esp_err_to_name(err)); +} + +void HalCardputer::setQuietMode(bool quiet) +{ + if (_quiet_mode == quiet) + return; + + _quiet_mode = quiet; + if (quiet && _speaker) + _speaker->stop(); + + nvs_handle_t handle; + // Wi-Fi scanning may have deinitialized NVS since startup. + esp_err_t err = nvs_flash_init(); + if (err == ESP_OK) + err = nvs_open("cardputer", NVS_READWRITE, &handle); + if (err == ESP_OK) + { + err = nvs_set_u8(handle, "quiet_mode", quiet); + if (err == ESP_OK) + err = nvs_commit(handle); + nvs_close(handle); + } + if (err != ESP_OK) + spdlog::warn("cannot save quiet mode: {}", esp_err_to_name(err)); +} + void HalCardputer::init() { spdlog::info("hal init"); @@ -102,6 +157,7 @@ void HalCardputer::init() _button_init(); _bat_init(); _sdcard_init(); + _load_quiet_mode(); } // double getBatVolBfb(double batVcc) //获取电压的百分比,经过换算并非线性关系 diff --git a/main/hal/hal_cardputer.h b/main/hal/hal_cardputer.h index 0c7f7595..01fa37cf 100644 --- a/main/hal/hal_cardputer.h +++ b/main/hal/hal_cardputer.h @@ -22,13 +22,36 @@ namespace HAL void _button_init(); void _bat_init(); void _sdcard_init(); + void _load_quiet_mode(); public: std::string type() override { return "cardputer"; } void init() override; - void playKeyboardSound() override { _speaker->setVolume(72); _speaker->tone(5000, 20); } - void playLastSound() override { _speaker->setVolume(32); _speaker->tone(6000, 20); } - void playNextSound() override { _speaker->setVolume(64); _speaker->tone(7000, 20); } + void setQuietMode(bool quiet) override; + void playKeyboardSound() override + { + if (!isQuietMode()) + { + _speaker->setVolume(72); + _speaker->tone(5000, 20); + } + } + void playLastSound() override + { + if (!isQuietMode()) + { + _speaker->setVolume(32); + _speaker->tone(6000, 20); + } + } + void playNextSound() override + { + if (!isQuietMode()) + { + _speaker->setVolume(64); + _speaker->tone(7000, 20); + } + } uint8_t getBatLevel() override; public: