Professional Documents
Culture Documents
state machine)
Module variables:
GunYState_t CurrentState; uint8_t MyPriority
States: GunUp, GunDown (Modular states)
Events: EV_GunUp, EV_GunDown, EV_RightHallRisingEdge,
EV_LeftHallRisingEdge
bool InitGunY (uint8_t Priority)
Takes a priority number , returns true.
Initialize the MyPriority variable with the passed in parameter.
Initialize the port line to receive the gun tilt events. (two
ports for two Hall sensors)
Set CurrentState to GunDown
End of InitGunY
// CheckRightHall and CheckLeftHall will be in EventCheckers.c
bool CheckRightHall(void)
Returns true if it detects an event and takes no parameters.
Static local: LastInputState; local ReturnVal=False, Current
InputState
Get the CurrentInputState from the input line
If the state of the Right Hall has changed
If the current state of the input line is high
Post event RightHallRisingEdge to GunTiltModule
Endif
Set ReturnVal to True
Endif
Set Last InputState to CurrentInputState
Return ReturnVal
End of CheckRightHall
bool CheckLeftHall(void)
Returns true if it detects an event and takes no parameters.
Static local: LastInputState; local ReturnVal=False, Current
InputState
Get the CurrentInputState from the input line
If the state of the Left Hall has changed
If the current state of the input line is high
Post event LeftHallRisingEdge to GunTiltModule
Endif
Set ReturnVal to True
Endif
Reset Timer
End of RunGunTiltSM
Pseudo-code for the Gun Button Module
a state machine)
Events: EV_Fire
Module variables:
GunButtonState_t CurrentState; uint8_t MyPriority
bool InitGunButton (uint8_t Priority)
Takes a priority number , returns true.
Initialize the MyPriority variable with the passed in parameter.
Initialize the port line to receive the gun firing events. (one
port for potentiometer input)
Set CurrentState to Debouncing
Start Debounce_Timer for 50ms
End of Function
bool CheckButton(void)
Returns true if it detects an event and takes no parameters.
Static local: LastButtonState; local ReturnVal=False, Current
ButtonState
Set CurrentButtonState to state read from Port A Pin 2
If the CurrentButtonState is different than the LastButton State
Set ReturnVal to true
If the CurrentButtonState is pushed
Post ButtonPushed to ButtonDebounce
Else
Post ButtonUp to ButtonDebounce
end if
end if
Set LastButtonState to CurrentButtonState
Return ReturnVal
End of Function
ES_Event RunButtonDebounceSM (ES_Event ThisEvent)
Returns ES_NOEVENT and takes an event from the event queue.
If CurrentState is Debouncing
If EventType is ES_TIMEOUT and parameter is Debounce_Timer
Set CurrentState to Ready2Sample
end if
(a service that
States:Fear, GamePlay,GameEnd
Events:ES_TIMEOUT, EV_StartGame, EV_EndGame
Timers: GameTimer, EndTimer
Module variables:
FearJoyState_t CurrentState; uint8_t MyPriority
bool InitFearJoy (uint8_t Priority)
Takes a priority number , returns true.
Initialize the MyPriority variable with the passed in parameter.
Enable output ports
Set CurrentState to Fear
End of Function
ES_Event RunFearJoySM (ES_Event ThisEvent) (implements a 4-state
state machine)
The EventType field of ThisEvent will be one of: EV_TIMEOUT,
EV_StartGame, EV_EndGame
Set NextState to CurrentState
If CurrentState is Fear
If EventType is EV_StartGame
Set NextState to GamePlay
Start 60 second GameTimer
RedMoodLEDs();
end if
end if
If CurrentState is GamePlay
If EventType is EV_EndGame
Set NextState to Joy
Start 30 second EndTimer
RedMoodLEDsOFF
GreenMoodLEDs
FlashBulb()
Send Event EndGame to Timer and LEDBoard
end if
If EventType is EV_TimeOut and Parameter is GameTimer
Set NextState to Loss
Start 30 second EndTimer
Set off Flash Bulb
Send Event EndGame to Timer and LEDBoard
end if
end if
If CurrentState is GameEnd
If EventType is EV_TimeOut and Parameter is EndTimer
Set NextState to Fear
Set MoodLEDs to Red
end if
end if
Set CurrentState to NextState
return ES_NOEVENT
end Function
void RedMoodLEDs(char) {
If Input is On
Turn on Red LEDs
end if
If Input is Off
Turn off Red LEDs
end if
}
void GreenMoodLEDs(char) {
If Input is On
Turn on Green LEDs
end if
If Input is Off
Turn off Green LEDs
end if
}
2L
2R
4R
3L
4L
3R
1R
Returns ES_NO_EVENT
Local variables: NextState
Set NextState to CurrentState
Based on the state of the CurrentState variable choose one of the
following blocks of code:
CurrentState is LeftUp
If ThisEvent.EventType is EV_LtoLM
Set NextState to LeftMiddleUp
end if
If ThisEvent.EventType is EV_GunDown
Set NextState to LeftDown
end if
If ThisEvent.EventType is EV_Fire
Post EV_1L to PostLEDBoardSM with Param 1
end if
CurrentState is LeftDown
If ThisEvent.EventType is EV_LtoLM
Set NextState to LeftMiddleDown
end if
If ThisEvent.EventType is EV_GunUp
Set NextState to LeftUp
end if
If ThisEvent.EventType is EV_Fire
Post EV_3L to LED Board Lighting SM with Param 3
end if
CurrentState is LeftMiddleUp
If ThisEvent.EventType is EV_LMtoL
Set NextState to LeftUp
end if
If ThisEvent.EventType is EV_LMtoM
Set NextState to MiddleUp
end if
If ThisEvent.EventType is EV_GunDown
Set NextState to LeftMiddleDown
end if
If ThisEvent.EventType is EV_Fire
Post EV_2L to LED Board Lighting SM with Param 2
end if
CurrentState is LeftMiddleDown
If ThisEvent.EventType is EV_LMtoL
Set NextState to LeftDown
end if
If ThisEvent.EventType is EV_LMtoM
Set NextState to MiddleDown
end if
If ThisEvent.EventType is EV_GunUp
Set NextState to LeftMiddleUp
end if
If ThisEvent.EventType is EV_Fire
Post EV_4L to LED Board Lighting SM with Param 4
end if
CurrentState is MiddleUp
If ThisEvent.EventType is EV_MtoLM
Set NextState to LeftMiddleUp
end if
If ThisEvent.EventType is EV_MtoRM
Set NextState to RightMiddleUp
end if
If ThisEvent.EventType is EV_GunDown
Set NextState to MiddleDown
if ThisEvent.EventType is EV_FireGun
Post EV_Dummy to LED Board lighting SM
end if
CurrentState is MiddleDown
If ThisEvent.EventType is EV_MtoLM
Set NextState to LeftMiddleDown
end if
If ThisEvent.EventType is EV_MtoRM
Set NextState to RightMiddleDown
end if
If ThisEvent.EventType is EV_GunUp
Set NextState to MiddleUp
end if
CurrentState is RightMiddleUp
If ThisEvent.EventType is EV_RMtoM
Set NextState to MiddleUp
end if
If ThisEvent.EventType is EV_RMtoR
Set NextState to RightUp
end if
If ThisEvent.EventType is EV_GunDown
Set NextState to RightMiddleDown
end if
If ThisEvent.EventType is EV_Fire
Post EV_2R to LED Board Lighting SM with Param 2
end if
CurrentState is RightMiddleDown
If ThisEvent.EventType is EV_RMtoM
Set NextState to MiddleDown
end if
If ThisEvent.EventType is EV_RMtoR
Set NextState to RightDown
end if
If ThisEvent.EventType is EV_GunUp
Set NextState to RightMiddleUp
end if
If ThisEvent.EventType is EV_Fire
Post EV_3R to LED Board Lighting SM with Param 3
end if
CurrentState is RightUp
If ThisEvent.EventType is EV_RtoRM
Set NextState to RightMiddleUp
end if
If ThisEvent.EventType is EV_GunDown
Set NextState to RightDown
end if
If ThisEvent.EventType is EV_Fire
Post EV_4R to LED Board Lighting SM with Param 4
end if
CurrentState is RightDown
If ThisEvent.EventType is EV_RtoRM
Set NextState to RightMiddleDown
end if
If ThisEvent.EventType is EV_GunUp
Set NextState to RightUp
end if
If ThisEvent.EventType is EV_Fire
Post EV_1R to LED Board Lighting SM with Param 1
end if
Set CurrentState to NextState
ReturnEvent
End of RunGunFiringSM
start vibrations
break;
}
else{
break;
}
If Current State is Shot2 :
if ( LED is not lit)){
Call Function LightIt
Set SecondShot to Event Parameter
if(FirstShot == SecondShot){
end vibrations
if( All LEDs lit ){
PostToFearJoy EV_EndGame
}
else{
Set CurrentState to Shot1
}
}
else{
Set SecondLit to EventType
Start timer 1 for 1 second
Set NextState to WaitTimer
end vibrations
}
}
break;
If Current State is WaitTimer :
if(EventType is ES_TIMEOUT EventParam is 1){
If FirstLit is EV_1L :
If SecondLit is EV_2L :
Turn off 1L and 2L
break;
If SecondLit is EV_2R :
Turn off 1L and 2R
break;
If SecondLit is EV_3L :
Turn off 1L and 3L
break;
If SecondLit is EV_3R :
Turn off 1L and 3R
break;
If SecondLit is EV_4L :
Turn off 1L and 4L
break;
If SecondLit is EV_4R :
Turn off 1L and 4R
break;
}
break;
If FirstLit is EV_1R :
If SecondLit is EV_2L :
Turn off 1R and 2L
break;
If SecondLit is EV_2R :
Turn off 1R and 2R
break;
If SecondLit is EV_3L :
Turn off 1R and 3L
break;
If SecondLit is EV_3R :
Turn off 1R and 3R
break;
If SecondLit is EV_4L :
Turn off 1R and 4L
break;
If SecondLit is EV_4R :
Turn off 1R and 4R
break;
}
break;
If FirstLit is EV_2L :
If SecondLit is EV_1L :
Turn off 1L and 2L
break;
If SecondLit is EV_1R :
Turn off 1R and 2L
break;
If SecondLit is EV_3L :
Turn off 2L and 3L
break;
If SecondLit is EV_3R :
Turn off 2L and 3R
break;
If SecondLit is EV_4L :
Turn off 2L and 4L
break;
If SecondLit is EV_4R :
Turn off 2L and 4R
break;
}
break;
If FirstLit is EV_2R :
If SecondLit is EV_1L :
Turn off 2R and 1L
break;
If SecondLit is EV_1R :
Turn off 2R and 1R
break;
If SecondLit is EV_3L :
Turn off 2R and 3L
break;
If SecondLit is EV_3R :
Turn off 2R and 3R
break;
If SecondLit is EV_4L :
Turn off 2R and 4L
break;
If SecondLit is EV_4R :
Turn off 2R and 4R
break;
}
break;
If FirstLit is EV_3L :
If SecondLit is EV_1L :
Turn off 3L and 1L
break;
If SecondLit is EV_1R :
Turn off 3L and 1R
break;
If SecondLit is EV_2L :
Turn off 3L and 2L
break;
If SecondLit is EV_2R :
Turn off 3L and 2R
break;
If SecondLit is EV_4L :
Turn off 3L and 4L
break;
If SecondLit is EV_4R :
Turn off 3L and 4R
break;
}
break;
If FirstLit is EV_3R :
If SecondLit is EV_1L :
Turn off 3R and 1L
break;
If SecondLit is EV_1R :
If
If
If
If
}
break;
If FirstLit is EV_4L :
If SecondLit is EV_1L :
Turn off 4L and 1L
break;
If SecondLit is EV_1R :
Turn off 4L and 1R
break;
If SecondLit is EV_2L :
Turn off 4L and 2L
break;
If SecondLit is EV_2R :
Turn off 4L and 2R
break;
If SecondLit is EV_3L :
Turn off 4L and 3L
break;
If SecondLit is EV_3R :
Turn off 4L and 3R
break;
}
break;
If FirstLit is EV_4R :
If SecondLit is EV_1L :
Turn off 4R and 1L
break;
If SecondLit is EV_1R :
Turn off 4R and 1R
break;
If SecondLit is EV_2L :
Turn off 4R and 2L
break;
If SecondLit is EV_2R :
Turn off 4R and 2R
break;
If SecondLit is EV_3L :
Turn off 4R and 3L
break;
If SecondLit is EV_3R :
Turn off 4R and 3R
break;
}
break;
}
Set NextState to Shot1;
}
break;
}
Set CurrentState to NextState
return ReturnValue
}
void LightIt ( ES_Event ThisEvent ){
Takes an ES_Event as a parameter, returns nothing
The EventType field of ThisEvent will be one of: EV_1L, EV_1R,
EV_2L, EV_2R, EV_3L, EV_3R, EV_4L, EV_4R
If Event Type
Light 1L
break;
If Event Type
Light 1R
break;
If Event Type
Light 1L
break;
If Event Type
Light 2L
break;
If Event Type
Light 3L
break;
If Event Type
Light 3R
break;
If Event Type
Light 4L
break;
is EV_1L :
is EV_1R :
is EV_2L :
is EV_2R :
is EV_3L :
is EV_3R :
is EV_4L :
break;
}
else{
break;
}
If Event Type is EV_3R :
if ( EV_3R on LightLED is 1 ){
set ReturnValue to true
break;
}
else{
break;
}
If Event Type is EV_4L :
if ( EV_4L on LightLED is 1 ){
set ReturnValue to true
break;
}
else{
break;
}
If Event Type is EV_4R :
if ( EV_4R on LightLED is 1 ){
set ReturnValue to true
break;
}
else{
break;
}
return ReturnValue;
}
}