# The battery and power monitor firmware: compile, upload, and the logic tests.
#   make             compile battery_monitor/ for an ESP32 DevKit and print its size
#   make flash PORT=/dev/ttyUSB0
#   make test        run the logic tests (INA226 and INA228 maths, coulomb counting, the full reset,
#                    nights, totals, cycles, overflow, the millis() wrap, the low-voltage cut) with g++,
#                    AddressSanitizer and UndefinedBehaviorSanitizer
# Needs the esp32 core (3.x or 4.x) and the libraries PubSubClient and ArduinoJson 7. The INA226 is read
# over Wire directly: no library for it.

FQBN ?= esp32:esp32:esp32
PORT ?= /dev/ttyUSB0
CLI  ?= arduino-cli

all:
	$(CLI) compile --fqbn $(FQBN) --warnings all battery_monitor

flash:
	$(CLI) compile --fqbn $(FQBN) --upload -p $(PORT) battery_monitor

test:
	g++ -std=c++17 -O1 -g -Wall -Wextra -Werror -fsanitize=address,undefined -fno-sanitize-recover=all -Ibattery_monitor test/test_battery_logic.cpp -o test/test_battery_logic
	./test/test_battery_logic

clean:
	rm -f test/test_battery_logic

.PHONY: all flash test clean
