You are on page 1of 29

COMPLETE PSEUDOCODE FOR ALL MODULES

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

SnitchLEDOff: takes no parameters, returns nothing


DDMOn: takes no parameters, returns nothing
DDMOff: takes no parameters, returns nothing
---------------------------------------------InitButton
Takes a priority number, returns true.
Initialize MyPriority with Priority
Initialize IO input pin
Initialize IO input pin for LED
Initialize IO pins for DDM
Set CurrentState to Debouncing
Start Debounce timer (timer posts to ButtonSM)
Turn DDM on (Call DDMOn function)
End of InitButton
PostButtonDebounceFSM
Takes parameter ThisEvent (type ES_Event) and returns boolean
Returns true if an event is posted to queue as per MyPriority
End of PostButtonDebounceFSM
RunButtonDebounceFSM
Event Types: BUTTON_UP, BUTTON_DOWN, ES_TIMEOUT
Switch on CurrentState
CurrentState is Debounce
If EventType is ES_TIMEOUT
Set CurrentState to Ready2Sample
Endif
CurrentState is Ready2Sample
If EventType is BUTTON_UP
Start BUTTON_TIMER
Set CurrentState to Debouncing
Else if EventType is BUTTON_DOWN
Start BUTTON_TIMER
Set CurrentState to Debouncing
Post ES_BUTTON_DOWN to GameFSM queue
End if on EventType
End switch on CurrentState
Return ES_NO_EVENT

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

Else if EventType is ES_TIMEOUT with parameter BUTTONBLINK_TIMER


If ButtonBlinkStatus is 1
Turn off Snitch LED
Set ButtonBlinkStatus to 0 (Button not blinking)
Restart BUTTONBLINK_TIMER
Else
Turn on Snitch LED

Set ButtonBlinkStatus to 1 (Blinking button)


Restart BUTTONBLINK_TIMER
End if
End if on EventType
End TerrorState case
CurrentState is WaitForTiltInput
Switch EventType
EventType is LEFT_TILT_DETECTED
Set redLEDStatus to leftRedLED
Turn on correct LED set (redLEDStatus or greenLEDStatus)
If openHoop is LEFT_HOOP
Set NextState to WaitForScore
End if
End LEFT_TILT_DETECTED case
EventType is CENTER_TILT_DETECTED
Set redLEDStatus to centerRedLED
Turn on correct LED set (redLEDStatus or greenLEDStatus)
If openHoop is CENTER_HOOP
Set NextState to WaitForScore
End if
End CENTER_TILT_DETECTED case
EventType is RIGHT_TILT_DETECTED
Set redLEDStatus to rightRedLED
Turn on correct LED set (redLEDStatus or greenLEDStatus)
If openHoop is RIGHT_HOOP
Set NextState to WaitForScore
End if
End RIGHT_TILT_DETECTED case
EventType is ES_TIMEOUT
Switch on EventParam
EventParam is GAME_TIMER
Post TimerEvent STOP_COUNT to
CountdownFSM
Turn on all timer LEDs
Close all hoop covers
Initiate reset timer (HAPPY_TIMER)

Post BroomEvent CAPTURE_SNITCH_READY to


BroomFSM
Set NextState to HappyState
End GAME_TIMER case
EventParam is SNITCH_TIMER
Set SnitchTimerRunning to 1
End SNITCH_TIMER case
EventParam is GLED_TIMER
Set greenLEDStatus to 0 (All green LEDs off)
Turn on correct LED set (redLEDStatus or
greenLEDStatus)
Turn off vibration on broom
End GLED_TIMER case
End switch on EventParam
End ES_TIMEOUT case
End switch on EventType
CurrentState is WaitForScore
Switch on EventType
EventType is LEFT_TILT_DETECTED
Set redLEDStatus to leftRedLED
Turn on correct LED set (redLEDStatus or greenLEDStatus)
If openHoop is not LEFT_HOOP
Set NextState to WaitForTiltInput
End if
End LEFT_TILT_DETECTED case
EventType is CENTER_TILT_DETECTED
Set redLEDStatus to centerRedLED
Turn on correct LED set (redLEDStatus or greenLEDStatus)
If openHoop is not CENTER_HOOP
Set NextState to WaitForTiltInput
End if
End CENTER_TILT_DETECTED case
EventType is RIGHT_TILT_DETECTED
Set redLEDStatus to rightRedLED
Turn on correct LED set (redLEDStatus or greenLEDStatus)

If openHoop is not RIGHT_HOOP


