Im trying to read data from the flash memory, my data is a two dimensional array of bytes. If I try to read individual bytes with pgm_read_byte(), the values all return 0. But if I use pgm_read_word and evaluate each byte the values are as expected. While this isn't a big deal since the second dimension of the array is 8 bytes, it is a little inconvenient. Clearly I've made a mistake, could someone advise me where I am going wrong?
Best Regards
Josh
const byte squareFont[84][8] PROGMEM = { ... }
// this doesn't work
byte a = pgm_read_byte(&squareFont[0]);
byte b = pgm_read_byte(&squareFont[1]);
Serial.println(a); // prints 0
Serial.println(b); // prints 0
// but if we get a uint16 and mask off each byte we get the correct values
int testByte = pgm_read_word(&squareFont[0]);
byte low = testByte & 255;
byte high = (testByte & (255 << 8)) >> 8;
Serial.println();
Serial.println(low); // prints 0
Serial.println(high); // prints 62
Thanks! For some reason I was sure I wouldn't be able to access the data on the flash using 2d square braces, I thought I would have to walk the array 0 to 84*8-1.
That is how I do it. I tried failed at passing two-dimension arrays to functions and decided, using a single-dimension array, with the math you mention, was what I needed. A flat array was good enough to read ten bits wide and two hundred fifty long, AND I could have more sets of arrays of any length (not 10x250)... the calculation was; pgm_read_byte(&list[rows * columns + column]))
Thanks for your reply. I had considered making the 2d array flat also, but ultimately I didn't need to. I think my reasoning regarding array access was based on passing 2d arrays to functions, which I couldn't figure out how to do. So my solution was to pass a pointer to the start of the array.
void fontCopy(const byte * f) {
byte b;
for (int i = 0; i < 84; ++ i) {
for (int j = 0; j < 8; ++j) {
font[i][j] = pgm_read_byte(&f[i*8 + j]);
}
}
calcCharBounds();
}