Refactor some more
This commit is contained in:
@@ -112,6 +112,7 @@ esp32c3-wiji/
|
|||||||
├── src/
|
├── src/
|
||||||
│ ├── Config.h # Pins, Constants, UUIDs
|
│ ├── Config.h # Pins, Constants, UUIDs
|
||||||
│ ├── BleManager.cpp # BLE setup, callbacks, notifications
|
│ ├── BleManager.cpp # BLE setup, callbacks, notifications
|
||||||
|
│ ├── CommandParser.cpp # Parses BLE string commands
|
||||||
│ ├── MotorController.cpp # AccelStepper wrap, homing state, idle timeout
|
│ ├── MotorController.cpp # AccelStepper wrap, homing state, idle timeout
|
||||||
│ ├── StatusLed.cpp # Onboard LED feedback logic
|
│ ├── StatusLed.cpp # Onboard LED feedback logic
|
||||||
│ └── main.cpp # Main app glue, command router
|
│ └── 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 "BleManager.h"
|
||||||
#include "MotorController.h"
|
#include "MotorController.h"
|
||||||
#include "StatusLed.h"
|
#include "StatusLed.h"
|
||||||
|
#include "CommandParser.h"
|
||||||
|
|
||||||
BleManager bleManager;
|
BleManager bleManager;
|
||||||
MotorController motorController;
|
MotorController motorController;
|
||||||
StatusLed statusLed;
|
StatusLed statusLed;
|
||||||
|
CommandParser commandParser(motorController, bleManager);
|
||||||
|
|
||||||
unsigned long lastNotify = 0;
|
unsigned long lastNotify = 0;
|
||||||
|
|
||||||
void onCommandReceived(const String &cmd) {
|
void onCommandReceived(const String &cmd) {
|
||||||
// S1+<n> or S1-<n> (Relative)
|
commandParser.parse(cmd);
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onIdleTimeout() {
|
void onIdleTimeout() {
|
||||||
@@ -94,35 +28,35 @@ void onIdleTimeout() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.setTxTimeoutMs(0);
|
Serial.setTxTimeoutMs(0);
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
delay(1000); // Wait for USB CDC to enumerate
|
delay(1000); // Wait for USB CDC to enumerate
|
||||||
Serial.println("\n[BOOT] WijiBoard Stepper Controller");
|
Serial.println("\n[BOOT] WijiBoard Stepper Controller");
|
||||||
|
|
||||||
statusLed.init();
|
statusLed.init();
|
||||||
motorController.init();
|
motorController.init();
|
||||||
motorController.setOnTimeoutCallback(onIdleTimeout);
|
motorController.setOnTimeoutCallback(onIdleTimeout);
|
||||||
bleManager.init(onCommandReceived);
|
bleManager.init(onCommandReceived);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
static bool wasMoving = false;
|
static bool wasMoving = false;
|
||||||
|
|
||||||
motorController.update();
|
motorController.update();
|
||||||
bool isMoving = motorController.isMoving();
|
bool isMoving = motorController.isMoving();
|
||||||
statusLed.update(bleManager.isConnected(), isMoving);
|
statusLed.update(bleManager.isConnected(), isMoving);
|
||||||
|
|
||||||
if (bleManager.isConnected()) {
|
if (bleManager.isConnected()) {
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
// Notify periodically while moving, or once when stopping
|
// Notify periodically while moving, or once when stopping
|
||||||
if (isMoving && (now - lastNotify >= NOTIFY_INTERVAL_MS)) {
|
if (isMoving && (now - lastNotify >= NOTIFY_INTERVAL_MS)) {
|
||||||
lastNotify = now;
|
lastNotify = now;
|
||||||
bleManager.notifyPosition(motorController.getPositionString());
|
bleManager.notifyPosition(motorController.getPositionString());
|
||||||
} else if (wasMoving && !isMoving) {
|
} else if (wasMoving && !isMoving) {
|
||||||
// Just stopped moving
|
// Just stopped moving
|
||||||
bleManager.notifyPosition(motorController.getPositionString());
|
bleManager.notifyPosition(motorController.getPositionString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
wasMoving = isMoving;
|
||||||
wasMoving = isMoving;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user