You are on page 1of 22

Pseudo-code for the GunY Module (a service that implements a

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

Set Last InputState to CurrentInputState


Return ReturnVal
End of CheckLeftHall
ES_Event RunGunYSM (ES_Event ThisEvent) (implements a 2-state
state machine)
The EventType field of ThisEvent will be one of:
LeftHallRisingEdge, LeftHallFallingEdge, RightHallRisingEdge,
RightHallFallingEdge
Returns ES_NO_EVENT
Local variables: Next State
Set NextState to CurrentState
Based on the state of the CurrentState variable choose one of the
following blocks of code:
CurrentState is GunDown
If ThisEvent.EventType is LeftHallRisingEdge
Set NextState to GunUp
Post GunUp to GunFiringSM
Actuate gun tilt up
CurrentState is GunUp
If ThisEvent.EventType is RightHallRisingEdge
Set NextState to GunDown
Post GunDown to GunFiringSM
Actuate gun tilt down
Set CurrentState to NextState
End of RunGunYSM

Pseudo-code for the GunX Module (a service that implements a


state machine)
Module variables: GunXState_t CurrentState; uint8_t MyPriority
States: Left, LeftMiddle, Middle, RightMiddle, Right
Events: ES_TIMEOUT
bool InitGunX (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 x-position events.
(one port for potentiometer input)
Initialize PWM 1 to frequency of 50 Hz and Duty of 7.5%
Initialize analog port line 9
Set CurrentState to Middle
Initialize and start TIMEOUT timer for 20ms
End of InitGunX
ES_Event RunGunXSM (ES_Event ThisEvent) (implements a 5-state
state machine)
The EventType field of ThisEvent will be : ES_TIMEOUT
Returns ES_NO_EVENT
Local variables: Next State
Set NextState to CurrentState
Based on the state of the CurrentState variable choose one of the
following blocks of code:
CurrentState is Left
If ThisEvent.EventType is ES_TIMOUT and EventParam is
GunX timer
Set CurrentInputState to read input pin
If CurrentInputState is greater than voltage
limit of Left State
Set NextState to LeftMiddle
Post LtoLM to GunFiringSM
CurrentState is LeftMiddle
If ThisEvent.EventType is ES_TIMOUT and EventParam is
GunX timer
Set CurrentInputState to read input pin
If CurrentInputState is greater than voltage
limit of LeftMiddle State
Set NextState to Middle
Post LMtoM to GunFiringSM

If CurrentInputState is less than voltage limit


of LeftMiddle State
Set NextState to Left
Post LMtoL to GunFiringSM
CurrentState is Middle
If ThisEvent.EventType is ES_TIMOUT and EventParam is
GunX timer
Set CurrentInputState to read input pin
If CurrentInputState is greater than voltage
limit of Middle State
Set NextState to RightMiddle
Post MtoRM to GunFiringSM
If CurrentInputState is less than voltage limit
of Middle State
Set NextState to LeftMiddle
Post MtoLM to GunFiringSM
CurrentState is RightMiddle
If ThisEvent.EventType is ES_TIMOUT and EventParam is
GunX timer
Set CurrentInputState to read input pin
If CurrentInputState is greater than voltage
limit of Left State
Set NextState to Right
Post RMtoR to GunFiringSM
If CurrentInputState is less than voltage limit
of Left State
Set NextState to Middle
Post RMtoM to GunFiringSM
CurrentState is Right
If ThisEvent.EventType is ES_TIMOUT and EventParam is
GunX timer
Set CurrentInputState to read input pin
If CurrentInputState is less than voltage limit
of Left State
Set NextState to RightMiddle
Post RtoRM to GunFiringSM
Set Servo-Input so that direction moves in accordance with
potentiometer
If ServoInput is too high
Set to max
End If
If ServoInput is too low
Set to min
End If
Actuate gun x-position movement
Set CurrentState to NextState

Reset Timer
End of RunGunTiltSM
Pseudo-code for the Gun Button Module
a state machine)

(a service that implements

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

Else if CurrentState is Ready2Sample


If EventType is ButtonPushed
Start Debounce_Timer for 50ms
Set CurrentState to Debouncing
Post EV_FireGun to GunFiringSM
Post EV_StartGame to FearJoySM
end if
If EventType is ButtonUp
Start Debounce_Timer for 50ms
Set CurrentState to Debouncing
end if
end else
Return ES_NOEVENT
End of Function

Pseudo-code for the Fear and Joy Module


implements a state machine)

(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
}

Pseudo-code for the Gun Firing Module (a service that implements


a state machine)
Module variables:
GunFireState_t CurrentState; uint8_t MyPriority
bool InitGunFire (uint8_t Priority)
Takes a priority number , returns true.
Initialize the MyPriority variable with the passed in parameter.
Set CurrentState to MiddleDown
End of Function
ES_Event RunGunFireSM (ES_Event ThisEvent) (implements an 8-state
state machine)
State:LeftUp, LeftDown, LeftMiddleUp, LeftMiddleDown,MiddleUp,
MiddleDown, RightMiddleUp, RightMiddleDown, RightUp, RightDown
The EventType field of ThisEvent will be one of: EV_Fire,
EV_LtoLM, EV_LMtoL, EV_LMtoM, EV_MtoLM, EV_MtoRM, EV_RMtoM,
EV_RMtoR, EV_RtoRM, EV_GunUp, EV_GunDown
1L

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

Pseudo-code for the LED Board Lighting Module


Module variables:
LED_t CurrentState, uint8_t FirstShot, uint8_t SecondShot,
ES_Event FirstLit, ES_Event SecondLit
Modular Functions:
ES_Event LEDSM (ES_Event ThisEvent);
void LightIt ( ES_Event ThisEvent );
bool IsLEDLit( ES_Event ThisEvent );
void LightLEDs (void);
bool InitializeLED ( uint8_t Priority )
Enable Output Ports
Set CurrentState to Game Finished
Turn off all LEDs
EndFunction
ES_Event LEDSM( ES_Event ThisEvent ){
Takes an event, returns ES_NOEVENT if successful
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
Static CurrentState of SM will be one of:
Shot1, Shot2, WaitTimer
Local Variables:
LED_t NextState, ES_Event ReturnValue
Set NextState to CurrentState
Set ReturnValue to ES_NOEVENT
If Current State is GameFinished:
if (Event is Start Game)
NextState is GameTime
end if
break;
If Current State is GameTime
On Any Event:
Set Next State to Shot 1
break;
If Current State is Shot1 :
if ( LED is not lit){
Call Function LightIt
Set FirstLit to Event Type
Set FirstShot to Event Parameter
set NextState to Shot2

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

Turn off 3R and 1R


break;
SecondLit is EV_2L :
Turn off 3R and 2L
break;
SecondLit is EV_2R :
Turn off 3R and 2R
break;
SecondLit is EV_4L :
Turn off 3R and 4L
break;
SecondLit is EV_4R :
Turn off 3R and 4R
break;

}
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 :

If Event Type is EV_4R :


Light 4R
break;
}
}
bool IsLEDLit( ES_Event ThisEvent ){
Takes an event, returns true if LED is on, false if LED is off
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
bool ReturnValue = false;
If Event Type is EV_1L :
if ( EV_1L on LightLED is 1 ){
set ReturnValue to true
break;
}
else{
break;
}
If Event Type is EV_1R :
if ( EV_1R on LightLED is 1 ){
set ReturnValue to true
break;
}
else{
break;
}
If Event Type is EV_2L :
if ( EV_2L on LightLED is 1 ){
set ReturnValue to true
break;
}
else{
break;
}
If Event Type is EV_2R :
if ( EV_2R on LightLED is 1 ){
set ReturnValue to true
break;
}
else{
break;
}
If Event Type is EV_3L :
if ( EV_3L on LightLED is 1 ){
set ReturnValue to true

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;
}
}

