CFLAGS = -DF_CPU=1000000UL -D BME280_32BIT_ENABLE -I BME280_driver -mmcu=atmega88p -Os -flto -Wall -Wextra -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
DEPS = spi.h nrf24l01.h nrf24l01_definitions.h bme280_interface.h BME280_driver/bme280.h led.h pin_programming.h crc.h encryption.h radio.h

all: main.hex

clean:
	rm -f main.hex
	rm -f main
	rm -f obj/*.o

flash: main.hex
	sudo avrdude -c buspirate -b 115200 -P /dev/ttyUSB0 -p m88p -v -U flash:w:main.hex

obj/%.o: %.c $(DEPS) key_0.h key_1.h key_2.h key_3.h key_4.h key_5.h key_6.h key_7.h
	avr-gcc -c $< -o $@ $(CFLAGS)

obj/%.o: BME280_driver/%.c $(DEPS)
	avr-gcc -c $< -o $@ $(CFLAGS)

main: obj/main.o obj/spi.o obj/nrf24l01.o obj/bme280_interface.o obj/bme280.o obj/pin_programming.o obj/crc.o obj/encryption.o obj/radio.o
	avr-gcc $^ -o $@ $(CFLAGS)

key_%.h: ../../common/weather_station_%_key.txt
	@cat $< | sed "s/^\[\(.*\)\]$$/\1/g" > $@

main.hex: main
	avr-objcopy -O ihex -R .eeprom main main.hex
