Code commented, Serial speed moved into constant

This commit is contained in:
2026-04-19 23:14:50 +02:00
parent b6e4bfea33
commit 2879be0ada
2 changed files with 121 additions and 21 deletions

View File

@@ -4,37 +4,47 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
## Build & Upload
This is a single-file Arduino sketch (`Gaugecontroller.ino`). Requires the **FastLED** library (`arduino-cli lib install FastLED`). Use the Arduino IDE or `arduino-cli`:
Main firmware lives in `Gaugecontroller/Gaugecontroller.ino`. Requires the **FastLED** library (`arduino-cli lib install FastLED`). Use the Arduino IDE or `arduino-cli`:
```bash
# Compile (replace board/port as needed)
arduino-cli compile --fqbn arduino:avr:mega Gaugecontroller.ino
arduino-cli compile --fqbn arduino:avr:mega Gaugecontroller
# Upload
arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:mega Gaugecontroller.ino
arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:mega Gaugecontroller
```
Serial monitor: 115200 baud (`Serial` is both CMD_PORT and DEBUG_PORT).
Current default serial setup: `CMD_PORT` and `DEBUG_PORT` both point to `Serial1` at 38400 baud.
## Switching serial ports (debug → production)
Two `#define`s at the top of `Gaugecontroller.ino` control where commands and debug output go:
```cpp
#define CMD_PORT Serial // command channel (host sends SET, HOME, etc.)
#define DEBUG_PORT Serial // diagnostic prints (homing, boot messages)
#define CMD_PORT Serial1 // command channel (host sends SET, HOME, etc.)
#define DEBUG_PORT Serial1 // diagnostic prints (homing, boot messages)
```
**Debug / USB-only (default):** both point to `Serial` (the USB-CDC port). Connect via `minicom` or the Arduino IDE serial monitor at 115200 baud.
**Current default:** both point to `Serial1`, so command and debug traffic share Mega pins TX1=18 / RX1=19 at 38400 baud.
**Production (hardware UART):** change `CMD_PORT` to a hardware serial port so a host MCU or Raspberry Pi can drive it without occupying the USB port:
**USB-only debug setup:** point both defines back at `Serial` if you want to talk to the sketch over the Arduino USB port instead:
```cpp
#define CMD_PORT Serial1 // TX1=pin18, RX1=pin19
#define DEBUG_PORT Serial // keep USB for monitoring, or silence it (see below)
#define CMD_PORT Serial
#define DEBUG_PORT Serial
```
Arduino Mega hardware UARTs:
At that point the matching `begin()` call in `setup()` also needs to use the same baud rate you expect on the host side.
**Split command/debug ports:** if `CMD_PORT` and `DEBUG_PORT` do not point to the same serial port, `setup()` must initialise both. Right now it only calls:
```cpp
DEBUG_PORT.begin(38400);
```
If you split them, add a second `CMD_PORT.begin(...)` call.
Arduino Mega hardware UARTs for reference:
| Port | TX pin | RX pin |
|---------|--------|--------|
@@ -42,14 +52,6 @@ Arduino Mega hardware UARTs:
| Serial2 | 16 | 17 |
| Serial3 | 14 | 15 |
`setup()` calls `DEBUG_PORT.begin(115200)` only. If `CMD_PORT` differs from `DEBUG_PORT` you must also begin it — add a second `begin` call in `setup()`:
```cpp
CMD_PORT.begin(115200);
```
**Silencing debug output entirely:** point `DEBUG_PORT` at a null stream, or wrap all `DEBUG_PORT` calls in an `#ifdef DEBUG` guard. The simplest option is to replace the define with a no-op object, but the easiest production approach is just to leave `DEBUG_PORT Serial` and ignore the USB output.
## Architecture
The sketch controls `GAUGE_COUNT` stepper-motor gauges using a trapezoidal velocity profile and a simple text serial protocol.
@@ -74,7 +76,7 @@ When `sweepEnabled`, `updateSweepTarget` bounces `targetPos` between `minPos` an
### LED strip
One shared WS2812B strip is driven from `LED_DATA_PIN` (default 6). Each gauge owns a contiguous segment of the strip; `gaugePins[i].ledCount` sets the segment length (0 = no LEDs). `TOTAL_LEDS` is computed at compile time via `constexpr sumLedCounts()` — no manual constant to keep in sync. Per-gauge offsets into the flat `leds[]` array are computed once in `setup()` into `gaugeLedOffset[]`. `FastLED.show()` is called immediately after each `LED` command.
One shared WS2812B strip is driven from `LED_DATA_PIN` (currently 22). Each gauge owns a contiguous segment of the strip; `gaugePins[i].ledCount` sets the segment length (0 = no LEDs). `TOTAL_LEDS` is computed at compile time via `constexpr sumLedCounts()` — no manual constant to keep in sync. Per-gauge offsets into the flat `leds[]` array are computed once in `setup()` into `gaugeLedOffset[]`. LED commands and effects mark the strip dirty, and `FastLED.show()` is called once per main-loop iteration if anything changed.
### Serial command protocol