Sei sulla pagina 1di 16

CardReaderSM

Purpose: Track the Position of The "Special" Card to understand when to start and stop the game Module Variables: - CurrentState of State Machine - Counter that keeps track of number of rising and falling edges Function: InitMorseStartFSM - Current State is WaitForTimeOut2 - Start a Timer That Prevents The Coin Sensor To Initial Noise in Circuit Function: RunCardReaderSM switch action based on statemachine state if state is WaitForTimeOUT2 if input event is the ES_TIMEOUT - Bring current state to WaitForFallInsert elseif state is WaitRiseInsert if event is rising edge increase counter bring next state to wait for insert falling edge if the counter has reached full insert bring current state to GamePlay declare start of game to both C32s set counter to 0 elseif state is WaitFall Insert if event is fallingedge increase counter next state is wait for rising edge elseif state is GamePlay if event is fallingedge increase counter set state to WaitRiseRemove if event is game End clear counter next state is WaitRiseRemove elseif state is WaitRiseRemove if event is RisingEdge increase counter bring next state to wait for removing falling edge if the counter has reached full removal

bring current state to WaitFallInsert declare end of game to both C32s set counter to 0 elseif state is WaitFallRemove if event is FallingEdge increase counter next state to WaitRiseRemove

Stage1SM
Function: InitializationStage1SM Takes a priority number, returns true. Initialize the MyPriority variable with the passed in parameter. Set CurrentState to Stage1Waiting. Initialize the port line to monitor the hall sensor. Set side1 and side 2 to false. Set otherPlayerComplete to 0. Set numberHits to 0. Post Event ES_Init to Stage1SM queue. End of InitializationStage1SM. Function: RunStage1SM The EventType field of ThisEvent is one of: ES_START_STAGE1, ES_SIDE1_TOUCHED, ES_SIDE2_TOUCHED, ES_TOUCH_RING, ES_COM_STAGE_1_COMPLETE, ES_GAME_END. Returns ES_NO_EVENT If ThisEvent is ES_GAME_END Set CurrentState to Stage1Waiting. Set otherPlayerComplete to 0. return TeturnEvent. End if Based on the state of the CurrentState variable choose one of the following blocks of code: CurrentState is Stage1Waiting If ThisEvent is ES_START_STAGE1 Post event ES_START_STAGE1 to TimeDisplaySM. Set CurrentState to Stage1Play. Post event ES_START_STAGE1 with parameter 1 to LogisticsSM. End if CurrentState is Stage1Play Based on the EventType of ThisEvent choose one of the following blocks of code:

ThisEvent.EventType is ES_SIDE1_TOUCHED If side1 is false Post event ES_HALL_EFFECT_LED to LogisticsSM. Set side 1 to be true. End if If side 1 and side 2 is true If otherPlayerComplete is 1 Call function callStateTwo. Else Set CurrentState to be Stage1WaitForOtherPlayer. Post event ES_WAITING_LED to LogisticsSM. Post event ES_SEND_REQUEST with parameter 1 to ComFSM. End else End if

ThisEvent.EventType is ES_SIDE2_TOUCHED If side2 is false Post event ES_HALL_EFFECT_LED to LogisticsSM. Set side 2 to be true. End if If side 1 and side 2 is true If otherPlayerComplete is 1 Call function callStateTwo. Else Set CurrentState to be Stage1WaitForOtherPlayer. Post event ES_WAITING_LED to LogisticsSM. Post event ES_SEND_REQUEST with parameter 1 to ComFSM. End else End if

ThisEvent.EventType is ES_TOUCH_RING Add 1 to numberHits. If numberHits mod 4 is zero Post event ES_DECREASE_LIFE to LogisticsSM. End if

CurrentState is Stage1WaitForOtherPlayer If ThisEvent is ES_COM_STAGE_1_COMPLETE Call function callStateTwo. End if

If ThisEvent is ES_COM_STAGE_1_COMPLETE Set otherPlayerComplete to 1 End if return ReturnEvent End of RunStage1SM.

