Use #define or enum in switch() case(s) ?
-
Academic question. Which do you prefer - using preprocessor directive #define to replace case state - example "case 0:" with #define NORTH 0 and code "case NORTH:" or use enum? Here is a sample code using enum: Enumerated variables have a natural partner in the switch statement, as in the following code example. #include enum compass_direction { north, east, south, west }; enum compass_direction get_direction() { return south; } /* To shorten example, not using argp */ int main () { enum compass_direction my_direction; puts ("Which way are you going?"); my_direction = get_direction(); switch (my_direction) { case north: puts("North? Say hello to the polar bears!"); break; case south: puts("South? Say hello to Tux the penguin!"); break; case east: puts("If you go far enough east, you'll be west!"); break; case west: puts("If you go far enough west, you'll be east!"); break; } return 0; }
-
Academic question. Which do you prefer - using preprocessor directive #define to replace case state - example "case 0:" with #define NORTH 0 and code "case NORTH:" or use enum? Here is a sample code using enum: Enumerated variables have a natural partner in the switch statement, as in the following code example. #include enum compass_direction { north, east, south, west }; enum compass_direction get_direction() { return south; } /* To shorten example, not using argp */ int main () { enum compass_direction my_direction; puts ("Which way are you going?"); my_direction = get_direction(); switch (my_direction) { case north: puts("North? Say hello to the polar bears!"); break; case south: puts("South? Say hello to Tux the penguin!"); break; case east: puts("If you go far enough east, you'll be west!"); break; case west: puts("If you go far enough west, you'll be east!"); break; } return 0; }
enum.
-
Academic question. Which do you prefer - using preprocessor directive #define to replace case state - example "case 0:" with #define NORTH 0 and code "case NORTH:" or use enum? Here is a sample code using enum: Enumerated variables have a natural partner in the switch statement, as in the following code example. #include enum compass_direction { north, east, south, west }; enum compass_direction get_direction() { return south; } /* To shorten example, not using argp */ int main () { enum compass_direction my_direction; puts ("Which way are you going?"); my_direction = get_direction(); switch (my_direction) { case north: puts("North? Say hello to the polar bears!"); break; case south: puts("South? Say hello to Tux the penguin!"); break; case east: puts("If you go far enough east, you'll be west!"); break; case west: puts("If you go far enough west, you'll be east!"); break; } return 0; }
As already answered, enum is better. One advantage is that modern compilers can throw a warning when not all enums are handled by the switch statement. So you will be noticed when you forgot to handle a newly added definition.
-
Academic question. Which do you prefer - using preprocessor directive #define to replace case state - example "case 0:" with #define NORTH 0 and code "case NORTH:" or use enum? Here is a sample code using enum: Enumerated variables have a natural partner in the switch statement, as in the following code example. #include enum compass_direction { north, east, south, west }; enum compass_direction get_direction() { return south; } /* To shorten example, not using argp */ int main () { enum compass_direction my_direction; puts ("Which way are you going?"); my_direction = get_direction(); switch (my_direction) { case north: puts("North? Say hello to the polar bears!"); break; case south: puts("South? Say hello to Tux the penguin!"); break; case east: puts("If you go far enough east, you'll be west!"); break; case west: puts("If you go far enough west, you'll be east!"); break; } return 0; }
enum...... Preprocessor directives can make troubleshooting and reading your code tricky, not worth going down that route unless you don't have alternatives, in this case, you have a perfectly viable solution with an enum.
-
Academic question. Which do you prefer - using preprocessor directive #define to replace case state - example "case 0:" with #define NORTH 0 and code "case NORTH:" or use enum? Here is a sample code using enum: Enumerated variables have a natural partner in the switch statement, as in the following code example. #include enum compass_direction { north, east, south, west }; enum compass_direction get_direction() { return south; } /* To shorten example, not using argp */ int main () { enum compass_direction my_direction; puts ("Which way are you going?"); my_direction = get_direction(); switch (my_direction) { case north: puts("North? Say hello to the polar bears!"); break; case south: puts("South? Say hello to Tux the penguin!"); break; case east: puts("If you go far enough east, you'll be west!"); break; case west: puts("If you go far enough west, you'll be east!"); break; } return 0; }
One more question - since the state is changed using interrupt it needs to be declared as volatile.It compiles with "volatile enum..." so it should be correct syntax, but I cannot test it as yet - the hardware is not ready, but I will try to emulate the interrupt. AM I on the right track?
-
One more question - since the state is changed using interrupt it needs to be declared as volatile.It compiles with "volatile enum..." so it should be correct syntax, but I cannot test it as yet - the hardware is not ready, but I will try to emulate the interrupt. AM I on the right track?
No you don't have to declare enum volatile, and the compiler should give you error. This because you are creating an enumerated series of constants and they can't change. You must declare volatile your input variable, because this is the one that changes under interrupt. You must do this to advice the compiler to take care when optimizing code to make no assumptions on its value. If you don't there will be cases in which the compiler will assume that the variable is in a specific condition and will omit some code or use an old value.
-
No you don't have to declare enum volatile, and the compiler should give you error. This because you are creating an enumerated series of constants and they can't change. You must declare volatile your input variable, because this is the one that changes under interrupt. You must do this to advice the compiler to take care when optimizing code to make no assumptions on its value. If you don't there will be cases in which the compiler will assume that the variable is in a specific condition and will omit some code or use an old value.
Thanks, the "problem" is that Arduino IDE "compiler" is so "automated" AKA stuff is hidden from user. It did not complain when I tested this base code. At present the input "pins" array is not volatile and I'll change that. I have a small additional challenge implementing the ISR ( interrupt service routine). The interrupt code involves callback and is defined as Arduino API. The ISR does not accept any parameters , consequently each interrupt process implementation has to have its own ISR. So in theory the input ( variable ) is processed only in one place. It is convoluted mess and I am working on it. But I needed the basic state machine to work first. SO far so good.
-
One more question - since the state is changed using interrupt it needs to be declared as volatile.It compiles with "volatile enum..." so it should be correct syntax, but I cannot test it as yet - the hardware is not ready, but I will try to emulate the interrupt. AM I on the right track?
Somewhat unrelated to your original question but........ REMOVE ALL THOSE #defines! That is some ugly code! If you need a #define on a print function, do it once with a macro and not every single place you want to use that function.
//pseudo-code
#ifdef DEBUG
#define UTIL_PROC(X) Utility_LCD_Process((X));
#else
#define UTIL_PROC(X)
#endif//When using it, you just call the macro
case LED_on:
{
UTIL_PROC("case LED_on")
State_Array[iInputPin] = LED_off;
break;
} -
Somewhat unrelated to your original question but........ REMOVE ALL THOSE #defines! That is some ugly code! If you need a #define on a print function, do it once with a macro and not every single place you want to use that function.
//pseudo-code
#ifdef DEBUG
#define UTIL_PROC(X) Utility_LCD_Process((X));
#else
#define UTIL_PROC(X)
#endif//When using it, you just call the macro
case LED_on:
{
UTIL_PROC("case LED_on")
State_Array[iInputPin] = LED_off;
break;
} -
Good suggestion, however I did moved the #ifdef / #endif to the function itself so it does look cleaner. Actually - is the #else necessary?
If you're using my method, yes... otherwise the compiler will tell you that function is undefined.