From 199007c0c578bbca9f3e08edf25be0ab22a5b324 Mon Sep 17 00:00:00 2001 From: Jakob Gegeniger Date: Sat, 15 Feb 2025 16:43:26 +0100 Subject: [PATCH] refactor: Rewiring MQTT constants to toplevel defined constants --- .gitmodules | 3 ++ core | 1 + main/watchMain.cpp | 104 ++++++++++++++++++++++++++++----------------- 3 files changed, 70 insertions(+), 38 deletions(-) create mode 100644 .gitmodules create mode 160000 core diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c19f9a1 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "core"] + path = core + url = git@github.com:Gustice/EspIdfDevicePlatform.git diff --git a/core b/core new file mode 160000 index 0000000..13d6532 --- /dev/null +++ b/core @@ -0,0 +1 @@ +Subproject commit 13d6532a927507f21c0cb4989c82f6a7bd579549 diff --git a/main/watchMain.cpp b/main/watchMain.cpp index d61a852..95495ce 100644 --- a/main/watchMain.cpp +++ b/main/watchMain.cpp @@ -18,12 +18,21 @@ struct { } ConfigKeys; // MQTT Topics -// Subscriptions: -// /watch/MoveTo -// /watch/Shuffle -// Publishes: -// /watch/inPosition -// /watch/changingPosition +struct { + // Subscriptions: + const char *moveTo = /* / */ "watch/moveTo"; + const char *startShuffle = /* / */ "watch/startShuffle"; + // Publishes: + const char *inPosition = /* / */ "watch/inPosition"; + const char *changingPosition = /* / */ "watch/changingPosition"; +} MqttTopics; + +// ToDo: +// - MQTT Integration +// - OTA +// - Syslog heartbeat +// - Streamlined DMX +// - #include "watchMain.hpp" @@ -39,7 +48,7 @@ struct DeviceState { float speed = 1.0; // Rad/sec float accel = 1.0; // Rad/sec^2 Color color{0x80, 0x80, 0x80}; - uint8_t pictogram{}; + uint8_t pictogram{}; // 0..5 active Pictogramm }; 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(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) { if (args.size() != 2) { 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(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) { ESP_LOGI(TAG, "on startDemoMovement:"); commandQueue.enqueue(std::make_unique(MoveType::Demo)); @@ -196,8 +205,8 @@ static void telnetSocketTask(int addrFamily) { {"dyn_set", "set dynamics of movement (velocity,accelerations) as floats", [](Cli::CArgs a) { setDynamics(a); }}); pShell->addBinding( - {"move", "start movement (profile-idx,steps) as integers ", - [](Cli::CArgs a) { triggerMovement(a); }}); + {"tstMove", "start movement (profile-idx,steps) as integers ", + [](Cli::CArgs a) { testMovement(a); }}); pShell->addBinding( {"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); commandQueue.enqueue(std::make_unique(MoveType::StepTo, p)); auto &client = MqttClient::getInstance(); - client.publish("watch/inPosition", data + "Picto"); + client.publish(MqttTopics.inPosition, data + "Picto"); } catch (const std::exception &e) { 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"); commandQueue.enqueue(std::make_unique(MoveType::Shuffle, 0)); auto &client = MqttClient::getInstance(); - client.publish("watch/changingPosition", "...."); + client.publish(MqttTopics.changingPosition, "...."); } catch (const std::exception &e) { 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) { Syslog::logv(Syslog::Severity::Informational, "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)) { auto idx = pos - 1; Syslog::logv(Syslog::Severity::Informational, @@ -508,7 +517,7 @@ void LightingTask() { sp[2] = (uint16_t)(col.getGreen()) << 7; sp[3] = (uint16_t)(col.getRed()) << 7; } - + ledDriver1.sendImage(values[0]); ledDriver2.sendImage(values[1]); fos::Task::delayMs(40); @@ -539,6 +548,9 @@ void MovementTask() { ESP_LOGI("Stepper", "Find Home"); stepper.findRef(); + // Illum 1 + pictoIllumQue.enqueue(std::make_unique(1)); + ESP_LOGI(TAG, "Fist Position is %f.2", stepper.getScaledPos() * 6); fos::Task::delayMs(1000); @@ -549,17 +561,33 @@ void MovementTask() { auto cmd = commandQueue.dequeue(); if (cmd->type == MoveType::StepTo) { if (stepper.isIdle()) { - // activeState.pictogram = ledPos; - // ledRing.send(ledStrip); - // activeState.pictogram - auto delta = std::min(cmd->pos, 128); + pictoIllumQue.enqueue(std::make_unique(0)); + int newPos = cmd->pos; + if ((newPos >= 1) && (newPos <= 6)) { + 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, "Running Stepper by %i steps with prof=%i a=%.2f w=%.2f", delta, activeState.profile, activeState.accel, activeState.speed); + pictoIllumQue.enqueue(std::make_unique(0)); stepper.moveBy(delta * std::numbers::pi / 3, activeState.profile, activeState.accel, activeState.speed); - } else { - fos::Task::delayMs(100); + pictoIllumQue.enqueue(std::make_unique(newPos)); + activeState.pictogram = newPos; } } else if (cmd->type == MoveType::Demo) { int i = 0; @@ -596,7 +624,7 @@ void app_main(void) { initialize_ethernet(); NvsApi::init(); static fos::Task lightingTask(LightingTask, "Lights", 0x4000uL); - // static fos::Task movementTask(MovementTask, "Motor", 0x4000uL); + static fos::Task movementTask(MovementTask, "Motor", 0x4000uL); etherActive.take(); ESP_LOGI(TAG, "Start net services"); @@ -618,8 +646,8 @@ void app_main(void) { auto prefix = store.getValue(ConfigKeys.mqttPrefix); auto &client = MqttClient::init(broker, prefix); if (!client.isRunning()) { - client.addSubscription("watch/moveTo", [](std::string d) { moveToSubs(d); }); - client.addSubscription("watch/shuffle", [](std::string d) { startShuffle(d); }); + client.addSubscription(MqttTopics.moveTo, [](std::string d) { moveToSubs(d); }); + client.addSubscription(MqttTopics.startShuffle, [](std::string d) { startShuffle(d); }); client.start(); } }