Stack of functions

Friends how to complete the task.
It is necessary to model a stack of functions, by command from a button, run all the functions on the stack.
For example: there are functions a(), b(), c(), d(), e(). The user pushed only functions a(), b() onto the stack. Pressed the button or entered a command into the port monitor, the functions a(), b() started.

So far I'm just thinking about the implementation.

The program resembles a terminal or console. I enter commands through the port monitor. According to the commands, the function is launched. But! If I enter another command into the port, the old function will stop running.

Sounds like homework.

For suggestions, post the code you have so far (using code tags) and tell us where you are stuck.

So far I'm just thinking about the implementation.

The program resembles a terminal or console. I enter commands through the port monitor. According to the commands, the function is launched. But! If I enter another command into the port, the old function will stop running.

Maybe this will help you on your way.

struct Command {
  char const* text;
  void (*f)();
};


void nothing() {}

void functionA() {
  Serial.println("This is function A.");
}

void functionB() {
  Serial.println("This is function B.");
}

Command commands[] {
  {"doA", functionA},
  {"doB", functionB}};


void setup() {
  Serial.begin(9600);
  Serial.println("Please enter a command.");
}

void loop() {
  static void (*f)() {nothing};
  f();

  delay(1000);  // Only here for demonstration purposes.
  if (not Serial.available()) {
    return;
  }

  char text[10] {};
  Serial.readBytesUntil('\n', text, sizeof(text));
  for (Command const& command: commands) {
    if (not strncmp(command.text, text, 3)) {
      f = command.f;
    }
  }
}

Here we use a function pointer f to select one of the functions (nothing(), functionA(), or functionB()). The functions we want to use are associated with text strings in the commands[] array.

When some serial data is available, we read until the end of the line (or until the buffer named text is full, or until a timeout occurs) and loop through the array to see whether we can find a match. If a match was found, the function pointer f is updated accordingly.

Needless to say, there is lots of room for improvement.

1 Like

Are you expecting the old function to continue running upon receipt of a new command?

yes

In that case, you may want to consider something like this.

struct Command {
  char const* text;
  void (*f)();
  bool active;
};


void functionA() {
  Serial.println("This is function A.");
}

void functionB() {
  Serial.println("This is function B.");
}

Command commands[] {
  {"doA", functionA, false},
  {"doB", functionB, false}};


void setup() {
  Serial.begin(9600);
  Serial.println("Please enter a command.");
}

void loop() {
  delay(1000);  // Only here for demonstration purposes.

  for (Command const& command: commands) {
    if (command.active) {
      command.f();
    }
  }

  if (not Serial.available()) {
    return;
  }

  char text[10] {};
  Serial.readBytesUntil('\n', text, sizeof(text));
  for (Command& command: commands) {
    if (not strncmp(command.text, text, 3)) {
      command.active = true;
    }
  }
}

Deactivating functions is left as an exercise to the reader.

1 Like

Define what you mean by "function"? If you literally mean a C++ function you can't just make a function "stop running". If you are calling a function over and over again you can stop calling it when you check the serial port and determine a new function is requested, but only when the current function is completed.

Please be more concise in your description of the problem.

provide an example of a function that can run continuously until another command?

and what do you mean by two commands "pushed" onto a stack? are they just queued for execution, is the pair repeated until another command? if each command can run continuously, how how can a() and b() run?

you may be interested in chp 8, described at Program Development but really about developing a small language in The Unix Programming Environment

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.