Code commented, Serial speed moved into constant
This commit is contained in:
@@ -10,6 +10,7 @@ static const uint8_t LED_DATA_PIN = 22;
|
||||
// For now, command and debug traffic share the same serial port.
|
||||
#define CMD_PORT Serial1
|
||||
#define DEBUG_PORT Serial1
|
||||
static const unsigned long SERIAL_BAUD = 38400;
|
||||
|
||||
struct GaugePins {
|
||||
uint8_t dirPin;
|
||||
@@ -92,14 +93,65 @@ uint8_t gaugeLedOffset[GAUGE_COUNT];
|
||||
BlinkState blinkState[TOTAL_LEDS];
|
||||
bool ledsDirty = false;
|
||||
|
||||
// Sends one-line command replies back over the control port.
|
||||
//
|
||||
// Serial protocol summary.
|
||||
//
|
||||
// Host -> controller commands (newline-terminated ASCII):
|
||||
// SET <id> <pos>
|
||||
// SPEED <id> <steps_per_s>
|
||||
// ACCEL <id> <steps_per_s2>
|
||||
// ENABLE <id> <0|1>
|
||||
// ZERO <id>
|
||||
// HOME <id>
|
||||
// HOMEALL
|
||||
// SWEEP <id> <accel> <speed>
|
||||
// POS?
|
||||
// LED?
|
||||
// LED <id> <idx|a-b> <r> <g> <b>
|
||||
// BLINK <id> <idx|a-b> <on_ms> <off_ms> [<r> <g> <b>]
|
||||
// BREATHE <id> <idx|a-b> <period_ms> <r> <g> <b>
|
||||
// DFLASH <id> <idx|a-b> <r> <g> <b>
|
||||
// PING
|
||||
//
|
||||
// Controller -> host replies / events:
|
||||
// READY
|
||||
// Sent once from setup() after boot completes.
|
||||
// OK
|
||||
// Sent after a valid mutating command, and after POS?/LED? once all data lines
|
||||
// for that query have been emitted.
|
||||
// PONG
|
||||
// Sent in response to PING.
|
||||
// ERR BAD_CMD
|
||||
// Sent when a complete line matches no parser.
|
||||
// ERR TOO_LONG
|
||||
// Sent when an input line exceeds the receive buffer limit.
|
||||
// ERR BAD_ID
|
||||
// Sent by commands that take a gauge id when the id is outside 0..GAUGE_COUNT-1.
|
||||
// ERR BAD_SPEED
|
||||
// Sent by SPEED when the requested speed is <= 0.
|
||||
// ERR BAD_ACCEL
|
||||
// Sent by ACCEL when the requested acceleration is <= 0.
|
||||
// ERR BAD_IDX
|
||||
// Sent by LED/BLINK/BREATHE/DFLASH when an LED index or range is invalid.
|
||||
// ERR BAD_TIME
|
||||
// Sent by BLINK/BREATHE when the timing parameter is invalid.
|
||||
// POS <id> <currentPos> <targetPos> <homed> <homingState> <sweepEnabled>
|
||||
// Emitted once per gauge before the trailing OK reply to POS?.
|
||||
// LED <id> <idx> <r> <g> <b>
|
||||
// Emitted once per configured LED before the trailing OK reply to LED?.
|
||||
// HOMED <id>
|
||||
// Debug event printed on DEBUG_PORT when a homing sequence settles successfully.
|
||||
void sendReply(const String& s) {
|
||||
CMD_PORT.println(s);
|
||||
}
|
||||
|
||||
// Tiny float absolute-value helper to avoid dragging more machinery into the sketch.
|
||||
float absf(float x) {
|
||||
return (x < 0.0f) ? -x : x;
|
||||
}
|
||||
|
||||
// Updates the cached enable state and toggles the hardware pin if one exists.
|
||||
void setEnable(uint8_t id, bool en) {
|
||||
if (id >= GAUGE_COUNT) return;
|
||||
gauges[id].enabled = en;
|
||||
@@ -111,11 +163,13 @@ void setEnable(uint8_t id, bool en) {
|
||||
digitalWrite(pin, level ? HIGH : LOW);
|
||||
}
|
||||
|
||||
// Applies the logical direction after accounting for per-gauge inversion.
|
||||
void setDir(uint8_t id, bool forward) {
|
||||
bool level = gaugePins[id].dirInverted ? !forward : forward;
|
||||
digitalWrite(gaugePins[id].dirPin, level ? HIGH : LOW);
|
||||
}
|
||||
|
||||
// Emits one step pulse with the polarity expected by the driver.
|
||||
void pulseStep(uint8_t id) {
|
||||
bool active = gaugePins[id].stepActiveHigh;
|
||||
digitalWrite(gaugePins[id].stepPin, active ? HIGH : LOW);
|
||||
@@ -123,6 +177,7 @@ void pulseStep(uint8_t id) {
|
||||
digitalWrite(gaugePins[id].stepPin, active ? LOW : HIGH);
|
||||
}
|
||||
|
||||
// Moves the motor by one step if the requested direction is still within allowed travel.
|
||||
void doStep(uint8_t id, int dir, bool allowPastMin = false) {
|
||||
Gauge& g = gauges[id];
|
||||
if (!g.enabled) return;
|
||||
@@ -140,6 +195,7 @@ void doStep(uint8_t id, int dir, bool allowPastMin = false) {
|
||||
}
|
||||
}
|
||||
|
||||
// Arms the homing state machine for one gauge and clears any in-flight motion.
|
||||
void requestHome(uint8_t id) {
|
||||
if (id >= GAUGE_COUNT) return;
|
||||
Gauge& g = gauges[id];
|
||||
@@ -150,12 +206,14 @@ void requestHome(uint8_t id) {
|
||||
g.sweepEnabled = false;
|
||||
}
|
||||
|
||||
// Starts the same homing sequence on every configured gauge.
|
||||
void requestHomeAll() {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
requestHome(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Advances the simple homing state machine until the gauge is parked at logical zero.
|
||||
void updateHoming(uint8_t id) {
|
||||
Gauge& g = gauges[id];
|
||||
unsigned long nowUs = micros();
|
||||
@@ -210,6 +268,7 @@ void updateHoming(uint8_t id) {
|
||||
}
|
||||
}
|
||||
|
||||
// Flips the sweep destination when the gauge has settled at either end of travel.
|
||||
void updateSweepTarget(uint8_t id) {
|
||||
Gauge& g = gauges[id];
|
||||
if (!g.sweepEnabled || !g.homed || g.homingState != HS_IDLE) return;
|
||||
@@ -229,6 +288,7 @@ void updateSweepTarget(uint8_t id) {
|
||||
}
|
||||
}
|
||||
|
||||
// Runs one gauge worth of motion control, including homing and optional sweeping.
|
||||
void updateGauge(uint8_t id) {
|
||||
Gauge& g = gauges[id];
|
||||
|
||||
@@ -326,6 +386,8 @@ void updateGauge(uint8_t id) {
|
||||
}
|
||||
}
|
||||
|
||||
// Parses `SET <id> <pos>` and updates the target position.
|
||||
// Replies: `OK`, `ERR BAD_ID`.
|
||||
bool parseSet(const String& line) {
|
||||
int id;
|
||||
long pos;
|
||||
@@ -345,6 +407,8 @@ bool parseSet(const String& line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parses `SPEED <id> <speed>` and updates the max step rate.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_SPEED`.
|
||||
bool parseSpeed(const String& line) {
|
||||
int firstSpace = line.indexOf(' ');
|
||||
int secondSpace = line.indexOf(' ', firstSpace + 1);
|
||||
@@ -368,6 +432,8 @@ bool parseSpeed(const String& line) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parses `ACCEL <id> <accel>` and updates the acceleration limit.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_ACCEL`.
|
||||
bool parseAccel(const String& line) {
|
||||
int firstSpace = line.indexOf(' ');
|
||||
int secondSpace = line.indexOf(' ', firstSpace + 1);
|
||||
@@ -391,6 +457,8 @@ bool parseAccel(const String& line) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parses `ENABLE <id> <0|1>` and toggles the selected driver.
|
||||
// Replies: `OK`, `ERR BAD_ID`.
|
||||
bool parseEnable(const String& line) {
|
||||
int id, en;
|
||||
if (sscanf(line.c_str(), "ENABLE %d %d", &id, &en) == 2) {
|
||||
@@ -406,6 +474,8 @@ bool parseEnable(const String& line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parses `ZERO <id>` and declares the current position to be home.
|
||||
// Replies: `OK`, `ERR BAD_ID`.
|
||||
bool parseZero(const String& line) {
|
||||
int id;
|
||||
if (sscanf(line.c_str(), "ZERO %d", &id) == 1) {
|
||||
@@ -427,6 +497,8 @@ bool parseZero(const String& line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parses `HOME <id>` or `HOMEALL` and kicks off the homing sequence.
|
||||
// Replies: `OK`, `ERR BAD_ID`. Successful completion later emits debug line `HOMED <id>`.
|
||||
bool parseHome(const String& line) {
|
||||
int id;
|
||||
if (sscanf(line.c_str(), "HOME %d", &id) == 1) {
|
||||
@@ -449,6 +521,8 @@ bool parseHome(const String& line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parses `SWEEP <id> <accel> <speed>` and enables or disables end-to-end motion.
|
||||
// Replies: `OK`, `ERR BAD_ID`.
|
||||
bool parseSweep(const String& line) {
|
||||
int firstSpace = line.indexOf(' ');
|
||||
int secondSpace = line.indexOf(' ', firstSpace + 1);
|
||||
@@ -485,6 +559,9 @@ bool parseSweep(const String& line) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Answers `POS?` with current motion state for every gauge.
|
||||
// Emits one `POS <id> <cur> <tgt> <homed> <homingState> <sweep>` line per gauge,
|
||||
// then replies `OK`.
|
||||
bool parsePosQuery(const String& line) {
|
||||
if (line == "POS?") {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
@@ -507,6 +584,8 @@ bool parsePosQuery(const String& line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Answers the mandatory life question: are you there?
|
||||
// Reply: `PONG`.
|
||||
bool parsePing(const String& line) {
|
||||
if (line == "PING") {
|
||||
sendReply("PONG");
|
||||
@@ -515,6 +594,8 @@ bool parsePing(const String& line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Answers `LED?` with the current RGB values for every configured LED.
|
||||
// Emits one `LED <id> <idx> <r> <g> <b>` line per configured LED, then replies `OK`.
|
||||
bool parseLedQuery(const String& line) {
|
||||
if (line == "LED?") {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
@@ -538,6 +619,8 @@ bool parseLedQuery(const String& line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parses `LED <id> <idx|a-b> <r> <g> <b>` and writes static colours.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_IDX`.
|
||||
bool parseLed(const String& line) {
|
||||
int id, r, g, b;
|
||||
char idxToken[16];
|
||||
@@ -561,6 +644,8 @@ bool parseLed(const String& line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parses `BLINK ...` and assigns a simple on/off effect to one LED or a range.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_IDX`, `ERR BAD_TIME`.
|
||||
bool parseBlink(const String& line) {
|
||||
int id, onMs, offMs, r, g, b;
|
||||
char idxToken[16];
|
||||
@@ -607,6 +692,8 @@ bool parseBlink(const String& line) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parses `BREATHE ...` and assigns a triangle-wave fade effect.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_IDX`, `ERR BAD_TIME`.
|
||||
bool parseBreathe(const String& line) {
|
||||
int id, periodMs, r, g, b;
|
||||
char idxToken[16];
|
||||
@@ -638,6 +725,8 @@ bool parseBreathe(const String& line) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parses `DFLASH ...` and assigns the double-flash pattern.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_IDX`.
|
||||
bool parseDflash(const String& line) {
|
||||
int id, r, g, b;
|
||||
char idxToken[16];
|
||||
@@ -667,6 +756,7 @@ bool parseDflash(const String& line) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Advances all active LED effects and marks the strip dirty when something changed.
|
||||
void updateBlink() {
|
||||
unsigned long nowMs = millis();
|
||||
bool changed = false;
|
||||
@@ -721,6 +811,8 @@ void updateBlink() {
|
||||
if (changed) ledsDirty = true;
|
||||
}
|
||||
|
||||
// Runs the command parsers in order until one claims the line.
|
||||
// Reply: `ERR BAD_CMD` when no parser accepts the line.
|
||||
void processLine(const String& line) {
|
||||
if (parseSet(line)) return;
|
||||
if (parseSpeed(line)) return;
|
||||
@@ -740,6 +832,8 @@ void processLine(const String& line) {
|
||||
sendReply("ERR BAD_CMD");
|
||||
}
|
||||
|
||||
// Reads newline-delimited commands from serial and hands complete lines to the parser.
|
||||
// Reply: `ERR TOO_LONG` when the buffered line exceeds the receive limit before newline.
|
||||
void readCommands() {
|
||||
while (CMD_PORT.available()) {
|
||||
char c = (char)CMD_PORT.read();
|
||||
@@ -760,8 +854,10 @@ void readCommands() {
|
||||
}
|
||||
}
|
||||
|
||||
// Initialises pins, LED bookkeeping and the initial homing cycle.
|
||||
// Reply/event: emits `READY` on CMD_PORT once boot is complete.
|
||||
void setup() {
|
||||
DEBUG_PORT.begin(38400);
|
||||
DEBUG_PORT.begin(SERIAL_BAUD);
|
||||
DEBUG_PORT.println("Gauge controller booting");
|
||||
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
@@ -792,9 +888,11 @@ void setup() {
|
||||
requestHomeAll();
|
||||
|
||||
DEBUG_PORT.println("READY");
|
||||
// Boot-complete handshake for the command channel.
|
||||
sendReply("READY");
|
||||
}
|
||||
|
||||
// Main service loop: ingest commands, advance effects, move gauges, flush LEDs.
|
||||
void loop() {
|
||||
readCommands();
|
||||
updateBlink();
|
||||
|
||||
Reference in New Issue
Block a user