Function: callStateTwo Post event ES_START_STAGE2 with parameter 2 to Stage2SM and LogisticsSM. Post event ES_SEND_REQUEST with parameter 1 to ComFSM. Set side 1 and side 2 to be false. Set CurrentState to Stage1Done. Set otherPlayerComplete to 0. End of callStateTwo.

Stage2SM
Function: InitializationStage2SM Takes a priority number, returns true. Initialize the MyPriority variable with the passed in parameter. Set CurrentState to Stage2Waiting. Set otherPlayerComplete to 0. Post Event ES_Init to Stage2SM queue. End of InitializationStage2SM.

Function: RunStage1SM The EventType field of ThisEvent is one of: ES_START_STAGE2, ES_TIMEOUT, ES_BUTTON_1_PRESSED, ES_BUTTON_2_PRESSED, ES_BUTTON_3_PRESSED, ES_BUTTON_4_PRESSED, ES_BUTTON_5_PRESSED, ES_GAME_END, ES_COM_STAGE_2_COMPLETE. Returns ES_NO_EVENT If ThisEvent is ES_GAME_END Set CurrentState to Stage2Waiting. Set otherPlayerComplete to 0. Set integer i to be 0, i is a module level variable, which indicates the number of elements in the MArray (Monster array) return TeturnEvent. End if Based on the state of the CurrentState variable choose one of the following blocks of code:

CurrentState is Stage2Waiting If ThisEvent is ES_START_STAGE2 Set CurrentState to WaitForButtonPress. Post event ES_MONSTER_LED with parameter MArray[i] to LogisticsSM. Start timer 2 End if CurrentState is WaitForButtonPress If ThisEvent is ES_TIMEOUT Call function Wrong. else If ThisEvent is ES_BUTTON_1_PRESSED If MArray[i] is 1 Call function Right Else if i>1 && MArray[i-1] ==1 do nothing else Call function Wrong. End if Else if ThisEvent is ES_BUTTON_2_PRESSED If MArray[i] is 2 Call function Right Else if i>1 && MArray[i-1] ==2 do nothing else Call function Wrong. End if Else if ThisEvent is ES_BUTTON_3_PRESSED If MArray[i] is 3 Call function Right Else if i>1 && MArray[i-1] ==3 do nothing else Call function Wrong. End if Else if ThisEvent is ES_BUTTON_4_PRESSED If MArray[i] is 4 Call function Right Else if i>1 && MArray[i-1] ==4 do nothing else Call function Wrong. End if Else if ThisEvent is ES_BUTTON_5_PRESSED

If MArray[i] is 5 Call function Right Else if i>1 && MArray[i-1] ==5 do nothing else Call function Wrong. End if End if End if CurrentState is Stage2WaitForOtherPlayer If ThisEvent is ES_COM_STAGE_2_COMPLETE Call function postEndOfStage2(). End if If ThisEvent is ES_COM_STAGE_2_COMPLETE Set otherPlayerComplete to 1 End if return ReturnEvent End of RunStage1SM. Function: Right If i is 4 If otherPlayerComplete is 1 Call function postEndOfStage2. Else Set CurrentState to Stage2WaitForOtherPlayer. Post event ES_WAITING_LED to LogisticsSM. Post event ES_SEND_REQUEST with parameter 2 to ComFSM. Post event ES_MONSTER_LED with parameter 10 to LogisticsSM. End if Else Restart timer 2. Post event ES_MONSTER_LED with parameter MArray[i+1] to LogisticsSM. Add 1 to integer i. End if End of Right.

Function: Wrong Restart timer 2. Post event ES_DECREASE_LIFE to LogisticsSM. Post event ES_BUZZ to LogisticsSM. Post event ES_SEND_REQUEST with parameter 5 to ComFSM. End of Wrong.

Function: postEndOfStage2 Post event ES_START_STAGE3 with parameter 3 to Stage3FSM and LogisticsSM. Post event ES_MONSTER_LED with parameter 10 to Stage3FSM and LogisticsSM. Post event ES_SEND_REQUEST with parameter 2 to ComFSM. Set CurrentState to Stage2Over. Set otherPlayerComplete to 0. Set i to be 0. End of postEndOfStage2.

