You are on page 1of 4

/*

ResetSM

This state machine/service does the resetting for the LEAF_RESET or


INACTIVE_RESET event. The actions it performs are:
Sound off
Switch off the score LEDs
Down the Flag
Bring Mario to the Start
Stop timers - Inactive, Scoring, Pipe
Switch off difficulty level pulsing

First Version: Nov 16, Friday

Data private to the module:

States:

*/

// the common headers for C99 types


#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

// the headers to access the GPIO subsystem


#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"

// the headers to access the TivaWare Library


#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"

#include "BITDEFS.H"

// Event & Services Framework


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

#include "ResetSM.h"
#include "SR_HillAndMotor.h"
#include "SoundSM.h"
#include "GameSM.h"
#include "VisualBlastSM.h"
#include "ControlSM.h"

//Including A/D converter and PWM input


#include "ADMulti.h"
#include "PWM16Tiva.h"

// readability defines

//defines for the flag checks


#define POSITION_PORT_HI BIT3HI
#define POSITION_PORT_LO BIT3LO

#define FLAG_UP_PIN_HI BIT0HI


#define FLAG_UP_PIN_LO BIT0LO
#define FLAG_DOWN_PIN_HI BIT1HI
#define FLAG_DOWN_PIN_LO BIT1LO

#define MARIO_START_PIN_HI BIT2HI


#define MARIO_START_PIN_LO BIT2LO

#define MARIO_END_PIN_HI BIT3HI


#define MARIO_END_PIN_LO BIT3LO

#define POSITION_SYSCTL_PRGPIO SYSCTL_PRGPIO_R3


#define POSITION_GPIO_BASE GPIO_PORTD_BASE

#define ALL_HI (BIT0LO | BIT0HI)

//*********************************MODULE
DEFINES***********************************************
// an image of the last 8 bits written to the shift register
static uint8_t MyPriority;

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

/****************************************************************************
Function
InitResetSM

Parameters
uint8_t : the priorty of this service

Returns
true if an INIT event has been posted. Else False which is equivalent to an
error in posting the event

Description
A function to initialize the shift register
The data and sclk lines start from low and the RCLK starts from high

Notes

Author
****************************************************************************/

bool InitResetSM(uint8_t Priority)


{
MyPriority = Priority;
ES_Event_t ThisEvent;
ThisEvent.EventType = ES_INIT;
puts("\r \n Initialized ResetSM");

if (ES_PostToService(MyPriority, ThisEvent) == true)


{
return true;
}
else
{
return false;
}
//return true; //saying that no
}

/****************************************************************************
Function
PostResetSM

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 PostResetSM(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunResetSM

Parameters
ES_Event_t : the event from its service queue to process in this State
Machine.
The EventParam will carry the index of the sound to be played.

Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise

Description

Notes

Author

****************************************************************************/
ES_Event_t RunResetSM(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
puts("\r \n Event received in ResetSM");

if ((ThisEvent.EventType == INACTIVE_RESET) || (ThisEvent.EventType ==


LEAF_RESET))
{
puts( "\r \n Reset Event received in RESET_SM");

puts( "\r \n Switching sound off");


ES_Event_t SoundEvent;
SoundEvent.EventType = SOUND_STOP;
PostSoundSM(SoundEvent);

//switching off the timers off here for checking inactive timer
//puts("\r \n Switching off the timers");
//ES_Timer_StopTimer(0); //inactive timer
//ES_Timer_StopTimer(1); //pipe timer

//moving mario in reverse direction


//only if mario is not already at start
if (!(HWREG(POSITION_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &
MARIO_START_PIN_HI))
{
puts("\r \n Start Mario Reverse <--");
uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT0LO & BIT1LO);
uint8_t NewValue = Value_Masked; //Bit0 is 1 and Bit1 need to be 0 for
Mario Start reverse direction
SR_HillAndMotor_Write(NewValue);
PWM_TIVA_SetDuty(100, 2); //Initializing Mario backward direction
}
//moving flag down
//only if flag is not already down
if (!(HWREG(POSITION_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &
FLAG_DOWN_PIN_HI))
{
puts("\r \n Start Flag Down");
uint8_t LastSR2_Value_2 = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked_2 = LastSR2_Value_2 & (BIT2LO & BIT3LO);
uint8_t NewValue_2 = Value_Masked_2 | BIT2HI; //Bit2 IS 1 and Bit3 need to
be 0 for Flag going down
SR_HillAndMotor_Write(NewValue_2);
}

//switching off the hill leds


uint8_t LastSR2_Value_3 = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked_3 = LastSR2_Value_3 & (BIT4LO & BIT5LO & BIT6LO);
uint8_t NewValue_3 = Value_Masked_3; //Bit 4,5, and 6 need to be 0
switching off the hill leds
SR_HillAndMotor_Write(NewValue_3);

puts("\r \n Switch off difficulty leds etc.");


PWM_TIVA_SetDuty(0, 0);

puts("\r \n Switch off hill leds etc.");


uint8_t LastSR2_Value_4 = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked_4 = LastSR2_Value_4 & (BIT4LO & BIT5LO & BIT6LO);
uint8_t NewValue_4 = Value_Masked_4;
SR_HillAndMotor_Write(NewValue_4);

puts("\r \n Switch off pipe leds etc.");


ActivatePipeLED(0);

ControlSM_States_t WelcomingCheck = Welcoming;


//switch of visual blast only if the controlSM is noti in welcoming
if (getControlSM_State() != WelcomingCheck)
{
puts("\r \n Switching off Visual Blast on Reset");
ES_Event_t PostingEvent;
PostingEvent.EventType = VISUAL_BLAST_OFF;
PostVisualBlastSM(PostingEvent);
}
}
return ReturnEvent;
}

You might also like