#include <Arduino.h>
#include "vector.h" // Include your vector implementation
// Define your functions
void function1() {
Serial.println("Function 1 executed");
}
void function2() {
Serial.println("Function 2 executed");
}
void function3() {
Serial.println("Function 3 executed");
}
void function4() {
Serial.println("Function 4 executed");
}
void function5() {
Serial.println("Function 5 executed");
}
// Define a struct to hold function pointer and suspension flag
struct FunctionWrapper {
void (*function)();
bool suspended;
const char* name; // Name of the function
};
// Define a vector to hold function wrappers
vector<FunctionWrapper> functionVector;
// Define current index
int currentIndex = 0;
// Function to execute next function
void next() {
if (currentIndex < functionVector.size() - 1) {
currentIndex++;
if (functionVector[currentIndex].suspended) {
next(); // Skip to the next function if the current one is suspended
} else {
functionVector[currentIndex].function();
}
} else {
Serial.println("Already at the last function");
}
}
// Function to execute previous function
void previous() {
if (currentIndex > 0) {
currentIndex--;
if (functionVector[currentIndex].suspended) {
previous(); // Skip to the previous function if the current one is suspended
} else {
functionVector[currentIndex].function();
}
} else {
Serial.println("Already at the first function");
}
}
// Function to suspend or resume a function dynamically by name
void suspendResumeFunction(const char* functionName, bool suspend) {
for (size_t i = 0; i < functionVector.size(); ++i) {
if (strcmp(functionName, functionVector[i].name) == 0) {
functionVector[i].suspended = suspend;
break;
}
}
}
void setup() {
Serial.begin(115200); // Initialize serial communication
// Populate the function vector with function wrappers including names
functionVector.push_back({function1, false, "function1"});
functionVector.push_back({function2, false, "function2"});
functionVector.push_back({function3, false, "function3"});
functionVector.push_back({function4, false, "function4"});
functionVector.push_back({function5, false, "function5"});
}
void loop() {
// Listen for commands from the serial monitor
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
if(command == "n")next();
if(command == "p")previous();
if (command.startsWith("suspend ")) {
// Extract function name from command and suspend the function
String functionName = command.substring(8);
suspendResumeFunction(functionName.c_str(), true);
} else if (command.startsWith("resume ")) {
// Extract function name from command and resume the function
String functionName = command.substring(7);
suspendResumeFunction(functionName.c_str(), false);
}
}
}
/*
#include <Arduino.h>
#include <EEPROM.h> // Include the EEPROM library
#include "vector.h" // Include your vector implementation
// Define your functions
void function1(int arg1, float arg2) {
Serial.print("Function 1 executed with arguments: ");
Serial.print(arg1);
Serial.print(", ");
Serial.println(arg2);
}
void function2(const char* arg1) {
Serial.print("Function 2 executed with argument: ");
Serial.println(arg1);
}
void function3() {
Serial.println("Function 3 executed");
}
// Define a struct to hold function pointer, arguments, and suspension flag
struct FunctionWrapper {
void (*function)();
int intArg;
float floatArg;
const char* charArg;
bool suspended;
const char* name; // Name of the function
};
// Define a vector to hold function wrappers
vector<FunctionWrapper> functionVector;
// Define current index
int currentIndex = 0;
// Function to execute next function
void next() {
if (currentIndex < functionVector.size() - 1) {
currentIndex++;
if (functionVector[currentIndex].suspended) {
next(); // Skip to the next function if the current one is suspended
} else {
functionVector[currentIndex].function();
}
} else {
Serial.println("Already at the last function");
}
}
// Function to suspend or resume a function dynamically by name
void suspendResumeFunction(const char* functionName, bool suspend) {
for (size_t i = 0; i < functionVector.size(); ++i) {
if (strcmp(functionName, functionVector[i].name) == 0) {
functionVector[i].suspended = suspend;
// Save suspension information to EEPROM
int address = i * sizeof(bool);
EEPROM.put(address, suspend);
break;
}
}
}
// Function to load suspension information from EEPROM
void loadSuspensionInfo() {
for (size_t i = 0; i < functionVector.size(); ++i) {
int address = i * sizeof(bool);
bool suspend;
EEPROM.get(address, suspend);
functionVector[i].suspended = suspend;
}
}
void setup() {
Serial.begin(115200); // Initialize serial communication
// Populate the function vector with function wrappers including names
functionVector.push_back({nullptr, 10, 3.14, nullptr, false, "function1"});
functionVector.push_back({nullptr, 0, 0, "Hello", false, "function2"});
functionVector.push_back({nullptr, 0, 0, nullptr, false, "function3"});
// Load suspension information from EEPROM
loadSuspensionInfo();
}
void loop() {
// Listen for commands from the serial monitor
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
if (command.startsWith("suspend ")) {
// Extract function name from command and suspend the function
String functionName = command.substring(8);
suspendResumeFunction(functionName.c_str(), true);
} else if (command.startsWith("resume ")) {
// Extract function name from command and resume the function
String functionName = command.substring(7);
suspendResumeFunction(functionName.c_str(), false);
}
}
// Example usage
next(); // Executes function1 with arguments
next(); // Executes function2 with arguments
next(); // Executes function3
}
*/