add_executable(${PROJECT_NAME}
    main.c    
)

target_link_libraries(${PROJECT_NAME}
    RegEdit
    ADC
    EnOut
    zero_cross_detection 
    load
    Enable
)

# Ensure the build rules are defined
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".elf")

if(NOT TARGET size)
# Set the size utility to display the size of the final binary
add_custom_target(size ALL
    COMMAND ${CMAKE_SIZE} --format=avr --mcu=${AVR_MCU} ${CMAKE_PROJECT_NAME}.elf
    DEPENDS ${CMAKE_PROJECT_NAME}.elf
)
endif()


if(NOT TARGET hex)
# Define how to convert ELF to HEX
add_custom_target(hex ALL
    COMMAND ${CMAKE_OBJCOPY} -O ihex -R .eeprom ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}.hex
    DEPENDS ${CMAKE_PROJECT_NAME}.elf
)
endif()

if(NOT TARGET bin)
# Define how to convert ELF to BIN
add_custom_target(bin ALL
    COMMAND ${CMAKE_OBJCOPY} -O binary -R .eeprom ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}.bin
    DEPENDS ${CMAKE_PROJECT_NAME}.elf
)
endif()


if(NOT TARGET upload)
# Upload command (adjust according to your programmer)
add_custom_target(upload ALL
    COMMAND avrdude -c ${PROGRAMMER} -P ${PORT} -p ${AVR_MCU} -U flash:w:${CMAKE_PROJECT_NAME}.hex
    DEPENDS hex
)
endif()


add_subdirectory(load)
add_subdirectory(zero_cross_detection)
add_subdirectory(ADC)
add_subdirectory(RegEdit)
add_subdirectory(usart)
add_subdirectory(EnOut)
add_subdirectory(Enable)
