# Build and flash the firmware. Override any of these on the command line,
# e.g. make ota BOARD=192.168.1.50
IDF_PATH  ?= $(HOME)/esp/esp-idf
TOOLCHAIN ?= $(HOME)/.arduino15/packages/esp32/tools/esp-x32/2507/bin
PYENV     ?= $(HOME)/.espressif/python_env/idf5.4_py3.12_env
PORT      ?= /dev/ttyACM0
BOARD     ?= 10.1.1.167

# IDF_MAINTAINER skips IDF's tool version check, as the toolchain above is
# the Arduino core's rather than the one IDF installs
export IDF_PATH
export IDF_MAINTAINER := 1
export PATH := $(TOOLCHAIN):$(PYENV)/bin:$(PATH)

PYTHON := $(PYENV)/bin/python
BIN    := build/vga_test.bin

.PHONY: all build flash ota monitor font clean

all: build

build/build.ninja:
	cmake -S . -B build -G Ninja -DPYTHON=$(PYTHON) -DPYTHON_DEPS_CHECKED=1 -DIDF_TARGET=esp32s3

build: build/build.ninja
	ninja -C build

# Over USB
flash: build
	cd build && $(PYTHON) -m esptool --chip esp32s3 -p $(PORT) -b 460800 --before default_reset --after hard_reset write_flash @flash_args

# Over WiFi
ota: build
	curl -X POST --data-binary @$(BIN) http://$(BOARD)/api/ota

monitor:
	$(PYTHON) $(IDF_PATH)/tools/idf_monitor.py -p $(PORT) build/vga_test.elf

# Needs Pillow and the DejaVu fonts
font:
	python3 tools/gen_font.py main/font_data.h

clean:
	rm -rf build