Set NextState to WaitForTiltInput
End if
End RIGHT_TILT_DETECTED case
EventType is LEFT_BALL_DETECTED
If openHoop is LEFT_HOOP
If score is less than ScoreLimit and SnitchTimerRunning is
0
Increase score by 1
Close openHoop
Pick next hoop
Open next hoop
Set greenLEDStatus as leftGreenLED
Turn on correct LED set (redLEDStatus or
greenLEDStatus)
Turn vibration on
Initiate GLED_TIMER (2 seconds)
Set NextState to WaitForTiltInput
Else
Increase score by 1
Close openHoop
Set greenLEDStatus as leftGreenLED
Turn on correct LED set (redLEDStatus or
greenLEDStatus)
Turn vibration on
Initiate GLED_TIMER (2 seconds)
Turn Snitch LED on
Set ButtonBlinkStatus to 1 (Button blinking)
Initiate BUTTONBLINK_TIMER (0.5 seconds)
Set NextState to WaitForSnitchPress
End if on score
End if on openHoop
End LEFT_BALL_DETECTED case
EventType is CENTER_BALL_DETECTED
If openHoop is CENTER_HOOP
If score is less than ScoreLimit and SnitchTimerRunning is
0
Increase score by 1
Close openHoop
Pick next hoop
Open next hoop
Set greenLEDStatus as centerGreenLED

Turn on correct LED set (redLEDStatus or


greenLEDStatus)
Turn vibration on
Initiate GLED_TIMER (2 seconds)
Set NextState to WaitForTiltInput
Else
Increase score by 1
Close openHoop
Set greenLEDStatus as centerGreenLED
Turn on correct LED set (redLEDStatus or
greenLEDStatus)
Turn vibration on
Initiate GLED_TIMER (2 seconds)
Turn Snitch LED on
Set ButtonBlinkStatus to 1 (Button blinking)
Initiate BUTTONBLINK_TIMER (0.5 seconds)
Set NextState to WaitForSnitchPress
End if on score
End if on openHoop
End CENTER_BALL_DETECTED case
EventType is RIGHT_BALL_DETECTED
If openHoop is RIGHT_HOOP
If score is less than ScoreLimit and SnitchTimerRunning is
0
Increase score by 1
Close openHoop
Pick next hoop
Open next hoop
Set greenLEDStatus as rightGreenLED
Turn on correct LED set (redLEDStatus or
greenLEDStatus)
Turn vibration on
Initiate GLED_TIMER (2 seconds)
Set NextState to WaitForTiltInput
Else
Increase score by 1
Close openHoop
Set greenLEDStatus as rightGreenLED
Turn on correct LED set (redLEDStatus or
greenLEDStatus)
Turn vibration on
Initiate GLED_TIMER (2 seconds)
Turn Snitch LED on

Set ButtonBlinkStatus to 1 (Button blinking)


Initiate BUTTONBLINK_TIMER (0.5 seconds)
Set NextState to WaitForSnitchPress
End if on score
End if on openHoop
End RIGHT_BALL_DETECTED case
EventType is ES_TIMEOUT
Switch on EventParam
EventParam is GAME_TIMER
Post TimerEvent STOP_COUNT to CountdownFSM
Turn on all Timer LEDs
Shut down Snitch LED
Close all hoop covers
Post BroomEvent CAPTURE_SNITCH_READY to
BroomFSM
Initiate reset timer (HAPPY_TIMER, 15 seconds)
Set NextState to HappyState
End GAME_TIMER case
EventParam is SNITCH_TIMER
Set SnitchTimerRunning to 1
End SNITCH_TIMER case
EventParam is GLED_TIMER
Set greenLEDStatus to 0
Turn on correct LED set (redLEDStatus or
greenLEDStatus)
Turn vibration off
End GLED_TIMER case
End switch on EventParam
End switch on EventType
End WaitForScore case
CurrentState is WaitForSnitchPress
Switch on EventType
EventType is ES_BUTTON_DOWN
Post TimerEvent STOP_COUNT to CountdownFSM
Post BroomEvent CAPTURE_SNITCH_READY to BroomFSM
Set all green LEDs to turn on
Set all red LEDs to turn off
Turn off vibration
Open all hoop covers

Turn off Snitch LED


