avr-gcc-list
[Top][All Lists]
Advanced

[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:10:16 -0600
User-agent: Mozilla Thunderbird 0.7.3 (Windows/20040803)

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())
Yes.

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
#else
   #define global_text(sym, value)   extern char sym[] PROGMEM
#endif

// 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.

HTH
Eric



reply via email to

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