From 55bde70b9651b411ac0135bd4704f5b6972ea799 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 10 Jan 2024 20:32:01 +1100 Subject: [PATCH] add accurate esp and samd versions + expose this info to lua --- CMakeLists.txt | 2 +- src/drivers/include/samd.hpp | 5 +++ src/drivers/samd.cpp | 10 ++++-- src/lua/CMakeLists.txt | 6 ++-- src/lua/bridge.cpp | 2 ++ src/lua/include/lua_version.hpp | 15 +++++++++ src/lua/lua_version.cpp | 51 ++++++++++++++++++++++++++++++ src/ui/CMakeLists.txt | 2 +- src/ui/include/screen_settings.hpp | 3 +- src/ui/screen_settings.cpp | 10 ++++-- src/ui/ui_fsm.cpp | 3 +- tools/cmake/common.cmake | 2 ++ 12 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 src/lua/include/lua_version.hpp create mode 100644 src/lua/lua_version.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 82f1ffe7..89ebf8aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,8 @@ # Copyright 2023 jacqueline # # SPDX-License-Identifier: GPL-3.0-only - cmake_minimum_required(VERSION 3.16) + include($ENV{PROJ_PATH}/tools/cmake/common.cmake) set(SDKCONFIG_DEFAULTS "sdkconfig.common") diff --git a/src/drivers/include/samd.hpp b/src/drivers/include/samd.hpp index f25f9575..ac265950 100644 --- a/src/drivers/include/samd.hpp +++ b/src/drivers/include/samd.hpp @@ -6,7 +6,9 @@ #pragma once +#include #include +#include #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" @@ -20,6 +22,8 @@ class Samd { Samd(); ~Samd(); + auto Version() -> std::string; + enum class ChargeStatus { // There is no battery plugged into the device. kNoBattery, @@ -63,6 +67,7 @@ class Samd { Samd& operator=(const Samd&) = delete; private: + uint8_t version_; std::optional charge_status_; UsbStatus usb_status_; }; diff --git a/src/drivers/samd.cpp b/src/drivers/samd.cpp index cf3b9c18..f12a18de 100644 --- a/src/drivers/samd.cpp +++ b/src/drivers/samd.cpp @@ -8,6 +8,7 @@ #include #include +#include #include "esp_err.h" #include "esp_log.h" @@ -37,23 +38,26 @@ Samd::Samd() { // Being able to interface with the SAMD properly is critical. To ensure we // will be able to, we begin by checking the I2C protocol version is // compatible, and throw if it's not. - uint8_t raw_res = 0; I2CTransaction transaction; transaction.start() .write_addr(kAddress, I2C_MASTER_WRITE) .write_ack(Registers::kSamdFirmwareVersion) .start() .write_addr(kAddress, I2C_MASTER_READ) - .read(&raw_res, I2C_MASTER_NACK) + .read(&version_, I2C_MASTER_NACK) .stop(); ESP_ERROR_CHECK(transaction.Execute(1)); - ESP_LOGI(kTag, "samd firmware rev: %u", raw_res); + ESP_LOGI(kTag, "samd firmware rev: %u", version_); UpdateChargeStatus(); UpdateUsbStatus(); } Samd::~Samd() {} +auto Samd::Version() -> std::string { + return std::to_string(version_); +} + auto Samd::GetChargeStatus() -> std::optional { return charge_status_; } diff --git a/src/lua/CMakeLists.txt b/src/lua/CMakeLists.txt index 5c67a57e..7174757a 100644 --- a/src/lua/CMakeLists.txt +++ b/src/lua/CMakeLists.txt @@ -3,8 +3,10 @@ # SPDX-License-Identifier: GPL-3.0-only idf_component_register( - SRCS "lua_thread.cpp" "bridge.cpp" "property.cpp" "lua_database.cpp" "lua_queue.cpp" + SRCS "lua_thread.cpp" "bridge.cpp" "property.cpp" "lua_database.cpp" + "lua_queue.cpp" "lua_version.cpp" INCLUDE_DIRS "include" REQUIRES "drivers" "lvgl" "tinyfsm" "events" "system_fsm" "database" - "esp_timer" "battery" "esp-idf-lua" "luavgl" "lua-linenoise" "lua-term") + "esp_timer" "battery" "esp-idf-lua" "luavgl" "lua-linenoise" "lua-term" + "esp_app_format") target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) diff --git a/src/lua/bridge.cpp b/src/lua/bridge.cpp index 1063cfbf..e7344e0e 100644 --- a/src/lua/bridge.cpp +++ b/src/lua/bridge.cpp @@ -18,6 +18,7 @@ #include "lua.hpp" #include "lua_database.hpp" #include "lua_queue.hpp" +#include "lua_version.hpp" #include "lvgl.h" #include "event_queue.hpp" @@ -74,6 +75,7 @@ Bridge::Bridge(system_fsm::ServiceLocator& services, lua_State& s) RegisterDatabaseModule(&s); RegisterQueueModule(&s); + RegisterVersionModule(&s); } static auto new_property_module(lua_State* state) -> int { diff --git a/src/lua/include/lua_version.hpp b/src/lua/include/lua_version.hpp new file mode 100644 index 00000000..4ba4be94 --- /dev/null +++ b/src/lua/include/lua_version.hpp @@ -0,0 +1,15 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include "lua.hpp" + +namespace lua { + +auto RegisterVersionModule(lua_State*) -> void; + +} // namespace lua diff --git a/src/lua/lua_version.cpp b/src/lua/lua_version.cpp new file mode 100644 index 00000000..ac72d3ae --- /dev/null +++ b/src/lua/lua_version.cpp @@ -0,0 +1,51 @@ + +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include "lua_version.hpp" + +#include + +#include "bridge.hpp" +#include "lua.hpp" + +#include "esp_app_desc.h" +#include "esp_log.h" +#include "lauxlib.h" +#include "lua.h" +#include "lua_thread.hpp" + +namespace lua { + +static auto esp(lua_State* L) -> int { + auto desc = esp_app_get_description(); + lua_pushstring(L, desc->version); + return 1; +} + +static auto samd(lua_State* L) -> int { + Bridge* instance = Bridge::Get(L); + auto& samd = instance->services().samd(); + auto version = samd.Version(); + lua_pushlstring(L, version.data(), version.size()); + return 1; +} + +static const struct luaL_Reg kVersionFuncs[] = {{"esp", esp}, + {"samd", samd}, + {NULL, NULL}}; + +static auto lua_version(lua_State* L) -> int { + luaL_newlib(L, kVersionFuncs); + return 1; +} + +auto RegisterVersionModule(lua_State* L) -> void { + luaL_requiref(L, "version", lua_version, true); + lua_pop(L, 1); +} + +} // namespace lua diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index a869053d..0d9de5a4 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -9,5 +9,5 @@ idf_component_register( "screen_lua.cpp" "splash.c" "font_fusion_12.c" "font_fusion_10.c" INCLUDE_DIRS "include" - REQUIRES "drivers" "lvgl" "tinyfsm" "events" "system_fsm" "database" "esp_timer" "battery" "bindey" "lua" "luavgl") + REQUIRES "drivers" "lvgl" "tinyfsm" "events" "system_fsm" "database" "esp_timer" "battery" "bindey" "lua" "luavgl" "esp_app_format") target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) diff --git a/src/ui/include/screen_settings.hpp b/src/ui/include/screen_settings.hpp index 033cb7fa..7402f9f9 100644 --- a/src/ui/include/screen_settings.hpp +++ b/src/ui/include/screen_settings.hpp @@ -20,6 +20,7 @@ #include "model_top_bar.hpp" #include "nvs.hpp" +#include "samd.hpp" #include "screen.hpp" namespace ui { @@ -103,7 +104,7 @@ class Storage : public MenuScreen { class FirmwareUpdate : public MenuScreen { public: - FirmwareUpdate(models::TopBar&); + FirmwareUpdate(models::TopBar&, drivers::Samd&); }; class About : public MenuScreen { diff --git a/src/ui/screen_settings.cpp b/src/ui/screen_settings.cpp index a3a24eeb..3f4c2c46 100644 --- a/src/ui/screen_settings.cpp +++ b/src/ui/screen_settings.cpp @@ -15,6 +15,7 @@ #include "core/lv_obj.h" #include "core/lv_obj_tree.h" #include "display.hpp" +#include "esp_app_desc.h" #include "esp_log.h" #include "core/lv_group.h" @@ -32,6 +33,7 @@ #include "misc/lv_area.h" #include "model_top_bar.hpp" #include "nvs.hpp" +#include "samd.hpp" #include "screen.hpp" #include "themes.hpp" #include "ui_events.hpp" @@ -529,12 +531,13 @@ Storage::Storage(models::TopBar& bar) : MenuScreen(bar, "Storage") { }); } -FirmwareUpdate::FirmwareUpdate(models::TopBar& bar) +FirmwareUpdate::FirmwareUpdate(models::TopBar& bar, drivers::Samd& samd) : MenuScreen(bar, "Firmware Update") { lv_obj_set_flex_align(content_, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); - label_pair(content_, "SAMD21 FW:", "vIDKLOL"); + auto samd_ver = samd.Version(); + label_pair(content_, "SAMD21 FW:", {samd_ver.data(), samd_ver.size()}); lv_obj_t* spacer = lv_obj_create(content_); lv_obj_set_size(spacer, 1, 4); @@ -549,7 +552,8 @@ FirmwareUpdate::FirmwareUpdate(models::TopBar& bar) spacer = lv_obj_create(content_); lv_obj_set_size(spacer, 1, 8); - label_pair(content_, "ESP32 FW:", "vIDKLOL"); + auto desc = esp_app_get_description(); + label_pair(content_, "ESP32 FW:", desc->version); spacer = lv_obj_create(content_); lv_obj_set_size(spacer, 1, 4); diff --git a/src/ui/ui_fsm.cpp b/src/ui/ui_fsm.cpp index 740383a4..75327a58 100644 --- a/src/ui/ui_fsm.cpp +++ b/src/ui/ui_fsm.cpp @@ -432,7 +432,8 @@ void Browse::react(const internal::ShowSettingsPage& ev) { screen.reset(new screens::Storage(sTopBarModel)); break; case internal::ShowSettingsPage::Page::kFirmwareUpdate: - screen.reset(new screens::FirmwareUpdate(sTopBarModel)); + screen.reset( + new screens::FirmwareUpdate(sTopBarModel, sServices->samd())); break; case internal::ShowSettingsPage::Page::kAbout: screen.reset(new screens::About(sTopBarModel)); diff --git a/tools/cmake/common.cmake b/tools/cmake/common.cmake index 545124b7..da4db9c1 100644 --- a/tools/cmake/common.cmake +++ b/tools/cmake/common.cmake @@ -5,6 +5,8 @@ # For more information about build system see # https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html +set(PROJECT_VER "0.1.0") + # Build only the subset of components that we actually depend on. set(COMPONENTS "")