FQBN  := esp32:esp32:lilygo_t_display:FlashSize=16M,PartitionScheme=app3M_fat9M_16MB
PORT  ?= /dev/cu.wchusbserial57460344141
BUILD := build

# Update über WLAN: Name aus config.h, Passwort aus secrets.h
OTA_HOST ?= $(shell sed -n 's/^\#define HOSTNAME *"\([^"]*\)".*/\1/p' config.h).local
OTA_PASS  = $(shell sed -n 's/^\#define OTA_PASSWORD *"\([^"]*\)".*/\1/p' secrets.h)
ESPOTA   := $(lastword $(sort $(wildcard $(HOME)/Library/Arduino15/packages/esp32/hardware/esp32/*/tools/espota.py)))

.PHONY: build flash ota monitor screenshot restore clean

build:
	arduino-cli compile --fqbn $(FQBN) --output-dir $(BUILD) .

flash: build
	arduino-cli upload --fqbn $(FQBN) --port $(PORT) --input-dir $(BUILD) .

ota: build
	@python3 $(ESPOTA) -r -i $(OTA_HOST) -a '$(OTA_PASS)' -f $(BUILD)/tdisplay.ino.bin

monitor:
	arduino-cli monitor --port $(PORT) --config baudrate=115200

# Aktuellen Bildschirminhalt als PNG holen
screenshot:
	python3 tools/screenshot.py $(PORT) screenshot.png

# Spielt die gesicherte Werks-Firmware zurück
restore:
	esptool --port $(PORT) --baud 230400 write-flash 0 backup/factory_16MB.bin

clean:
	rm -rf $(BUILD)
