From e1849f0dd19c354708e1df1522c4c76730e42f2c Mon Sep 17 00:00:00 2001 From: "Adrian A. Baumann" Date: Thu, 21 May 2026 22:10:43 +0200 Subject: [PATCH] refactor: uniform sscanf parsing in parseSpeed, parseAccel, parseSweep --- Gaugecontroller/Gaugecontroller.ino | 97 +++++++++-------------------- 1 file changed, 31 insertions(+), 66 deletions(-) diff --git a/Gaugecontroller/Gaugecontroller.ino b/Gaugecontroller/Gaugecontroller.ino index 62be8ab..ce1164a 100644 --- a/Gaugecontroller/Gaugecontroller.ino +++ b/Gaugecontroller/Gaugecontroller.ino @@ -453,51 +453,29 @@ bool parseSet(const String& line) { // Parses `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"); + 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; } - if (speed <= 0.0f) { - sendReply("ERR BAD_SPEED"); - return true; - } - - gauges[id].maxSpeed = speed; - sendReply("OK"); - return true; + return false; } // Parses `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"); + 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; } - if (accel <= 0.0f) { - sendReply("ERR BAD_ACCEL"); - return true; - } - - gauges[id].accel = accel; - sendReply("OK"); - return true; + return false; } // Parses `ENABLE <0|1>` and toggles the selected driver. @@ -567,39 +545,26 @@ bool parseHome(const String& line) { // Parses `SWEEP ` 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; - } - - Gauge& g = gauges[id]; - - if (accel <= 0.0f || speed <= 0.0f) { - g.sweepEnabled = false; - g.velocity = 0.0f; - stopTimerStepping(id); + 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; + stopTimerStepping(id); + sendReply("OK"); + return true; + } + g.accel = accel; + g.maxSpeed = speed; + g.sweepEnabled = true; + g.sweepTowardMax = true; + atomicWriteLong(g.targetPos, g.maxPos); sendReply("OK"); return true; } - - g.accel = accel; - g.maxSpeed = speed; - g.sweepEnabled = true; - g.sweepTowardMax = true; - atomicWriteLong(g.targetPos, g.maxPos); - sendReply("OK"); - return true; + return false; } // Answers `POS?` with current motion state for every gauge.