cmake_minimum_required(VERSION 3.22)

#
# This file is generated only once,
# and is not re-generated if converter is called multiple times.
#
# User is free to modify the file as much as necessary
#

# Setup compiler settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)


# Define the build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

# Set the project name
set(CMAKE_PROJECT_NAME polarimeter)


# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# Core project settings
project(${CMAKE_PROJECT_NAME})
message("Build type: " ${CMAKE_BUILD_TYPE})

# Enable CMake support for ASM and C languages
enable_language(C ASM)

# Create an executable object type
add_executable(${CMAKE_PROJECT_NAME})

# Add STM32CubeMX generated sources
add_subdirectory(cmake/stm32cubemx)

# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined library search paths
)

# Specify entry symbol as entry
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE

)

# Floating point hardware support (e.g. Cortex-M4F/M7F), add compile and link options
# STM32F103ZET6 is Cortex-M3 core, no hardware FPU, should use soft float
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
    -mfloat-abi=softfp
    -u _printf_float
)
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
    -mfloat-abi=softfp
    -u _printf_float
)

# ETK module configuration options - based on etk CMakeLists.txt option naming
# Algorithm modules
# set(ETK_ALGORITHM ON CACHE BOOL "Enable algorithm module")
# set(ALG_DSP_UTILS ON CACHE BOOL "Enable dsp_utils submodule")
# set(ALG_FILTER ON CACHE BOOL "Enable filter submodule")
# set(ALG_GROOVE_TRACKER OFF CACHE BOOL "Enable groove_tracker submodule")
# set(ALG_MATH ON CACHE BOOL "Enable math submodule")
# set(ALG_PID ON CACHE BOOL "Enable pid submodule")
# set(ALG_TREND OFF CACHE BOOL "Enable trend submodule")

# # DSP Utils submodules
# set(ALG_FFT OFF CACHE BOOL "Enable FFT submodule")
# set(ALG_FREQ_ANALYZER OFF CACHE BOOL "Enable frequency analyzer submodule")
# set(ALG_GOERTZEL ON CACHE BOOL "Enable Goertzel submodule")
# set(ALG_INTERP OFF CACHE BOOL "Enable interpolation submodule")
# set(ALG_WAVES OFF CACHE BOOL "Enable waves submodule")
# set(ALG_WINDOWS OFF CACHE BOOL "Enable windows submodule")
# set(ETK_ZERO_CROSS ON CACHE BOOL "Enable zero_cross submodule")

# # Filter submodules
# set(ALG_BUTTERWORTH_FILTER OFF CACHE BOOL "Enable Butterworth filter submodule")
# set(ALG_CHEBYSHEV_FILTER OFF CACHE BOOL "Enable Chebyshev filter submodule")
# set(ALG_EMA_FILTER ON CACHE BOOL "Enable EMA filter submodule")
# set(ALG_FIR_FILTER OFF CACHE BOOL "Enable FIR filter submodule")
# set(ALG_GAUSSIAN_FILTER OFF CACHE BOOL "Enable Gaussian filter submodule")
# set(ALG_HIGH_PASS_FILTER OFF CACHE BOOL "Enable high pass filter submodule")
# set(ALG_IIR_FILTER OFF CACHE BOOL "Enable IIR filter submodule")
# set(ALG_KALMAN_FILTER OFF CACHE BOOL "Enable Kalman filter submodule")
# set(ALG_MEDIAN_FILTER OFF CACHE BOOL "Enable median filter submodule")
# set(ALG_SAVITZKY_FILTER OFF CACHE BOOL "Enable Savitzky filter submodule")
# set(ALG_SLIDING_FILTER OFF CACHE BOOL "Enable sliding filter submodule")

# # Math submodules
# set(ALG_WAVEFORM_ANALYSIS OFF CACHE BOOL "Enable waveform analysis submodule")
# set(ALG_LINEAR_SOLVER OFF CACHE BOOL "Enable linear solver submodule")
# set(ALG_NEWTON OFF CACHE BOOL "Enable Newton method submodule")
# set(ALG_DIFF OFF CACHE BOOL "Enable differentiation submodule")
# set(ALG_SLOPE ON CACHE BOOL "Enable slope calculation submodule")

# # Drivers modules
# set(ETK_DRIVERS ON CACHE BOOL "Enable drivers module")
# set(DRV_AD779X OFF CACHE BOOL "Enable ad779x driver submodule")
# set(DRV_ENCODER ON CACHE BOOL "Enable encoder driver submodule")

