cmake_minimum_required(VERSION 3.12)

set(CMAKE_C_STANDARD 23)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_BUILD_PARALLEL_LEVEL 4)

include(pico_sdk_import.cmake)

project(pico_extension C CXX ASM)

pico_sdk_init()

add_subdirectory(lib)

include_directories(include)

set(SOURCES
    main.cpp
    src/common.cpp
    # src/tasks/c_printer.c
    # src/tasks/task_agent.cpp
    src/tasks/board_task.cpp
    src/tasks/board/regs.cpp
    src/tasks/board/board.cpp
    src/tasks/board/twi.cpp
    # src/timers/timer_agent.cpp
    src/timers/debounce_timer.cpp
    src/timers/heartbeat_timer.cpp
    src/dac/dac.cpp
    src/demux/demux.cpp
    src/thermo/thermo.cpp
)

set(LIBS
    pico_stdlib
    hardware_adc
    hardware_pwm
    hardware_i2c
    hardware_spi
    pico_i2c_slave
    my_freertos
)


#add_compile_definitions(PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS=3000)
set(STDIO_USB_ENABLED 0)
set(STDIO_UART_ENABLED 0)

set(TARGETS_AND_TYPES
    "${CMAKE_PROJECT_NAME}_out_230AC_24DC_thermo:1"
    "${CMAKE_PROJECT_NAME}_out_230AC_24DC:2"
    "${CMAKE_PROJECT_NAME}_fueller:3"
    "${CMAKE_PROJECT_NAME}_testing:100"
)

foreach(tgt_type ${TARGETS_AND_TYPES})
    # extrahiere den Namen des Targets (alles vor ':')
    string(REGEX REPLACE "^(.*):[0-9]+$" "\\1" tgt ${tgt_type})
    # extrahiere DREHER_EXT_TYPE für das Target (die Zahl nach ':')
    string(REGEX REPLACE "^.*:([0-9]+)$" "\\1" type ${tgt_type})
    # wenn tgt und type gleich sind, stimmt der String nicht mit dem Ausdruck 
    # überein
    string(COMPARE EQUAL ${tgt} ${type} regex_fail)
    if(regex_fail)
        message(FATAL_ERROR
            "Regex für '${tgt_type}' fehlgeschlagen. Der String muss die Form \
            '<string>:<int>' haben")
    endif()
    message("[Dreher] prepare target ${tgt} with DREHER_EXT_TYPE=${type}")
    add_executable(${tgt} ${SOURCES})
    target_link_libraries(${tgt} ${LIBS})
    target_compile_definitions(${tgt} PRIVATE "DREHER_EXT_TYPE=${type}")
    pico_enable_stdio_usb(${tgt} ${STDIO_USB_ENABLED})
    pico_enable_stdio_uart(${tgt} ${STDIO_UART_ENABLED})
    pico_add_extra_outputs(${tgt})
endforeach()

