figments: command: rewrite command api to use Task instances instead of static functions

This commit is contained in:
Torrie Fischer 2023-12-20 09:13:23 +01:00
parent 214825c1d3
commit 6e138175be
14 changed files with 76 additions and 81 deletions

View File

@ -1,7 +1,2 @@
#include "./Command.h" #include "./Command.h"
void
doNothing(Args& args, Print& printer)
{}
Command::Command() : func(doNothing) {}

View File

@ -21,13 +21,25 @@ class Args {
} }
}; };
struct CommandList; struct Task;
struct Command { struct Command {
using Executor = std::function<void(Args&, Print& output)>; using Executor = void(Task::*)(Args&, Print&);
Executor func;
const char* name = NULL; template<typename T>
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) {} Command(const char* name, Executor func) : name(name), func(func) {}
template<class T>
Command(const char* name, MemberExecutor<T> func) : name(name), func(static_cast<Executor>(func)) {}
}; };

View File

@ -256,19 +256,18 @@ ConfigService::handleEvent(const InputEvent &evt)
} }
void void
doMapList(Args& args, Print& out) ConfigService::doMapList(Args& args, Print& out)
{ {
static const auto conf = Static<ConfigService>::instance();
out.println("Available maps:"); out.println("Available maps:");
LittleFS.begin(); LittleFS.begin();
for(auto it = conf->mapsBegin();it != conf->mapsEnd(); it++) { for(auto it = mapsBegin();it != mapsEnd(); it++) {
out.println(*it); out.println(*it);
} }
LittleFS.end(); LittleFS.end();
} }
void void
doSave(Args& args, Print& print) ConfigService::doSave(Args& args, Print& print)
{ {
MainLoop::instance()->dispatch(InputEvent::SaveConfigurationRequest); MainLoop::instance()->dispatch(InputEvent::SaveConfigurationRequest);
} }
@ -276,17 +275,17 @@ doSave(Args& args, Print& print)
static String s; static String s;
void void
doSetProfile(Args& args, Print& out) ConfigService::doSetProfile(Args& args, Print& out)
{ {
s = args[1]; s = args[1];
MainLoop::instance()->dispatch(InputEvent{InputEvent::LoadConfigurationByName, s.c_str()}); MainLoop::instance()->dispatch(InputEvent{InputEvent::LoadConfigurationByName, s.c_str()});
} }
void void
doCoordMap(Args& args, Print& out) ConfigService::doCoordMap(Args& args, Print& out)
{ {
VirtualCoordinates coords{atoi(args[1].c_str()), atoi(args[2].c_str())}; VirtualCoordinates coords{atoi(args[1].c_str()), atoi(args[2].c_str())};
auto map = Static<ConfigService>::instance()->coordMap(); auto map = coordMap();
auto pPos = map->virtualToPhysicalCoords(coords); auto pPos = map->virtualToPhysicalCoords(coords);
auto idx = map->physicalCoordsToIndex(pPos); auto idx = map->physicalCoordsToIndex(pPos);
char buf[32]; char buf[32];
@ -298,10 +297,10 @@ const std::vector<Command>&
ConfigService::commands() const ConfigService::commands() const
{ {
static const std::vector<Command> _commands = { static const std::vector<Command> _commands = {
{"save", doSave}, {"save", &ConfigService::doSave},
{"profile", doSetProfile}, {"profile", &ConfigService::doSetProfile},
{"maps", doMapList}, {"maps", &ConfigService::doMapList},
{"coordmap", doCoordMap} {"coordmap", &ConfigService::doCoordMap}
}; };
return _commands; return _commands;
} }

View File

@ -121,4 +121,9 @@ private:
bool loadProfile(const char* name); bool loadProfile(const char* name);
bool loadMap(const String& mapName); 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);
}; };

View File

