figments: start building generic command-execution framework

This commit is contained in:
Torrie Fischer 2023-12-11 07:52:44 +01:00
parent 50c98bc5b5
commit b1ec20982b
4 changed files with 52 additions and 0 deletions

7
lib/Figments/Command.cpp Normal file
View File

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

33
lib/Figments/Command.h Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include <Arduino.h>
class Args {
private:
String *str;
public:
Args(String *str) : str(str) {}
String operator[](int pos) {
char buf[64];
strncpy(buf, str->c_str(), sizeof(buf));
char *args = strtok(buf, " ");
while (pos > 0 && args != NULL) {
args = strtok(NULL, " ");
pos--;
}
if (args == NULL) {
return String();
}
return String(args);
}
};
struct CommandList;
struct Command {
using Executor = std::function<void(Args&, Print& output)>;
Executor func;
const char* name = NULL;
Command();
Command(const char* name, Executor func) : name(name), func(func) {}
};

9
lib/Figments/Figment.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "./Figment.h"
const std::vector<Command> emptyCommands;
const std::vector<Command>&
Task::commands() const
{
return emptyCommands;
}

View File

@ -2,6 +2,7 @@
#include <Arduino.h>
#include <functional>
#include <ArduinoLog.h>
#include "./Command.h"
#define F_LIKELY(x) __builtin_expect(!!(x), true)
#define F_UNLIKELY(x) __builtin_expect(!!(x), false)
@ -68,6 +69,8 @@ struct Task : public virtual Loopable {
const char* name = "";
State state = Stopped;
virtual const std::vector<Command> &commands() const;
};
/**