Sie sind auf Seite 1von 3

/*

CONTROL_STATE_MACHINE

This state machine controls the game at a higher level, like start, end, quit.
The game dynamics take place in the InGame state machine

States:
PseudoState
Welcoming
Setup
ReadingDifficulty
Resetting
InGame
Ending

*/

Include Dependent Files


Function define module functions
Define hardware for the coin checkers for Flag Up, Flag Down, Mario Start, Mario
End
Define hardware for the StartButton Press pin
Define hardware for the StartButton LED

Specify Module Variables


MyPriority - holds the priority for this SM
CurrentState - holds current state of this State Machine
Difficulty - parameter to hold the selected difficulty
AnalogState[4] - an array to hold the analog state
LastAnalogState - holds the last analog state
Last_FlagUp_PinState
Last_FlagDown_PinState
Last_MarioStart_PinState
Last_MarioEnd_PinState
Last_StartButton_State;

InitControlSM

Parameters
the priorty of this service

Returns
bool, false if error in initialization, true otherwise

Description
Saves away the priority, and does any other required initialization

Function

Initialize the MyPriority module variable with the passed priority


Initialize the hardware pins:
Initialize position sensor pins as digital inputs
Initialize start button pin as digital input
Initialize start button led sensor pin as digital output
Set the last pin states for the check hardwares in Last_*_PinState module
variables

Initialize the shift register controlling the hill LEDs and motor movements
using the static
function for the same

Initialize the PWM outputs according to the pin diagram. PB6 is used for the
brightness of the analog input, PB7 is used for the Servomotor, and PB4 will be
PWM for Mario control motor
Set PWM Output Frequency for group 0, setting frequency to 50 HZ for group 0 and
initial position
Set PWM Output Frequency for group 1, setting frequency to 200 HZ for group 1
and guarantying 0 HZ

Initializizing only 1 Analog Input Channel (PE0)


Read first analog input value and assign it to the LastAnalogState

Set current state of this state machine as pseudo state

Post ES_INIT to this state machine. Return true if successful, false in case of
any error.

PostControlSM

Parameters
ES_Event_t ThisEvent ,the event to post to the queue

Returns
bool false if the Enqueue operation failed, true otherwise

Description
Posts an event to this state machine's queue

Function:

Call ES_PostToService passing the priority through MyPriority and the assigned
event

RunControlSM

Parameters
ES_Event_t : the event from its service queue to process in this State
Machine

Returns
ES_NO_EVENT if no error ES_ERROR otherwise

Function

Declare a ReturnEvent and initialise it as ES_NO_EVENT. This would be returned


in case of no error.

Switch CurrentState
If PseudoState_Control:
If event ES_INIT:
Do a visual blast
Switch on the Start button LED
Make current state - Welcoming

If Welcoming:
If event is LEAF_ACTIVATED:
Start the inactive timer
Stop the visual blast of welcoming mode
Switch on the start button LED
Start pulsing difficulty LED by taking the first analog read

Make current state - Setup


If Setup:
If event is LEAF_RESET:
Post reset event
Make current state - Resetting
If event is ES_TIMEOUT (Inactive timer):
Post inactive reset event
Make current state - Resetting
If event is START_BUTTON_PRESSED:
Restart inactive timer
Switch off pulsing of difficulty led
Post START_GAME event with parameter set to the chosen
difficulty to the GAME state machine
Start moving the Mario by posting MOVE_MARIO event to
the MARIOMOVEMENT State machine
Play the MARIO game start sound by posting to the
Sound State machine with appropriate selection
Switch off the start button LED
Make current state as InGame
If event is DIFFICULTY_CHANGE:
Restart the Inactivity timer
Assign the read difficulty to the difficulty variable
Make current state as ReadingDifficulty

If ReadingDifficulty:
If event is ANALOG_READ_COMPLETE:
Pulse difficulty LED according to the chosen difficulty
Make current state as Setup

If InGame:
If event is LEAF_RESET:
Post reset event
Make current state - Resetting
If event is ES_TIMEOUT (Inactive timer):
Post inactive reset event
Make current state - Resetting
If event is MARIO_END:
Stop the Mario
Start the flag to move up
Play the game end sound according to the score achieved
Stamp the LEAF
Make current state — Ending

If Resetting:
If event is MARIO_START_AND_FLAG_DOWN
Start the welcoming visual blast
Make current state as Welcoming

If Ending:
If the FLAG is up:
Stop the FLAG motor
Move Mario to the start
Make current state as MovingMarioBack

If MovingMarioBack:
If event is MARIO_START (Mario reached start):
Post reset
Switch on leaf led
Make current state as Resetting

return ReturnEvent

Das könnte Ihnen auch gefallen