export INSTALL_PATH := $(PWD)

SDK_VERSION := 2.1.0

export PICOTOOL_PATH=$(INSTALL_PATH)/picotool/$(SDK_VERSION)/build
export PICO_SDK_PATH=$(INSTALL_PATH)/sdk/$(SDK_VERSION)
 # if using a different platform or arch, set it here
export PICO_PLATFORM=rp2350

picotool = ${PICOTOOL_PATH}/picotool
# project name, need to be passed as make input (ex: make project name=test)
name := ""

install:
	@./scripts/install_sdk.bash $(SDK_VERSION)
	@./scripts/install_picotool.bash $(SDK_VERSION)
	@./scripts/install_freertos.bash

info:
	$(picotool) info -a

.check-name:
ifeq ($(strip $(name)), "")
	@echo "error: project name must be set, ex: name=<project_name>"
	@false
endif

ifeq ($(shell test -d $(name) && echo "ok"),)
	@echo "error: project $(name) does not exist"
	@false
endif

project:
ifeq ($(strip $(name)), "")
	@echo "error: project name must be set, ex: name=<project_name>"
	@false
endif
	@./scripts/scaffold_project.bash $(name) $(SDK_VERSION)

format: .check-name
	@(test -f "$(shell find /usr/bin/ -name astyle)" && astyle --style=java --indent=spaces=4 -K -xC120 $(name)/*.c) || echo "astyle bin formatter not found, skip it"

run-debug: format
	@rm -f  ${name}/${name}.debug
	@gcc -DLOG_LEVEL=TRACE -std=c99 -o2 ${name}/*.c  -lm -o ${name}/${name}.debug && ./${name}/${name}.debug

compile: format
	@rm -f ${name}/build/CMakeCache.txt
	@(test -d ${name} && mkdir -p ${name}/build && cd ${name}/build && cmake .. && make -j$(nproc) && echo "project compile successfully") || echo "error: unable to compile project: $(name)"

push: compile
	@(test -f $(name)/build/$(name).elf && $(picotool) load -fx -v $(name)/build/$(name).elf) || echo "error: unable to push project $(name) to device"