# # Utils modules
# set(ETK_UTILS ON CACHE BOOL "Enable utils module")
# set(UTIL_BYTE_CONV ON CACHE BOOL "Enable byte_conversion submodule")
# set(UTIL_FLEX_QUEUE OFF CACHE BOOL "Enable flex_queue submodule")
# set(UTIL_RINGBUFFER ON CACHE BOOL "Enable ringbuffer submodule")

# # System modules
# set(ETK_OS_ADAPT OFF CACHE BOOL "Enable os_adapt module")
# set(ETK_LOG_FRAMEWORK OFF CACHE BOOL "Enable log framework module")
# set(UTIL_LOG_SIMPLE ON CACHE BOOL "Enable logging module")

# # Communication modules
# set(ETK_COMMUNICATION OFF CACHE BOOL "Enable communication module")

# # Thirdparty modules
# set(THIRDPARTY ON CACHE BOOL "Enable thirdparty module")
# set(NANOMODBUS ON CACHE BOOL "Enable nanomodbus submodule")

# # Common modules (always enabled as base dependency)
# set(ETK_COMMON ON CACHE BOOL "Enable common module")
# set(ETK_ROOT_CMAKE ON CACHE BOOL "Build all modules into a single etk library")
# set(SYSTEM_TYPE 2 CACHE STRING "System type: 0(SYS_NONE_OS), 1(SYS_FREERTOS), 2(SYS_RTTHREAD), 3(SYS_LINUX), 4(SYS_POSIX)")

# RT-Thread specific configuration for ETK
#set(USE_EXTERNAL_RTTHREAD ON CACHE BOOL "Use external RT-Thread library")
#set(FETCH_RTTHREAD OFF CACHE BOOL "Download RT-Thread via FetchContent")
# Set RTT_ROOT to tell ETK where to find RT-Thread source code
#set(RTT_ROOT "${CMAKE_SOURCE_DIR}/Middlewares/Third_Party/RealThread_RTOS_RT-Thread" CACHE PATH "RT-Thread source root directory for ETK")
# Set RTT_CONFIG_DIR for rtconfig.h location
#set(RTT_CONFIG_DIR "${CMAKE_SOURCE_DIR}/RT-Thread" CACHE PATH "RT-Thread config directory")


# if(NOT EXISTS "${RTT_CONFIG_DIR}/rtconfig.h")
#     message(WARNING "RT-Thread config file rtconfig.h not found at: ${RTT_CONFIG_DIR}")
# endif()

# Additional configuration options for specific features
#set(ENABLE_ARM_CMSIS_DSP OFF CACHE BOOL "Enable ARM CMSIS-DSP library")


# Set RT-Thread include paths - include both source and config directories
# set(RTTHREAD_INCLUDE_DIRS
#     ${RTT_ROOT}/include
#     ${RTT_ROOT}/components/finsh
#     ${RTT_ROOT}/components/finsh/inc
#     ${RTT_ROOT}/components/drivers/include
#     ${RTT_CONFIG_DIR}  # Add rtconfig.h directory
# )

# # Set RTT_ROOT as environment variable for etk os_adapt module to find
# if(DEFINED RTT_ROOT)
#     set(ENV{RTT_ROOT} "${RTT_ROOT}")
#     message(STATUS "RTT_ROOT environment variable set to: $ENV{RTT_ROOT}")
# else()
#     message(FATAL_ERROR "RTT_ROOT is not defined. Please set RTT_ROOT path.")
# endif()

# # Also set RTT_CONFIG_DIR as environment variable for rtconfig.h
# if(DEFINED RTT_CONFIG_DIR)
#     set(ENV{RTT_CONFIG_DIR} "${RTT_CONFIG_DIR}")
#     message(STATUS "RTT_CONFIG_DIR environment variable set to: $ENV{RTT_CONFIG_DIR}")
# endif()

# add_subdirectory(etk)

# # Link ETK library to main project to get access to all ETK headers
# if(ETK_ROOT_CMAKE)
#     target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE etk)
#     # Ensure etk library can get SYSTEM_TYPE value
#     target_compile_definitions(etk PUBLIC SYSTEM_TYPE=${SYSTEM_TYPE})
# endif()

