call function pointer from progmem


what correct syntax calling function pointer in progmem?  combination of cast , pgm_read_word or whatever need call function struct.



code: [select]

typedef boolean (*item_function)();
typedef struct menuitem_t {
char text[16];
item_function func;
} menuitem;

menuitem progmem firstlist[4] = { { "item_one", fun1 }, { "item_two", fun2 }, { "item_three",
fun3 }, { "item_four", fun4 } };

menuitem* itemptr = firstlist;

void setup(){
   
     // here want use itemptr call function
     //      struct instance pointing to.
     // in case should call fun1()

}

you had few things wrong, here how it.

code: [select]
boolean fun1() {
  serial.println(f("function 1"));
}
boolean fun2() {
  serial.println(f("function 2"));
}
boolean fun3() {
  serial.println(f("function 3"));
}
boolean fun4() {
  serial.println(f("function 4"));
}

typedef boolean (*item_function)();

const typedef struct menuitem_t {
  char *text;
  item_function func;
} menuitem;

menuitem firstlist[4] progmem = {
  { "item_one", &fun1 }, // need have reference function
  { "item_two", &fun2 },
  { "item_three", &fun3},
  { "item_four", &fun4 }
};

menuitem* itemptr = firstlist;

void setup() {

  // here want use itemptr call function the
  //      struct instance pointing to.
  // in case should call fun1()

  boolean (*function)(void); // function buffer

  serial.begin(115200);
  for (byte = 0; < 4; i++)
  {
    serial.println((char*)pgm_read_word(&(firstlist[i].text)));
    function = (item_function)pgm_read_word(&(firstlist[i].func)); // needs assigned function pointer in order use it.

    function(); // run function.
    serial.println();
  }

}
void loop() {}

this solution should added progmem page.


Arduino Forum > Using Arduino > Programming Questions > call function pointer from progmem


arduino

Comments