I'm working on a small HTTP server. I am building a router and since there could be quite a few routes, I wanted to put them into flash memory so that I don't have to use the valuable SRAM. However either I don't understand something correctly or something weird is happening since I can't seem to be able to read back my stored data from flash.
I have a struct which contains a function pointer and a char pointer. I want to store an array of these structs into flash and read them back. However with a small debug print I can see I can't read back the char pointer correctly. It prints garbish to the serial port.
Here is a small example.
#include <avr/pgmspace.h>
typedef struct {
void (*func)();
const char *URI;
} Route;
void test1() {
Serial.println("Executed testfunc1");
}
void test2() {
Serial.println("Executed testfunc2");
}
const char route1URI[] PROGMEM = "/route1";
const Route route1 PROGMEM = {
test1,
route1URI
};
const char route2URI[] PROGMEM = "/route2";
const Route route2 PROGMEM = {
test2,
route2URI
};
const Route routingTable[] PROGMEM = {
route1,
route2
};
void (*getRoute(char *URI))() {
Route *r = (Route *)pgm_read_word(routingTable + 0);
char *f = (char *)pgm_read_word(r->URI);
Serial.println(f);
return r->func;
}
void setup() {
Serial.begin(9600);
while (!Serial) { }
Serial.println("started setup");
void (*fn)() = getRoute("sometest");
// will cause errors if called
//fn();
Serial.println("ended setup");
}
void loop() {
// put your main code here, to run repeatedly:
}