Module: Stage3SM
Purpose: Controls the Stage Machine and Functionality Related to the Safe Module Variables: OtherPlayerComplete binary value as to whether the other player completed AnalogValues[3] is an array of ints for the safe "combo" CurrentState is place in state machine Function InitStage3FSM Set OtherPlayerComplete to 0; Current state is Stage3Waiting Read Value of Analog Pin For Find Starting Value of Safe If start value is low Set AnalogValues to high low high else Set AnalogValues to low high low Function Check4LockVoltage Read Analog Value if CurrentState is WaitForFirstTurn if starting was low if voltage is greater than analogValue[0] Post Event Turn Reached else if voltage is less than analogValue[0] end else if CurrentState is WaitForSecondTurn if starting was low if voltage is less than analogValue[1] Post Event Turn Reached else if voltage is greater than analogValue[1] end else if CurrentState is WaitForThirdTurn if starting was low if voltage is greater than analogValue[2]

Post Event Turn Reached else if voltage is less than analogValue[2] end end Function RunStage3FSM if Event is Game End Set State to Stage3Waiting Set OtherPlayerComplete to 0 switch Current State case Stage3Waiting if Event is Stage Stage3 Current state is WaitForFirstTurn Post Light Event to turn on First Light case WaitForFirstTurn if Event is Turn Reached Current state is WaitForSecondTurn Post Light Event to turn on Second Light case WaitForSecondTurn if Event is Turn Reached Current state is WaitForThirdTurn Post Light Event to turn on Third Light case WaitForThirdTurn if Event is Turn Reached Post Light Event to turn on Fourth Light if OtherPlayer is complete Call Stage3Complete Current State is Stage3Completed else Turn on Player WaitingLight CurrentState is Stage3WaitForOtherPlayer Tell Other C32 That Player is Complete end end case Stage3WaitForOtherPlayer if Event is ES_COM_STAGE_3_COMPLETE Current State is Stage3Completed Call Stage3Complete } if Event is ES_COM_STAGE_3_COMPLETE otherPlayerComplete is 1; end Function Stage3Complete Post to Reward ES_WON Post to other C32 that the game has been won otherplayercomplete equals zero

Reward
Purpose: When End of Game Is Detected, Move Servo Motor To Release Candy Reward Function: InitReward - Set Servo To A Position Blocking Candy Reward Function: RunReward - Input Parameter: ES_Event Declaring Event to Respond to if event is that the game is won - Put Servo To Position Releasing Candy - Start Timer endif if event is that the timer has timed out - Put Servo In Off Position - Post to All Services and Through Communication that Game is Over endif

TimeDisplay
Purpose: Keep Track Of Game Time To Set Visual Display And Notify other Functions Of Time Expiration Function: InitTimeDisplaySM - Set Servo to Starting Position - Set The Timer to Control Movement - Initialize the Timestep of the Motor Function: RunTimeDisplaySM Input Parameters: ES_Event Passed Into Simple Service if ES_Event is the start of Stage One - start the first timing cycle end if if ES_Event is a time out of the timer - Restart the timer - Shift the servo position by the pulse width if the servo position is past the end point declare game end within module and in communication endif endif if ES_Event is a penalty increase the size of the motor movement per time step end if

if ES_Event is the end of the game reset the motor movement width bring motor back to starting position stop timer endif

Debounce1SM and Debounce2SM


Note: Two debounce state machines are very similar, except that we use timer 5 for Debounce1SM and timer 6 for Debounce2SM. We use Debounce1SM for RingTouch and Debounce2SM for Monster buttons. We only show the pseudo code of Debounce1SM. Function: InitializeDebounce1SM Takes a priority number, returns True. Initialize the MyPriority variable with the passed in parameter. Set CurrentState to be DEBOUNCING 1. Start timer 5. Post Event ES_Init to Debounce1SM queue. End of InitializeDebounce1SM.

