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; }
int id = line.substring(firstSpace + 1, secondSpace).toInt();
float speed = line.substring(secondSpace + 1).toFloat();
if (id < 0 || id >= GAUGE_COUNT) {
sendReply("ERR BAD_ID");
return true;
}
if (speed <= 0.0f) {
sendReply("ERR BAD_SPEED");
return true;
}
gauges[id].maxSpeed = speed; gauges[id].maxSpeed = speed;
sendReply("OK"); sendReply("OK");
return true; return true;
}
return false;
} }
// 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; }
int id = line.substring(firstSpace + 1, secondSpace).toInt();
float accel = line.substring(secondSpace + 1).toFloat();
if (id < 0 || id >= GAUGE_COUNT) {
sendReply("ERR BAD_ID");
return true;
}
if (accel <= 0.0f) {
sendReply("ERR BAD_ACCEL");
return true;
}
gauges[id].accel = accel; gauges[id].accel = accel;
sendReply("OK"); sendReply("OK");
return true; return true;
}
return false;
} }
// Parses `ENABLE <id> <0|1>` and toggles the selected driver. // Parses `ENABLE <id> <0|1>` and toggles the selected driver.
@@ -567,24 +545,10 @@ 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; }
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();
if (id < 0 || id >= GAUGE_COUNT) {
sendReply("ERR BAD_ID");
return true;
}
Gauge& g = gauges[id]; Gauge& g = gauges[id];
if (accel <= 0.0f || speed <= 0.0f) { if (accel <= 0.0f || speed <= 0.0f) {
g.sweepEnabled = false; g.sweepEnabled = false;
g.velocity = 0.0f; g.velocity = 0.0f;
@@ -592,7 +556,6 @@ bool parseSweep(const String& line) {
sendReply("OK"); sendReply("OK");
return true; return true;
} }
g.accel = accel; g.accel = accel;
g.maxSpeed = speed; g.maxSpeed = speed;
g.sweepEnabled = true; g.sweepEnabled = true;
@@ -600,6 +563,8 @@ bool parseSweep(const String& line) {
atomicWriteLong(g.targetPos, g.maxPos); atomicWriteLong(g.targetPos, g.maxPos);
sendReply("OK"); sendReply("OK");
return true; return true;
}
return false;
} }
// Answers `POS?` with current motion state for every gauge. // Answers `POS?` with current motion state for every gauge.