initial version with serial link

This commit is contained in:
2026-01-18 02:25:38 +01:00
parent d432db9985
commit 2311647885
11 changed files with 582 additions and 463 deletions

View File

@@ -0,0 +1,54 @@
COMMAND BEHAVIOR SUMMARY
========================
PING
----
Format:
PING
Response:
Analog_System_Monitor_<version>
Purpose:
- Confirms the device is alive
- Allows PC-side auto-discovery
- Returns firmware version
SETALL
------
Format:
SETALL: v0,v1,v2,v3,v4,v5,v6,v7
Rules:
- Exactly 8 comma-separated values
- Each value must be numeric
- Each value must be between 0 and 100
- No extra values allowed
- No missing values allowed
- No trailing commas or extra characters
Success Response:
OK
Error Response:
ERROR
Purpose:
- Updates all 8 channels in one atomic operation
- Applies calibration to each channel
- Resets watchdog timer
UNKNOWN / MALFORMED COMMANDS
----------------------------
Format:
Anything not matching PING or a valid SETALL command
Response:
ERROR
Purpose:
- Keeps protocol strict and predictable
- Prevents accidental meter movement
- Simplifies debugging

View File

@@ -103,19 +103,68 @@ void loop() {
continue;
}
int eq = s.indexOf('=');
if (eq > 0) {
int ch = s.substring(0, eq).toInt();
float val = s.substring(eq + 1).toFloat();
if (ch >= 0 && ch < NUM_CHANNELS && val >= 0.0f && val <= 100.0f) {
// --- Batch update command ---
if (s.startsWith("SETALL:")) {
String payload = s.substring(7);
payload.trim();
currentDuty[ch] = val;
float newValues[NUM_CHANNELS];
int count = 0;
bool error = false;
// Parse up to NUM_CHANNELS values
while (payload.length() > 0 && count < NUM_CHANNELS) {
int comma = payload.indexOf(',');
String part;
if (comma >= 0) {
part = payload.substring(0, comma);
payload = payload.substring(comma + 1);
} else {
part = payload;
payload = "";
}
part.trim();
if (part.length() == 0) {
error = true;
break;
}
float val = part.toFloat();
if (val < 0.0f || val > 100.0f) {
error = true;
break;
}
newValues[count++] = val;
}
// Conditions for a valid SETALL:
// - exactly NUM_CHANNELS values parsed
// - no leftover payload
// - no parse/range errors
if (error || count != NUM_CHANNELS || payload.length() > 0) {
Serial.println("ERROR");
continue;
}
// All good → apply values
for (int ch = 0; ch < NUM_CHANNELS; ch++) {
currentDuty[ch] = newValues[ch];
float calibratedDuty = applyCalibration(ch, currentDuty[ch]);
pwm[ch]->setPWM(pwmPins[ch], pwmFrequency, calibratedDuty);
lastSerialTime = millis(); // reset watchdog
}
Serial.println("OK");
lastSerialTime = millis();
continue;
}
// --- Unknown command ---
if (s.length() > 0) {
Serial.println("ERROR");
}
}