Function: RunDebounce1SM The EventType field of ThisEvent will be one of: ES_TIMEOUT or START_DEBOUNCE If EventType is ES_TIMEOUT & parameter is debounce timer number Set CurrentState to READY2SAMPLE1 Else if EventType is START_DEBOUNCE Start debounce timer 5 Set CurrentState to DEBOUNCING 1 End if Return ES_NO_EVENT End of RunDebounce1SM

EventCheckers
Function: checkCardInsert Takes no parameters, returns true if an event posted. Local returnVal = false. Set lastInputState to be 0. Read from PORTE & TAPE_SENSOR_PORT and save it to currentInputState. If currentInputState is different from lastInputState If currentInputState is the same as TAPE_SENSOR_PORT_HI Post event ES_RISING_EDGE to CardReaderSM

10

Else Post event ES_FALLING_EDGE to CardReaderSM End if End if Set lastInputState to be currentInputState. Return returnVal. End of checkCardInsert Function: checkHallSensors Takes no parameters, returns true if an event posted. Local returnVal = false. Read from PTT&HALL_1_PORT and save it to currentHall1State. Read from PTT&HALL_2_PORT and save it to currentHall2State. If currentHall1State is not 0 Post event ES_SIDE1_TOUCHED to Stage1SM. Else if currentHall2State is not 0 Post event ES_SIDE2_TOUCHED to Stage2SM. End if Return returnVal. End of checkHallSensors Function: checkRingTouch Takes no parameters, returns true if an event posted. Local returnVal = false. Set lastRingState to be 0. If CurrentState of Debounce1SM is READY2SAMPLE Post event START_DEBOUNCE to Debounce1SM. Read from PTT&HALL_1_PORT and save it to currentHall1State. Read from PTT&HALL_2_PORT and save it to currentHall2State. Read from PTIAD&Ring_PORT and save it to currentRingState. If currentRingState is different from lastRingState and currentRingState is the same as RING_PORT_HI If currentHall1State is the same as not HALL_1_PORT_HI and currentHall2State is the same as not HALL_2_PORT_HI Post event ES_TOUCH_RING to Stage1SM. End if End if Set lastRingState to be currentRingState. Return returnVal. End of checkRingTouch Function: checkMonsterButtons Takes no parameters, returns true if an event posted. Local returnVal = false. Set lastButton1State, lastButton2State, lastButton3State, lastButton4State, lastButton5State to be 0.

11

If CurrentState of Debounce2SM is READY2SAMPLE Post event START_DEBOUNCE to Debounce2SM. Read from PTIAD&BUTTON_1_PORT and save it to currentButton1State. Read from PTIAD&BUTTON_2_PORT and save it to currentButton2State. Read from PTIAD&BUTTON_3_PORT and save it to currentButton3State. Read from PTIAD&BUTTON_4_PORT and save it to currentButton4State. Read from PTIAD&BUTTON_5_PORT and save it to currentButton5State. If currentButton1State is not the same as last Button1State and currentButton1State is the same as BUTTON_1_PORT_HI Post event ES_BUTTON_1_PRESSED to Stage2SM. Else if currentButton2State is not the same as last Button2State and currentButton2State is the same as BUTTON_2_PORT_HI Post event ES_BUTTON_2_PRESSED to Stage2SM. Else if currentButton3State is not the same as last Button3State and currentButton3State is the same as BUTTON_3_PORT_HI Post event ES_BUTTON_3_PRESSED to Stage2SM. Else if currentButton4State is not the same as last Button4State and currentButton4State is the same as BUTTON_4_PORT_HI Post event ES_BUTTON_4_PRESSED to Stage2SM. Else if currentButton5State is not the same as last Button5State and currentButton5State is the same as BUTTON_5_PORT_HI Post event ES_BUTTON_5_PRESSED to Stage2SM. End if End if Set lastButton1State to be currentButton1State. Set lastButton2State to be currentButton2State. Set lastButton3State to be currentButton3State. Set lastButton4State to be currentButton4State. Set lastButton5State to be currentButton5State. Return returnVal End of checkMonsterButtons

Logistics
Purpose: Control the LED, Health Status, and Buzzer Support Functions of The Game ModuleVariables:

12

- CurrentHealth keeps track off player health - bitsArray that holds binary values to write to the shift registers

