figments: start building generic command-execution framework
This commit is contained in:
parent
50c98bc5b5
commit
b1ec20982b
7
lib/Figments/Command.cpp
Normal file
7
lib/Figments/Command.cpp
Normal 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
33
lib/Figments/Command.h
Normal 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
9
lib/Figments/Figment.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include "./Figment.h"
|
||||||
|
|
||||||
|
const std::vector<Command> emptyCommands;
|
||||||
|
|
||||||
|
const std::vector<Command>&
|
||||||
|
Task::commands() const
|
||||||
|
{
|
||||||
|
return emptyCommands;
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <ArduinoLog.h>
|
#include <ArduinoLog.h>
|
||||||
|
#include "./Command.h"
|
||||||
|
|
||||||
#define F_LIKELY(x) __builtin_expect(!!(x), true)
|
#define F_LIKELY(x) __builtin_expect(!!(x), true)
|
||||||
#define F_UNLIKELY(x) __builtin_expect(!!(x), false)
|
#define F_UNLIKELY(x) __builtin_expect(!!(x), false)
|
||||||
@ -68,6 +69,8 @@ struct Task : public virtual Loopable {
|
|||||||
|
|
||||||
const char* name = "";
|
const char* name = "";
|
||||||
State state = Stopped;
|
State state = Stopped;
|
||||||
|
|
||||||
|
virtual const std::vector<Command> &commands() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user