2023-12-11 06:52:44 +00:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-20 08:13:23 +00:00
|
|
|
struct Task;
|
2023-12-11 06:52:44 +00:00
|
|
|
|
|
|
|
struct Command {
|
2023-12-20 08:13:23 +00:00
|
|
|
using Executor = void(Task::*)(Args&, Print&);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
using MemberExecutor = void(T::*)(Args&, Print&);
|
|
|
|
|
2023-12-11 06:52:44 +00:00
|
|
|
const char* name = NULL;
|
2023-12-20 08:13:23 +00:00
|
|
|
Executor func = NULL;
|
|
|
|
|
|
|
|
void invoke(Task* task, Args& args, Print& printer) const {
|
|
|
|
if (func) {
|
|
|
|
(*task.*func)(args, printer);
|
|
|
|
}
|
|
|
|
}
|
2023-12-11 06:52:44 +00:00
|
|
|
|
|
|
|
Command(const char* name, Executor func) : name(name), func(func) {}
|
2023-12-20 08:13:23 +00:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
Command(const char* name, MemberExecutor<T> func) : name(name), func(static_cast<Executor>(func)) {}
|
2023-12-11 06:52:44 +00:00
|
|
|
};
|