Sei sulla pagina 1di 10

/*

SCORING_STATE_MACHINE

This state machine keeps track of the score

First Version: Nov 11, Sunday


Second Version: Nov 12, Monday

Data private to the module:


MyPriority

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 Files -----------------------------*/

/* 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
*/

// Includes from other state machines


//#include "ControlSM.h"

#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"

// Event & Services Framework


#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"

/*----------------------------- Module Defines ----------------------------*/


// define constants for the states for this machine
// and any other local defines

#define ALL_HI (BIT1LO | BIT1HI)

/*---------------------------- Module Functions ---------------------------*/


/* prototypes for private functions for this machine, things like during
functions, entry & exit functions.They should be functions relevant to the
behavior of this state machine
*/
//functions defined in this module
bool InitScoringSM(uint8_t Priority);
bool PostScoringSM(ES_Event_t ThisEvent);
ES_Event_t RunScoringSM(ES_Event_t ThisEvent);
int GetScore(void);

/*---------------------------- Module Variables ---------------------------*/


// everybody needs a state variable, you may need others as well
static uint8_t MyPriority;
static ScoringSM_States_t CurrentState;
static int Score;

/****************************************************************************
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");

//initialize the hardware pins if any


/*
Initialize the hardware pin to read the analog input
*/

//setting current state as the pseudo state


CurrentState = PseudoState_Scoring;

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
****************************************************************************/

ES_Event_t RunScoringSM(ES_Event_t ThisEvent)


{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors

/*
//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
{

print("\r \n Not playing game");


CurrentState = Uninitialized;
//post reset to the service
return;
}
*/

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

// now put the machine into the actual initial state


puts("\r \n Pseudo --> Uninitialized");
CurrentState = Uninitialized_Scoring;
}
}
break;

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);

if (Score > T1)


{
CurrentState = GreenLED;
puts("\r \n Same State (GreenLED)");
}
else if (Score < T1)
{
puts("\r \n Yellow LED On");
uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT5LO); //getting the yellow
led bit
uint8_t NewValue = Value_Masked | BIT5HI; //Yellow LED lit up
SR_HillAndMotor_Write(NewValue);

CurrentState = GreenAndYellowLED;

puts("\r \n GreenLED --> GreenAndYellowLED");


}
}
//LEAF_RESET
else if (ThisEvent.EventType == LEAF_RESET)
{
//actions
//puts("\r \n Leaf Reset");

puts("\r \n GreenLED --> Uninitialized");

CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
//puts("\r \n Inactivity Reset");

puts("\r \n GreenLED --> Uninitialized");

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 :) :) :) :) :) ");
*/

puts("\r \n GreenLED --> WaitforEnding");

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);

if (Score > T2)


{
CurrentState = GreenAndYellowLED;
puts("\r \n Same State (GreenAndYellowLED)");
}
else if (Score < T2)
{
CurrentState = YellowLED;
puts("\r \n Green LED OFF");
uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT4LO); //getting the green
led bit
uint8_t NewValue = Value_Masked; //Green LED switched
off
SR_HillAndMotor_Write(NewValue);

puts("\r \n GreenAndYellowLED --> YellowLED");


}
}
//LEAF_RESET
else if (ThisEvent.EventType == LEAF_RESET)
{
//actions
puts( "\r \n Leaf Reset");

puts( "\r \n GreenAndYellowLED --> Uninitialized");

CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
puts( "\r \n Inactivity Reset");

puts( "\r \n GreenAndYellowLED --> Uninitialized");

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 :) :) :) :) :) ");
*/

puts("\r \n GreenAndYellowLED --> WaitforEnding");

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);

if (Score > T3)


{
CurrentState = YellowLED;
puts("\r \n Same State (YellowLED)");
}
else if (Score < T3)
{
CurrentState = YellowAndRedLED;
puts("\r \n Red LED On");
uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT6LO); //getting the red
led bit
uint8_t NewValue = Value_Masked | BIT6HI; //Red LED lit up
SR_HillAndMotor_Write(NewValue);

puts("\r \n YellowLED --> YellowAndRedLED");


}
}
//LEAF_RESET
else if (ThisEvent.EventType == LEAF_RESET)
{
//actions
puts( "\r \n Leaf Reset");

puts( "\r \n YellowLED --> Uninitialized");

CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
puts( "\r \n Inactivity Reset");

puts( "\r \n YellowLED --> Uninitialized");

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 YellowLED --> WaitforEnding");

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);

if (Score > T4)


{
CurrentState = YellowAndRedLED;
puts("\r \n Same State (YellowAndRedLED)");
}
else if (Score < T4)
{
CurrentState = RedLED;

puts("\r \n Yellow LED OFF");


uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT5LO); //getting the yellow
led bit
uint8_t NewValue = Value_Masked; //Yellow led
switched off
SR_HillAndMotor_Write(NewValue);

puts("\r \n YellowAndRedLED --> RedLED");


}
}
//LEAF_RESET
else if (ThisEvent.EventType == LEAF_RESET)
{
//actions
puts( "\r \n Leaf Reset");

puts( "\r \n YellowAndRedLED --> Uninitialized");

CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
puts( "\r \n Inactivity Reset");

puts( "\r \n YellowAndRedLED --> Uninitialized");

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);
}

puts("\r \n Same State (RedLed)");

CurrentState = RedLED;
}
//LEAF_RESET
else if (ThisEvent.EventType == LEAF_RESET)
{
//actions
puts( "\r \n Leaf Reset");

puts( "\r \n RedLED --> Uninitialized");

CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
puts( "\r \n Inactivity Reset");

puts( "\r \n RedLED --> Uninitialized");

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 :( :/ :( :/ :( ");
*/

puts("\r \n RedLED --> WaitforEnding");

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

puts( "\r \n WaitForEnding --> Uninitialized");

CurrentState = Uninitialized_Scoring;
}
//LEAF_RESET
else if (ThisEvent.EventType == LEAF_RESET)
{
//actions
//puts("\r \n Leaf Reset");

puts("\r \n WaitForEnding --> Uninitialized");

CurrentState = Uninitialized_Scoring;
}
//INACTIVITY_RESET
else if (ThisEvent.EventType == INACTIVE_RESET)
{
//actions
//puts("\r \n Inactivity Reset");

puts("\r \n WaitForEnding --> Uninitialized");

CurrentState = Uninitialized_Scoring;
}
}
break;
}

return ReturnEvent;
}

//*****************************************************HELPER
FUNCTIONS********************************
int GetScore(void)
{
return Score;
}

Potrebbero piacerti anche