[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Space optimisation for PSTR()

From: E. Weddington
Subject: Re: [avr-gcc-list] Space optimisation for PSTR()
Date: Fri, 27 Aug 2004 11:18:37 -0600
User-agent: Mozilla Thunderbird 0.7.3 (Windows/20040803)

E. Weddington wrote:

E. Weddington wrote:

Bernard Fouche wrote:

or should I manage my strings myself? (by passing the address of a string if
I re-use it instead of relying on PSTR())

It's fairly straightforward to set up a header file that contains all your defined strings with identifiers, then use the identifiers everywhere in your code.

Ok, I've been told (thanks Ted) that I should give an example as my statement above might be confusing.

There are two different ways to manage a centralized string repository.

1. You can place all the definitions in a .c file and have a corresponding .h file that declares the same identifiers but with extern.

2. Another way to do this, admittedly more complex, is to have a single header file, with the following:

file text.h:
#include <avr/pgmspace.h>

// Must have a C file with the MAIN_C definition in it.
#ifdef MAIN_C
   #define global_text(sym, value)   char sym[] PROGMEM = value
   #define global_text(sym, value)   extern char sym[] PROGMEM

// Define global text strings here.
global_text(text_foo, "FOO");
global_text(text_bar, "BAR");
global_text(text_baz, "BAZ");
// etc.

The trick with this header file is that it *must* be included in one .c file, such as your file that has the main() function, and the .c file must have the define above with the include file after this:

file main.c
#define MAIN_C

#include "text.h"

Then, the strings are defined in main.c but when the text.h file is included in other files, the same string identifiers are declared as extern.

The advantages of this method is that you don't have to write the identifers twice (less typing) and it reduces the possibilty of typo errors between the definition and the extern declaration.

Oh, and another advantage is if you are internationalizing your product, then all the text strings (usually used on a display) are in one location and can more easily be translated and switched with conditional compilation but keeping the same identifiers. Example:

#if defined(LANG_ENGLISH)
global_text(text_foo, "FOO");
global_text(text_bar, "BAR");
global_text(text_baz, "BAZ");

#if defined(LANG_PIG_LATIN)
global_text(text_foo, "OOFAY");
global_text(text_bar, "ARBAY");
global_text(text_baz, "AZBAY");


reply via email to

[Prev in Thread] Current Thread [Next in Thread]