refactor: uniform sscanf parsing in parseSpeed, parseAccel, parseSweep
This commit is contained in:
@@ -453,52 +453,30 @@ 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int id; float speed;
|
||||
if (sscanf(line.c_str(), "SPEED %d %f", &id, &speed) == 2) {
|
||||
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;
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int id; float accel;
|
||||
if (sscanf(line.c_str(), "ACCEL %d %f", &id, &accel) == 2) {
|
||||
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;
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parses `ENABLE <id> <0|1>` and toggles the selected driver.
|
||||
// Replies: `OK`, `ERR BAD_ID`.
|
||||
@@ -567,24 +545,10 @@ 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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int id; float accel, speed;
|
||||
if (sscanf(line.c_str(), "SWEEP %d %f %f", &id, &accel, &speed) == 3) {
|
||||
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
||||
Gauge& g = gauges[id];
|
||||
|
||||
if (accel <= 0.0f || speed <= 0.0f) {
|
||||
g.sweepEnabled = false;
|
||||
g.velocity = 0.0f;
|
||||
@@ -592,7 +556,6 @@ bool parseSweep(const String& line) {
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
|
||||
g.accel = accel;
|
||||
g.maxSpeed = speed;
|
||||
g.sweepEnabled = true;
|
||||
@@ -601,6 +564,8 @@ bool parseSweep(const String& line) {
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Answers `POS?` with current motion state for every gauge.
|
||||
// Emits one `POS <id> <cur> <tgt> <homed> <homingState> <sweep>` line per gauge,
|
||||
|
||||
Reference in New Issue
Block a user