# Add sources to executable
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user sources here
    ${CMAKE_SOURCE_DIR}/User/board/pm_board.c
    ${CMAKE_SOURCE_DIR}/User/board/bsp_encoder.c
    ${CMAKE_SOURCE_DIR}/User/driver/at24cx/at24cx.c
    ${CMAKE_SOURCE_DIR}/User/driver/ad779x/ad7793.c
    ${CMAKE_SOURCE_DIR}/segger_rtt/RTT/SEGGER_RTT_printf.c
    ${CMAKE_SOURCE_DIR}/segger_rtt/RTT/SEGGER_RTT.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h0c_comm.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/sv_common.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h00_motor_args.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h03_di_do.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h02_basic_control.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h05_pos_control.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h06_speed_control.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h0d_af.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h31_comm_related_var.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/sv_device.c
    ${CMAKE_SOURCE_DIR}/User/driver/pt_res/pt100x.c

    ${CMAKE_SOURCE_DIR}/User/app/pm_common.c
    ${CMAKE_SOURCE_DIR}/User/app/storage.c
    ${CMAKE_SOURCE_DIR}/User/app/tec_control.c
    ${CMAKE_SOURCE_DIR}/User/app/servo.c
    ${CMAKE_SOURCE_DIR}/User/app/pm_device.c
    ${CMAKE_SOURCE_DIR}/User/app/data_process.c
    ${CMAKE_SOURCE_DIR}/User/app/data_sampling.c
    ${CMAKE_SOURCE_DIR}/User/app/mb_hmi/mb_command.c
    ${CMAKE_SOURCE_DIR}/User/app/mb_hmi/mb_interface.c
    ${CMAKE_SOURCE_DIR}/User/app/mb_hmi/hmi_server.c
    ${CMAKE_SOURCE_DIR}/User/app/pm_meas.c
    ${CMAKE_SOURCE_DIR}/User/app/pm_params.c
    ${CMAKE_SOURCE_DIR}/nanoMODBUS/nanomodbus.c
    ${CMAKE_SOURCE_DIR}/etk/src/drivers/encoder/src/et_encoder.c
    ${CMAKE_SOURCE_DIR}/etk/src/drivers/encoder/src/et_encoder_utils.c
    # ETK logging
    ${CMAKE_SOURCE_DIR}/etk/src/logging/et_log.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/filter/src/et_ema_filter.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/pid/src/etk_pid.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/dsp_utils/src/et_goertzel.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/math/src/et_zero_cross.c
    ${CMAKE_SOURCE_DIR}/etk/src/utils/byte_conv/src/etk_byte_conv.c
    ${CMAKE_SOURCE_DIR}/etk/src/utils/ringbuffer/src/etk_ringbuffer.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/trend/src/et_trend.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/math/src/etk_slope.c
)


# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined include paths
    # Note: ETK module include paths are now automatically included via the etk library
    ${CMAKE_SOURCE_DIR}/User/board
    ${CMAKE_SOURCE_DIR}/segger_rtt/Config/
    ${CMAKE_SOURCE_DIR}/segger_rtt/RTT/
    ${CMAKE_SOURCE_DIR}/User/driver/at24cx/
    ${CMAKE_SOURCE_DIR}/User/driver/soft_spi/
    ${CMAKE_SOURCE_DIR}/User/driver/ad779x/
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/
    ${CMAKE_SOURCE_DIR}/User/driver/pt_res/
    ${CMAKE_SOURCE_DIR}/User/
    ${CMAKE_SOURCE_DIR}/User/app/
    ${CMAKE_SOURCE_DIR}/User/app/mb_hmi/
    # Add nanoMODBUS include path explicitly
    ${CMAKE_SOURCE_DIR}/nanoMODBUS
    ${CMAKE_SOURCE_DIR}/etk/src/logging
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/math/include
    ${CMAKE_SOURCE_DIR}/etk/src/drivers/encoder/include
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/filter/include
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/pid/include
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/dsp_utils/include
    ${CMAKE_SOURCE_DIR}/etk/src/common/include
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/math/include
    ${CMAKE_SOURCE_DIR}/etk/src/utils/byte_conv/include
    ${CMAKE_SOURCE_DIR}/etk/src/utils/ringbuffer/include
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/trend/include
)

# Add project symbols (macros)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined symbols
    ARM_MATH
    SYSTEM_TYPE=2
    ET_SLIDING_FILTER_SIZE=4
    NMBS_DEBUG
    USING_RTT_AS_CONSOLE
    RT_DEBUG
)

# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
    stm32cubemx
)