|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
#define and #ifdef/endif
If I have code that says (example only)
Quote:
Thank you in advance, |
|
#2
|
||||||
|
||||||
|
Re: #define and #ifdef/endif
That should not work. #define statements, #ifdef, #endif, and anything starting with the # symbol are preprocessor directives. Therefore, they are not runtime code - they just tell the compiler how to compile. They do nothing at run time.
|
|
#3
|
|||
|
|||
|
Re: #define and #ifdef/endif
Code:
#ifdef PROG1
printf("Hello, world.");
#endif
#ifdef PROG2
printf("FIRST Robotics");
#endif
If we #define PROG1, then it puts the "Hello, world" printf in. If we #define PROG2, then it puts the "FIRST Robotics" printf in. I used these for a few sensor values that differed from our practice to competition robot.. I'll post the code we used when I get a chance. |
|
#4
|
||||
|
||||
|
Re: #define and #ifdef/endif
Quote:
However, the toggling of your #defines happens before you compile, not at runtime like it looks like Dan was suggesting. |
|
#5
|
||||
|
||||
|
Re: #define and #ifdef/endif
...Chris posted while I was typing this....
Short answer is that #define and #if switches are processed before the compiler ever sees the code. Therefore you can't do what you are trying to do. You'd be better off storing the program number into a variable and deciding what to do based on that Code:
enum
{
PROG_DO_NOTHING = 0,
PROG1,
PROG2
};
static int g_prog_num = PROG_DO_NOTHING;
....
if(rc_dig_in01==1)
{
g_prog_num = PROG1;
}
else if(rc_dig_in02==1)
{
g_prog_num = PROG2;
}
switch(g_prog_num)
{
case PROG1:
printf("Hello, world.");
break;
case PROG2:
printf("FIRST Robotics");
break;
default:
printf("DO NOTHING");
break;
}
|
|
#6
|
|||||
|
|||||
|
Re: #define and #ifdef/endif
Quote:
Dave, I am unfamiliar with the 'enum' command, what does it do? Assign values to the identities inside, ascending from the value assigned to the first identity? Again, thank you very much, ![]() |
|
#7
|
||||
|
||||
|
Re: #define and #ifdef/endif
Quote:
Code:
//This is equivalent to
#define STEP_A 0
#define STEP_B (STEP_A + 1)
#define STEP_C 5
#define STEP_D (STEP_C+1)
enum
{
STEP_A = 0,
STEP_B,
STEP_C = 5,
STEP_D,
}
Say that you have 3 steps to a process defined as #defines Code:
#define STEP_A 1 #define STEP_B 2 #define STEP_C 3 Code:
#define STEP_A 1 #define STEP_B 2 #define STEP_C 3 #define STEP_D 4 If your original code was written with enums Code:
enum
{
STEP_A = 1,
STEP_B,
STEP_C
};
Code:
enum
{
STEP_A = 1,
STEP_B,
STEP_D,
STEP_C
};
In the context that I used in my original response, #defines would have been just fine, I just chose to go with the enum because I wasn't too concerned with the values that the constants took on....I was more interested in having unique constants. You can find some more info about enums here |
|
#8
|
|||||
|
|||||
|
Re: #define and #ifdef/endif
Ok, now, I have another question. Can I use 'enum' in a header, and just include that header in all files that I need the constants declared in enum?
Thank you, Dan |
|
#9
|
||||
|
||||
|
Re: #define and #ifdef/endif
Yup, we do it all the time. Remember that when you do a #include that it is the same thing as copy/pasting the included file into your current file.
One thing to be careful of is that enums have scope, so you will want to be careful with the way you name your constants. If you ran the following Code:
enum
{
A=1,
B
};
void my_function(void)
{
enum
{
B = 4
}
printf("A: %d B: %d\n",A,B);
}
void main(void)
{
printf("A: %d B: %d\n",A,B);
my_function();
printf("A: %d B: %d\n",A,B);
}
Code:
A: 1 B: 2 A: 1 B: 4 A: 1 B: 2 |
|
#10
|
|||||
|
|||||
|
Re: #define and #ifdef/endif
When using the scripting/navigation code provided by Kevin Watson, u would call a switch statement with the following parameter:
Code:
command_list[current_command].command Code:
int check_switch_box(void)
{
if(rc_dig_in01==1)
return command_list_1[current_command].command;
}
Code:
switch(check_switch_box()){}
Code:
switch(command_list[current_command].command){}
That should work, right? Since command_list[current_command].command really is just a value inside a struct. |
|
#11
|
||||
|
||||
|
Re: #define and #ifdef/endif
Quote:
It may be a little cleaner to do it in two steps Code:
new_cmd = check_switch_box(); switch(new_cmd) An even cleaner way to do it would be to get your program number up front and then use pass that in to your function. Code:
//Outside of autonomous loop....
if(rc_dig_in01==1)
{
g_prog_num = 1;
}
else if(rc_dig_in02==1)
{
g_prog_num = 2;
}
...
//Inside autonomous loop....
//Assuming that the function can see current_command
new_cmd = get_auton_command(g_prog_num);
switch(new_cmd)
{
.....
}
...
// Using unsigned ints for the sake of argument
unsigned int get_auton_command(unsigned int prog_num)
{
unsigned int command = DO_NOTHING;
if(prog_num == 1)
{
command = command_list_prog1[current_command].command
}
else if(prog_num == 2)
{
command = command_list_prog2[current_command].command
}
return command;
}
|
|
#12
|
|||||
|
|||||
|
Re: #define and #ifdef/endif
Quote:
Thanks again, |
|
#13
|
||||
|
||||
|
Re: #define and #ifdef/endif
Quote:
if(rc_dig_in01==1) { printf("Hello, world.\r"); } else if (rc_dig_in02==1) { printf("FIRST Robotics\r"); } |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|