# l8 Bluetooth controller -- run it on a LimeSDR Mini 2.0.  "make check" first; "make deps" installs the
# Python packages.  Then (JTAG cable) "make prog", "make bridge", and in another terminal "make scan" / "make play".
FILE ?= song.mp3
ADDR ?=
OFL  ?= openFPGALoader
# how the radio follows Bluetooth's hops: osc (the synthesizer hops), lowif (it hops 1 MHz beside the channel, the
# chip's digital mixers shift), digital (it stands, the digital mixers hop) -- README.md, "Three ways to tune"
TUNE ?= osc

.PHONY: check deps prog prog-usb tune bridge bridge-usb scan pair forget play help

help:
	@echo "THE ORDER:  make prog   then   make bridge (leave it running)   then, in a second terminal,   make scan / make play"
	@echo "make check                 is this machine ready (tools, Python packages, board)"
	@echo "make deps                  install the Python packages (bumble, pyusb)"
	@echo "make prog | prog-usb       program the controller (JTAG cable | USB-data image); TUNE=osc | lowif | digital"
	@echo "                           picks how the radio follows the hops (default osc; README.md)"
	@echo "make bridge | bridge-usb   run the HCI bridge (JTAG | USB); leave it running"
	@echo "make scan                  list the Bluetooth devices in range"
	@echo "make pair ADDR=..          pair with a device in pairing mode and keep its key (once per device)"
	@echo "make play ADDR=.. FILE=..  connect to ADDR and stream FILE"
	@echo "make forget ADDR=..        drop a device's key"

check:
	python3 check.py

deps:
	python3 -m pip install -r requirements.txt
	@echo
	@echo "Also needed, not pip packages: openFPGALoader and ffmpeg -- see README.md (Dependencies)."

prog:
	$(OFL) -c ft2232 firmware/top_hcic.bit
	python3 tune_set.py $(TUNE)

prog-usb:
	$(OFL) -c ft2232 firmware/top_hciu.bit
	python3 tune_set.py $(TUNE)

# the tuning alone, on the image that is loaded (stop the bridge first)
tune:
	python3 tune_set.py $(TUNE)

bridge:
	python3 jtag_hci_bridge.py

bridge-usb:
	python3 hci_bridge.py

scan:
	python3 bt_play.py scan

pair:
	@test -n "$(ADDR)" || { echo "give a device: make pair ADDR=AA:BB:CC:DD:EE:FF   (it must be in pairing mode; make scan lists it)"; exit 1; }
	python3 bt_play.py pair $(ADDR)

forget:
	@test -n "$(ADDR)" || { echo "give a device: make forget ADDR=AA:BB:CC:DD:EE:FF"; exit 1; }
	python3 bt_play.py forget $(ADDR)

play:
	@test -n "$(ADDR)" || { echo "give a device: make play ADDR=AA:BB:CC:DD:EE:FF FILE=song.mp3"; exit 1; }
	python3 bt_play.py play $(ADDR) $(FILE)