@ -256,17 +256,15 @@ Platform::restart() {
} }
__attribute__((noreturn))
void void
doReboot(Args& args, Print& out) Platform::doReboot(Args& args, Print& out)
{ {
out.println("Rebooting"); out.println("Rebooting");
Platform::restart(); Platform::restart();
} }
__attribute__((noreturn))
void void
doSafeMode(Args& args, Print& out) Platform::doSafeMode(Args& args, Print& out)
{ {
out.println("Rebooting into safe mode"); out.println("Rebooting into safe mode");
Platform::bootopts.forceSafeMode(); Platform::bootopts.forceSafeMode();
@ -276,21 +274,21 @@ doSafeMode(Args& args, Print& out)
String s; String s;
void void
doTaskStart(Args& args, Print& out) Platform::doTaskStart(Args& args, Print& out)
{ {
s = args[1]; s = args[1];
MainLoop::instance()->dispatch(InputEvent{InputEvent::StartThing, s.c_str()}); MainLoop::instance()->dispatch(InputEvent{InputEvent::StartThing, s.c_str()});
} }
void void
doTaskStop(Args& args, Print& out) Platform::doTaskStop(Args& args, Print& out)
{ {
s = args[1]; s = args[1];
MainLoop::instance()->dispatch(InputEvent{InputEvent::StopThing, s.c_str()}); MainLoop::instance()->dispatch(InputEvent{InputEvent::StopThing, s.c_str()});
} }
void void
doTaskList(Args& args, Print& out) Platform::doTaskList(Args& args, Print& out)
{ {
auto sched = MainLoop::instance()->scheduler; auto sched = MainLoop::instance()->scheduler;
auto printer = Static<SerialInput>::instance()->printer(); auto printer = Static<SerialInput>::instance()->printer();
@ -312,18 +310,16 @@ doTaskList(Args& args, Print& out)
} }
const std::vector<Command> _commands = {
{"tasks", doTaskList},
{"safe-mode", doSafeMode},
{"reboot", doReboot},
{"stop", doTaskStop},
{"start", doTaskStart}
};
const std::vector<Command>& const std::vector<Command>&
Platform::commands() const Platform::commands() const
{ {
static const std::vector<Command> _commands = {
{"tasks", &Platform::doTaskList},
{"safe-mode", &Platform::doSafeMode},
{"reboot", &Platform::doReboot},
{"stop", &Platform::doTaskStop},
{"start", &Platform::doTaskStart}
};
return _commands; return _commands;
} }

View File

@ -6,6 +6,12 @@
class Platform : public Task { class Platform : public Task {
static int s_timezone; static int s_timezone;
static char s_deviceID[15]; 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: public:
Platform() : Task("Platform") {state = Task::Running;} Platform() : Task("Platform") {state = Task::Running;}
static BootOptions bootopts; static BootOptions bootopts;

View File

@ -95,10 +95,10 @@ Sequencer::handleEvent(const InputEvent& evt)
} }
void void
doScenes(Args& args, Print& out) Sequencer::doScenes(Args& args, Print& out)
{ {
out.println("Available scenes: "); out.println("Available scenes: ");
for (auto scene : Static<Sequencer>::instance()->scenes()) { for (auto scene : scenes()) {
out.println(scene.name); out.println(scene.name);
} }
} }
@ -106,20 +106,19 @@ doScenes(Args& args, Print& out)
static String s; static String s;
void void
doScene(Args& args, Print& out) Sequencer::doScene(Args& args, Print& out)
{ {
s = args[1]; s = args[1];
MainLoop::instance()->dispatch(InputEvent{InputEvent::SetPattern, s.c_str()}); MainLoop::instance()->dispatch(InputEvent{InputEvent::SetScene, s.c_str()});
} }
const std::vector<Command> _commands = {
{"scene", doScene},
{"scenes", doScenes}
};
const std::vector<Command>& const std::vector<Command>&
Sequencer::commands() const Sequencer::commands() const
{ {
static const std::vector<Command> _commands = {
{"scene", &Sequencer::doScene},
{"scenes", &Sequencer::doScenes}
};
return _commands; return _commands;
} }

View File

@ -28,4 +28,7 @@ public:
private: private:
int m_idx; int m_idx;
std::vector<Scene> m_scenes; std::vector<Scene> m_scenes;
void doScene(Args& args, Print& out);
void doScenes(Args& args, Print& out);
}; };

