Arduino: How to pass a pointer in Program Space as parameters to a function

179 views Asked by At

I am developing in Arduino, some menus to get them on the screen. I declare the menus as tables of text strings defined in Program space as in the example. How can I pass the pointer Menu_Principal as a parameter to a function?

Thank you all very much in advance.

const char _str_opcion_0[] PROGMEM = "Opción Menú 1";
const char _str_opcion_1[] PROGMEM = "Opción Menú -2";
const char _str_opcion_2[] PROGMEM = "Opción Menú --3";
const char _str_opcion_3[] PROGMEM = "Opción Menú ---4";
const char _str_opcion_4[] PROGMEM = "Opción Menú ----5";
const char _str_opcion_5[] PROGMEM = "Opción Menú -----6";
const char _str_opcion_6[] PROGMEM = "Opción Menú ------7";
const char _str_opcion_7[] PROGMEM = "Opción Menú -------8";
const char _str_opcion_8[] PROGMEM = "Opción Menú --------9";
const char _str_opcion_9[] PROGMEM = "Opción Menú ---------A";
const char _str_opcion_A[] PROGMEM = "Opción Menú ----------B";
const char _str_opcion_B[] PROGMEM = "Opción Menú -----------C";

char* const Menu_Principal [] PROGMEM =
{
    _str_opcion_0, _str_opcion_1, _str_opcion_2, _str_opcion_3, _str_opcion_4, _str_opcion_5,
    _str_opcion_6, _str_opcion_7, _str_opcion_8, _str_opcion_9, _str_opcion_A, _str_opcion_B,
    NULL
};
1

There are 1 answers

2
hcheung On

You can pass a pointer to a function just like the usual way you do, but you can't just use it without copying the content from the flash to memory first.

const char _str_opcion_0[] PROGMEM = "Opción Menú 1";
const char _str_opcion_1[] PROGMEM = "Opción Menú -2";
const char _str_opcion_2[] PROGMEM = "Opción Menú --3";
const char _str_opcion_3[] PROGMEM = "Opción Menú ---4";
const char _str_opcion_4[] PROGMEM = "Opción Menú ----5";
const char _str_opcion_5[] PROGMEM = "Opción Menú -----6";
const char _str_opcion_6[] PROGMEM = "Opción Menú ------7";
const char _str_opcion_7[] PROGMEM = "Opción Menú -------8";
const char _str_opcion_8[] PROGMEM = "Opción Menú --------9";
const char _str_opcion_9[] PROGMEM = "Opción Menú ---------A";
const char _str_opcion_A[] PROGMEM = "Opción Menú ----------B";
const char _str_opcion_B[] PROGMEM = "Opción Menú -----------C";

PGM_P const Menu_Principal [] PROGMEM =
{
    _str_opcion_0, _str_opcion_1, _str_opcion_2, _str_opcion_3, _str_opcion_4, _str_opcion_5,
    _str_opcion_6, _str_opcion_7, _str_opcion_8, _str_opcion_9, _str_opcion_A, _str_opcion_B,
    NULL
};

void printMenu_P(PGM_P str) {
     char buf[strlen_P(str)+1];
     strcpy_P(buf, str);
     Serial.println(buf);
 }


void setup() {
 
    Serial.begin(115200);
    Serial.println();

    printMenu_P(Menu_Principal[0]);
}

BTW, PGM_P is just a macro defined by avrlib for const char*.