Function: InitLogisticsSM - Set T0-T3 as output pins to control the shift register - Set M0 as output to control the buzzer - Initialize All Output Pins Low at First - Call ResetLED function Function: RunLogisticsSM switch based on input event type case ES_TIMEOUT TurnBuzzerOff case ES_BUZZ TurnBuzzerON case ES_WAITINGLED Turn On Waiting LED case ES_DECREASE_LIFE Call HealthLED with currenthealth value Turn On Buzzer switch based on currentHealth if health is low Speed Up Timer With Penalty if health is zero Reset Game On Both C32s case ES_MONSTER_LED Set BitsArray to One Passed On Monster Value and all other values zero writeArray to shift register case ES_START_STAGE1 InitializeCurrent health to maxhealth Turn on HealthLEDs Turn on Stage One LED Turn off WaitingLED writeArray case ES_START_STAGE2 / ES_START_STAGE3 Turn on Stage 2/3 LED writeArray case ES_HALL_EFFECT_LED Turn on LED corresponding to Hall Effect Sensor in pass parameter case ES_SAFE_GUIDE_LED Turn on LED corresponding to Level that the safe has been opened case ES_INSERT_CARD_LED Turn on LED telling player to insert card to start game case ES_REWARD_LED Turn on LED corresponding to Reward being given case ES_GAME_END ResetGameLED

13

end switch Function ResetGameLED Clear Array Post Event to Turn on the Insert_CARD_LED

Function ClearArray Set all Values in BitsArray to 0

Function WriteArray For each position in BitArray set the shiftregister input pin to high/low based on bitarray value pulse the shift clock end pulse register clock Function HealthLED For each bit position corresponding to a health LED if that bit position is greater than health value set bitArray index to zero else set bitarray index to one end Function Buzz if passing parameter is to turn on Set Buzzer Output Pin High Start Timer if passing parameter is to turn off Set output pin low

Communication
Purpose: Enables Communication Between C32 Devices and Creates Framework for them to send messages Module Definitions: IdleAll: set all comm ports to inputs StrobeHI, StrobeLo, AckHi, AckLo are used to remove confusion over bit numbers Module Variables: StrobeVal variable to understand is strobe was most recently high or low DeferralQue to enable the module to defer events CurrentState of State Machine Function InitComFSM Set all comm ports to inputs Set Strobe value to 0

14

Set CurrentState = WaitForTimeOut; Start timer to prevent communication from circuit startup noise Function Check4Com If StrobeVal is 0 and the Strobe Pin Reads High Post ES_STROBE_RISE else if StrobeValue is 1 and the StrobePin Reads Low Post ES_STROBE_FALL else if Acknowledge is high Post ES_ACK_RISE Function RunComFSM if Event is ES_GAME_END The CurrentState is Idle All pins are set to inputs switch based on current state of the state machine case WaitForTimeout if Event is ES_TIMEOUT CurrentState is Idle case Idle if Event is ES_STROBE_RISE Read Pin Values Process Pin Values AssertAck; CurrentState is WaitForStrobeFall if Event is ES_SEND_REQUEST if Strobe Value is High DeferEvent else Set Pin Values as Appropriate outputs Set Strobe High Start Timer to Wait For Acknowledge CurrentState is WaitForAcknowledge end end case WaitForStrobeFall if Event is ES_STROBE_FALL Set all pin to Idle inputs CurrentState is Idle Recall Any Deferred Events if Event is ES_SEND_REQUEST Defer Event end case WaitForAcknowledge switch EventType case ES_TIMEOUT Current State is Idle Set Data Pins to Idle

15

Post Previous Event to State Machine Again case ES_ACK_RISE Current State is Idle Set Data Pins to Idle Recall any deferred events case ES_SEND_REQUEST Defer Event for later } } Function ComEventReciept Input Parameters: three ints representing data pin binary values { Depending on Combination of Three Pin Values Post Appropriate event to appropriate function based on hash table in header file } Function ComEventDelivery Input Parameters: Event to Deliver { Depending on Parameter Value of Event, Set Data Pins high or low }

16

Potrebbero piacerti anche