View File

@ -30,26 +30,6 @@ doForceBrightness(Args& args, Print& out)
Static<Power>::instance()->forceBrightness(newBrightness); Static<Power>::instance()->forceBrightness(newBrightness);
} }
void
Power::forceBrightness(uint8_t v)
{
m_forced = true;
FastLED.setBrightness(v);
}
const std::vector<Command> _commands = {
{"brightness", doBrightness},
{"brightness-force", doForceBrightness},
{"on", doOn},
{"off", doOff}
};
const std::vector<Command>&
Power::commands() const
{
return _commands;
}
void void
Power::handleConfigChange(const Configuration& config) Power::handleConfigChange(const Configuration& config)
{ {

View File

@ -53,9 +53,6 @@ public:
} }
void forceBrightness(uint8_t v); void forceBrightness(uint8_t v);
const std::vector<Command>& commands() const override;
private: private:
AnimatedNumber m_powerState = 255; AnimatedNumber m_powerState = 255;
AnimatedNumber m_brightness = 255; AnimatedNumber m_brightness = 255;

View File

@ -2,19 +2,19 @@
#include "../Static.h" #include "../Static.h"
void void
doBPM(Args& args, Print& out) BPM::doSetBPM(Args& args, Print& out)
{ {
uint8_t newBPM(atoi(args[1].c_str())); uint8_t newBPM(atoi(args[1].c_str()));
Static<BPM>::instance()->setBPM(newBPM); setBPM(newBPM);
} }
const std::vector<Command> _commands = {
{"bpm", doBPM}
};
const std::vector<Command>& const std::vector<Command>&
BPM::commands() const BPM::commands() const
{ {
static const std::vector<Command> _commands = {
{"bpm", &BPM::doSetBPM}
};
return _commands; return _commands;
} }

View File

@ -77,4 +77,6 @@ private:
uint16_t trash; uint16_t trash;
m_timings.take(trash); m_timings.take(trash);
} }
void doSetBPM(Args& args, Print& print);
}; };

View File

@ -126,7 +126,7 @@ SerialInput::read()
} }
void void
doHelp(Args& args, Print& out) SerialInput::doHelp(Args& args, Print& out)
{ {
out.println("Available commands:"); out.println("Available commands:");
auto sched = MainLoop::instance()->scheduler; auto sched = MainLoop::instance()->scheduler;
@ -139,14 +139,13 @@ doHelp(Args& args, Print& out)
out.println(); out.println();
} }
const std::vector<Command> serialCommands = {
{"help", doHelp}
};
const std::vector<Command>& const std::vector<Command>&
SerialInput::commands() const SerialInput::commands() const
{ {
return serialCommands; static const std::vector<Command> _commands = {
{"help", &SerialInput::doHelp}
};
return _commands;
} }
void void
@ -158,7 +157,7 @@ SerialInput::doCommand() {
for(auto task : sched.tasks) { for(auto task : sched.tasks) {
for(auto &command : task->commands()) { for(auto &command : task->commands()) {
if (cmdName == command.name) { if (cmdName == command.name) {
command.func(args, m_logPrinter); command.invoke(task, args, m_logPrinter);
return; return;
} }
} }

View File

@ -59,4 +59,6 @@ private:
InputEvent parseNormal(char nextChar); InputEvent parseNormal(char nextChar);
InputEvent parseEscape(char nextChar); InputEvent parseEscape(char nextChar);
InputEvent parseCSI(char nextChar); InputEvent parseCSI(char nextChar);
void doHelp(Args& args, Print& out);
}; };