diff --git a/.gitignore b/.gitignore index 6e233f0a..c6ee3a1c 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,4 @@ obj/ sdkconfig sdkconfig.old +sdkconfig.m5stack-stopwatch diff --git a/CMakeLists.txt b/CMakeLists.txt index ff7bc652..3ec202ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,10 @@ -# The following five lines of boilerplate have to be in your project's -# CMakeLists in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.21) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(StopWatch-UserDemo) + +option(STOPWATCH_MCUI_OPTIMIZE "Compile the MooncakeUI component with -O2" ON) +if(STOPWATCH_MCUI_OPTIMIZE) + idf_component_get_property(mcui_component mooncake-ui COMPONENT_LIB) + target_compile_options(${mcui_component} PRIVATE -O2) +endif() diff --git a/README.md b/README.md index 15fe08b0..fc46f90f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # M5StopWatch-UserDemo -M5Stack StopWatch user demo for hardware evaluation. +M5Stack StopWatch user demo for hardware evaluation. The same firmware supports the C152 and C152-V11 touch controllers, with a multitouch test under **Settings → Diagnostics** on the V11. ## Build @@ -11,12 +11,12 @@ python3 ./fetch_repos.py ### Tool Chains -[ESP-IDF v5.5.4](https://docs.espressif.com/projects/esp-idf/en/v5.5.4/esp32s3/index.html) +[ESP-IDF v5.5.5](https://docs.espressif.com/projects/esp-idf/en/v5.5.5/esp32s3/index.html) ### Build ```bash -idf.py build +idf.py -DIDF_TARGET=esp32s3 build ``` ### Flash @@ -33,4 +33,12 @@ Incorporated into the *M5StopWatch-UserDemo* project by [Simon J. Stuart](https: Incorporated into the *M5StopWatch-UserDemo* project by [Simon J. Stuart](https://github.com/LK-Simon) the green power indicator LED will now switch off when the AMOLED display is on. This will save a small amount of power, but also eliminate the distracting green LED when it is not necessary. ## PlatformIO/PIOArduino build support -Incorporated into the *M5StopWatch-UserDemo* project by [Simon J. Stuart](https://github.com/LK-Simon) you can now load the repository folder in Visual Studio Code with either PlatformIO or PIOArduino installed, and build/upload the demo directly to your M5Stack StopWatch device. \ No newline at end of file +Incorporated into the *M5StopWatch-UserDemo* project by [Simon J. Stuart](https://github.com/LK-Simon) you can now load the repository folder in Visual Studio Code with either PlatformIO or PIOArduino installed, and build/upload the demo directly to your M5Stack StopWatch device. + +The pinned platform requires Core 6.2 or newer. The command below uses [PIOArduino Core 6.2.0](https://pypi.org/project/pioarduino/6.2.0/). + +```bash +pio run -e m5stack-stopwatch +``` + +See [the SDL simulator instructions](simu/README.md) for desktop builds. diff --git a/dependencies.lock b/dependencies.lock index 9bf58667..54e519a3 100644 --- a/dependencies.lock +++ b/dependencies.lock @@ -70,6 +70,6 @@ direct_dependencies: - espressif/esp_codec_dev - espressif/i2c_bus - idf -manifest_hash: c617373befe8b1fa75c13efe199c02fb007e1c544872d4ec5ecea62d8b7d7b93 +manifest_hash: 8b8da793bcfa2f6aeb822ba349c41d89cb77026c31f54a43e541ed21295884ce target: esp32s3 version: 2.0.0 diff --git a/fetch_repos.py b/fetch_repos.py index 17cc96f7..d01e4c65 100644 --- a/fetch_repos.py +++ b/fetch_repos.py @@ -1,61 +1,45 @@ -import os -import subprocess import json +import subprocess +from pathlib import Path -def clone_or_update_repo( - repo_url, path, ref=None, with_submodules=False, patch_path=None -): - import os - - if not os.path.exists(path): - subprocess.run(["git", "clone", repo_url, path], check=True) - else: - subprocess.run(["git", "-C", path, "fetch"], check=True) - - if ref: - subprocess.run(["git", "-C", path, "checkout", ref], check=True) - - if with_submodules: - subprocess.run( - ["git", "-C", path, "submodule", "update", "--init", "--recursive"], - check=True, - ) - - # 应用 patch - if patch_path: - patch_full_path = ( - patch_path - if os.path.isabs(patch_path) - else os.path.join(os.getcwd(), patch_path) - ) - # 使用 git apply --check 先检测补丁是否能应用,避免报错 - check_result = subprocess.run( - ["git", "-C", path, "apply", "--check", patch_full_path] - ) - if check_result.returncode == 0: - subprocess.run(["git", "-C", path, "apply", patch_full_path], check=True) - print(f"Applied patch {patch_path} to {path}") - else: - print(f"Patch {patch_path} cannot be applied cleanly to {path}, skipped.") +PROJECT_DIR = Path(__file__).resolve().parent -def fetch_dependencies(): - script_dir = os.path.dirname(os.path.abspath(__file__)) - config_path = os.path.join(script_dir, "repos.json") +def git(path, *args, check=True, quiet=False): + return subprocess.run( + ["git", "-C", str(path), *args], + check=check, + text=True, + stdout=subprocess.PIPE if quiet else None, + stderr=subprocess.DEVNULL if quiet else None, + ) - with open(config_path) as f: - repos = json.load(f) +def fetch_dependencies(): + repos = json.loads((PROJECT_DIR / "repos.json").read_text(encoding="utf-8")) for repo in repos: - repo_path = os.path.join(script_dir, repo["path"]) - branch = repo.get("branch") - with_submodules = repo.get("with_submodules", False) - patch = repo.get("patch") - if patch and not os.path.isabs(patch): - patch = os.path.join(script_dir, patch) - clone_or_update_repo(repo["url"], repo_path, branch, with_submodules, patch) + path = PROJECT_DIR / repo["path"] + if not path.exists(): + path.parent.mkdir(parents=True, exist_ok=True) + git(PROJECT_DIR, "clone", "--no-checkout", repo["url"], str(path)) + + ref = repo["branch"] + "^{commit}" + target = git(path, "rev-parse", "--verify", ref, check=False, quiet=True) + if target.returncode: + git(path, "fetch", "--tags", "origin") + target = git(path, "rev-parse", "--verify", ref, quiet=True) + # A no-checkout clone may already have the correct HEAD but an empty worktree. + git(path, "checkout", "--detach", target.stdout.strip()) + if repo.get("with_submodules"): + git(path, "submodule", "update", "--init", "--recursive") + + if patch := repo.get("patch"): + patch_file = str(PROJECT_DIR / patch) + applied = git(path, "apply", "--reverse", "--check", patch_file, check=False, quiet=True) + if applied.returncode: + git(path, "apply", patch_file) if __name__ == "__main__": - fetch_dependencies() \ No newline at end of file + fetch_dependencies() diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index dfd12ee2..33c472e4 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,10 +1,8 @@ +include("${CMAKE_CURRENT_LIST_DIR}/../tools/asset_baker/assets.cmake") file(GLOB_RECURSE MY_SRCS "apps/*.c" "apps/*.cc" "apps/*.cpp" - "assets/*.c" - "assets/*.cc" - "assets/*.cpp" "hal/*.c" "hal/*.cc" "hal/*.cpp" @@ -24,4 +22,25 @@ idf_component_register( "assets/sfx/boot_sfx.bin" EMBED_TXTFILES "hal/utils/config_ap/assets/badge_config_ap.html" -) \ No newline at end of file +) + +idf_build_get_property(asset_python PYTHON) +stopwatch_bake_assets(${COMPONENT_LIB} "${asset_python}" "${CMAKE_CURRENT_LIST_DIR}" --rgb565-swapped) +idf_build_get_property(firmware_version PROJECT_VER) +target_compile_definitions(${COMPONENT_LIB} PRIVATE STOPWATCH_FIRMWARE_VERSION="${firmware_version}") +target_include_directories(${COMPONENT_LIB} PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/apps/common/arc_top_clock" + "${CMAKE_CURRENT_LIST_DIR}/apps/common/status_bar") + +if(CONFIG_STOPWATCH_LAUNCHER_PROFILE) + target_compile_definitions(${COMPONENT_LIB} PRIVATE STOPWATCH_LAUNCHER_PROFILE=1) + target_include_directories(${COMPONENT_LIB} PRIVATE "${CMAKE_CURRENT_LIST_DIR}/hal/display_probe") +endif() + +set(STOPWATCH_LAUNCHER_STRIP_ROWS 16 CACHE STRING "Launcher RGB565 strip height") +set_source_files_properties("apps/app_launcher/view/view.cpp" PROPERTIES + COMPILE_DEFINITIONS "STOPWATCH_LAUNCHER_STRIP_ROWS=${STOPWATCH_LAUNCHER_STRIP_ROWS}") + +target_compile_options(${COMPONENT_LIB} PRIVATE + $<$:-std=gnu++20> +) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild new file mode 100644 index 00000000..538bbff5 --- /dev/null +++ b/main/Kconfig.projbuild @@ -0,0 +1,33 @@ +config STOPWATCH_FFT_VECTOR + bool + default y + select MCUI_RENDER_VECTOR + +config STOPWATCH_BADGE_CODECS + bool + default y + select MCUI_RENDER_CODEC_JPEG + +menu "StopWatch diagnostics" + + config STOPWATCH_DISPLAY_PROBE + bool "Boot into the mooncake-ui display probe" + default n + help + Give the probe exclusive access to the CO5300 and log idle, + local-refresh and full-refresh statistics on the console. + + config STOPWATCH_INPUT_PROBE + bool "Log touch and IMU snapshots in the display probe" + depends on STOPWATCH_DISPLAY_PROBE + default n + + config STOPWATCH_LAUNCHER_PROFILE + bool "Replay and measure Launcher animations on startup" + depends on !STOPWATCH_DISPLAY_PROBE + default n + help + Replay status-bar and carousel animations through the normal Launcher + and emit buffered frame timings over serial. + +endmenu diff --git a/main/apps/app_alarm_clock/app_alarm_clock.cpp b/main/apps/app_alarm_clock/app_alarm_clock.cpp index a80acf88..313e380f 100644 --- a/main/apps/app_alarm_clock/app_alarm_clock.cpp +++ b/main/apps/app_alarm_clock/app_alarm_clock.cpp @@ -4,18 +4,18 @@ * SPDX-License-Identifier: MIT */ #include "app_alarm_clock.h" +#include "view/trigger_alarm.h" +#include #include #include #include -#include -#include using namespace mooncake; AppAlarmClock::AppAlarmClock() { setAppInfo().name = "AlarmClock"; - setAppInfo().icon = (void*)&icon_clock; + setAppInfo().icon = const_cast(&stopwatch_assets::icon_clock); } void AppAlarmClock::onCreate() @@ -28,29 +28,17 @@ void AppAlarmClock::onCreate() return; } - _alarm_clock->onTriggered().connect([this](const model::AlarmClock::AlarmTriggeredEvent& event) { - std::unique_ptr trigger_view; - { - LvglLockGuard lock; - trigger_view = std::make_unique(); - trigger_view->init(event.time); - } - - GetHAL().startAlarm(); + _alarm_clock->setTriggerCallback([](const model::AlarmClock::Time24& time) { + auto& hal = GetHAL(); + auto trigger_view = std::make_unique(time); - // Block until confirmed - while (1) { - GetHAL().feedTheDog(); - GetHAL().delay(100); - - LvglLockGuard lock; - if (trigger_view->isConfirmed()) { - trigger_view.reset(); - break; - } + hal.startAlarm(); + while (!trigger_view->update()) { + hal.feedTheDog(); + hal.delay(10); } - - GetHAL().stopAlarm(); + hal.stopAlarm(); + hal.invalidateMooncakeFrame(); }); } @@ -60,11 +48,8 @@ void AppAlarmClock::onOpen() _key_manager = std::make_unique(); - LvglLockGuard lock; - if (_alarm_clock) { - _list_view = std::make_unique(*_alarm_clock); - _list_view->init(lv_screen_active()); + _view = std::make_unique(*_alarm_clock); } } @@ -79,26 +64,7 @@ void AppAlarmClock::onRunning() _alarm_clock->update(); } - LvglLockGuard lock; - - if (_list_view) { - _list_view->update(); - - if (_list_view->consumeAddRequested()) { - _list_view.reset(); - _add_view = std::make_unique(); - _add_view->init(lv_screen_active()); - } - } - - if (_add_view && _add_view->isConfirmed() && _alarm_clock) { - auto time = _add_view->selectedTime(); - _alarm_clock->addAlarm(time, true); - - _add_view.reset(); - _list_view = std::make_unique(*_alarm_clock); - _list_view->init(lv_screen_active()); - } + if (_view) _view->update(); } void AppAlarmClock::onSleeping() @@ -114,10 +80,7 @@ void AppAlarmClock::onClose() _key_manager.reset(); - LvglLockGuard lock; - - _list_view.reset(); - _add_view.reset(); + _view.reset(); } void AppAlarmClock::onDestroy() diff --git a/main/apps/app_alarm_clock/app_alarm_clock.h b/main/apps/app_alarm_clock/app_alarm_clock.h index 9d862049..b45ab6fd 100644 --- a/main/apps/app_alarm_clock/app_alarm_clock.h +++ b/main/apps/app_alarm_clock/app_alarm_clock.h @@ -29,6 +29,5 @@ class AppAlarmClock : public mooncake::AppAbility { private: std::unique_ptr _key_manager; std::unique_ptr _alarm_clock; - std::unique_ptr _list_view; - std::unique_ptr _add_view; -}; \ No newline at end of file + std::unique_ptr _view; +}; diff --git a/main/apps/app_alarm_clock/model/alarm_clock.cpp b/main/apps/app_alarm_clock/model/alarm_clock.cpp index 7694f880..290aa1e2 100644 --- a/main/apps/app_alarm_clock/model/alarm_clock.cpp +++ b/main/apps/app_alarm_clock/model/alarm_clock.cpp @@ -46,7 +46,7 @@ bool AlarmClock::init() { mclog::tagInfo(_tag, "init"); - _alarms.clear(); + clearAlarms(); _last_update_ms = 0; AlarmStorageSnapshot snapshot; @@ -76,37 +76,36 @@ void AlarmClock::update() const int date_key = (local_time->tm_year + 1900) * 10000 + (local_time->tm_mon + 1) * 100 + local_time->tm_mday; - _alarms.forEach([&](Alarm* alarm, int alarm_id) { - if (alarm == nullptr || !alarm->enabled()) { + forEachAlarm([&](int alarm_id, Alarm& alarm) { + if (!alarm.enabled()) { return; } - if (alarm->time().hour != local_time->tm_hour || alarm->time().minute != local_time->tm_min) { + if (alarm.time().hour != local_time->tm_hour || alarm.time().minute != local_time->tm_min) { return; } - if (alarm->_last_triggered_date_key == date_key) { + if (alarm._last_triggered_date_key == date_key) { return; } - alarm->_last_triggered_date_key = date_key; - AlarmTriggeredEvent event; - event.alarmId = alarm_id; - event.time = alarm->time(); - mclog::tagInfo(_tag, "alarm triggered, id: {}, time: {}", alarm_id, format_time(event.time)); - _on_triggered.emit(event); + alarm._last_triggered_date_key = date_key; + const auto time = alarm.time(); + mclog::tagInfo(_tag, "alarm triggered, id: {}, time: {}", alarm_id, format_time(time)); + if (_on_triggered) _on_triggered(time); }); } int AlarmClock::addAlarm(const Time24& time, bool enabled) { - if (!time.isValid() || _alarms.activeCount() >= AlarmStorageSnapshot::maxAlarmCount) { + const auto active = _slot_count - _free_count; + if (!time.isValid() || active >= Capacity) { mclog::tagError(_tag, "add alarm failed, time: {}, enabled: {}, active count: {}", format_time(time), enabled, - _alarms.activeCount()); + active); return -1; } - auto alarm_id = _alarms.create(std::make_unique(time, enabled)); + const int alarm_id = insertAlarm(time, enabled); mclog::tagInfo(_tag, "add alarm, id: {}, time: {}, enabled: {}", alarm_id, format_time(time), enabled); if (!persist()) { mclog::tagError(_tag, "persist after add failed, id: {}", alarm_id); @@ -117,11 +116,13 @@ int AlarmClock::addAlarm(const Time24& time, bool enabled) bool AlarmClock::removeAlarm(int alarmId) { - if (!_alarms.destroy(alarmId)) { + if (!getAlarm(alarmId)) { mclog::tagError(_tag, "remove alarm failed, invalid id: {}", alarmId); return false; } + _alarms[alarmId].reset(); + _free_ids[_free_count++] = alarmId; mclog::tagInfo(_tag, "remove alarm, id: {}", alarmId); return persist(); @@ -165,16 +166,29 @@ bool AlarmClock::setAlarmTime(int alarmId, const Time24& time) AlarmClock::Alarm* AlarmClock::getAlarm(int alarmId) { - return _alarms.get(alarmId); + if (alarmId < 0 || std::size_t(alarmId) >= _slot_count) return nullptr; + auto& slot = _alarms[alarmId]; + return slot ? &*slot : nullptr; } void AlarmClock::forEachAlarm(const std::function& visitor) { - _alarms.forEach([&](Alarm* alarm, int alarm_id) { - if (alarm != nullptr) { - visitor(alarm_id, *alarm); - } - }); + for (std::size_t id = 0; id < _slot_count; ++id) { + if (auto& slot = _alarms[id]) visitor(int(id), *slot); + } +} + +void AlarmClock::clearAlarms() +{ + _alarms = {}; + _slot_count = _free_count = 0; +} + +int AlarmClock::insertAlarm(const Time24& time, bool enabled) +{ + const int id = _free_count ? _free_ids[--_free_count] : int(_slot_count++); + _alarms[id].emplace(time, enabled); + return id; } AlarmStorageSnapshot AlarmClock::exportStorage() @@ -182,15 +196,11 @@ AlarmStorageSnapshot AlarmClock::exportStorage() AlarmStorageSnapshot snapshot; snapshot.count = 0; - _alarms.forEach([&](Alarm* alarm, int) { - if (alarm == nullptr || snapshot.count >= AlarmStorageSnapshot::maxAlarmCount) { - return; - } - + forEachAlarm([&](int, Alarm& alarm) { auto& out = snapshot.alarms[snapshot.count++]; - out.hour = alarm->time().hour; - out.minute = alarm->time().minute; - out.enabled = alarm->enabled() ? 1 : 0; + out.hour = alarm.time().hour; + out.minute = alarm.time().minute; + out.enabled = alarm.enabled() ? 1 : 0; out.reserved = 0; }); @@ -199,7 +209,7 @@ AlarmStorageSnapshot AlarmClock::exportStorage() bool AlarmClock::importStorage(const AlarmStorageSnapshot& snapshot, bool persistAfterImport) { - _alarms.clear(); + clearAlarms(); mclog::tagInfo(_tag, "import storage, count: {}, persist: {}", snapshot.count, persistAfterImport); @@ -216,7 +226,7 @@ bool AlarmClock::importStorage(const AlarmStorageSnapshot& snapshot, bool persis .hour = entry.hour, .minute = entry.minute, }; - _alarms.create(std::make_unique(time, entry.enabled != 0)); + insertAlarm(time, entry.enabled != 0); } if (!persistAfterImport) { diff --git a/main/apps/app_alarm_clock/model/alarm_clock.h b/main/apps/app_alarm_clock/model/alarm_clock.h index a94a6582..69d9d0b3 100644 --- a/main/apps/app_alarm_clock/model/alarm_clock.h +++ b/main/apps/app_alarm_clock/model/alarm_clock.h @@ -4,12 +4,12 @@ * SPDX-License-Identifier: MIT */ #pragma once +#include +#include #include #include -#include -#include -#include -#include +#include +#include namespace model { @@ -22,12 +22,7 @@ class AlarmClock { bool isValid() const; }; - struct AlarmTriggeredEvent { - int alarmId = -1; - Time24 time; - }; - - class Alarm : public uitk::Poolable { + class Alarm { public: Alarm(const Time24& time, bool enabled); @@ -66,16 +61,22 @@ class AlarmClock { AlarmStorageSnapshot exportStorage(); bool importStorage(const AlarmStorageSnapshot& snapshot, bool persistAfterImport = false); - uitk::Signal& onTriggered() + void setTriggerCallback(std::function callback) { - return _on_triggered; + _on_triggered = std::move(callback); } private: + void clearAlarms(); + int insertAlarm(const Time24& time, bool enabled); bool persist(); - uitk::ObjectPool _alarms; - uitk::Signal _on_triggered; + static constexpr auto Capacity = AlarmStorageSnapshot::maxAlarmCount; + std::array, Capacity> _alarms{}; + std::array _free_ids{}; + std::size_t _slot_count = 0; + std::size_t _free_count = 0; + std::function _on_triggered; uint32_t _last_update_ms = 0; }; diff --git a/main/apps/app_alarm_clock/view/add_alarm.cpp b/main/apps/app_alarm_clock/view/add_alarm.cpp index fb4eff72..b0320f8b 100644 --- a/main/apps/app_alarm_clock/view/add_alarm.cpp +++ b/main/apps/app_alarm_clock/view/add_alarm.cpp @@ -4,128 +4,115 @@ * SPDX-License-Identifier: MIT */ #include "view.h" -#include -#include - -using namespace view; -using namespace uitk::lvgl_cpp; +#include +#include +#include +#include +namespace view { namespace { -constexpr int _panel_width = 466; -constexpr int _panel_height = 466; -constexpr int _selector_width = 160; -constexpr int _selector_height = 200; -constexpr int _ok_width = 374; -constexpr int _ok_height = 130; +using namespace mcui::core; +using namespace mcui::kit; +using namespace mcui::render; -constexpr uint32_t _bg_color = 0x000000; -constexpr uint32_t _label_color = 0xFFFFFF; -constexpr uint32_t _selector_color = 0x343434; -constexpr uint32_t _selector_selected_color = 0x696969; -constexpr uint32_t _ok_color = 0x4AD78C; -constexpr uint32_t _ok_text_color = 0x0F5831; +constexpr int16_t RollerHeight = 164; +constexpr int16_t RollerWidth = 160; +constexpr int16_t RollerTop = 138; +constexpr int16_t ConfirmTop = 343; +constexpr int16_t ConfirmWidth = 374; +constexpr int16_t ConfirmHeight = 130; +constexpr auto White = Color::FromHex(0xffffff); +constexpr std::array Counts{24, 60}; +constexpr auto NumberLabels = [] { + std::array, 60> labels{}; + for (int i = 0; i < int(labels.size()); ++i) labels[i] = {char('0' + i / 10), char('0' + i % 10), 0}; + return labels; +}(); +constexpr auto Numbers = [] { + std::array options{}; + for (size_t i = 0; i < options.size(); ++i) options[i] = NumberLabels[i].data(); + return options; +}(); -std::string build_number_options(int begin, int end) +int wrapRow(int64_t index, int count) { - std::string options; - for (int value = begin; value <= end; ++value) { - char buffer[4] = {}; - std::snprintf(buffer, sizeof(buffer), "%02d", value); - if (!options.empty()) { - options.push_back('\n'); - } - options += buffer; - } - return options; + return int((index % count + count) % count); } -} // namespace - -void AlarmAddView::init(lv_obj_t* parent) +void centerText(Canvas& canvas, uint32_t id, int x, int y, const char* text, const BitmapFont& font) { - _panel = std::make_unique(parent); - _panel->align(LV_ALIGN_CENTER, 0, 0); - _panel->setSize(_panel_width, _panel_height); - _panel->setRadius(0); - _panel->setBorderWidth(0); - _panel->setPaddingAll(0); - _panel->setBgColor(lv_color_hex(_bg_color)); - _panel->setBgOpa(LV_OPA_COVER); - _panel->removeFlag(LV_OBJ_FLAG_SCROLLABLE); + const auto size = font.MeasureText(text, std::strlen(text)); + canvas.Text(id, {int16_t(x - size.width / 2), int16_t(y)}, text, font, White); +} - _title_label = std::make_unique