View previous topic :: View next topic |
Author |
Message |
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
Strings: strcpy and constant strings |
Posted: Fri Jan 19, 2007 6:23 am |
|
|
Hi,
I have
Code: | #define BRAILLE_COMMANDO "BRAI"
#define E3_SUSPEND "SUSP"
#define BATTERIJ_COMMANDO "BATT"
... |
Code: | strcpy (command,BRAILLE_COMMANDO); // Commando gaan vergelijken met de geprogrammeerde commando's
if ( !strncmp (temp_array,command,4) )
return SCHRIJF_NAAR_LEESREGEL; // Schrijf Braille
strcpy (command,E3_SUSPEND);
if ( strncmp (temp_array,command,4) == 0 )
return E3_IN_SUSPEND; // Na 30s idlen gaat de E3 in suspend
strcpy (command,BATTERIJ_COMMANDO);
if ( strncmp (temp_array,command,4) == 0 )
return VBAT_METEN;
... |
Everytime I have to do the same function. I want this:
Copy_string_to_ram ( command, BRAILLE_COMMANDO )
{
strcpy (command, ?);
}
Is that possbile?
Are their other ways to make this code smaller? |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Fri Jan 19, 2007 7:49 am |
|
|
The shortest but least managable code would be to make a jump tree based on single characters.
Do you have to check for syntax errors ? Or you can assume all commandos are from the valid set ? (i.e. is it human input or machine to machine communication ?) |
|
|
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
|
Posted: Fri Jan 19, 2007 8:10 am |
|
|
machine to machine communication. All commandos should be OK. |
|
|
ferrumvir
Joined: 01 Feb 2006 Posts: 64 Location: England
|
|
Posted: Fri Jan 19, 2007 8:12 am |
|
|
I use code like this to initialise some global variables, this uses up RAM instead of ROM but would speed the execution of the section you mentioned. This routine is called once, at start up. You can then lose all the copy ROM to RAM (strcpy) functions from your section, by using the global STR variables. Use this if you want to increase speed and are not limited on RAM.
The jump tree mentioned by libor above will be even quicker, but much harder to develop, debug and change.
Code: |
//==========================================================================
//==========================================================================
// STRING CONSTANTS
//==========================================================================
//==========================================================================
char STRstart[6];
char STRstop[5];
char STRshelp[6];
char STRset_[5];
char STRpwm_[5];
void str_init()
{
strcpy(STRstart,"start");
strcpy(STRstop,"stop");
strcpy(STRshelp,"shelp");
strcpy(STRset_,"set ");
strcpy(STRpwm_,"pwm ");
}
//==========================================================================
//==========================================================================
// END - STRING CONSTANTS
//==========================================================================
//==========================================================================
|
|
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Fri Jan 19, 2007 8:33 am |
|
|
You can store all your commandos also in ROM:
const char commandos[] = {"BRAISUSPBATT"}
or for more readibility: though you lose the 4-letter word phenomena (no pun intended :-)
const char commandos[] = {"BRAI SUSP BATT"}
...and write an #asm routine* (or a C routine with read_program_memory() instruction) to find your string in the program memory-located table returning the position index. Which index (assuming all your commandos are 4 letter words) you could rotate right to get the commandos sequential address in the table. This index could serve in a switch() case jump table.
Be sure not to make a default: case, this will help the compiler to construct a much smaller jump table from the switch() inctruction (rather use one single index beyond the table as the 'everything else' case.
* you will have to #locate your ROM table in a fixed memory postion to find it. or use the label_address()
BTW. How many different commandos do you have ? It is not worth the hassle if you have only these three. |
|
|
|