Go Down

Topic: reading char array from PROGMEM? (Read 1 time) previous topic - next topic

roypardi

Dec 23, 2010, 11:34 pm Last Edit: Dec 23, 2010, 11:39 pm by roypardi Reason: 1
Hi~ I have a lot of static data to store - in the form of multidim char arrays of varying lengths. I figure this is a good opportunity to learn about PROGMEM since I haven't used it before.

Looking at the reference pg (http://www.arduino.cc/en/Reference/PROGMEM) I get some of it but... not quite. Before I start just poking the code I thought I would ask here.

In the sketch below, what I would like to do is get a pointer to CHR_21 and then be able to step through it without knowing it's length.


Code: [Select]
#include <avr/pgmspace.h>

PROGMEM prog_char CHR_21[][2]={{10,0},{5,21},{5,7},{128,128},{5,2},{4,1},{5,0},{6,1},{5,2}};

char *buffer;

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


void loop(){
 
 strcpy_P(buffer, (char*)pgm_read_word(&CHR_21));


for(; *buffer; buffer++){
 
  Serial.println(buffer[0]);
Serial.println(buffer[1]);
}

delay(500);
}

westfw

What you want to do is move the address calculations before the progmem fetch.  Addresses are addresses, and it doesn't matter if wait until the very end to actually tell the program to address the progmem instead of the ram
Code: [Select]
PROGMEM prog_char CHR_21[][2]={{10,0},{5,21},{5,7},{128,128},{5,2},{4,1},{5,0},{6,1},{5,2}};

char *buffer;

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

void loop(){

for(i = 0; true; i++){
  Serial.println(pgm_read_byte(&CHR_21[i][0]);
 Serial.println(pgm_read_byte(&CHR_21[i][1]);
 if (end_of_array(...))  // better do something!
   break;
 }
}

Go Up