Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SCORING_STATE_MACHINE
States:
Intaking Events:
SCORE_DEC signal corresponding to right no connection from the Game SM
Mario End
Internal:
Score crossing a threshold
Posting Events:
*/
/* include header files for this state machine as well as any machines at the
next lower level in the hierarchy that are sub-machines to this machine
*/
#include "ScoringSM.h"
#include "SR_HillAndMotor.h"
// Hardware
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
/****************************************************************************
Function
InitScoringSM
Parameters
uint8_t : 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 for this service
Notes
Author
****************************************************************************/
bool InitScoringSM(uint8_t Priority)
{
MyPriority = Priority;
printf("Initializing ScoringSM \n\r");
ES_Event_t ThisEvent;
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}
/****************************************************************************
Function
PostScoringSM
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
Notes
Author
****************************************************************************/
bool PostScoringSM(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunScoringSM
Parameters
ES_Event_t : the event from its service queue to process in this State
Machine
Types of ES_Event_t:
Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise
Description
Runs InGame state machine
Notes
Author
J. Edward Carryer, 01/15/12, 15:23
****************************************************************************/
/*
//inGame state check from ControlSM
ScoringSM_States_t GameState = InGame;
//if not in the InGame state from ControlSM, no point being here in the GameSM
if getControlSM_State() != InGame
{
switch (CurrentState)
{
case PseudoState_Scoring: // If current state is initial Psedudo
State
{
if (ThisEvent.EventType == ES_INIT) // only respond to ES_Init
{
// this is where you would put any actions associated with the
// transition from the initial pseudo-state into the actual
// initial state
case Uninitialized_Scoring:
{
//checking for the event StartGame
if (ThisEvent.EventType == START_GAME) //Leaf removed
{
//actions
/*
ON green leds
Set score counter
*/
puts("\r \n Green LED On");
uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT4LO); //getting the green
led bit
uint8_t NewValue = Value_Masked | BIT4HI; //Green LED lit up
SR_HillAndMotor_Write(NewValue);
Score = START_SCORE;
printf("\r \n Initialized score = %d ", Score);
puts("\r \n Uninitialized --> GreenLED");
CurrentState = GreenLED;
}
}
break;
case GreenLED:
{
//checking for the event ScoreDecrease and ensuring I haven't crossed the
LED yet
if (ThisEvent.EventType == SCORE_DEC) //score decreased but threshold
not crossed
{
//actions
/*
Decrease score
*/
Score--;
//puts("\r \n Decreased Score");
//printf("\r \n Score = %d",Score);
CurrentState = GreenAndYellowLED;
CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
//puts("\r \n Inactivity Reset");
CurrentState = Uninitialized_Scoring;
}
else if (ThisEvent.EventType == MARIO_END) //MARIO has reached the
end. Timer off
{
//actions
/*
play good sound
*/
/*
puts("\r \n Stop the last sound");
puts("\r \n :) :) :) :) :) ");
puts("\r \n Playing good sound");
puts("\r \n :) :) :) :) :) ");
*/
CurrentState = WaitForEnding;
}
}
break;
case GreenAndYellowLED:
{
//checking for the event ScoreDecrease and ensuring I haven't crossed the
LED yet
if (ThisEvent.EventType == SCORE_DEC) //score decreased but threshold
not crossed
{
//actions
/*
Decrease score
*/
Score--;
//puts("\r \n Decreased Score");
//printf("\r \n Score = %d",Score);
CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
puts( "\r \n Inactivity Reset");
CurrentState = Uninitialized_Scoring;
}
else if (ThisEvent.EventType == MARIO_END) //MARIO has reached the end.
Timer off
{
//actions
/*
play good sound
*/
/*
puts("\r \n Stop the last sound");
puts("\r \n :) :) :) :) :) ");
puts("\r \n Playing good sound");
puts("\r \n :) :) :) :) :) ");
*/
CurrentState = WaitForEnding;
}
}
break;
case YellowLED:
{
//checking for the event ScoreDecrease and ensuring I haven't crossed the
LED yet
if (ThisEvent.EventType == SCORE_DEC) //score decreased but threshold
not crossed
{
//actions
/*
Decrease score
*/
Score--;
//puts("\r \n Decreased Score");
//printf("\r \n Score = %d",Score);
CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
puts( "\r \n Inactivity Reset");
CurrentState = Uninitialized_Scoring;
}
else if (ThisEvent.EventType == MARIO_END) //MARIO has reached the end.
Timer off
{
//actions
/*
play good sound
*/
/*
puts("\r \n Stop the last sound");
puts("\r \n :) :/ :) :/ :) ");
puts("\r \n Playing sow-sow sound");
puts("\r \n :) :/ :) :/ :) ");
*/
CurrentState = WaitForEnding;
}
}
break;
case YellowAndRedLED:
{
//checking for the event ScoreDecrease and ensuring I haven't crossed the
LED yet
if (ThisEvent.EventType == SCORE_DEC) //score decreased but threshold
not crossed
{
//actions
/*
Decrease score
*/
Score--;
//puts("\r \n Decreased Score");
//printf("\r \n Score = %d",Score);
CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
puts( "\r \n Inactivity Reset");
CurrentState = Uninitialized_Scoring;
}
else if (ThisEvent.EventType == MARIO_END) //MARIO has reached the end.
Timer off
{
//actions
/*
play good sound
*/
/*
puts("\r \n Stop the last sound");
puts("\r \n :) :/ :) :/ :) ");
puts("\r \n Playing sow-sow sound");
puts("\r \n :) :/ :) :/ :) ");
*/
puts("\r \n YellowAndRedLED --> WaitforEnding");
CurrentState = WaitForEnding;
}
}
break;
case RedLED:
{
//checking for the event ScoreDecrease
if (ThisEvent.EventType == SCORE_DEC)
{
//actions
/*
Decrease score only if the score does not turn negative
*/
if (Score > 0)
{
Score--;
//puts("\r \n Decreased Score");
//printf("\r \n Score = %d",Score);
}
CurrentState = RedLED;
}
//LEAF_RESET
else if (ThisEvent.EventType == LEAF_RESET)
{
//actions
puts( "\r \n Leaf Reset");
CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
puts( "\r \n Inactivity Reset");
CurrentState = Uninitialized_Scoring;
}
else if (ThisEvent.EventType == MARIO_END) //MARIO has reached the end.
Timer off
{
//actions
/*
play good sound
*/
/*
puts("\r \n Stop the last sound");
puts("\r \n :( :/ :( :/ :( ");
puts("\r \n Playing sad sound");
puts("\r \n :( :/ :( :/ :( ");
*/
CurrentState = WaitForEnding;
}
}
break;
case WaitForEnding:
{
if (ThisEvent.EventType == FLAG_UP) //score decreased but threshold not
crossed
{
//actions
/*
*/
puts( "\r \n Flag is Up");
//puts("\r \n Stop sound"); //being taken care of in ResetSM
CurrentState = Uninitialized_Scoring;
}
//LEAF_RESET
else if (ThisEvent.EventType == LEAF_RESET)
{
//actions
//puts("\r \n Leaf Reset");
CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
//puts("\r \n Inactivity Reset");
CurrentState = Uninitialized_Scoring;
}
}
break;
}
return ReturnEvent;
}
//*****************************************************HELPER
FUNCTIONS********************************
int GetScore(void)
{
return Score;
}