A Mode State Machine
typedef enum _SM_USER_MODE
{
UM_POWERUP,
UM_IDLE
} SM_USER_MODE;
static uint8_t CurrentMode = UM_POWERUP;
static uint8_t CurrentModeLast = 0xff;
uint8_t JustEnteredNewMode = 0;
//------------------------------
//----- CHECK FOR NEW MODE -----
//------------------------------
JustEnteredNewMode= 0;
if (CurrentMode != CurrentModeLast)
{
//Flag that we've just entered a new mode
JustEnteredNewMode = 1;
CurrentModeLast = CurrentMode;
}
//------------------------------------
//------------------------------------
//----- PROCESS THE CURRENT MODE -----
//------------------------------------
//------------------------------------
switch (CurrentMode)
{
case UM_POWERUP:
//-------------------
//-------------------
//----- POWERUP -----
//-------------------
//-------------------
if (JustEnteredNewMode)
{
//----- JUST ENTERED THIS MODE -----
}
//------------------------
//----- PROCESS MODE -----
//------------------------
CurrentMode = UM_IDLE;
break;
case UM_IDLE:
//-------------------------------
//-------------------------------
//----- IDLE - DEFAULT MODE -----
//-------------------------------
//-------------------------------
if (JustEnteredNewMode)
{
//----- JUST ENTERED THIS MODE -----
}
//------------------------
//----- PROCESS MODE -----
//------------------------
break;
//<<<<< ADD OTHER MODES HERE
} //switch (CurrentMode)
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.