Refactor some more
This commit is contained in:
@@ -112,6 +112,7 @@ esp32c3-wiji/
|
||||
├── src/
|
||||
│ ├── Config.h # Pins, Constants, UUIDs
|
||||
│ ├── BleManager.cpp # BLE setup, callbacks, notifications
|
||||
│ ├── CommandParser.cpp # Parses BLE string commands
|
||||
│ ├── MotorController.cpp # AccelStepper wrap, homing state, idle timeout
|
||||
│ ├── StatusLed.cpp # Onboard LED feedback logic
|
||||
│ └── main.cpp # Main app glue, command router
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
#include "CommandParser.h"
|
||||
|
||||
CommandParser::CommandParser(MotorController& motorCtrl, BleManager& bleMgr)
|
||||
: motorController(motorCtrl), bleManager(bleMgr) {}
|
||||
|
||||
void CommandParser::parse(const String& cmd) {
|
||||
// S1+<n> or S1-<n> (Relative)
|
||||
if (cmd.startsWith("S1") && cmd.length() > 2) {
|
||||
long steps = cmd.substring(2).toInt();
|
||||
motorController.moveRelative1(steps);
|
||||
return;
|
||||
}
|
||||
// S2+<n> or S2-<n> (Relative)
|
||||
if (cmd.startsWith("S2") && cmd.length() > 2) {
|
||||
long steps = cmd.substring(2).toInt();
|
||||
motorController.moveRelative2(steps);
|
||||
return;
|
||||
}
|
||||
// X:<n1>,<n2> (Absolute combined)
|
||||
if (cmd.startsWith("X:")) {
|
||||
int commaIdx = cmd.indexOf(',');
|
||||
if (commaIdx != -1) {
|
||||
long target1 = cmd.substring(2, commaIdx).toInt();
|
||||
long target2 = cmd.substring(commaIdx + 1).toInt();
|
||||
motorController.moveAbsolute(target1, target2);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// SPD:<n>
|
||||
if (cmd.startsWith("SPD:")) {
|
||||
float spd = cmd.substring(4).toFloat();
|
||||
motorController.setMaxSpeed(spd);
|
||||
return;
|
||||
}
|
||||
// ACC:<n>
|
||||
if (cmd.startsWith("ACC:")) {
|
||||
float acc = cmd.substring(4).toFloat();
|
||||
motorController.setAcceleration(acc);
|
||||
return;
|
||||
}
|
||||
// HOME1
|
||||
if (cmd == "HOME1") {
|
||||
motorController.home1();
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
return;
|
||||
}
|
||||
// HOME2
|
||||
if (cmd == "HOME2") {
|
||||
motorController.home2();
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
return;
|
||||
}
|
||||
// HOMEALL
|
||||
if (cmd == "HOMEALL") {
|
||||
motorController.homeAll();
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
return;
|
||||
}
|
||||
// ZEROALL
|
||||
if (cmd == "ZEROALL") {
|
||||
motorController.zeroAll();
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
return;
|
||||
}
|
||||
// DISABLE
|
||||
if (cmd == "DISABLE") {
|
||||
motorController.disableMotors();
|
||||
return;
|
||||
}
|
||||
// POS – explicit position request
|
||||
if (cmd == "POS") {
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
return;
|
||||
}
|
||||
Serial.printf("[CMD] Unknown command: '%s'\n", cmd.c_str());
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include "MotorController.h"
|
||||
#include "BleManager.h"
|
||||
|
||||
class CommandParser {
|
||||
public:
|
||||
CommandParser(MotorController& motorCtrl, BleManager& bleMgr);
|
||||
void parse(const String& cmd);
|
||||
|
||||
private:
|
||||
MotorController& motorController;
|
||||
BleManager& bleManager;
|
||||
};
|
||||
+28
-94
@@ -10,83 +10,17 @@
|
||||
#include "BleManager.h"
|
||||
#include "MotorController.h"
|
||||
#include "StatusLed.h"
|
||||
#include "CommandParser.h"
|
||||
|
||||
BleManager bleManager;
|
||||
MotorController motorController;
|
||||
StatusLed statusLed;
|
||||
CommandParser commandParser(motorController, bleManager);
|
||||
|
||||
unsigned long lastNotify = 0;
|
||||
|
||||
void onCommandReceived(const String &cmd) {
|
||||
// S1+<n> or S1-<n> (Relative)
|
||||
if (cmd.startsWith("S1") && cmd.length() > 2) {
|
||||
long steps = cmd.substring(2).toInt();
|
||||
motorController.moveRelative1(steps);
|
||||
return;
|
||||
}
|
||||
// S2+<n> or S2-<n> (Relative)
|
||||
if (cmd.startsWith("S2") && cmd.length() > 2) {
|
||||
long steps = cmd.substring(2).toInt();
|
||||
motorController.moveRelative2(steps);
|
||||
return;
|
||||
}
|
||||
// X:<n1>,<n2> (Absolute combined)
|
||||
if (cmd.startsWith("X:")) {
|
||||
int commaIdx = cmd.indexOf(',');
|
||||
if (commaIdx != -1) {
|
||||
long target1 = cmd.substring(2, commaIdx).toInt();
|
||||
long target2 = cmd.substring(commaIdx + 1).toInt();
|
||||
motorController.moveAbsolute(target1, target2);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// SPD:<n>
|
||||
if (cmd.startsWith("SPD:")) {
|
||||
float spd = cmd.substring(4).toFloat();
|
||||
motorController.setMaxSpeed(spd);
|
||||
return;
|
||||
}
|
||||
// ACC:<n>
|
||||
if (cmd.startsWith("ACC:")) {
|
||||
float acc = cmd.substring(4).toFloat();
|
||||
motorController.setAcceleration(acc);
|
||||
return;
|
||||
}
|
||||
// HOME1
|
||||
if (cmd == "HOME1") {
|
||||
motorController.home1();
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
return;
|
||||
}
|
||||
// HOME2
|
||||
if (cmd == "HOME2") {
|
||||
motorController.home2();
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
return;
|
||||
}
|
||||
// HOMEALL
|
||||
if (cmd == "HOMEALL") {
|
||||
motorController.homeAll();
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
return;
|
||||
}
|
||||
// ZEROALL
|
||||
if (cmd == "ZEROALL") {
|
||||
motorController.zeroAll();
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
return;
|
||||
}
|
||||
// DISABLE
|
||||
if (cmd == "DISABLE") {
|
||||
motorController.disableMotors();
|
||||
return;
|
||||
}
|
||||
// POS – explicit position request
|
||||
if (cmd == "POS") {
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
return;
|
||||
}
|
||||
Serial.printf("[CMD] Unknown command: '%s'\n", cmd.c_str());
|
||||
commandParser.parse(cmd);
|
||||
}
|
||||
|
||||
void onIdleTimeout() {
|
||||
@@ -94,35 +28,35 @@ void onIdleTimeout() {
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.setTxTimeoutMs(0);
|
||||
Serial.begin(115200);
|
||||
delay(1000); // Wait for USB CDC to enumerate
|
||||
Serial.println("\n[BOOT] WijiBoard Stepper Controller");
|
||||
Serial.setTxTimeoutMs(0);
|
||||
Serial.begin(115200);
|
||||
delay(1000); // Wait for USB CDC to enumerate
|
||||
Serial.println("\n[BOOT] WijiBoard Stepper Controller");
|
||||
|
||||
statusLed.init();
|
||||
motorController.init();
|
||||
motorController.setOnTimeoutCallback(onIdleTimeout);
|
||||
bleManager.init(onCommandReceived);
|
||||
statusLed.init();
|
||||
motorController.init();
|
||||
motorController.setOnTimeoutCallback(onIdleTimeout);
|
||||
bleManager.init(onCommandReceived);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static bool wasMoving = false;
|
||||
|
||||
motorController.update();
|
||||
bool isMoving = motorController.isMoving();
|
||||
statusLed.update(bleManager.isConnected(), isMoving);
|
||||
static bool wasMoving = false;
|
||||
|
||||
motorController.update();
|
||||
bool isMoving = motorController.isMoving();
|
||||
statusLed.update(bleManager.isConnected(), isMoving);
|
||||
|
||||
if (bleManager.isConnected()) {
|
||||
unsigned long now = millis();
|
||||
// Notify periodically while moving, or once when stopping
|
||||
if (isMoving && (now - lastNotify >= NOTIFY_INTERVAL_MS)) {
|
||||
lastNotify = now;
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
} else if (wasMoving && !isMoving) {
|
||||
// Just stopped moving
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
if (bleManager.isConnected()) {
|
||||
unsigned long now = millis();
|
||||
// Notify periodically while moving, or once when stopping
|
||||
if (isMoving && (now - lastNotify >= NOTIFY_INTERVAL_MS)) {
|
||||
lastNotify = now;
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
} else if (wasMoving && !isMoving) {
|
||||
// Just stopped moving
|
||||
bleManager.notifyPosition(motorController.getPositionString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wasMoving = isMoving;
|
||||
|
||||
wasMoving = isMoving;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user