refactor: uniform sscanf parsing in parseSpeed, parseAccel, parseSweep

This commit is contained in:
2026-05-21 22:10:43 +02:00
parent 30dfcc59df
commit e1849f0dd1

View File

@@ -453,51 +453,29 @@ bool parseSet(const String& line) {
// Parses `SPEED <id> <speed>` and updates the max step rate. // Parses `SPEED <id> <speed>` and updates the max step rate.
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_SPEED`. // Replies: `OK`, `ERR BAD_ID`, `ERR BAD_SPEED`.
bool parseSpeed(const String& line) { bool parseSpeed(const String& line) {
int firstSpace = line.indexOf(' '); int id; float speed;
int secondSpace = line.indexOf(' ', firstSpace + 1); if (sscanf(line.c_str(), "SPEED %d %f", &id, &speed) == 2) {
if (firstSpace < 0 || secondSpace < 0) return false; if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
if (line.substring(0, firstSpace) != "SPEED") return false; if (speed <= 0.0f) { sendReply("ERR BAD_SPEED"); return true; }
gauges[id].maxSpeed = speed;
int id = line.substring(firstSpace + 1, secondSpace).toInt(); sendReply("OK");
float speed = line.substring(secondSpace + 1).toFloat();
if (id < 0 || id >= GAUGE_COUNT) {
sendReply("ERR BAD_ID");
return true; return true;
} }
if (speed <= 0.0f) { return false;
sendReply("ERR BAD_SPEED");
return true;
}
gauges[id].maxSpeed = speed;
sendReply("OK");
return true;
} }
// Parses `ACCEL <id> <accel>` and updates the acceleration limit. // Parses `ACCEL <id> <accel>` and updates the acceleration limit.
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_ACCEL`. // Replies: `OK`, `ERR BAD_ID`, `ERR BAD_ACCEL`.
bool parseAccel(const String& line) { bool parseAccel(const String& line) {
int firstSpace = line.indexOf(' '); int id; float accel;
int secondSpace = line.indexOf(' ', firstSpace + 1); if (sscanf(line.c_str(), "ACCEL %d %f", &id, &accel) == 2) {
if (firstSpace < 0 || secondSpace < 0) return false; if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
if (line.substring(0, firstSpace) != "ACCEL") return false; if (accel <= 0.0f) { sendReply("ERR BAD_ACCEL"); return true; }
gauges[id].accel = accel;
int id = line.substring(firstSpace + 1, secondSpace).toInt(); sendReply("OK");
float accel = line.substring(secondSpace + 1).toFloat();
if (id < 0 || id >= GAUGE_COUNT) {
sendReply("ERR BAD_ID");
return true; return true;
} }
if (accel <= 0.0f) { return false;
sendReply("ERR BAD_ACCEL");
return true;
}
gauges[id].accel = accel;
sendReply("OK");
return true;
} }
// Parses `ENABLE <id> <0|1>` and toggles the selected driver. // Parses `ENABLE <id> <0|1>` and toggles the selected driver.
@@ -567,39 +545,26 @@ bool parseHome(const String& line) {
// Parses `SWEEP <id> <accel> <speed>` and enables or disables end-to-end motion. // Parses `SWEEP <id> <accel> <speed>` and enables or disables end-to-end motion.
// Replies: `OK`, `ERR BAD_ID`. // Replies: `OK`, `ERR BAD_ID`.
bool parseSweep(const String& line) { bool parseSweep(const String& line) {
int firstSpace = line.indexOf(' '); int id; float accel, speed;
int secondSpace = line.indexOf(' ', firstSpace + 1); if (sscanf(line.c_str(), "SWEEP %d %f %f", &id, &accel, &speed) == 3) {
int thirdSpace = line.indexOf(' ', secondSpace + 1); if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
Gauge& g = gauges[id];
if (firstSpace < 0 || secondSpace < 0 || thirdSpace < 0) return false; if (accel <= 0.0f || speed <= 0.0f) {
if (line.substring(0, firstSpace) != "SWEEP") return false; g.sweepEnabled = false;
g.velocity = 0.0f;
int id = line.substring(firstSpace + 1, secondSpace).toInt(); stopTimerStepping(id);
float accel = line.substring(secondSpace + 1, thirdSpace).toFloat(); sendReply("OK");
float speed = line.substring(thirdSpace + 1).toFloat(); return true;
}
if (id < 0 || id >= GAUGE_COUNT) { g.accel = accel;
sendReply("ERR BAD_ID"); g.maxSpeed = speed;
return true; g.sweepEnabled = true;
} g.sweepTowardMax = true;
atomicWriteLong(g.targetPos, g.maxPos);
Gauge& g = gauges[id];
if (accel <= 0.0f || speed <= 0.0f) {
g.sweepEnabled = false;
g.velocity = 0.0f;
stopTimerStepping(id);
sendReply("OK"); sendReply("OK");
return true; return true;
} }
return false;
g.accel = accel;
g.maxSpeed = speed;
g.sweepEnabled = true;
g.sweepTowardMax = true;
atomicWriteLong(g.targetPos, g.maxPos);
sendReply("OK");
return true;
} }
// Answers `POS?` with current motion state for every gauge. // Answers `POS?` with current motion state for every gauge.