From 6e138175be1bd52341898ceaef1bbc9b97bac653 Mon Sep 17 00:00:00 2001 From: Torrie Fischer Date: Wed, 20 Dec 2023 09:13:23 +0100 Subject: [PATCH] figments: command: rewrite command api to use Task instances instead of static functions --- lib/Figments/Command.cpp | 5 ----- lib/Figments/Command.h | 22 +++++++++++++++++----- src/Config.cpp | 21 ++++++++++----------- src/Config.h | 5 +++++ src/Platform.cpp | 28 ++++++++++++---------------- src/Platform.h | 6 ++++++ src/Sequencer.cpp | 17 ++++++++--------- src/Sequencer.h | 3 +++ src/animations/Power.cpp | 20 -------------------- src/animations/Power.h | 3 --- src/inputs/BPM.cpp | 10 +++++----- src/inputs/BPM.h | 2 ++ src/inputs/Serial.cpp | 13 ++++++------- src/inputs/Serial.h | 2 ++ 14 files changed, 76 insertions(+), 81 deletions(-) diff --git a/lib/Figments/Command.cpp b/lib/Figments/Command.cpp index 33df959..18af7f4 100644 --- a/lib/Figments/Command.cpp +++ b/lib/Figments/Command.cpp @@ -1,7 +1,2 @@ #include "./Command.h" -void -doNothing(Args& args, Print& printer) -{} - -Command::Command() : func(doNothing) {} diff --git a/lib/Figments/Command.h b/lib/Figments/Command.h index 87cf490..893e045 100644 --- a/lib/Figments/Command.h +++ b/lib/Figments/Command.h @@ -21,13 +21,25 @@ class Args { } }; -struct CommandList; +struct Task; struct Command { - using Executor = std::function; - Executor func; - const char* name = NULL; + using Executor = void(Task::*)(Args&, Print&); + + template + using MemberExecutor = void(T::*)(Args&, Print&); + + const char* name = NULL; + Executor func = NULL; + + void invoke(Task* task, Args& args, Print& printer) const { + if (func) { + (*task.*func)(args, printer); + } + } - Command(); Command(const char* name, Executor func) : name(name), func(func) {} + + template + Command(const char* name, MemberExecutor func) : name(name), func(static_cast(func)) {} }; diff --git a/src/Config.cpp b/src/Config.cpp index 0b6b318..e98b095 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -256,19 +256,18 @@ ConfigService::handleEvent(const InputEvent &evt) } void -doMapList(Args& args, Print& out) +ConfigService::doMapList(Args& args, Print& out) { - static const auto conf = Static::instance(); out.println("Available maps:"); LittleFS.begin(); - for(auto it = conf->mapsBegin();it != conf->mapsEnd(); it++) { + for(auto it = mapsBegin();it != mapsEnd(); it++) { out.println(*it); } LittleFS.end(); } void -doSave(Args& args, Print& print) +ConfigService::doSave(Args& args, Print& print) { MainLoop::instance()->dispatch(InputEvent::SaveConfigurationRequest); } @@ -276,17 +275,17 @@ doSave(Args& args, Print& print) static String s; void -doSetProfile(Args& args, Print& out) +ConfigService::doSetProfile(Args& args, Print& out) { s = args[1]; MainLoop::instance()->dispatch(InputEvent{InputEvent::LoadConfigurationByName, s.c_str()}); } void -doCoordMap(Args& args, Print& out) +ConfigService::doCoordMap(Args& args, Print& out) { VirtualCoordinates coords{atoi(args[1].c_str()), atoi(args[2].c_str())}; - auto map = Static::instance()->coordMap(); + auto map = coordMap(); auto pPos = map->virtualToPhysicalCoords(coords); auto idx = map->physicalCoordsToIndex(pPos); char buf[32]; @@ -298,10 +297,10 @@ const std::vector& ConfigService::commands() const { static const std::vector _commands = { - {"save", doSave}, - {"profile", doSetProfile}, - {"maps", doMapList}, - {"coordmap", doCoordMap} + {"save", &ConfigService::doSave}, + {"profile", &ConfigService::doSetProfile}, + {"maps", &ConfigService::doMapList}, + {"coordmap", &ConfigService::doCoordMap} }; return _commands; } diff --git a/src/Config.h b/src/Config.h index 0ee6102..64d5b7c 100644 --- a/src/Config.h +++ b/src/Config.h @@ -121,4 +121,9 @@ private: bool loadProfile(const char* name); bool loadMap(const String& mapName); + + void doSave(Args& args, Print& print); + void doSetProfile(Args& args, Print& print); + void doCoordMap(Args& args, Print& print); + void doMapList(Args& args, Print& print); }; diff --git a/src/Platform.cpp b/src/Platform.cpp index 7b90b03..6e0dffd 100644 --- a/src/Platform.cpp +++ b/src/Platform.cpp @@ -256,17 +256,15 @@ Platform::restart() { } -__attribute__((noreturn)) void -doReboot(Args& args, Print& out) +Platform::doReboot(Args& args, Print& out) { out.println("Rebooting"); Platform::restart(); } -__attribute__((noreturn)) void -doSafeMode(Args& args, Print& out) +Platform::doSafeMode(Args& args, Print& out) { out.println("Rebooting into safe mode"); Platform::bootopts.forceSafeMode(); @@ -276,21 +274,21 @@ doSafeMode(Args& args, Print& out) String s; void -doTaskStart(Args& args, Print& out) +Platform::doTaskStart(Args& args, Print& out) { s = args[1]; MainLoop::instance()->dispatch(InputEvent{InputEvent::StartThing, s.c_str()}); } void -doTaskStop(Args& args, Print& out) +Platform::doTaskStop(Args& args, Print& out) { s = args[1]; MainLoop::instance()->dispatch(InputEvent{InputEvent::StopThing, s.c_str()}); } void -doTaskList(Args& args, Print& out) +Platform::doTaskList(Args& args, Print& out) { auto sched = MainLoop::instance()->scheduler; auto printer = Static::instance()->printer(); @@ -312,18 +310,16 @@ doTaskList(Args& args, Print& out) } - -const std::vector _commands = { - {"tasks", doTaskList}, - {"safe-mode", doSafeMode}, - {"reboot", doReboot}, - {"stop", doTaskStop}, - {"start", doTaskStart} -}; - const std::vector& Platform::commands() const { + static const std::vector _commands = { + {"tasks", &Platform::doTaskList}, + {"safe-mode", &Platform::doSafeMode}, + {"reboot", &Platform::doReboot}, + {"stop", &Platform::doTaskStop}, + {"start", &Platform::doTaskStart} + }; return _commands; } diff --git a/src/Platform.h b/src/Platform.h index 1e5df6f..8deae6c 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -6,6 +6,12 @@ class Platform : public Task { static int s_timezone; static char s_deviceID[15]; + + void doTaskList(Args& args, Print& out); + void doTaskStop(Args& args, Print& out); + void doTaskStart(Args& args, Print& out); + __attribute__((noreturn)) void doSafeMode(Args& args, Print& out); + __attribute__((noreturn)) void doReboot(Args& args, Print& out); public: Platform() : Task("Platform") {state = Task::Running;} static BootOptions bootopts; diff --git a/src/Sequencer.cpp b/src/Sequencer.cpp index cda0187..eb89a3c 100644 --- a/src/Sequencer.cpp +++ b/src/Sequencer.cpp @@ -95,10 +95,10 @@ Sequencer::handleEvent(const InputEvent& evt) } void -doScenes(Args& args, Print& out) +Sequencer::doScenes(Args& args, Print& out) { out.println("Available scenes: "); - for (auto scene : Static::instance()->scenes()) { + for (auto scene : scenes()) { out.println(scene.name); } } @@ -106,20 +106,19 @@ doScenes(Args& args, Print& out) static String s; void -doScene(Args& args, Print& out) +Sequencer::doScene(Args& args, Print& out) { s = args[1]; - MainLoop::instance()->dispatch(InputEvent{InputEvent::SetPattern, s.c_str()}); + MainLoop::instance()->dispatch(InputEvent{InputEvent::SetScene, s.c_str()}); } -const std::vector _commands = { - {"scene", doScene}, - {"scenes", doScenes} -}; - const std::vector& Sequencer::commands() const { + static const std::vector _commands = { + {"scene", &Sequencer::doScene}, + {"scenes", &Sequencer::doScenes} + }; return _commands; } diff --git a/src/Sequencer.h b/src/Sequencer.h index 885f7f5..0c8ed29 100644 --- a/src/Sequencer.h +++ b/src/Sequencer.h @@ -28,4 +28,7 @@ public: private: int m_idx; std::vector m_scenes; + + void doScene(Args& args, Print& out); + void doScenes(Args& args, Print& out); }; diff --git a/src/animations/Power.cpp b/src/animations/Power.cpp index ecedb61..349fdd0 100644 --- a/src/animations/Power.cpp +++ b/src/animations/Power.cpp @@ -30,26 +30,6 @@ doForceBrightness(Args& args, Print& out) Static::instance()->forceBrightness(newBrightness); } -void -Power::forceBrightness(uint8_t v) -{ - m_forced = true; - FastLED.setBrightness(v); -} - -const std::vector _commands = { - {"brightness", doBrightness}, - {"brightness-force", doForceBrightness}, - {"on", doOn}, - {"off", doOff} -}; - -const std::vector& -Power::commands() const -{ - return _commands; -} - void Power::handleConfigChange(const Configuration& config) { diff --git a/src/animations/Power.h b/src/animations/Power.h index 938d88c..efaa42b 100644 --- a/src/animations/Power.h +++ b/src/animations/Power.h @@ -53,9 +53,6 @@ public: } void forceBrightness(uint8_t v); - - const std::vector& commands() const override; - private: AnimatedNumber m_powerState = 255; AnimatedNumber m_brightness = 255; diff --git a/src/inputs/BPM.cpp b/src/inputs/BPM.cpp index e11d2c8..09b1d01 100644 --- a/src/inputs/BPM.cpp +++ b/src/inputs/BPM.cpp @@ -2,19 +2,19 @@ #include "../Static.h" void -doBPM(Args& args, Print& out) +BPM::doSetBPM(Args& args, Print& out) { uint8_t newBPM(atoi(args[1].c_str())); - Static::instance()->setBPM(newBPM); + setBPM(newBPM); } -const std::vector _commands = { - {"bpm", doBPM} -}; const std::vector& BPM::commands() const { + static const std::vector _commands = { + {"bpm", &BPM::doSetBPM} + }; return _commands; } diff --git a/src/inputs/BPM.h b/src/inputs/BPM.h index e15b1c0..cdc619b 100644 --- a/src/inputs/BPM.h +++ b/src/inputs/BPM.h @@ -77,4 +77,6 @@ private: uint16_t trash; m_timings.take(trash); } + + void doSetBPM(Args& args, Print& print); }; diff --git a/src/inputs/Serial.cpp b/src/inputs/Serial.cpp index bc30a66..c8ee2c1 100644 --- a/src/inputs/Serial.cpp +++ b/src/inputs/Serial.cpp @@ -126,7 +126,7 @@ SerialInput::read() } void -doHelp(Args& args, Print& out) +SerialInput::doHelp(Args& args, Print& out) { out.println("Available commands:"); auto sched = MainLoop::instance()->scheduler; @@ -139,14 +139,13 @@ doHelp(Args& args, Print& out) out.println(); } -const std::vector serialCommands = { - {"help", doHelp} -}; - const std::vector& SerialInput::commands() const { - return serialCommands; + static const std::vector _commands = { + {"help", &SerialInput::doHelp} + }; + return _commands; } void @@ -158,7 +157,7 @@ SerialInput::doCommand() { for(auto task : sched.tasks) { for(auto &command : task->commands()) { if (cmdName == command.name) { - command.func(args, m_logPrinter); + command.invoke(task, args, m_logPrinter); return; } } diff --git a/src/inputs/Serial.h b/src/inputs/Serial.h index 6bbc286..783b39a 100644 --- a/src/inputs/Serial.h +++ b/src/inputs/Serial.h @@ -59,4 +59,6 @@ private: InputEvent parseNormal(char nextChar); InputEvent parseEscape(char nextChar); InputEvent parseCSI(char nextChar); + + void doHelp(Args& args, Print& out); };