puzzled about some pointers to text

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I am working on a XMEGA A4U.

I have some const char arrays in PROGMEM that have short names for all the time zones:

const char TimeZoneSmall01[] PROGMEM = " -12 DateLine";
const char TimeZoneSmall02[] PROGMEM = " -11 Mdwy Isl";
const char TimeZoneSmall03[] PROGMEM = " -10   Hawaii";

(etc.. 36 of them)

 

I then have an PROGMEM const array of pointers to those above entries:

 

const char * const TimeZonesSmall [] PROGMEM = { TimeZoneSmall01, TimeZoneSmall02, TimeZoneSmall03, .....

 

I am running into trouble today getting that text label out of PROGMEM.

 

I wanted to simply take a pointer to it as such:

unsigned char theSelection = theList->currentSelection;

const char *textPointer;
textPointer = TimeZonesSmall[theSelection];

The compiler likes it, but it yields garbage when I print out the timezone on the screen. However, If I do the seemingly EXACTLY SAME thing, this way works --

unsigned char theSelection = theList->currentSelection;

const char *textPointer;
if ( theSelection == 0 )  {  textPointer = TimeZonesSmall[theSelection]; }
if ( theSelection == 1 )  {  textPointer = TimeZonesSmall[theSelection]; }

The later prints the correct information to the screen.

How can this be?!?

 

Thanks for any insight

Mike

 

 

 

 

Last Edited: Tue. Dec 29, 2015 - 12:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The PROGMEM system has no mechanism has no concept of pointer to data in program memory.

Use strcpy_P and its ilk on strings in program memory.

 

If available, __flash would be better.

__flash has roughly the same status as const and volatile.

There is such a thing as pointer to __flash char.

"Demons after money.
Whatever happened to the still beating heart of a virgin?
No one has any standards anymore." -- Giles

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are using two levels of PROGMEM indirection so you would need to unwind this with two levels of pgm_read*()  functions. As Michael says,  the new__flash is a much cleaner solution as it obviates the need for an explicit LPM dereference. If your compiler version is beyond 4.5 try __flash. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This thread might be useful (starting at #8):

https://www.avrfreaks.net/comment/1436071#comment-1436071

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

progmem is the old way to do it. __flash works much nicer and you have the memx concept so you can make a function that takes flash or ram addresses.

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks everyone. I moved my entire solution over to __flash and have everything running nicely now. It's certainly cleaner !

 

I am wondering... I have some types of screen "controls" which get their labels from flash and some which get their label from normal data space.

What is the elegant way to make a screen printer function which can print from either space?

 

Do I need to pass a void * to the text string, and pass another param which says "flash or data", then re-cast the void * as either __flash const char *  or  char *?

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]