The following code does not display the correct values for x and y. If I omit PROGMEM then it does display the correct values. I am trying to use PROGMEM because the my actual code is using an array of 8 256 char bitmaps but this simplified version demonstrates the problem. Can anyone point out what I am doing wrong?
The compiler doesn't keep track of which data is in SRAM and which is in FLASH (PROGMEM). You have to remember for it and call the function for reading from FLASH if you want to read a value from FLASH.
In your case the data is type 'int' which is a 16-bit value. To read it you have to pass the address to pgm_read_word_near();
The compiler is optimizing out the structure, everything is known at compile time: test[2].y becomes a literal value and is removed just like normal static data. PROGMEM means nothing to the compiler, it just tells the linker which memory section the data will reside in.
Its similar to doing something like this:
const int i = 4;
Serial.print( i ); // i is stored as a simple load instruction. The variable i takes up no ram.
If this is how you use the data, you can simply avoid PROGMEM as values inlined into code are effectively in PROGMEM.
If you need to access the array sequentially (in a loop or from an index not known at compile time), then you'll need to use PROGMEM, otherwise the structure will creep back into ram.
pYro_65:
The compiler is optimizing out the structure, everything is known at compile time: test[2].y becomes a literal value and is removed just like normal static data. PROGMEM means nothing to the compiler, it just tells the linker which memory section the data will reside in.
Its similar to doing something like this:
const int i = 4;
Serial.print( i ); // i is stored as a simple load instruction. The variable i takes up no ram.
If this is how you use the data, you can simply avoid PROGMEM as values inlined into code are effectively in PROGMEM.
If you need to access the array sequentially (in a loop or from an index not known at compile time), then you'll need to use PROGMEM, otherwise the structure will creep back into ram.