Initiate reset timer (HAPPY_TIMER, 30 seconds)
Turn DDM off
Play happy music
Set NextState to HappyState
End ES_BUTTON_DOWN case
EventType is ES_TIMEOUT
Switch on EventParam
EventParam is GAME_TIMER
Post TimerEvent STOP_COUNT to CountdownFSM
Turn on all Timer LEDs
Close all hoop covers
Post BroomEvent CAPTURE_SNITCH_READY to
BroomFSM
Initiate reset timer (HAPPY_TIMER, 15 seconds)
Set NextState to HappyState
End GAME_TIMER case
EventParam is BUTTONBLINK_TIMER
If Button is blinking (ButtonBlinkStatus = 1)
Turn Snitch LED off
Set ButtonBlinkStatus to 0
Initiate BUTTONBLINK_TIMER (0.25 seconds)
Else
Turn Snitch LED on
Set ButtonBlinkStatus to 1
Initiate BUTTONBLINK_TIMER (0.5 seconds)
End if
End BUTTONBLINK_TIMER case
End switch on EventParam
End ES_TIMEOUT case
End switch on EventType
End WaitForSnitchPress case
CurrentState is HappyState
If EventType is ES_TIMEOUT
If EventParam is HAPPY_TIMER
Close all hoop covers
Set all green LEDs to turn off
Set all red LEDs to turn off
Turn vibration off

Turn Snitch LED on


Play terror music
Turn DDM on
Set ButtonBlinkStatus to 1
Initiate BUTTONBLINK_TIMER (1 second)
Set NextState to TerrorState
End if on EventParam
End if on EventType
End HappyState case
Set CurrentState to NextState
End of RunGameFSM
---------------------------------------------------------------------------------------------------------------------------Private Helper Functions in GameFSM:
PickNextHoop
Takes no parameters, returns nothing
Set random = double type random number between 0 and 1
If openHoop is LEFT_HOOP
If random is less than 0.5
Set openHoop to CENTER_HOOP
Else
Set openHoop to RIGHT_HOOP
End if on random
Else if openHoop is CENTER_HOOP
If random is less than 0.5
Set openHoop to LEFT_HOOP
Else
Set openHoop to RIGHT_HOOP
End if on random
Else if openHoop is RIGHT_HOOP
If random is less than 0.5
Set openHoop to CENTER_HOOP
Else
Set openHoop to LEFT_HOOP
End if on random
End if on openHoop
End of PickNextHoop
---------------------------------------------------------------------------------------------------------------------------BroomFSM module (State Machine)

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

Set CurrentState to NextState


End of RunBroomFSM
---------------------------------------------------------------------------------------------------------------------------Private Helper Functions in BroomFSM:
moveBroom
Accepts uint32_t type A/D input (tiltInput)
Calculate broomPosition from A/D input
Write broomPosition to servomotor (Using PWM_TIVA_SetPulseWidth)
End of moveBroom
---------------------------------------------------------------------------------------------------------------------------CountdownFSM module (State Machine)
Module defines:
LEDport C
DataPin BIT5HI
SCKPIn BIT6HI
RCKPin BIT7HI
Module variables:
CurrentState (CountdownState_t), MyPriority (uint8_t), LEDCounter (uint8_t)
CountdownFSM states:
StopCount, Counting
Module Functions:
LightAllTimerLEDs: takes no parameters, returns nothing
TurnOffAllLEDs: takes no parameters, returns nothing
TurnOffLED: takes no parameters, returns nothing
InitCountdownFSM
Takes a priority number, returns true.
Initialize MyPriority with priority
Initialize port line for timer LEDs
Set all LED pins to outputs
Initialize LED counter variable
Set CurrentState to StopCount
End of InitCountdownFSM

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

Initialize vibration motor pin to IO and set it to be output


End of InitTargetLED
LightTargetLED
Takes an unsigned 8 bit number (LED), returns nothing
{digits 6,5,4 indicate the status of RED Target LED 1,2,3
digits 3,2,1 indicate the status of Green Target LED 1,2,3
1 implies ON, 0 implies OFF
EX: to turn on Red 2, Green 1: LightTargetLED(BIT5HI|BIT3HI)}
For loop 8 times
Set data pin to the right most digit of LED input (Call write on TargetLEDPort,
TargetLEDDataPin, with LED & BIT0HI)
Pulse SCK (Call pulse on TargetLEDPort, TargetLEDSCKPin)
Shift LED right by one bit
End of for loop
Pulse RCK (Call pulse on TargetLEDPort, TargetLEDRCKPin)
End of LightTargetLED
VibrationOn
Takes no parameters, returns nothing
Set vibration control pin to high (Call write with VibrationPort, VibrationIO and 1)
End of VibrationOn
VibrationOff
Takes no parameters, returns nothing
Set vibration control pin to low (Call write with VibrationPort, VibrationIO and 0)
End of VibrationOff
---------------------------------------------------------------------------------------------------------------------------Hoop Module (Hoop.c)
Module defines:
ALL_BITS
(0xff<<2)
Module Functions:
decodeHoopNum: takes 8 bit unsigned integer hoopNum, returns an 8 bit unsigned integer

MoveHoopUp: takes 8 bit unsigned integer hoopNum, returns nothing


