From 74eb8308708efd2ae6f6765fda1d0cfbcd650702 Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Sat, 24 May 2025 17:27:37 +0200 Subject: [PATCH] Buildscript and kernel improvements (#280) - Implemented `constexpr long int getMicros()` in `Kernel.h` - Changed `secondsToTicks()`, `minutesToTicks()` and `bool isIsr()` to `constexpr` - Added more relevant build info --- Buildscripts/board.cmake | 13 +++++++-- Buildscripts/colors.cmake | 14 ++++++++++ Buildscripts/logo.cmake | 28 +++++++++---------- CMakeLists.txt | 20 +++++++++---- TactilityCore/CMakeLists.txt | 2 +- .../Include/Tactility/kernel/Kernel.h | 24 ++++++++++++---- 6 files changed, 73 insertions(+), 28 deletions(-) create mode 100644 Buildscripts/colors.cmake diff --git a/Buildscripts/board.cmake b/Buildscripts/board.cmake index 4e7d0c64..1dc297da 100644 --- a/Buildscripts/board.cmake +++ b/Buildscripts/board.cmake @@ -1,3 +1,12 @@ +if (NOT WIN32) + string(ASCII 27 Esc) + set(ColorReset "${Esc}[m") + set(Cyan "${Esc}[36m") +else () + set(ColorReset "") + set(Cyan "") +endif () + function(INIT_TACTILITY_GLOBALS SDKCONFIG_FILE) get_filename_component(SDKCONFIG_FILE_ABS ${SDKCONFIG_FILE} ABSOLUTE) # Find the board identifier in the sdkconfig file @@ -10,7 +19,7 @@ function(INIT_TACTILITY_GLOBALS SDKCONFIG_FILE) set(id_length 0) math(EXPR id_length "${sdkconfig_board_id_length} - 21") string(SUBSTRING ${sdkconfig_board_id} 20 ${id_length} board_id) - message("Building board ${board_id}") + message("Building board: ${Cyan}${board_id}${ColorReset}") if (board_id STREQUAL "cyd-2432s024c") set(TACTILITY_BOARD_PROJECT CYD-2432S024C) @@ -53,7 +62,7 @@ function(INIT_TACTILITY_GLOBALS SDKCONFIG_FILE) if (TACTILITY_BOARD_PROJECT STREQUAL "") message(FATAL_ERROR "No subproject mapped to \"${TACTILITY_BOARD_ID}\" in root Buildscripts/board.cmake") else () - message("Board project: Boards/${TACTILITY_BOARD_PROJECT}") + message("Board project: ${Cyan}Boards/${TACTILITY_BOARD_PROJECT}${ColorReset}\n") set_property(GLOBAL PROPERTY TACTILITY_BOARD_PROJECT ${TACTILITY_BOARD_PROJECT}) set_property(GLOBAL PROPERTY TACTILITY_BOARD_ID ${board_id}) endif () diff --git a/Buildscripts/colors.cmake b/Buildscripts/colors.cmake new file mode 100644 index 00000000..584f2edc --- /dev/null +++ b/Buildscripts/colors.cmake @@ -0,0 +1,14 @@ +if (NOT WIN32) + string(ASCII 27 Esc) + set(ColorReset "${Esc}[m") + set(Cyan "${Esc}[36m") + set(Grey "${Esc}[37m") + set(LightPurple "${Esc}[1;35m") + set(White "${Esc}[1;37m") +else () + set(ColorReset "") + set(Cyan "") + set(Grey "") + set(LightPurple "") + set(White "") +endif () diff --git a/Buildscripts/logo.cmake b/Buildscripts/logo.cmake index 27d34292..5b257f03 100644 --- a/Buildscripts/logo.cmake +++ b/Buildscripts/logo.cmake @@ -4,24 +4,24 @@ file(READ ${VERSION_TEXT_FILE} TACTILITY_VERSION) if (DEFINED ENV{ESP_IDF_VERSION}) set(TACTILITY_TARGET " @ ESP-IDF") -else() +else () set(TACTILITY_TARGET " @ Simulator") -endif() +endif () -if(NOT WIN32) +if (NOT WIN32) string(ASCII 27 Esc) - set(ColourReset "${Esc}[m") - set(Cyan "${Esc}[36m") - set(Grey "${Esc}[37m") + set(ColorReset "${Esc}[m") + set(Cyan "${Esc}[36m") + set(Grey "${Esc}[37m") set(LightPurple "${Esc}[1;35m") - set(White "${Esc}[1;37m") -else() - set(ColourReset "") - set(Cyan "") - set(Grey "") + set(White "${Esc}[1;37m") +else () + set(ColorReset "") + set(Cyan "") + set(Grey "") set(LightPurple "") - set(White "") -endif() + set(White "") +endif () # Some terminals (e.g. GitHub Actions) reset colour for every in a multiline message(), # so we add the colour to each line instead of assuming it would automatically be re-used. @@ -41,4 +41,4 @@ message("\n\n\ ${Cyan}@@@\n\ ${Cyan}@@@\n\ ${Cyan}@@@\n\ - ${Cyan}@@\n\n${ColourReset}") + ${Cyan}@@\n\n${ColorReset}") diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d310e4b..d7907658 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,14 +8,24 @@ set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_CXX_COMPILER_TARGET}") include("Buildscripts/logo.cmake") +if (NOT WIN32) + string(ASCII 27 Esc) + set(ColorReset "${Esc}[m") + set(Cyan "${Esc}[36m") +else () + set(ColorReset "") + set(Cyan "") +endif () + file(READ version.txt TACTILITY_VERSION) add_compile_definitions(TT_VERSION="${TACTILITY_VERSION}") if (DEFINED ENV{ESP_IDF_VERSION}) - message("Building with ESP-IDF v$ENV{ESP_IDF_VERSION}") + message("Building with ESP-IDF ${Cyan}v$ENV{ESP_IDF_VERSION}${ColorReset}") include($ENV{IDF_PATH}/tools/cmake/project.cmake) include("Buildscripts/board.cmake") + init_tactility_globals("sdkconfig") get_property(TACTILITY_BOARD_PROJECT GLOBAL PROPERTY TACTILITY_BOARD_PROJECT) @@ -45,11 +55,11 @@ if (DEFINED ENV{ESP_IDF_VERSION}) add_compile_definitions(LV_CONF_PATH="${LVGL_CONFIG_FULL_PATH}/lv_conf_kconfig.h") idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=esp_panic_handler" APPEND) idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=esp_log_write" APPEND) -else() +else () message("Building for sim target") add_compile_definitions(CONFIG_TT_BOARD_ID="simulator") add_compile_definitions(CONFIG_TT_BOARD_NAME="Simulator") -endif() +endif () project(Tactility) @@ -58,7 +68,7 @@ if (NOT DEFINED ENV{ESP_IDF_VERSION}) add_subdirectory(Tactility) add_subdirectory(TactilityCore) add_subdirectory(Boards/Simulator) -endif() +endif () if (NOT DEFINED ENV{ESP_IDF_VERSION}) # FreeRTOS @@ -94,4 +104,4 @@ if (NOT DEFINED ENV{ESP_IDF_VERSION}) target_compile_definitions(lvgl PUBLIC "-DLV_CONF_PATH=\"${PROJECT_SOURCE_DIR}/Libraries/lvgl_conf/lv_conf_simulator.h\"") target_link_libraries(lvgl PRIVATE SDL2-static) -endif() +endif () diff --git a/TactilityCore/CMakeLists.txt b/TactilityCore/CMakeLists.txt index fad358b3..a55e7dab 100644 --- a/TactilityCore/CMakeLists.txt +++ b/TactilityCore/CMakeLists.txt @@ -9,7 +9,7 @@ if (DEFINED ENV{ESP_IDF_VERSION}) idf_component_register( SRCS ${SOURCE_FILES} INCLUDE_DIRS "Include/" - REQUIRES mbedtls nvs_flash esp_rom + REQUIRES mbedtls nvs_flash esp_rom esp_timer ) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") diff --git a/TactilityCore/Include/Tactility/kernel/Kernel.h b/TactilityCore/Include/Tactility/kernel/Kernel.h index bf4467f8..3f3488b8 100644 --- a/TactilityCore/Include/Tactility/kernel/Kernel.h +++ b/TactilityCore/Include/Tactility/kernel/Kernel.h @@ -2,8 +2,10 @@ #ifdef ESP_PLATFORM #include "freertos/FreeRTOS.h" +#include #else #include "FreeRTOS.h" +#include #endif namespace tt::kernel { @@ -16,9 +18,9 @@ typedef enum { /** Return true when called from an Interrupt Service Routine (~IRQ mode) */ #ifdef ESP_PLATFORM -inline constexpr bool isIsr() { return (xPortInIsrContext() == pdTRUE); } +constexpr bool isIsr() { return (xPortInIsrContext() == pdTRUE); } #else -inline constexpr bool isIsr() { return false; } +constexpr bool isIsr() { return false; } #endif /** Check if kernel is running @@ -52,7 +54,17 @@ uint32_t getTickFrequency(); TickType_t getTicks(); -inline size_t getMillis() { return getTicks() / portTICK_PERIOD_MS; } +constexpr size_t getMillis() { return getTicks() / portTICK_PERIOD_MS; } + +constexpr long int getMicros() { +#ifdef ESP_PLATFORM + return static_cast(esp_timer_get_time()); +#else + timeval tv; + gettimeofday(&tv, nullptr); + return 1000000 * tv.tv_sec + tv.tv_usec; +#endif +} /** Delay execution * @warning don't call from ISR context @@ -68,11 +80,11 @@ void delayTicks(TickType_t ticks); */ bool delayUntilTick(TickType_t tick); -constexpr inline TickType_t secondsToTicks(uint32_t seconds) { - return (TickType_t)seconds * 1000U / portTICK_PERIOD_MS; +constexpr TickType_t secondsToTicks(uint32_t seconds) { + return static_cast(seconds) * 1000U / portTICK_PERIOD_MS; } -constexpr inline TickType_t minutesToTicks(uint32_t minutes) { +constexpr TickType_t minutesToTicks(uint32_t minutes) { return secondsToTicks(minutes * 60U); }