Вы находитесь на странице: 1из 7

GameplayServiceSM

Data private to module: MyPriority, CurrentState,


WelcomePatternBank, VoteForUsPatternBank, index,
LastLeverState, LastPinStateD0, LastPinStateD1,
LastPinStateD2, LastPinStateD3, LastPinStateF4,
LastPinStateD7, LastPinStates, NumVoteTimeouts, HandLEDsOn

InitializeGameplayServiceSM
Takes a priority number, returns true

Initialize the MyPriority variable with the passed-in parameter


Set CurrentState to InitialGameState
Set FourBitOutputPattern to FIRST_LED_ROW
Call SR_WriteMapAllOff to clear map LEDs
Set LastButtonState_Button1,2,3,4 to 0
Set LastButtonStates to 0
Set First, Second, Third and FourthButton to 0
Set MapCounter to 0
Set NumTimeouts to 0
Set NumLEDsOn to 0
Set AnyButtonsPressed to false
Turn off button backlights

End of InitializeGameplayService

GetCurrentButtonStates
Takes no inputs, returns ReturnVal
Local variables: ReturnVal, Button2, Button1, C_bits

Initialize Button2 to the value from bit 6 in port A


Initialize Button1 to the value from bit 7 in port A
Initialize C_bits to bits 6 and 7 from port C
Set ReturnVal to C_bits, Button1, Button2
Return ReturnVal

End of GetCurrentButtonStates

CheckButtonEvents
Takes no inputs, returns ReturnValue
Local variables: ReturnValue, CurrentButtonState_Button1,
CurrentButtonState_Button2, CurrentButtonState_Button3,
CurrentButtonState_Button4, CurrentButtonStates

Initialize ReturnValue to false


Initialize uint8_t CurrentStateButtons to return value from
GetCurrentButtonStates()
Set CurrentButtonState_Button1, CurrentButtonState_Button2,
CurrentButtonState_Button3, CurrentButtonState_Button4 to
the bits in CurrentButtonStates

If CurrentButtonStates does not equal LastButtonStates


Declare ES_Event ThisEvent
Set event type to BUTTONPRESSED
If CurrentButtonState_Button1 does not equal
LastButtonState_Button1
If CurrentButtonState_Button1 is high
Set event parameter to 1
Post ThisEvent to GameplayService
Set ReturnValue to true
If CurrentButtonState_Button2 does not equal
LastButtonState_Button2
If CurrentButtonState_Button2 is high
Set event parameter to 2
Post ThisEvent to GameplayService
Set ReturnValue to true
If CurrentButtonState_Button3 does not equal
LastButtonState_Button3
If CurrentButtonState_Button3 is high
Set event parameter to 3
Post ThisEvent to GameplayService
Set ReturnValue to true
If CurrentButtonState_Button4 does not equal
LastButtonState_Button4
If CurrentButtonState_Button4 is high
Set event parameter to 4
Post ThisEvent to GameplayService
Set ReturnValue to true
Set LastButtonStates to CurrentButtonStates
Set LastButtonState_Button1 to CurrentButtonState_Button1
Set LastButtonState_Button2 to CurrentButtonState_Button2
Set LastButtonState_Button3 to CurrentButtonState_Button3
Set LastButtonState_Button4 to CurrentButtonState_Button4
Return ReturnValue

End of CheckButtonEvents

RunGameplayService
Takes ES_Event ThisEvent, returns ReturnEvent
EventType field is one of: ES_TIMEOUT, WELCOME_DONE,
FENCE_HIT, BUTTONPRESSED
Local variables: results, ReturnEvent, ThisEvent

Declare ReturnEvent
Set event type to ES_NO_EVENT
Initialize NextState to CurrentState
Switch CurrentState

If InitialGameState
If event type is WELCOME_DONE
Set NextState to ActiveState
Declare results with length 2
Write knob pin ADC count to results
Declare ES_Event ThisEvent
Set event type to SEED_RECEIVED
Set event parameter to results
Post this event to LEDServiceSM
Randomize LEDRowBank based on random
number generated with seed
Turn on button backlights
Break

If ActiveState
If event type is FENCE_HIT AND event parameter is
PLAYER_FINISH_LINE OR event parameter is
OPPONENT_FINISH_LINE OR event type is
ES_TIMEOUT AND event parameter is
GAME_TIMER
Turn off button backlights
If AnyButtonsPressed is false
Set LastButtonState_Button1 to 0
Set LastButtonState_Button2 to 0
Set LastButtonState_Button3 to 0
Set LastButtonState_Button4 to 0
Set LastButtonStates to 0
Set FirstButton to 0
Set SecondButton to 0
Set ThirdButton to 0
Set FourthButton to 0
Set ButtonPattern to 0
Set MapCounter to 0
Set NumTimeouts to 0
Set NumLEDsOn to 0
Set NextState to InitialGameState
Declare ES_Event ThisEvent
Set event type to USER_DEPARTED
Post this event to Dist. List #3
Else
Set NextState to FinishedGameState
If event type is FENCE_HIT AND event
parameter is PLAYER_FINISH_LINE
Turn on next map LED
If event type is BUTTONPRESSED
If event parameter is 1
Set FirstButton to 1
If event parameter is 2
Set SecondButton to 1
If event parameter is 3
Set ThirdButton to 1
If event parameter is 4
Set FourthButton to 1
If event type is ES_TIMEOUT AND event parameter
is ROW_TIMER
Set CurrentBStates to return value of
GetCurrentButtonStates()
Increment NumTimeouts by 1
Set ButtonPattern using FirstButton,
SecondButton, ThirdButton, FourthButton, and
bitwise operations
If ButtonPattern is not 0
Set AnyButtonsPressed to true
Set ButtonPattern to ButtonPattern OR
CurrentBStates
If ButtonPattern matches appropriate element
in LEDRowBank AND NumTimeouts has
an appropriate value
Drive player motor forward
Increment MapCounter by 1
If MapCounter is at its maximum value
AND NumLEDsOn is less than 6
Light up next map LED
Increment NumLEDsOn by 1
Set MapCounter to 0
Else
Drive buzz motors based on
ButtonPattern
Set ButtonPattern to 0
Set FirstButton to 0
Set SecondButton to 0
Set ThirdButton to 0
Set FourthButton to 0
Break

If FinishedGameState
If event type is FENCE_HIT AND event parameter is
FLAG_START_POSITION
Set LastButtonState_Button1 to 0
Set LastButtonState_Button2 to 0
Set LastButtonState_Button3 to 0
Set LastButtonState_Button4 to 0
Set LastButtonStates to 0
Set FirstButton to 0
Set SecondButton to 0
Set ThirdButton to 0
Set FourthButton to 0
Set ButtonPattern to 0
Set MapCounter to 0
Set NumTimeouts to 0
Set NumLEDsOn to 0
Set NextState to InitialGameState

End of RunGameplayService

End of GameplayService

Вам также может понравиться