MoveHoopDown: takes 8 bit unsigned integer hoopNum, returns nothing
InitHoops
Takes no parameters, returns nothing
Initialize all PWM pins for three hoops
Set PWM frequency for group 0
Set PWM frequency for group 1
End of InitHoops
decodeHoopNum
Takes 8 bit unsigned integer hoopNum, returns an 8 bit unsigned integer
If hoopNum corresponds to right hoop
return 2
else if hoopNum corresponds to center hoop
return 3
else if hoopNum corresponds to left hoop
return 0
else
return 4
End of decodeHoopNum
MoveHoopUp
Takes 8 bit unsigned integer hoopNum, returns nothing
Set channelNum as output of decodeHoopNum (translates servo channel number by input)
Set pulse width for channelNum of servo to lift corresponding hoop cover
End of MoveHoopUp
MoveHoopDown
Takes 8 bit unsigned integer hoopNum, returns nothing
Set channelNum as output of decodeHoopNum (translates servo channel number by input)
Set pulse width for channelNum of servo to close corresponding hoop cover
End of MoveHoopDown
Music Module (Music.c)
Module defines:

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

EventCheckers Module (EventCheckers.c)


Module variables:
previous (integer) (monitors state of tilt range)
CheckTiltEvents
Takes no parameters, returns true if event posted
Local variables:
ReturnVal (boolean) = false, LastBroomPosition (32 bit unsigned integer) = 0,
CurrentBroomPosition (32 bit unsigned integer), threshold (32 bit unsigned integer)
Set CurrentBroomPosition to state read from A/D converter
If absolute value of (LastBroomPosition - CurrentBroomPosition) > threshold
Generate Event of type TILT_DETECTED and parameter CurrentBroomPosition
If CurrentBroomPosition is valid left
Post Event LEFT_TILT_DETECTED to GameFSM
Set previous to 0 (sets current range to left)
Else if CurrentBroomPosition is valid center AND previous is not 1
Post Event CENTER_TILT_DETECTED to GameFSM
Set previous to 1 (sets current range to center)
Else if CurrentBroomPosition is valid right AND previous is not 2
Post Event RIGHT_TILT_DETECTED to GameFSM
Set previous to 2 (sets current range to right)
Endif on CurrentBroomPosition
Post Event TILT_DETECTED to BroomFSM
Set ReturnVal to true
End if
Set LastBroomPosition to CurrentBroomPosition
return ReturnVal
End of CheckTiltEvents
CheckButton
Takes no parameters, returns true if an event posted
Local variables:
ReturnVal (boolean) = False, LastButtonState (32 bit unsigned integer) = 1, CurrentButtonState
(32 bit unsigned integer)

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

Set all 3 LastIR to all 3 CurrentIR


Return ReturnVal
End of CheckIR
GeneralBase Module (GeneralBase.c)
Includes general helper functions used throughout all modules
Module functions:
pause
Takes integer variable t, returns nothing
Multiply t by 10 (10 dummy loops)
for loop t times
End of for loop
End of pause
write
Takes character variable port, 8 bit unsigned integer pin, 8 bit unsigned integer level and
returns nothing
If port is B
If level is not 0
Set pin to high
Else
Set pin to low
End if
Else if port is C
If level is not 0
Set pin to high
else
Set pin to low
End if
Else if port is D
If level is not 0
Set pin to high
else
Set pin to low
End if
Else if port is E
If level is not 0

Set pin to high


else
Set pin to low
End if
Else if port is F
If level is not 0
Set pin to high
else
Set pin to low
End if
End if
End of write
read
Takes character variable port, 8 bit unsigned integer pin and returns 8 bit unsigned integer
If port is B
If pin is high
return 1
Else
return 0
End if
Else if port is C
If pin is high
return 1
Else
return 0
End if
Else if port is D
If pin is high
return 1
Else
return 0
End if
Else if port is E
If pin is high
return 1
Else
return 0
End if
Else if port is F
If pin is high
return 1
Else

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

Print error message Attempt to enable invalid pin to IO


End if
End of InitPinIO
InitPinDir
Takes character variable port, 8 bit unsigned integer pin, 8 bit unsigned integer level and
returns nothing
If port is B
If level is not 0
Set pin to output
Else
Set pin to input
End if
Else if port is C
If level is not 0
Set pin to output
Else
Set pin to input
End if
Else if port is D
If level is not 0
Set pin to output
Else
Set pin to input
End if
Else if port is E
If level is not 0
Set pin to output
Else
Set pin to input
End if
Else if port is F
If level is not 0
Set pin to output
Else
Set pin to input
End if
End if
End of InitPinDir()

You might also like