Quote:
|
Originally Posted by chakorules
Is there such a thing like this in C Code?
|
I think what you're looking for is to assign a pointer to the correct array of autonomous commands based on some input at runtime.
Example:
.h file:
Code:
struct commands command_list_drive_straight[] = {
/* Command parm 1 parm 2 parm 3 */
{CMD_WAIT, 1000, 0, 0},
{CMD_DRIVE, 1500, 0, 0},
{CMD_WAIT, 4000, 0, 0}
};
struct commands command_list_grab_tetra[] = {
/* Command parm 1 parm 2 parm 3 */
{CMD_WAIT, 1000, 0, 0},
{CMD_DRIVE, 1500, 0, 0},
{CMD_WAIT, 4000, 0, 0}
};
In .c:
Code:
struct commands* cmd_list = NULL;
if(input == 1)
{
cmd_list = command_list_drive_straight;
}
else if(input == 2)
{
cmd_list = command_list_grab_tetra;
}
else
{
/* Don't forget the else, else you'll be accessing a null pointer... :( */
}
etc...
Quote:
|
Originally Posted by Workaphobia
I wouldn't use a pointer to an array element because you'd be allocating space for structs that would not be used - you wouldn't switch modes after starting the process.
|
This actually doesn't matter - the compiler has to store the values that your code will use to initialize the array somewhere inside the program anyway, so it's essentially the same. Actually, using globals with assigned default values (especially if you make them const) is more efficient because then they can be stored directly in Flash and don't need to consume any RAM.