Professional Documents
Culture Documents
MODULES:
ButtonFSM (Button.c)
GameFSM (Game.c)
BroomFSM (Broom.c)
CountdownFSM (Countdown.c)
TargetLED Module (TargetLED.c)
Hoop Module (Hoop.c)
Music Module (Music.c)
Event Checkers Module (EventCheckers.c)
General Base Module (GeneralBase.c)
STATES:
In Button.c:
Debouncing
Ready2Sample
In Game.c:
TerrorState
WaitForTiltInput
WaitForScore
WaitForSnitchPress
HappyState
In Broom.c:
WaitForTilt
NoTiltAccepted
In Countdown.c:
StopCount
Counting
EVENTS:
ES_NO_EVENT
ES_TIMEOUT
ES_BUTTON_UP
BUTTON_DOWN
LEFT_TILT_DETECTED
CENTER_TILT_DETECTED
RIGHT_TILT_DETECTED
LEFT_BALL_DETECTED
CENTER_BALL_DETECTED
RIGHT_BALL_DETECTED
START_COUNT
STOP_COUNT
TILT_READY
TITL_DETECTED
CAPTURE_SNITCH_READY
TIMERS:
GAME_TIMER: Counts 60 seconds until game to end by itself
Post to GameFSM queue
SNITCH_TIMER: Counts 30 seconds until game ends
Post to GameFSM queue
HAPPY_TIMER: Counts reset time (30 seconds) after game ends
Post to GameFSM queue
GLED_TIMER: Counts one second for Green LED on duration
Post to GameFSM queue
BUTTONBLINK_TIMER: Counts number of seconds for button blink state duration
COUNT_TIMER: Counts 7.5 seconds before shutting down each Timer LED
Post to CountdownFSM queue
EVENT CHECKERS:
CheckButton()
CheckIR()
CheckTiltEvents()
HELPER FUNCTIONS:
In Button.c:
SnitchLEDOn
SntchLEDOff
DDMOn
DDMOff
In Game.c:
PickNextHoop
In Broom.c:
moveBroom
In Countdown.c:
LightAllTimerLEDs
TurnOffAllLEDs
TurnOffLED
In TargetLED.c:
InitTargetLED
LightTargetLED
VibrationOn
VibrationOff
In Hoop.c:
InitHoops
decodeHoopNum
MoveHoopUp
MoveHoopDown
In Music.c:
InitMusic
ResetMusic
MusicPulse
PlayFirst
PlaySecond
In GeneralBase.c:
pause
write
read
pulse
delay
InitSystemClock
InitPinIO
InitPinDir
---------------------------------------------------------------------------------------------------------------------------ButtonFSM Module (State Machine)
Module Defines:
ButtonPort
F
ButtonIO
BIT1HI
ButtonLEDIO
BIT0HI
DDMPort
D
DDM
BIT7HI
Module variables:
MyPriority (8 bit unsigned integer), CurrentState (ButtonState_t)
ButtonFSM states:
Debouncing, Ready2Sample
Module Functions:
SnitchLEDOn: takes no parameters, returns nothing
End of RunButtonDebounceFSM
Private Helper Functions in ButtonFSM:
SnitchLEDOn
Takes no parameters, returns nothing
Sets Button LED port to HI
End SnitchLEDOn
SnitchLEDOff
Takes no parameters, returns nothing
Sets Button LED port to LO
End SnitchLEDOff
DDMOn
Takes no parameters, returns nothing
Sets DDM pin to HI
EndDDMOn
DDMOff
Takes no parameters, returns nothing
Sets DDM pin to LO
EndDDMOff
---------------------------------------------------------------------------------------------------------------------------GameFSM module (State Machine)
Module Defines:
rightRedLED
BIT4HI
centerRedLED
BIT5HI
leftRedLED
BIT6HI
rightGreenLED
BIT1HI
centerGreenLED BIT2HI
leftGreenLED
BIT3HI
IRPort
IRpin1
IRpin2
IRpin3
F
BIT4HI
BIT3HI
BIT2HI
LEFT_HOOP
BIT2HI
CENTER_HOOP BIT1HI
RIGHT_HOOP
BIT0HI
ScoreLimit
Module variables:
MyPriority (8 bit unsigned integer), CurrentState (GameState_t), openHoop (integer),
redLEDStatus, greenLEDStatus (8 bit unsigned integers),
SnitchTimerRunning (boolean),
ButtonBlinkStatus (8 bit unsigned integer)
GameFSM states:
WaitStart, WaitRightTilt, WaitScore, WaitEnd, EndGame
Module Functions:
PickNextHoop: takes no parameters, returns nothing
---------------------------------------------InitGameFSM
Takes a priority number, returns true
Initialize the MyPriority with priority
Initialize Target LED ports
Initialize Hoop servo ports
Initialize music control pins
Reset music module
Play terror music
Initialize all IR port pins to be inputs
Turn on Snitch LED
Set ButtonBlinkStatus to HI
Start BUTTONBLINK_TIMER
Set CurrentState to TerrorState
End of InitGame
PostGameFSM
Takes parameter ThisEvent (type ES_Event) and returns boolean
Returns true if an event is posted to queue as per MyPriority
End of PostGameFSM
RunGameFSM
EventType: ES_Init, ES_TIMEOUT , ES_ButtonDown, ES_TiltTarget, ES_BallDetected
Returns ES_NO_EVENT
States: TerrorState, WaitForTiltInput, WaitForScore, WaitForSnitchPress, HappyState
Local Variables: NextState (type GameState_t), score (type uint8_t), TimerEvent (type
ES_Event), BroomEvent (type ES_Event)
Set NextState to CurrentState
Switch CurrentState
CurrentState is TerrorState
If EventType is ES_BUTTON_DOWN
Play Terror music
Set TimerEvent type as START_COUNT
Post TimerEvent to CountdownFSM
Turn off Snitch LED
Set BroomEvent type as TILT_READY
Post BroomEvent to BroomFSM
Set openHoop to CENTER_HOOP
Lift cover for openHoop (Call to MoveHoopUp)
Set score to 0
Set greenLEDStatus to 0 (All green LEDs off)
Start main GAME_TIMER (60S)
Start SNITCH_TIMER (30S) (Snitch lights up after 30 seconds)
Set SnitchTimerRunning to 0
Set NextState to WaitForTiltInput
Module variables:
CurrentState (BroomState_t), MyPriority (uint8_t)
BroomFSM states:
WaitForTilt, NoTiltAccepted
Module Functions:
moveBroom: returns nothing, takes 32 bit integer parameter tiltInput
InitBroomFSM
Takes a priority number, returns true
Initialize MyPriority with priority
Initialize A/D converter (port PE4)
Initialize PWM for broom pointer servo (port PB4)
Set CurrentState to NoTiltAccepted
End of InitBroomFSM
PostBroomFSM
Takes parameter ThisEvent (type ES_Event) and returns boolean
Returns true if an event is posted to queue as per MyPriority
End of PostBroomFSM
RunBroomFSM
EventType: TILT_DETECTED, CAPTURE_SNITCH_READY, TILT_READY
States: WaitForTilt, NoTiltAccepted
Returns ES_NO_EVENT
Local: BroomState_t NextState
Set NextState to CurrentState
If CurrentState is WaitForTilt
If EventType is TILT_DETECTED
call MoveBroom(ThisEvent.EventParam)
Elseif EventType is CAPTURE_SNITCH_READY
Set NextState to NoTiltAccepted
Endif on EventType
Else if CurrentState is NoTiltAccepted
If EventType is TILT_READY
Set NextState = WaitForTilt
Endif on EventType
Endif on CurrentState
PostCountdownFSM
Takes parameter ThisEvent (type ES_Event) and returns boolean
Returns true if an event is posted to queue as per MyPriority
End of PostCountdownFSM
RunCountDownFSM
EventType: START_COUNT, STOP_COUNT, ES_TIMEOUT
Set NextState to CurrentState
If CurrentState is StopCount
If EventType is START_COUNT
Start LED_TIMER (7.5 seconds)
Turn on all Timer LEDs
Set NextState to Counting
Endif on EventType
Else if CurrentState is Counting
If EventType is ES_TIMEOUT AND parameter is LED_TIMER
Turn off one LED
Restart LED_TIMER (7.5 seconds)
Else if EventType is STOP_COUNT
Turn off all LEDs
Set NextState to StopCount
End if on EventType
End if on CurrentState
Set CurrentState to NextState
Return ES_NO_EVENT
End of RunCountdownFSM
---------------------------------------------------------------------------------------------------------------------------Private Helper Functions in CountdownFSM:
LightAllTimerLEDs
Takes no parameter, returns nothing
For loop 8 times
Set Timer LED DATA pin high
Pulse SCK
End of for loop
Pulse RCK
End LightAllTimerLEDs
TurnOffAllLEDs
Takes no parameters, returns nothing
For loop 8 times
Set Timer LED DATA pin low
Pulse SCK
End of for loop
Pulse RCK
End TurnOffAllLEDs
TurnOffLED
Takes no parameter, returns nothing
For loop 8 times
Set DATA pin low
Pulse SCK
Pulse RCK
Shift LED Counter by 1 (tracks which LED is turned off)
End of for loop
End TurnOffLED
---------------------------------------------------------------------------------------------------------------------------TargetLED Module (TargetLED.c)
Module defines:
TargetLEDport
'D'
TargetLEDDataPin BIT1HI
TargetLEDSCKPin BIT2HI
TargetLEDRCKPin BIT3HI
VibrationPort
'D'
VibrationIO
BIT0HI
ALL_BITS
(0xff<<2)
Module Functions:
InitTargetLED: takes no parameters, returns nothing
LightTargetLED: takes 8 bit integer LED, returns nothing
InitTargetLED
Takes no parameters, returns nothing
Activate system clock for TargetLEDport
Initialize all Target LED pins to IO and set all to be outputs
MusicPort
ResetPin
DataPin
CKPin
ALL_BITS
B
BIT0HI
BIT1HI
BIT2HI
(0xff<<2)
Module Functions:
MusicPulse: takes a character type variable and 8 bit unsigned integer, returns nothing
InitMusic
Takes no parameters, returns nothing
Activate system clock for MusicPort
Initialize music pins to IO and set them to output
End of InitMusic
ResetMusic
Takes no parameters, returns nothing
Set reset pin low (Call to write function with MusicPort, ResetPin and 0)
Wait for 10 milliseconds
Set reset pinhi (Call to write function with MusicPort, ResetPin and 1)
Wait for 300 milliseconds
End of ResetMusic
MusicPulse
Takes a character type variable (port) and 8 bit unsigned integer (pin), returns nothing
If port is B
Set pin to be high
Wait for 200 microseconds
Set pin to low
Wait for 200 microseconds
Else if port is C
Set pin to be high
Wait for 200 microseconds
Set pin to low
Wait for 200 microseconds
Else if port is D
Set pin to be high
Wait for 200 microseconds
Set pin to low
Wait for 200 microseconds
Else if port is E
Set pin to be high
Wait for 200 microseconds
Set pin to low
Wait for 200 microseconds
Else if port is F
Set pin to be high
Wait for 200 microseconds
Set pin to low
Wait for 200 microseconds
Else
Print message Error: you indicated a wrong port for pulse
End if
End of MusicPulse
PlayFirst
Takes no parameters, returns nothing
Set CK pin low (Call to write function with MusicPort, CKPin and 0)
Wait for 5 milliseconds
For loop 15 times
Set DATA pin to low (Call to write function with MusicPort, DataPin and 0)
Pulse CK pin (Call to MusicPulse with MusicPort and CKPin)
End of for loop
Set DATA pin to low (Call to write function with MusicPort, DataPin and 0)
Pulse CK pin (Call to MusicPulse with MusicPort and CKPin)
Set DATA pin to high (Call to write function with MusicPort, DataPin and 1)
Set CK pin to high (Call to write function with MusicPort, CKPin and 1)
End of PlayFirst
PlaySecond
Takes no parameters, returns nothing
Set CK pin low (Call to write function with MusicPort, CKPin and 0)
Wait for 5 milliseconds
For loop 15 times
Set DATA pin to low (Call to write function with MusicPort, DataPin and 0)
Pulse CK pin (Call to MusicPulse with MusicPort and CKPin)
End of for loop
Set DATA pin to high (Call to write function with MusicPort, DataPin and 1)
Pulse CK pin (Call to MusicPulse with MusicPort and CKPin)
Set DATA pin to high (Call to write function with MusicPort, DataPin and 1)
Set CK pin to high (Call to write function with MusicPort, CKPin and 1)
End of PlaySecond
Read CurrentButtonState from IO pin (Call to read function with port F and BIT1HI)
If CurrentButtonState is different from the LastButtonState
Set ReturnVal to true
If the CurrentButtonState is low (button pressed)
PostEvent BUTTON_DOWN to ButtonDebounceFSM queue
Else
PostEvent BUTTON_UP to ButtonDebounceFSM queue
End if
End if
Set LastButtonState to CurrentButtonState
Return ReturnVal
End of CheckButton
CheckIR
Takes no parameters, returns true if event posted
Local variables:
LastIRLeft, LastIRCenter, LastIRRight (8 bit unsigned integers all initialized to 1)
ReturnVal (boolean) = false
CurrentIRLeft, CurrentIRCenter, CurrentIRRight (8 bit unsigned integers)
Read all 3 CurrentIR states from input port pins (F2-4)
If CurrentIRLeft different from LastIRLeft
If CurrentIRLeft is low
Post Event LEFT_BALL_DETECTED to GameFSM queue
EndIf
Set ReturnVal to true
Endif
If CurrentIRCenter different from LastIRCenter
If CurrentIRCenter is low
Post Event CENTER_BALL_DETECTED to GameFSM queue
EndIf
Set ReturnVal to true
Endif
If CurrentIRRight different from LastIRRight
If CurrentIRRight is low
Post Event RIGHT_BALL_DETECTED to GameFSM queue
EndIf
Set ReturnVal to true
Endif
return 0
End if
Else
return 2 (Error number)
End if
End of read()
pulse
Takes character variable port, 8 bit unsigned integer pin and returns nothing
If port is B
Set pin to high
Wait for 20 microseconds (Call to pause())
Set pin to low
Wait for 20 microseconds (Call to pause())
Else if port is C
Set pin to high
Wait for 20 microseconds (Call to pause())
Set pin to low
Wait for 20 microseconds (Call to pause())
Else if port is D
Set pin to high
Wait for 20 microseconds (Call to pause())
Set pin to low
Wait for 20 microseconds (Call to pause())
Else if port is E
Set pin to high
Wait for 20 microseconds (Call to pause())
Set pin to low
Wait for 20 microseconds (Call to pause())
Else if port is F
Set pin to high
Wait for 20 microseconds (Call to pause())
Set pin to low
Wait for 20 microseconds (Call to pause())
Else
Print error message you indicated a wrong port for pulse
End if
End of pulse()
delay
Takes integer t, returns nothing
Set integer i to current time (by calling ES_Timer_GetTime())
For loop
Runs till current time - i is less than t
End of for loop
End of delay()
InitSystemClock
Takes character variable port and returns nothing
If port is B
Activate system clock for port B
Initialize dummy variable
Else if port is C
Activate system clock for port C
Initialize dummy variable
Else if port is D
Activate system clock for port D
Initialize dummy variable
Else if port is E
Activate system clock for port E
Initialize dummy variable
Else if port is F
Activate system clock for port F
Initialize dummy variable
Else
Print error message Attempt to activate system clock for invalid port
End if
End of InitSystemClock()
InitPinIO
Takes character variable port, 8 bit unsigned integer pin and returns nothing
If port is B
Enable pin to IO
Else if port is C
Enable pin to IO
Else if port is D
Enable pin to IO
Else if port is E
Enable pin to IO
Else if port is F
Enable pin to IO
Else