Pages: [1]   Go Down
Author Topic: reading char array from PROGMEM?  (Read 1067 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Got Karma?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
 
« Last Edit: December 23, 2010, 05:39:30 pm by roypardi » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 137
Posts: 6805
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
  }
}
Logged

Pages: [1]   Go Up
Jump to: