Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create functions based on user input?

I am currently working on a combination software and Arduino project that has the following general structure:

  1. User inputs a string of commands through a terminal program such as CoolTerm
  2. Commands are sent to Arduino through USB Serial
  3. First command is parsed, along with included arguments
  4. Function associated with first command is executed
  5. Second command is parsed, along with included arguments
  6. Function associated with second command is executed
  7. Etc. until all commands have been parsed and executed

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.


To illustrate my ideas in very basic C++ pseudocode:

(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();

}

Question:

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.

Edit:

This project requires compatibility with Arduino hardware and the Arduino IDE. Standard C++ will not work.

like image 416
BK610 Avatar asked Apr 28 '26 21:04

BK610


1 Answers

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.

like image 175
Maksim Solovjov Avatar answered Apr 30 '26 12:04

Maksim Solovjov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!