The Serial Monitor is nothing more than a window that displays the serial data sent to the PC. Are you saying that you are passing characters to the ATMega328 and calling certain functions based on what you send?
But as for your question:
typedef void (* Caller)()
Is a typedef
for a void*
function pointer (with void type) with the name of Called
.
Caller FuncCall[] = {a, b, c);
Will evaluate at compile-time to:
void (* Caller)() FuncCall[] = {a, b, c);
Which is essentially an array of void*
function pointers.
I am not sure of what you mean by void a()
, but to use function pointers you must initialize them to point to functions. Consider the following example:
void printFunc(){
printf("Print Test\n");
}
int main(){
void (*functionPtr)();
functionPtr = &printFunc; //functionPtr points to the address of printFunc()
functionPtr();
return 0;
}
Consider the following code:
#include <SoftwareSerial.h>
/* These are function prototypes - required by the compiler */
void a();
void b();
void c();
typedef void (* Caller)();
Caller FuncCall[] = {&a, &b, &c}; //initialize addresses for pointers
//String func_list[] = {"a","b","c"}; //this is a terrible way to do this
char func_list[] = {'a', 'b', 'c'}; //use this instead - if you wish
void setup(){
Serial.begin(9600);
}
void a(){
Serial.println("Called Function: a\n");
}
void b(){
Serial.println("Called Function: b\n");
}
void c(){
Serial.println("Called Function: c\n");
}
void loop(){
if(Serial.available())
switch(Serial.read()){
case 'a': //this could also be func_list[0], if you want
FuncCall[0]();
break;
case 'b':
FuncCall[1]();
break;
case 'c':
FuncCall[2]();
break;
default:
Serial.println("There was an error\n");
break;
}
}
Where you simply send the Arduino a character (a, b, or c) through the Serial Monitor. You can make the implementation as complicated you want. You could, for example, have string parsing and indexing into the array as suggested - but the above implementation could not get any more simple to the best of my knowledge.