# led-controller
Controller for LED drivers on a Waveshare ESP32-P4-Nano: Ethernet, web UI, microphone beat detection and an ESP-NOW link to the drivers through the board's ESP32-C6 ([`docs/P4_NANO.md`](docs/P4_NANO.md)). The firmware is C (ESP-IDF) at the repository root; [`docs/BENCH_TEST.md`](docs/BENCH_TEST.md) has the bench tests of the earlier ESP32-S3 controller. The ESP32-S3 is only used as a bridge board now ([`bridge/`](bridge/README.md)).
It keeps profiles, zones, presets, device groups, sequences, palettes, patterns and outputs in its flash, and serves the web UI and JSON API ([`docs/API.md`](docs/API.md)). Zones play to device groups. Sequences advance on the beats the microphone detects, or on a BPM clock while audio is stopped. Profiles, presets and sequences import and export as JSON bundles.
A simulated rig at `/sim/` (Menu > Simulator) shows every LED bar in the browser, driven by the same controller: on the board, in the desktop program and in Docker. See [`led-simulator/README.md`](led-simulator/README.md).

 |
 |
| Sequence editor |
Pattern gallery (/sim/gallery) |
## Build and flash
Clone with the submodules: `git clone --recurse-submodules `, or `git submodule update --init` afterwards.
```bash
make sim-wasm # once, and after led-driver changes: the simulator's drivers (Emscripten in Docker)
make build # minify the web UI from web/, pack the simulator, then build the C6 radio and the controller (ESP-IDF in Docker)
make flash # flash over USB (the controller and the C6 image)
make ota [HOST=] # update firmware (web UI included) over Ethernet, no USB (POST /ota); HOST defaults to 192.168.50.20
make monitor # serial console
make test-envelope # the C and Python envelope packers must agree
make test-api HOST= # HTTP API tests against a running controller (its data is restored afterwards)
make test-browser HOST= # Selenium UI tests (needs selenium and chromedriver)
```
`make` lists every target. The web UI sources are in `web/` (`static/`, `templates/`, `audio.html`); `tools/build_web.sh` also packs `led-driver/src/patterns/*.py` into the firmware when they exist; the C++ led-driver has none. For the same reason `make test` stops at `test-driver`: the MicroPython driver tests it ran are gone from `led-driver`.
## Desktop
The same controller runs as a program on Linux, with no ESP-IDF: `make desktop-run` (then add the bridge under Settings > Network > Bridges), or a `.deb` with a systemd service (`make deb`). See [`desktop/README.md`](desktop/README.md). The USB ESP-NOW bridge board that gives a computer its radio is in [`bridge/`](bridge/README.md).
## Other directories
- `led-driver/` (submodule, branch `cpp`): C++ (ESP-IDF) firmware for the ESP32 LED drivers.
- `dmx-driver/` (submodule): DMX gateway (Art-Net and serial out).
- `led-tool/` (submodule): `led-cli`, configures drivers over serial and updates the controller over Ethernet (`led-cli --ota HOST`).
- `radio-c6/`: firmware of the P4-Nano's ESP32-C6 (esp-hosted plus ESP-NOW).
- `bridge/`: the USB/network ESP-NOW bridge board (ESP32-S3 or C3).
- `led-simulator/`: the simulated rig's page. `sim-driver/` builds led-driver to WebAssembly for it; `sim-controller/` is the controller for ESP-IDF's linux target, and holds the rig's server code.
- `fixture-driver/`: led-driver's engine built for the host, one process per DMX fixture (desktop).
- `espnow-sender/`: a test helper.
- `db/`: the records a controller starts with (the `default` profile and group the firmware seeds at first boot, plus pattern metadata), importable with `PUT /db/`.
- `test/api/`: the API tests (`test/api/README.md`); `test/browser/`: the UI tests.
## Docs
- The board, its C6 radio, pins and partitions: `docs/P4_NANO.md`
- API reference: `docs/API.md`
- User guide: `docs/help.md` (`scripts/build_help_pdf.sh` regenerates `docs/help.pdf`)
- Specification of the first, MicroPython system (historical): `docs/SPECIFICATION.md`
## Network
The controller advertises `.local` over mDNS (default `led-controller`, plus an `_http._tcp` service). Both settings below come from `/settings` and apply on reboot.
`static_ips` is a list of networks to try, in order, each time the Ethernet link comes up. The first entry whose gateway answers ARP is used; if none does (or the list is empty) the controller uses DHCP. `netmask` defaults to `255.255.255.0`, `dns` to the gateway.
If DHCP gets no answer within 5 seconds, the controller takes `192.168.50.20/24` (menuconfig "ESP32-P4-Nano board"). For a cable straight from a PC, give the PC a static IP on that subnet, such as `192.168.50.10/24`, and open `http://192.168.50.20/`. The controller tries DHCP again only when the link next comes up.
```bash
curl -X PUT http://led-controller.local/settings/settings -H 'Content-Type: application/json' -d '{
"hostname": "stage",
"static_ips": [
{"ip": "192.168.1.50", "netmask": "255.255.255.0", "gw": "192.168.1.1"},
{"ip": "10.0.0.50", "netmask": "255.255.255.0", "gw": "10.0.0.1"}
]}'
```