0% found this document useful (0 votes)
30 views

Visualblastsm

Uploaded by

api-438010548
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Visualblastsm

Uploaded by

api-438010548
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

/*

VisualBlastSM

This state machine controls the visual blast for the welcoming mode and turning
it off

First Version: Nov 25, Sunday

Data private to the module:


MyPriority

States:
VisualBlastOff
SelectingPipeLed
GlowingPipeLed

Intaking Events:
VISUAL_BLAST_ON
VISUAL_BLAST_OFF
GLOW_RANDOM_PIPE (from itself)
ES_TIMEOUT (from VisualBlast_PipeTimer)

Posting Events:
GLOW_RANDOM_PIPE (to itself)

*/

/*----------------------------- Include Files -----------------------------*/


// Basic includes for a program using the Events and Services Framework

/* 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 "VisualBlastSM.h"
#include "SR_HillAndMotor.h"
#include "GameSM.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 ONE_SEC 976
#define TWENTIETH_OF_SEC (ONE_SEC / 20)

/*---------------------------- 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
*/
static void ControlHillLed(int HillLedNumber);

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


// everybody needs a state variable, you may need others as well
static uint8_t MyPriority;
static VisualSM_States_t CurrentState;
static int random_glow_pipe_index;
static int random_hill_led;
/****************************************************************************
Function
InitVisualBlastSM

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 InitVisualBlastSM(uint8_t Priority)
{
MyPriority = Priority;
printf("\r \n Initializing VisualSM");

//Initialize the hardware pin to read the analog input


//The hardware for the Pipe leds and hill leds is already initialized

ES_Event_t ThisEvent;
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}

/****************************************************************************
Function
PostVisualBlastSM

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

/****************************************************************************
Function
RunVisualBlastSM

Parameters
ES_Event_t : the event from its service queue to process in this State
Machine
Types of ES_Event_t: VISUAL_BLAST_OFF, VISUAL_BLAST_ON, GLOW_RANDOM_PIPE,
ES_TIMEOUT

Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise

Description
add your description here
Notes

Author

****************************************************************************/

ES_Event_t RunVisualBlastSM(ES_Event_t ThisEvent)


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

switch (CurrentState)
{
case PseudoState_VisualBlastSM: // 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

//switch off all the hill led


ControlHillLed(0);

//switch off all the pipe leds


ActivatePipeLED(0);

// now put the machine into the actual initial state


//puts("\r \n Pseudo_VisualBlastSM --> VisualBlastOff");
CurrentState = VisualBlastOff;
}
}
break;

case VisualBlastOff:
{
if (ThisEvent.EventType == VISUAL_BLAST_ON)
{
//post GLOW_RANDOM_PIPE to this service
ES_Event_t PostingEvent;
PostingEvent.EventType = GLOW_RANDOM_PIPE;
PostVisualBlastSM(PostingEvent);

//switch off the last hill led and glow the current random hill led
ControlHillLed( 0);

random_hill_led = rand() % 3 + 4;
ControlHillLed( random_hill_led);

//puts("\r \n VisualBlastOff --> SelectingPipeLed");

CurrentState = SelectingPipeLed;
}
}
break;

case SelectingPipeLed:
{
if (ThisEvent.EventType == GLOW_RANDOM_PIPE)
{
//select a random pipe
random_glow_pipe_index = rand() % 5 + 1; //pipes from 1-5

//glow led on a random pipe


ActivatePipeLED(random_glow_pipe_index);
SRPipe_Write(0);

//glow a random hill led after switching off the last one
ControlHillLed( 0);

random_hill_led = rand() % 3 + 4;
ControlHillLed( random_hill_led);

//restart/start visual blast timer


ES_Timer_InitTimer(VisualBlast_PipeTimer, TWENTIETH_OF_SEC);

//puts("\r \n SelectingPipeLed --> GlowingPipeLed");


CurrentState = GlowingPipeLed;
}
else if (ThisEvent.EventType == VISUAL_BLAST_OFF)
{
//switch off the hill leds
ControlHillLed(0);

//switch off the pipe leds


ActivatePipeLED(0);

//change the state


//puts("\r \n SelectingPipeLed --> VisualBlastOff");
CurrentState = VisualBlastOff;
}
}
break;

case GlowingPipeLed:
{
if (ThisEvent.EventType == ES_TIMEOUT)
{
//actions

//post GLOW_RANDOM_PIPE to this service


ES_Event_t PostingEvent;
PostingEvent.EventType = GLOW_RANDOM_PIPE;
PostVisualBlastSM(PostingEvent);

//switch off the last active pipe led


ActivatePipeLED(0);

//puts("\r \n GlowingPipeLed --> SelectingPipeLed");


CurrentState = SelectingPipeLed;
}
else if (ThisEvent.EventType == VISUAL_BLAST_OFF)
{
//switch off the hill leds
ControlHillLed(0);

//switch off the pipe leds


ActivatePipeLED(0);

//change the state


//puts("\r \n GlowingPipeLed --> VisualBlastOff");
CurrentState = VisualBlastOff;
}
}
break;
}
return ReturnEvent;
}

/****************************************************************************
Function
ControlHillLed

Parameters
No Parameters

Returns
Nothing

Description
Controls the hill led lighting at random for the welcoming mode.
Sending 0 switches off all the leds
Notes

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

static void ControlHillLed(int HillLedNumber)


{
switch (HillLedNumber)
{
case 0: //switching all LEDs off
{
uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT4LO & BIT5LO & BIT6LO);
uint8_t NewValue = Value_Masked;
SR_HillAndMotor_Write(NewValue);
}
break;
case 4: //switching on the green led
{
uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT4LO);
uint8_t NewValue = Value_Masked | BIT4HI;
SR_HillAndMotor_Write(NewValue);
}
break;
case 5: //switching yellow led on
{
uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT5LO);
uint8_t NewValue = Value_Masked | BIT5HI;
SR_HillAndMotor_Write(NewValue);
}
break;
case 6: //switching red led on
{
uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT6LO);
uint8_t NewValue = Value_Masked | BIT6HI;
SR_HillAndMotor_Write(NewValue);
}
break;
}
}

You might also like