refactor: Rewiring MQTT constants to toplevel defined constants

This commit is contained in:
2025-02-15 16:43:26 +01:00
parent c872f1a9ae
commit 199007c0c5
3 changed files with 70 additions and 38 deletions
+3
View File
@@ -0,0 +1,3 @@
[submodule "core"]
path = core
url = git@github.com:Gustice/EspIdfDevicePlatform.git
Submodule
+1
Submodule core added at 13d6532a92
+65 -37
View File
@@ -18,12 +18,21 @@ struct {
} ConfigKeys; } ConfigKeys;
// MQTT Topics // MQTT Topics
// Subscriptions: struct {
// <mqttPrefix>/watch/MoveTo // Subscriptions:
// <mqttPrefix>/watch/Shuffle const char *moveTo = /* <mqttPrefix>/ */ "watch/moveTo";
// Publishes: const char *startShuffle = /* <mqttPrefix>/ */ "watch/startShuffle";
// <mqttPrefix>/watch/inPosition // Publishes:
// <mqttPrefix>/watch/changingPosition const char *inPosition = /* <mqttPrefix>/ */ "watch/inPosition";
const char *changingPosition = /* <mqttPrefix>/ */ "watch/changingPosition";
} MqttTopics;
// ToDo:
// - MQTT Integration
// - OTA
// - Syslog heartbeat
// - Streamlined DMX
// -
#include "watchMain.hpp" #include "watchMain.hpp"
@@ -39,7 +48,7 @@ struct DeviceState {
float speed = 1.0; // Rad/sec float speed = 1.0; // Rad/sec
float accel = 1.0; // Rad/sec^2 float accel = 1.0; // Rad/sec^2
Color color{0x80, 0x80, 0x80}; Color color{0x80, 0x80, 0x80};
uint8_t pictogram{}; uint8_t pictogram{}; // 0..5 active Pictogramm
}; };
enum class MoveType { enum class MoveType {
@@ -101,6 +110,22 @@ void testPictoIllum(Cli::CArgs args) {
} }
} }
void testMovement(Cli::CArgs args) {
ESP_LOGI(TAG, "on triggerMovement:");
if (args.size() != 1) {
pShell->write("need target position (1..6)'\n");
return;
}
try {
int p = std::stoi(args[0]);
activeState.profile = p;
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::StepTo, p));
} catch (const std::exception &e) {
ESP_LOGI(TAG, "Failed to parse %s and %s", args[0].c_str(), args[1].c_str());
}
}
void setConfig(Cli::CArgs args) { void setConfig(Cli::CArgs args) {
if (args.size() != 2) { if (args.size() != 2) {
pShell->write("Needs string key and value'\n"); pShell->write("Needs string key and value'\n");
@@ -144,22 +169,6 @@ void setDynamics(Cli::CArgs args) {
} }
} }
void triggerMovement(Cli::CArgs args) {
ESP_LOGI(TAG, "on triggerMovement:");
if (args.size() != 1) {
pShell->write("need target position'\n");
return;
}
try {
int p = std::stoi(args[0]);
activeState.profile = p;
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::StepTo, p));
} catch (const std::exception &e) {
ESP_LOGI(TAG, "Failed to parse %s and %s", args[0].c_str(), args[1].c_str());
}
}
void startDemoMovement(Cli::CArgs args) { void startDemoMovement(Cli::CArgs args) {
ESP_LOGI(TAG, "on startDemoMovement:"); ESP_LOGI(TAG, "on startDemoMovement:");
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::Demo)); commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::Demo));
@@ -196,8 +205,8 @@ static void telnetSocketTask(int addrFamily) {
{"dyn_set", "set dynamics of movement (velocity,accelerations) as floats", {"dyn_set", "set dynamics of movement (velocity,accelerations) as floats",
[](Cli::CArgs a) { setDynamics(a); }}); [](Cli::CArgs a) { setDynamics(a); }});
pShell->addBinding( pShell->addBinding(
{"move", "start movement (profile-idx,steps) as integers ", {"tstMove", "start movement (profile-idx,steps) as integers ",
[](Cli::CArgs a) { triggerMovement(a); }}); [](Cli::CArgs a) { testMovement(a); }});
pShell->addBinding( pShell->addBinding(
{"tstIllum", "test illumination command 0=off /1..6", {"tstIllum", "test illumination command 0=off /1..6",
@@ -261,7 +270,7 @@ void moveToSubs(std::string data) {
Syslog::logv(Syslog::Severity::Informational, "got MoveTo-Cmd->%i", p); Syslog::logv(Syslog::Severity::Informational, "got MoveTo-Cmd->%i", p);
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::StepTo, p)); commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::StepTo, p));
auto &client = MqttClient::getInstance(); auto &client = MqttClient::getInstance();
client.publish("watch/inPosition", data + "Picto"); client.publish(MqttTopics.inPosition, data + "Picto");
} catch (const std::exception &e) { } catch (const std::exception &e) {
ESP_LOGE(TAG, "Error to process data %s, with ex: %s", data.c_str(), e.what()); ESP_LOGE(TAG, "Error to process data %s, with ex: %s", data.c_str(), e.what());
} }
@@ -274,7 +283,7 @@ void startShuffle(std::string data) {
Syslog::logv(Syslog::Severity::Informational, "got Shuffle-Command"); Syslog::logv(Syslog::Severity::Informational, "got Shuffle-Command");
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::Shuffle, 0)); commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::Shuffle, 0));
auto &client = MqttClient::getInstance(); auto &client = MqttClient::getInstance();
client.publish("watch/changingPosition", "...."); client.publish(MqttTopics.changingPosition, "....");
} catch (const std::exception &e) { } catch (const std::exception &e) {
ESP_LOGE(TAG, "Error to process data %s, with ex: %s", data.c_str(), e.what()); ESP_LOGE(TAG, "Error to process data %s, with ex: %s", data.c_str(), e.what());
} }
@@ -479,7 +488,7 @@ void LightingTask() {
if (pos == 0) { if (pos == 0) {
Syslog::logv(Syslog::Severity::Informational, Syslog::logv(Syslog::Severity::Informational,
"Dimming picto-illum pos:%i ", lastPicto); "Dimming picto-illum pos:%i ", lastPicto);
processors[lastPicto-1].setEffect(Effect::macDark, &colorSource, 0); processors[lastPicto - 1].setEffect(Effect::macDark, &colorSource, 0);
} else if ((pos >= 1) && (pos <= 6)) { } else if ((pos >= 1) && (pos <= 6)) {
auto idx = pos - 1; auto idx = pos - 1;
Syslog::logv(Syslog::Severity::Informational, Syslog::logv(Syslog::Severity::Informational,
@@ -539,6 +548,9 @@ void MovementTask() {
ESP_LOGI("Stepper", "Find Home"); ESP_LOGI("Stepper", "Find Home");
stepper.findRef(); stepper.findRef();
// Illum 1
pictoIllumQue.enqueue(std::make_unique<PictoIllumCommand>(1));
ESP_LOGI(TAG, "Fist Position is %f.2", stepper.getScaledPos() * 6); ESP_LOGI(TAG, "Fist Position is %f.2", stepper.getScaledPos() * 6);
fos::Task::delayMs(1000); fos::Task::delayMs(1000);
@@ -549,17 +561,33 @@ void MovementTask() {
auto cmd = commandQueue.dequeue(); auto cmd = commandQueue.dequeue();
if (cmd->type == MoveType::StepTo) { if (cmd->type == MoveType::StepTo) {
if (stepper.isIdle()) { if (stepper.isIdle()) {
// activeState.pictogram = ledPos; pictoIllumQue.enqueue(std::make_unique<PictoIllumCommand>(0));
// ledRing.send(ledStrip); int newPos = cmd->pos;
// activeState.pictogram if ((newPos >= 1) && (newPos <= 6)) {
auto delta = std::min<uint8_t>(cmd->pos, 128); auto idx = newPos - 1;
Syslog::logv(Syslog::Severity::Informational,
"Triggered movement to picto pos:%i ", newPos);
} else {
Syslog::logv(Syslog::Severity::Error,
"Got invalid index for illuminated pictogram =%i ", newPos);
continue;
}
int delta = newPos - activeState.pictogram;
if (delta <= 0) {
delta += 6;
}
delta = delta + (activeState.minJumps % 6) * 6;
if (delta < activeState.minJumps) {
delta += 6;
}
Syslog::logv(Syslog::Severity::Notice, Syslog::logv(Syslog::Severity::Notice,
"Running Stepper by %i steps with prof=%i a=%.2f w=%.2f", "Running Stepper by %i steps with prof=%i a=%.2f w=%.2f",
delta, activeState.profile, activeState.accel, activeState.speed); delta, activeState.profile, activeState.accel, activeState.speed);
pictoIllumQue.enqueue(std::make_unique<PictoIllumCommand>(0));
stepper.moveBy(delta * std::numbers::pi / 3, stepper.moveBy(delta * std::numbers::pi / 3,
activeState.profile, activeState.accel, activeState.speed); activeState.profile, activeState.accel, activeState.speed);
} else { pictoIllumQue.enqueue(std::make_unique<PictoIllumCommand>(newPos));
fos::Task::delayMs(100); activeState.pictogram = newPos;
} }
} else if (cmd->type == MoveType::Demo) { } else if (cmd->type == MoveType::Demo) {
int i = 0; int i = 0;
@@ -596,7 +624,7 @@ void app_main(void) {
initialize_ethernet(); initialize_ethernet();
NvsApi::init(); NvsApi::init();
static fos::Task lightingTask(LightingTask, "Lights", 0x4000uL); static fos::Task lightingTask(LightingTask, "Lights", 0x4000uL);
// static fos::Task movementTask(MovementTask, "Motor", 0x4000uL); static fos::Task movementTask(MovementTask, "Motor", 0x4000uL);
etherActive.take(); etherActive.take();
ESP_LOGI(TAG, "Start net services"); ESP_LOGI(TAG, "Start net services");
@@ -618,8 +646,8 @@ void app_main(void) {
auto prefix = store.getValue(ConfigKeys.mqttPrefix); auto prefix = store.getValue(ConfigKeys.mqttPrefix);
auto &client = MqttClient::init(broker, prefix); auto &client = MqttClient::init(broker, prefix);
if (!client.isRunning()) { if (!client.isRunning()) {
client.addSubscription("watch/moveTo", [](std::string d) { moveToSubs(d); }); client.addSubscription(MqttTopics.moveTo, [](std::string d) { moveToSubs(d); });
client.addSubscription("watch/shuffle", [](std::string d) { startShuffle(d); }); client.addSubscription(MqttTopics.startShuffle, [](std::string d) { startShuffle(d); });
client.start(); client.start();
} }
} }