# State Machine

Status
Not open for further replies.

#### TucsonDon

##### Member
I have some code that I down loaded that I have been studying, it uses a state machine for a menu structure.

C:
/* Instance data definition of state machine */
typedef struct{
} XLP_8BIT_MENU_INSTANCEDATA_T;
C:
/* Macros to test if the machine is in a certain state. */
#define XLP_8BIT_MENU_IS_IN_LENGTH_TOGGLE_SM(instance)((((&instance)->stateVarXLP_SM==Length_Toggle_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_LENGTH_DISPLAY_SM(instance)((((&instance)->stateVarXLP_SM==Length_Display_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_SHOW_DEGREES_SM(instance)((((&instance)->stateVarXLP_SM==Show_Degrees_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_TIMEOUT_SM(instance)((((&instance)->stateVar==Timeout_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_BUZZER_SM(instance)((((&instance)->stateVarXLP_SM==Buzzer_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_SAVE_CUSTOM_SM(instance)((((&instance)->stateVarXLP_SM==Save_Custom_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_HFPLL_SM(instance)((((&instance)->stateVarXLP_SM==HFPLL_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_TIME_EXIT_SM(instance)((((&instance)->stateVarXLP_SM==Time_Exit_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_BUZZER_TOGGLE_SM(instance)((((&instance)->stateVarXLP_SM==Buzzer_Toggle_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_INIT_CONFIG_SM(instance)((((&instance)->stateVar==Init_Config_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_LF_SM(instance)((((&instance)->stateVarXLP_SM==LF_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
I have been trying to follow the flow by stepping through the code to lean and then adapt to my application. What is the benefit of defining the states in the header file?

#### dknguyen

##### Well-Known Member
I have some code that I down loaded that I have been studying, it uses a state machine for a menu structure.

C:
/* Instance data definition of state machine */
typedef struct{
} XLP_8BIT_MENU_INSTANCEDATA_T;
C:
/* Macros to test if the machine is in a certain state. */
#define XLP_8BIT_MENU_IS_IN_LENGTH_TOGGLE_SM(instance)((((&instance)->stateVarXLP_SM==Length_Toggle_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_LENGTH_DISPLAY_SM(instance)((((&instance)->stateVarXLP_SM==Length_Display_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_SHOW_DEGREES_SM(instance)((((&instance)->stateVarXLP_SM==Show_Degrees_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_TIMEOUT_SM(instance)((((&instance)->stateVar==Timeout_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_BUZZER_SM(instance)((((&instance)->stateVarXLP_SM==Buzzer_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_SAVE_CUSTOM_SM(instance)((((&instance)->stateVarXLP_SM==Save_Custom_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_HFPLL_SM(instance)((((&instance)->stateVarXLP_SM==HFPLL_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_TIME_EXIT_SM(instance)((((&instance)->stateVarXLP_SM==Time_Exit_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_BUZZER_TOGGLE_SM(instance)((((&instance)->stateVarXLP_SM==Buzzer_Toggle_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_INIT_CONFIG_SM(instance)((((&instance)->stateVar==Init_Config_SM)) ? (1U) : (0U))
#define XLP_8BIT_MENU_IS_IN_LF_SM(instance)((((&instance)->stateVarXLP_SM==LF_SM)&&((&instance)->stateVar==XLP_SM)) ? (1U) : (0U))
I have been trying to follow the flow by stepping through the code to lean and then adapt to my application. What is the benefit of defining the states in the header file?
Take into account that normal practice is to include header files, not c or cpp files. Therefore, the benefit is that you can define them in just one place and include the headers wherever the definitions are used.

Otherwise you would have to define the definitions every time they are used and update them all every time they are modified...error prone to say the least.

It also has to do with the fact that functions should be "basically" usable to the programmer from just looking at the header file. If they were defined in the cpp file, then the programmer would have to look beyond the header file just to be able to use them at all.

If you do not expect to use the definitions anywhere else, then define them in the cpp file where they are used to limit the scope of the definitions.

#### TucsonDon

##### Member
Thanks for the info

Status
Not open for further replies.