c++arduinoe-ink

Arduino static progmem cannot be accessed via variables


I cannot reference a 3d array of bytes stored in memory via variables

  letter[18][0]=letters[L][18][0];
  letter[18][1]=letters[L][18][1];
  letter[19][0]=letters[L][19][0];
  letter[19][1]=letters[L][19][1];
  for(int i = 20;i<32;i++){
    static int test = i;
    letter[i][0]=letters[L][i][0];
    letter[i][1]=letters[L][i][1];
  }

https://i.sstatic.net/Fkvxw.jpg

The section in the for loop produces garbage, in this case all zeros but it can be other nonsense if other characters are selected

const uint8_t letters[96][32][2] PROGMEM = {{{255,255}//' '
,{255,255}
,{255,255}
,{255,255}
,{255,255}
,{255,255}
,{255,255}
,{255,255}
,{255,255}
,{255,255}
...

this is how i declared the array i am using as a lookup table


Solution

  • You cannot access variables marked as PROGMEM like standard variable. You have to use special functions to read values from them.

    PROGMEM - Arduino Reference

    It will be like this:

      letter[18][0]=pgm_read_byte_near(&letters[L][18][0]);
      letter[18][1]=pgm_read_byte_near(&letters[L][18][1]);
      letter[19][0]=pgm_read_byte_near(&letters[L][19][0]);
      letter[19][1]=pgm_read_byte_near(&letters[L][19][1]);
      for(int i = 20;i<32;i++){
        static int test = i;
        letter[i][0]=pgm_read_byte_near(&letters[L][i][0]);
        letter[i][1]=pgm_read_byte_near(&letters[L][i][1]);
      }