# Seedbox firmware for the Pico 2 W.
#   cmake -S firmware -B firmware/build -G Ninja && ninja -C firmware/build
# Needs PICO_SDK_PATH (SDK 2.1 or later). The host engine test is separate: firmware/test/Makefile.
cmake_minimum_required(VERSION 3.13)

set(PICO_BOARD pico2_w CACHE STRING "Board type")
include(pico_sdk_import.cmake)
project(seedbox C CXX ASM)
set(CMAKE_C_STANDARD 11)
pico_sdk_init()

# the portable engine, the same source the host test checks
add_library(seedbox_engine INTERFACE)
target_sources(seedbox_engine INTERFACE ${CMAKE_CURRENT_LIST_DIR}/engine/engine.c)
target_include_directories(seedbox_engine INTERFACE ${CMAKE_CURRENT_LIST_DIR}/engine)

add_library(seedbox_drivers INTERFACE)
target_sources(seedbox_drivers INTERFACE
  ${CMAKE_CURRENT_LIST_DIR}/drivers/midi_uart.c
  ${CMAKE_CURRENT_LIST_DIR}/drivers/pcd8544.c
  ${CMAKE_CURRENT_LIST_DIR}/drivers/inputs.c
  ${CMAKE_CURRENT_LIST_DIR}/drivers/encoder.c
  ${CMAKE_CURRENT_LIST_DIR}/drivers/pots.c)
target_include_directories(seedbox_drivers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/drivers)
target_link_libraries(seedbox_drivers INTERFACE pico_stdlib hardware_uart hardware_spi hardware_adc)

# bring-up: board test over USB serial
add_executable(seedbox_bringup bringup/main.c)
target_compile_options(seedbox_bringup PRIVATE -Wall -Wextra)
target_link_libraries(seedbox_bringup seedbox_engine seedbox_drivers pico_stdlib pico_cyw43_arch_none)
pico_enable_stdio_usb(seedbox_bringup 1)    # also keeps the reset interface for picotool reboot -f -u
pico_enable_stdio_uart(seedbox_bringup 0)   # UART0 is MIDI: stdio on it would send text to the kit device
pico_add_extra_outputs(seedbox_bringup)

# the instrument: core 0 UI, core 1 sequencer
add_executable(seedbox app/main.c app/sequencer.c app/storage.c)
target_compile_options(seedbox PRIVATE -Wall -Wextra)
target_include_directories(seedbox PRIVATE app)
target_link_libraries(seedbox seedbox_engine seedbox_drivers pico_stdlib pico_multicore pico_flash pico_rand hardware_flash)
pico_enable_stdio_usb(seedbox 1)    # keeps the reset interface: picotool reboot -f -u flashes with the box closed
pico_enable_stdio_uart(seedbox 0)   # UART0 is MIDI
pico_add_extra_outputs(seedbox)
