I am currently working on a combination software and Arduino project that has the following general structure:
So far, all of this works as I would expect it to. However, the project I am working on requires very precise timing, and having to parse each individual command creates a considerable amount of processing time (not sure if this is the right term) between each command execution.
For example, in a user input string that contains three commands, between the first command being parsed and the last command being executed, there is an additional 5.8 milliseconds of processing time from start to finish.
To be clear, all parts of my program are functional, including user input, string parsing, and function execution as described above. I need to improve on my existing code, not correct errors.
Ideally, I imagine that the program will parse each command, "set aside" the function that is associated with the command, and execute all commands sequentially once they have all been "set aside." This will shorten the processing time significantly by getting rid of the need to continue parsing commands between each function execution. I am not sure how to accomplish this, or if it is even possible.
(assuming example user input is "A, B, C")
loop() {
// Example user input received: "A, B, C" corresponds to:
// functionA, functionB, functionC
String userInput = receiveInput();
// Parse user input
parse(userInput);
// Execute functions specified by user input
executeFunctions();
}
/*Parsing separates "A, B, C" to functionA, functionB, functionC
Functions are "set aside" to be executed sequentially,
the next beginning directly after the last ends*/
executeFunctions{
// Example functions to be executed
functionA();
functionB();
functionC();
}
I need a way to create a function based on user input, or based on another function. I have never heard of such a concept through the extensive research I have done, and I am not sure if it exists. If possible, this is the method I would like to use to proceed with my project, as I believe it will require the least amount of restructuring of my code.
This project requires compatibility with Arduino hardware and the Arduino IDE. Standard C++ will not work.
You could use a Command Pattern.
Basically, make your parser to put a different command object for each user input into some sort of queue. You can use a basic function object for this:
struct Command {
virtual ~Command() {}
virtual void operator()(); // this will execute the command
};
class FirstCommand : public Command {
// some private data + constructor
public:
virtual void operator()() { /* do stuff for this user input */ }
};
class SecondCommand : public Command {
// some private data + constructor
public:
virtual void operator()() { /* do stuff for this user input */ }
};
A parser would create either FirstCommand or SecondCommand, and store them in the std::queue<Command*> or something more sophisticated. Your consumer code would then execute every command by doing something like:
while (!q.empty() {
Command* command = q.front();
(*command)();
q.pop();
}
With thread-safe queues, the consumer code can even be run in parallel to your parser.
You could use a queue of simple pointers to functions instead of command objects, but if you do, their signatures will have to be the same, while a constructor for a specific command can be arbitrary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With