Timing changed on Arduino script, latest version of ESP-Home script added
This commit is contained in:
@@ -36,6 +36,8 @@ char displayBuffer[kDigitCount] = {' ', ' ', ' ', ' '};
|
||||
bool pointEnabled = false;
|
||||
bool bellEnabled = false;
|
||||
uint8_t currentPhase = 0;
|
||||
unsigned long phaseStartMicros = 0;
|
||||
bool phaseActive = false;
|
||||
|
||||
uint8_t encodeCharacter(char c) {
|
||||
switch (c) {
|
||||
@@ -110,12 +112,12 @@ void clear() {
|
||||
bellEnabled = false;
|
||||
}
|
||||
|
||||
bool parseCommand(const String& command) {
|
||||
bool parseCommand(const char* command) {
|
||||
char displayText[16];
|
||||
size_t inputIndex = 0;
|
||||
size_t displayIndex = 0;
|
||||
|
||||
if (command.length() == 0) {
|
||||
if (command[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -127,7 +129,7 @@ bool parseCommand(const String& command) {
|
||||
}
|
||||
|
||||
const size_t digitStart = inputIndex;
|
||||
while (inputIndex < static_cast<size_t>(command.length()) &&
|
||||
while (command[inputIndex] != '\0' &&
|
||||
isxdigit(static_cast<unsigned char>(command[inputIndex]))) {
|
||||
if (displayIndex + 1 >= sizeof(displayText)) {
|
||||
return false;
|
||||
@@ -142,7 +144,7 @@ bool parseCommand(const String& command) {
|
||||
|
||||
bool newPointEnabled = false;
|
||||
bool newBellEnabled = false;
|
||||
while (inputIndex < static_cast<size_t>(command.length())) {
|
||||
while (command[inputIndex] != '\0') {
|
||||
if (command[inputIndex] == '.') {
|
||||
newPointEnabled = true;
|
||||
} else if (command[inputIndex] == '!') {
|
||||
@@ -201,8 +203,19 @@ void begin() {
|
||||
shiftDriverWord(0);
|
||||
}
|
||||
|
||||
// Non-blocking. Returns immediately while the current digit is still being held;
|
||||
// only does work when the hold time has elapsed and it is time to advance phases.
|
||||
void refresh() {
|
||||
unsigned long now = micros();
|
||||
if (phaseActive && (now - phaseStartMicros) < kDigitHoldMicros) {
|
||||
return;
|
||||
}
|
||||
|
||||
setBlanked(true);
|
||||
if (phaseActive) {
|
||||
currentPhase = (currentPhase + 1) % (kDigitCount + 1);
|
||||
}
|
||||
|
||||
if (currentPhase < kDigitCount) {
|
||||
renderDigit(currentPhase);
|
||||
} else if (pointEnabled || bellEnabled) {
|
||||
@@ -211,10 +224,9 @@ void refresh() {
|
||||
shiftDriverWord(0);
|
||||
}
|
||||
setBlanked(false);
|
||||
delayMicroseconds(kDigitHoldMicros);
|
||||
setBlanked(true);
|
||||
|
||||
currentPhase = (currentPhase + 1) % (kDigitCount + 1);
|
||||
phaseStartMicros = micros();
|
||||
phaseActive = true;
|
||||
}
|
||||
|
||||
} // namespace vfd
|
||||
@@ -280,6 +292,10 @@ struct Gauge {
|
||||
|
||||
bool sweepEnabled = false;
|
||||
bool sweepTowardMax = true;
|
||||
|
||||
// Cached direction-pin state. 0 = uninitialised, 1 = forward, -1 = reverse.
|
||||
// Lets setDir() skip redundant digitalWrites when the direction hasn't flipped.
|
||||
int8_t lastDir = 0;
|
||||
};
|
||||
|
||||
enum LedFx : uint8_t { FX_BLINK = 0, FX_BREATHE = 1, FX_DFLASH = 2 };
|
||||
@@ -298,29 +314,26 @@ struct BlinkState {
|
||||
};
|
||||
|
||||
Gauge gauges[GAUGE_COUNT];
|
||||
String rxLine;
|
||||
|
||||
// Fixed receive buffer — Arduino String would heap-fragment on the Mega over time.
|
||||
// kRxBufSize - 1 is the max payload (one byte reserved for the null terminator).
|
||||
static const uint8_t kRxBufSize = 128;
|
||||
char rxBuf[kRxBufSize];
|
||||
uint8_t rxLen = 0;
|
||||
|
||||
CRGB leds[TOTAL_LEDS];
|
||||
uint8_t gaugeLedOffset[GAUGE_COUNT];
|
||||
uint8_t gaugeLedCount[GAUGE_COUNT];
|
||||
BlinkState blinkState[TOTAL_LEDS];
|
||||
// Precomputed in setup() from gaugePins[].ledOrder so per-LED writes don't
|
||||
// have to walk the gauge table on every call.
|
||||
bool ledNeedsSwap[TOTAL_LEDS];
|
||||
bool ledsDirty = false;
|
||||
|
||||
// FastLED drives the shared strip as RGB. Each gauge's ledOrder string marks per-LED
|
||||
// type ('R' = RGB, 'G' = GRB); writes to GRB-ordered LEDs pre-swap R and G to compensate.
|
||||
inline bool ledNeedsRgSwap(uint8_t globalIdx) {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
uint8_t off = gaugeLedOffset[i];
|
||||
if (globalIdx >= off && globalIdx < off + gaugeLedCount[i]) {
|
||||
char c = gaugePins[i].ledOrder[globalIdx - off];
|
||||
return c == 'G' || c == 'g';
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline CRGB encodeForStrip(uint8_t globalIdx, CRGB color) {
|
||||
if (ledNeedsRgSwap(globalIdx)) {
|
||||
if (ledNeedsSwap[globalIdx]) {
|
||||
uint8_t tmp = color.r;
|
||||
color.r = color.g;
|
||||
color.g = tmp;
|
||||
@@ -338,6 +351,9 @@ inline CRGB readLed(uint8_t globalIdx) {
|
||||
|
||||
// Sends one-line command replies back over the control port.
|
||||
//
|
||||
// All parse* functions take a null-terminated char* (no Arduino String) so the
|
||||
// command pipeline never touches the heap.
|
||||
//
|
||||
// Serial protocol summary.
|
||||
//
|
||||
// Host -> controller commands (newline-terminated ASCII):
|
||||
@@ -354,7 +370,7 @@ inline CRGB readLed(uint8_t globalIdx) {
|
||||
// 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> <ig> <b>
|
||||
// DFLASH <id> <idx|a-b> <r> <g> <b>
|
||||
// VFD <text[.!]>
|
||||
// PING
|
||||
//
|
||||
@@ -388,15 +404,10 @@ inline CRGB readLed(uint8_t globalIdx) {
|
||||
// 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) {
|
||||
void sendReply(const char* 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;
|
||||
@@ -410,9 +421,19 @@ void setEnable(uint8_t id, bool en) {
|
||||
}
|
||||
|
||||
// Applies the logical direction after accounting for per-gauge inversion.
|
||||
// digitalWrite is skipped when the direction is already correct, which is the
|
||||
// common case (a step run almost always reuses the previous direction).
|
||||
void setDir(uint8_t id, bool forward) {
|
||||
Gauge& g = gauges[id];
|
||||
int8_t want = forward ? 1 : -1;
|
||||
if (g.lastDir == want) return;
|
||||
g.lastDir = want;
|
||||
|
||||
bool level = gaugePins[id].dirInverted ? !forward : forward;
|
||||
digitalWrite(gaugePins[id].dirPin, level ? HIGH : LOW);
|
||||
// DIR-to-STEP setup time. Most A4988/DRV8825-class drivers want at least
|
||||
// ~200 ns; 1 us is cheap insurance and only paid when the direction flips.
|
||||
delayMicroseconds(1);
|
||||
}
|
||||
|
||||
// Emits one step pulse with the polarity expected by the driver.
|
||||
@@ -521,13 +542,13 @@ void updateSweepTarget(uint8_t id) {
|
||||
|
||||
if (g.sweepTowardMax) {
|
||||
g.targetPos = g.maxPos;
|
||||
if (g.currentPos >= g.maxPos && absf(g.velocity) < 1.0f) {
|
||||
if (g.currentPos >= g.maxPos && fabsf(g.velocity) < 1.0f) {
|
||||
g.sweepTowardMax = false;
|
||||
g.targetPos = g.minPos;
|
||||
}
|
||||
} else {
|
||||
g.targetPos = g.minPos;
|
||||
if (g.currentPos <= g.minPos && absf(g.velocity) < 1.0f) {
|
||||
if (g.currentPos <= g.minPos && fabsf(g.velocity) < 1.0f) {
|
||||
g.sweepTowardMax = true;
|
||||
g.targetPos = g.maxPos;
|
||||
}
|
||||
@@ -562,7 +583,7 @@ void updateGauge(uint8_t id) {
|
||||
|
||||
long error = g.targetPos - g.currentPos;
|
||||
|
||||
if (error == 0 && absf(g.velocity) < 0.01f) {
|
||||
if (error == 0 && fabsf(g.velocity) < 0.01f) {
|
||||
g.velocity = 0.0f;
|
||||
g.stepAccumulator = 0.0f;
|
||||
return;
|
||||
@@ -570,7 +591,8 @@ void updateGauge(uint8_t id) {
|
||||
|
||||
float dir = (error > 0) ? 1.0f : (error < 0 ? -1.0f : 0.0f);
|
||||
// Basic trapezoidal profile: brake if the remaining travel is shorter than the stop distance.
|
||||
float brakingDistance = (g.velocity * g.velocity) / (2.0f * g.accel + 0.0001f);
|
||||
// parseAccel rejects accel <= 0, so the divisor is always positive.
|
||||
float brakingDistance = (g.velocity * g.velocity) / (2.0f * g.accel);
|
||||
|
||||
if ((float)labs(error) <= brakingDistance) {
|
||||
if (g.velocity > 0.0f) {
|
||||
@@ -634,10 +656,10 @@ void updateGauge(uint8_t id) {
|
||||
|
||||
// Parses `SET <id> <pos>` and updates the target position.
|
||||
// Replies: `OK`, `ERR BAD_ID`.
|
||||
bool parseSet(const String& line) {
|
||||
bool parseSet(const char* line) {
|
||||
int id;
|
||||
long pos;
|
||||
if (sscanf(line.c_str(), "SET %d %ld", &id, &pos) == 2) {
|
||||
if (sscanf(line, "SET %d %ld", &id, &pos) == 2) {
|
||||
if (id < 0 || id >= GAUGE_COUNT) {
|
||||
sendReply("ERR BAD_ID");
|
||||
return true;
|
||||
@@ -655,14 +677,15 @@ bool parseSet(const String& line) {
|
||||
|
||||
// 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);
|
||||
if (firstSpace < 0 || secondSpace < 0) return false;
|
||||
if (line.substring(0, firstSpace) != "SPEED") return false;
|
||||
// AVR libc sscanf doesn't support %f by default, so we hand-split with strchr/atof.
|
||||
bool parseSpeed(const char* line) {
|
||||
if (strncmp(line, "SPEED ", 6) != 0) return false;
|
||||
const char* p = line + 6;
|
||||
const char* sp = strchr(p, ' ');
|
||||
if (!sp) return false;
|
||||
|
||||
int id = line.substring(firstSpace + 1, secondSpace).toInt();
|
||||
float speed = line.substring(secondSpace + 1).toFloat();
|
||||
int id = atoi(p);
|
||||
float speed = atof(sp + 1);
|
||||
|
||||
if (id < 0 || id >= GAUGE_COUNT) {
|
||||
sendReply("ERR BAD_ID");
|
||||
@@ -680,14 +703,14 @@ bool parseSpeed(const String& line) {
|
||||
|
||||
// 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);
|
||||
if (firstSpace < 0 || secondSpace < 0) return false;
|
||||
if (line.substring(0, firstSpace) != "ACCEL") return false;
|
||||
bool parseAccel(const char* line) {
|
||||
if (strncmp(line, "ACCEL ", 6) != 0) return false;
|
||||
const char* p = line + 6;
|
||||
const char* sp = strchr(p, ' ');
|
||||
if (!sp) return false;
|
||||
|
||||
int id = line.substring(firstSpace + 1, secondSpace).toInt();
|
||||
float accel = line.substring(secondSpace + 1).toFloat();
|
||||
int id = atoi(p);
|
||||
float accel = atof(sp + 1);
|
||||
|
||||
if (id < 0 || id >= GAUGE_COUNT) {
|
||||
sendReply("ERR BAD_ID");
|
||||
@@ -705,9 +728,9 @@ bool parseAccel(const String& line) {
|
||||
|
||||
// Parses `ENABLE <id> <0|1>` and toggles the selected driver.
|
||||
// Replies: `OK`, `ERR BAD_ID`.
|
||||
bool parseEnable(const String& line) {
|
||||
bool parseEnable(const char* line) {
|
||||
int id, en;
|
||||
if (sscanf(line.c_str(), "ENABLE %d %d", &id, &en) == 2) {
|
||||
if (sscanf(line, "ENABLE %d %d", &id, &en) == 2) {
|
||||
if (id < 0 || id >= GAUGE_COUNT) {
|
||||
sendReply("ERR BAD_ID");
|
||||
return true;
|
||||
@@ -722,9 +745,9 @@ bool parseEnable(const String& line) {
|
||||
|
||||
// Parses `ZERO <id>` and declares the current position to be home.
|
||||
// Replies: `OK`, `ERR BAD_ID`.
|
||||
bool parseZero(const String& line) {
|
||||
bool parseZero(const char* line) {
|
||||
int id;
|
||||
if (sscanf(line.c_str(), "ZERO %d", &id) == 1) {
|
||||
if (sscanf(line, "ZERO %d", &id) == 1) {
|
||||
if (id < 0 || id >= GAUGE_COUNT) {
|
||||
sendReply("ERR BAD_ID");
|
||||
return true;
|
||||
@@ -745,9 +768,9 @@ bool parseZero(const String& line) {
|
||||
|
||||
// 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) {
|
||||
bool parseHome(const char* line) {
|
||||
int id;
|
||||
if (sscanf(line.c_str(), "HOME %d", &id) == 1) {
|
||||
if (sscanf(line, "HOME %d", &id) == 1) {
|
||||
if (id < 0 || id >= GAUGE_COUNT) {
|
||||
sendReply("ERR BAD_ID");
|
||||
return true;
|
||||
@@ -758,7 +781,7 @@ bool parseHome(const String& line) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (line == "HOMEALL") {
|
||||
if (strcmp(line, "HOMEALL") == 0) {
|
||||
requestHomeAll();
|
||||
sendReply("OK");
|
||||
return true;
|
||||
@@ -769,17 +792,17 @@ bool parseHome(const String& line) {
|
||||
|
||||
// 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);
|
||||
int thirdSpace = line.indexOf(' ', secondSpace + 1);
|
||||
bool parseSweep(const char* line) {
|
||||
if (strncmp(line, "SWEEP ", 6) != 0) return false;
|
||||
const char* p = line + 6;
|
||||
const char* sp1 = strchr(p, ' ');
|
||||
if (!sp1) return false;
|
||||
const char* sp2 = strchr(sp1 + 1, ' ');
|
||||
if (!sp2) return false;
|
||||
|
||||
if (firstSpace < 0 || secondSpace < 0 || thirdSpace < 0) return false;
|
||||
if (line.substring(0, firstSpace) != "SWEEP") return false;
|
||||
|
||||
int id = line.substring(firstSpace + 1, secondSpace).toInt();
|
||||
float accel = line.substring(secondSpace + 1, thirdSpace).toFloat();
|
||||
float speed = line.substring(thirdSpace + 1).toFloat();
|
||||
int id = atoi(p);
|
||||
float accel = atof(sp1 + 1);
|
||||
float speed = atof(sp2 + 1);
|
||||
|
||||
if (id < 0 || id >= GAUGE_COUNT) {
|
||||
sendReply("ERR BAD_ID");
|
||||
@@ -808,8 +831,8 @@ bool parseSweep(const String& line) {
|
||||
// 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?") {
|
||||
bool parsePosQuery(const char* line) {
|
||||
if (strcmp(line, "POS?") == 0) {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
CMD_PORT.print("POS ");
|
||||
CMD_PORT.print(i);
|
||||
@@ -832,8 +855,8 @@ bool parsePosQuery(const String& line) {
|
||||
|
||||
// Answers `CFG?` with speed and acceleration for every gauge.
|
||||
// Emits one `CFG <id> <maxSpeed> <accel>` line per gauge, then replies `OK`.
|
||||
bool parseCfgQuery(const String& line) {
|
||||
if (line == "CFG?") {
|
||||
bool parseCfgQuery(const char* line) {
|
||||
if (strcmp(line, "CFG?") == 0) {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
CMD_PORT.print("CFG ");
|
||||
CMD_PORT.print(i);
|
||||
@@ -850,8 +873,8 @@ bool parseCfgQuery(const String& line) {
|
||||
|
||||
// Answers the mandatory life question: are you there?
|
||||
// Reply: `PONG`.
|
||||
bool parsePing(const String& line) {
|
||||
if (line == "PING") {
|
||||
bool parsePing(const char* line) {
|
||||
if (strcmp(line, "PING") == 0) {
|
||||
sendReply("PONG");
|
||||
return true;
|
||||
}
|
||||
@@ -860,17 +883,17 @@ bool parsePing(const String& line) {
|
||||
|
||||
// Parses `VFD <text>` where <text> is up to four hex characters with optional `.` and `!` suffixes.
|
||||
// Replies: `OK`, `ERR BAD_VFD`.
|
||||
bool parseVfd(const String& line) {
|
||||
if (line == "VFD") {
|
||||
bool parseVfd(const char* line) {
|
||||
if (strcmp(line, "VFD") == 0) {
|
||||
vfd::clear();
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!line.startsWith("VFD ")) return false;
|
||||
if (strncmp(line, "VFD ", 4) != 0) return false;
|
||||
|
||||
const String payload = line.substring(4);
|
||||
if (payload.length() == 0) {
|
||||
const char* payload = line + 4;
|
||||
if (*payload == '\0') {
|
||||
vfd::clear();
|
||||
sendReply("OK");
|
||||
return true;
|
||||
@@ -886,8 +909,8 @@ bool parseVfd(const String& line) {
|
||||
|
||||
// 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?") {
|
||||
bool parseLedQuery(const char* line) {
|
||||
if (strcmp(line, "LED?") == 0) {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
for (uint8_t j = 0; j < gaugeLedCount[i]; j++) {
|
||||
CRGB c = readLed(gaugeLedOffset[i] + j);
|
||||
@@ -911,10 +934,10 @@ bool parseLedQuery(const String& line) {
|
||||
|
||||
// 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) {
|
||||
bool parseLed(const char* line) {
|
||||
int id, r, g, b;
|
||||
char idxToken[16];
|
||||
if (sscanf(line.c_str(), "LED %d %15s %d %d %d", &id, idxToken, &r, &g, &b) == 5) {
|
||||
if (sscanf(line, "LED %d %15s %d %d %d", &id, idxToken, &r, &g, &b) == 5) {
|
||||
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
||||
char* dash = strchr(idxToken, '-');
|
||||
int idxFirst = atoi(idxToken);
|
||||
@@ -936,11 +959,11 @@ bool parseLed(const String& line) {
|
||||
|
||||
// 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) {
|
||||
bool parseBlink(const char* line) {
|
||||
int id, onMs, offMs, r, g, b;
|
||||
char idxToken[16];
|
||||
// Optional RGB values let BLINK either reuse or replace the current colour.
|
||||
int count = sscanf(line.c_str(), "BLINK %d %15s %d %d %d %d %d",
|
||||
int count = sscanf(line, "BLINK %d %15s %d %d %d %d %d",
|
||||
&id, idxToken, &onMs, &offMs, &r, &g, &b);
|
||||
if (count != 4 && count != 7) return false;
|
||||
|
||||
@@ -984,10 +1007,10 @@ bool parseBlink(const String& line) {
|
||||
|
||||
// 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) {
|
||||
bool parseBreathe(const char* line) {
|
||||
int id, periodMs, r, g, b;
|
||||
char idxToken[16];
|
||||
if (sscanf(line.c_str(), "BREATHE %d %15s %d %d %d %d",
|
||||
if (sscanf(line, "BREATHE %d %15s %d %d %d %d",
|
||||
&id, idxToken, &periodMs, &r, &g, &b) != 6) return false;
|
||||
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
||||
char* dash = strchr(idxToken, '-');
|
||||
@@ -1017,10 +1040,10 @@ bool parseBreathe(const String& line) {
|
||||
|
||||
// Parses `DFLASH ...` and assigns the double-flash pattern.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_IDX`.
|
||||
bool parseDflash(const String& line) {
|
||||
bool parseDflash(const char* line) {
|
||||
int id, r, g, b;
|
||||
char idxToken[16];
|
||||
if (sscanf(line.c_str(), "DFLASH %d %15s %d %d %d",
|
||||
if (sscanf(line, "DFLASH %d %15s %d %d %d",
|
||||
&id, idxToken, &r, &g, &b) != 5) return false;
|
||||
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
||||
char* dash = strchr(idxToken, '-');
|
||||
@@ -1104,7 +1127,7 @@ void updateBlink() {
|
||||
|
||||
// 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) {
|
||||
void processLine(const char* line) {
|
||||
if (parseSet(line)) return;
|
||||
if (parseSpeed(line)) return;
|
||||
if (parseAccel(line)) return;
|
||||
@@ -1132,16 +1155,24 @@ void readCommands() {
|
||||
char c = (char)CMD_PORT.read();
|
||||
|
||||
if (c == '\n') {
|
||||
rxLine.trim();
|
||||
if (rxLine.length() > 0) {
|
||||
processLine(rxLine);
|
||||
// Trim trailing whitespace.
|
||||
while (rxLen > 0 && (rxBuf[rxLen - 1] == ' ' || rxBuf[rxLen - 1] == '\t')) {
|
||||
rxLen--;
|
||||
}
|
||||
rxLine = "";
|
||||
rxBuf[rxLen] = '\0';
|
||||
// Skip leading whitespace.
|
||||
char* start = rxBuf;
|
||||
while (*start == ' ' || *start == '\t') start++;
|
||||
if (*start) {
|
||||
processLine(start);
|
||||
}
|
||||
rxLen = 0;
|
||||
} else if (c != '\r') {
|
||||
rxLine += c;
|
||||
if (rxLine.length() > 120) {
|
||||
rxLine = "";
|
||||
if (rxLen >= kRxBufSize - 1) {
|
||||
rxLen = 0;
|
||||
sendReply("ERR TOO_LONG");
|
||||
} else {
|
||||
rxBuf[rxLen++] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1168,11 +1199,17 @@ void setup() {
|
||||
gauges[i].lastUpdateMicros = micros();
|
||||
}
|
||||
|
||||
// Flatten the per-gauge LED counts into offsets on the shared strip.
|
||||
// Flatten the per-gauge LED counts into offsets on the shared strip,
|
||||
// and precompute the GRB-vs-RGB swap flag so writeLed/readLed don't have
|
||||
// to walk the gauge table on every call.
|
||||
uint8_t ledOff = 0;
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
gaugeLedCount[i] = cstrLen(gaugePins[i].ledOrder);
|
||||
gaugeLedOffset[i] = ledOff;
|
||||
for (uint8_t j = 0; j < gaugeLedCount[i]; j++) {
|
||||
char c = gaugePins[i].ledOrder[j];
|
||||
ledNeedsSwap[ledOff + j] = (c == 'G' || c == 'g');
|
||||
}
|
||||
ledOff += gaugeLedCount[i];
|
||||
}
|
||||
FastLED.addLeds<WS2812B, LED_DATA_PIN, RGB>(leds, TOTAL_LEDS);
|
||||
|
||||
46
changes.md
Normal file
46
changes.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# Changes
|
||||
|
||||
## 2026-04-27 — Arduino firmware refactor (`Gaugecontroller/Gaugecontroller.ino`)
|
||||
|
||||
### Non-blocking VFD multiplexer
|
||||
`vfd::refresh()` previously held each digit for 2000 µs via `delayMicroseconds`,
|
||||
which capped the effective stepper pulse rate at roughly 500 Hz regardless of
|
||||
`maxSpeed`. It now tracks `phaseStartMicros`/`phaseActive` and returns
|
||||
immediately while the digit is still being held; the main loop runs at
|
||||
microsecond cadence again and the configured `maxSpeed = 4000.0f` steps/s is
|
||||
actually achievable.
|
||||
|
||||
### Fixed-buffer command parser (no more `String` heap churn)
|
||||
Replaced `String rxLine` with `char rxBuf[128]` and converted the entire
|
||||
command pipeline to take `const char*`:
|
||||
|
||||
- `processLine`, `sendReply`, `vfd::parseCommand`
|
||||
- All `parse*` functions: `parseSet`, `parseSpeed`, `parseAccel`, `parseEnable`,
|
||||
`parseZero`, `parseHome`, `parseSweep`, `parsePosQuery`, `parseCfgQuery`,
|
||||
`parseLedQuery`, `parseLed`, `parseBlink`, `parseBreathe`, `parseDflash`,
|
||||
`parseVfd`, `parsePing`.
|
||||
|
||||
`parseSpeed` / `parseAccel` / `parseSweep` use `strncmp` + `atof` because the
|
||||
default AVR-libc `sscanf` doesn't support `%f`. No allocations on the command
|
||||
path; the Mega's heap no longer fragments over time.
|
||||
|
||||
### Cached `ledNeedsSwap[TOTAL_LEDS]`
|
||||
Per-LED RGB-vs-GRB swap flag is now precomputed once in `setup()` from
|
||||
`gaugePins[].ledOrder`. `encodeForStrip` is a single array index instead of
|
||||
walking the gauge table on every LED read/write.
|
||||
|
||||
### Cached step direction per gauge
|
||||
Added `Gauge.lastDir`. `setDir()` skips the DIR-pin `digitalWrite` when the
|
||||
direction hasn't flipped (the common case during a step run) and adds a 1 µs
|
||||
DIR-to-STEP setup delay only when it actually flips.
|
||||
|
||||
### Cleanups
|
||||
- Removed the `absf` helper; use `fabsf` consistently.
|
||||
- Removed the `+ 0.0001f` epsilon in the trapezoidal braking-distance divisor.
|
||||
`parseAccel` already rejects `accel <= 0`, so the divisor is always positive.
|
||||
- Fixed the `<r> <ig> <b>` typo to `<r> <g> <b>` in the protocol comment for
|
||||
`DFLASH`.
|
||||
|
||||
### Build verification
|
||||
`arduino-cli compile --fqbn arduino:avr:mega Gaugecontroller`:
|
||||
17758 B flash (6%), 1845 B SRAM (22%).
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user