Pseudo-code for Robot timer:


Module variables: unit16_t static TimeLED= 0x0000
PINused :PortF pin 2,3,4
Bool InitRobotTimer (uint8_t Priority)
Takes a priority number , returns true.
If ThisEvent.EventType = ES_GameStart
Initialize the MyPriority variable with the passed in parameter.
Set EventType = ES_Init
Call InitLED()
End if

ES_Event RunRobotTimer( ES_Event ThisEvent )


If ThisEvent is ES_Init
Start LED_TIMER -- use 3750ms
Set Robot_Timer --- use15000ms
End if
If ThisEvent is ES_Timeout and EventParam is LED_Timer
Set Most Significant 0 in TimeLED to 1
Call Lightup(TimeLED) Function
Start LED_TIMER -- use 3750ms
If ThisEvent is ES_Timeout and EventParam is Robot_Timer
Call RobotVibration() Function
Start Robot_TIMER ---use 15000ms
End if
End if
void Lightup_LEDtimer(TimeLED)

For (i = 0-15, increasing i each time)


if ((TimeLED left shifted by i)&(0x8000) == 0)
Post 0 to Port F Pin 4end if
else
Post 1 to Port F Pin 4
end else
PulseSRCLK()
end For
Pulse RCLK()
End Function
void InitLED (void)
Initialize PortFpin2 to be used SRCLK pulse input
Initialize PortFpin3 to be use as RCLK pulse input
Initialize PortFpin4 to be use as dataInput for shift register
void PulseSRCLK (void)
Set PortF_Pin2 = 1
Set PortF_Pin2 = 0
End Function
void PulseRCLK(void)
Set PortF_pin3=1
Set PortF_pin3 =0
End of Function

You might also like