You are on page 1of 6

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

Module
DotStarService.c

Description
This is a Service to update the DotStar in response to certain events in galaga

Notes

History
When Who What/Why
-------------- --- --------
11/10/20 CP Created File
****************************************************************************/
/*----------------------------- 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
*/
#include "ES_Configure.h"
#include "ES_Framework.h"

#include "Gameplay.h"
#include "spi_master.h"
#include "DotStarService.h"

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

#define Fiftyms 50
#define TenthSecond 100
#define HalfSecond 500
#define SlowFade 300

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


/* prototypes for private functions for this machine.They should be functions
relevant to the behavior of this state machine
*/
void dotStar_Write(uint8_t bright1, uint8_t red1, uint8_t green1,
uint8_t blue1,uint8_t bright2, uint8_t red2, uint8_t green2 ,
uint8_t blue2);

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


// everybody needs a state variable, you may need others as well.
// type of state variable should match htat of enum in header file

// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
static DotStarState_t CurrentState;
static uint8_t Brightness;
static uint8_t Count; //used for celebratory thing

/*------------------------------ Module Code ------------------------------*/


/****************************************************************************
Function
InitDotStarService

Parameters
uint8_t : the priority of this service

Returns
bool, false if error in initialization, true otherwise

Description
Saves away the priority,
Notes

Author
C. Paullin 11/10/20
****************************************************************************/
bool InitDotStarService(uint8_t Priority)
{
ES_Event_t ThisEvent;

MyPriority = Priority;
Brightness = 0;
Count = 0;

ThisEvent.EventType = ES_INIT;
// post the initial transition event
CurrentState = InitDotStar;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}

/****************************************************************************
Function
PostDotStarService

Parameters
ES_Event_t ThisEvent , the event to post to the queue

Returns
boolean False if the Enqueue operation failed, True otherwise

Description
Posts an event to this state machine's queue
Notes

Author
C. Paullin
****************************************************************************/
bool PostDotStarService(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunDotStarService

Parameters
ES_Event_t : the event to process

Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise

Description
simple service that writes to the dot star given different events.
Notes
uses nested switch/case to implement the machine.
Author
c. paullin 11/10/20
****************************************************************************/
ES_Event_t RunDotStarService(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
DotStarState_t NextState = CurrentState;
switch (CurrentState)
{
case InitDotStar:
{
if(ThisEvent.EventType== ES_INIT)
{
ES_Timer_InitTimer(DOTSTAR_TIMER,SlowFade);
NextState= NotPlaying;
}//ES_INIT
}break;// end case InitDotStar
case NotPlaying:
{
switch(ThisEvent.EventType)
{
case ES_TIMEOUT:
{
ES_Timer_InitTimer(DOTSTAR_TIMER,SlowFade); //to keep on fading in and
out
if(Brightness<=20){
dotStar_Write(Brightness,50,0,182,Brightness,50,0,182); //purplish
blue
Brightness++;//increment until max brightness
}else if(Brightness<40){
dotStar_Write(40-Brightness,50,0,182,40-Brightness,50,0,182);
//purpley blue
Brightness++;
}else{//restart it
Brightness=1;
}
}break;//ES_Timeout
case ES_START_GAME:
{
dotStar_Write(0,0,0,0,0,0,0,0); //so that dot star isn't on anymore
ES_Timer_StopTimer(DOTSTAR_TIMER); //stop timer that was doing the fade
in
NextState = Playing; //we are playing now
}break;//ES_Start_game
default:
;
}// end switch on ThisEvent for state InitDotStar

}break;
case Playing:
{
switch(ThisEvent.EventType)
{
case ES_GAME_OVER:
{
ES_Timer_StopTimer(DOTSTAR_TIMER);
if(QueryGameplayCollisions()== MAX_COLLISIONS){
NextState = Won;
ES_Timer_InitTimer(DOTSTAR_TIMER,Fiftyms);
}else{
ES_Timer_InitTimer(DOTSTAR_TIMER,SlowFade);
NextState = NotPlaying;
}
}break; //es_game_over
case ES_COLLISION:
{
dotStar_Write(5,255,80,0,5,255,80,0); //nice orange color when there is
collision
ES_Timer_InitTimer(DOTSTAR_TIMER,HalfSecond); //start timer for Second
}break; //es_collision
case ES_TIMEOUT:
{
dotStar_Write(0,0,0,0,0,0,0,0); //clear dot star until next collision
}break; //es_timeout
case ES_BUTTON:
{
dotStar_Write(1,255,255,255,1,255,255,255); //Bright light for
projectiles being shot
ES_Timer_InitTimer(DOTSTAR_TIMER,TenthSecond); //only a quick burst
}break; //Es_button
default:
;
}//end switch on this event
}break; //case state is playing
case Won:
{
switch(ThisEvent.EventType){
case ES_TIMEOUT:
{
//get random numbers for each thing in the dotstar
if(Count >= 100){ //it will flash for 5 seconds and then end
Count = 0;
dotStar_Write(20,50,0,182,20,50,0,182); //WRITE FULL BRIGHTNESS
BLUISH PURPLE
NextState = NotPlaying;
ES_Timer_InitTimer(DOTSTAR_TIMER,SlowFade);
}else{
ES_Timer_InitTimer(DOTSTAR_TIMER,Fiftyms); //
uint8_t red1 = rand()%255;
uint8_t blue1 = rand()%255;
uint8_t green1 = rand()%255;
uint8_t red2 = rand()%255;
uint8_t blue2 = rand()%255;
uint8_t green2 = rand()%255;
dotStar_Write(15,red1,green1,blue1,15,red2,green2,blue2);
//celebratory thing
Count++;
}

}break;//end case ES_TIMEOUT


case ES_START_GAME:
{
dotStar_Write(0,0,0,0,0,0,0,0); //so that dot star isn't on anymore
ES_Timer_StopTimer(DOTSTAR_TIMER); //stop timer that was doing the
fade in
NextState = Playing; //we are playing now
}break; //end case ES_START_GAME
}// end switch on this event
}// end case won
default:
;
}// end switch on current state
CurrentState = NextState;
return ReturnEvent;
}

/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
dotStar_Write

Parameters
several uint8_t that encode the brightness of the led and intensity of the red blue and
green in them

Returns
none

Description
writes to the dotStar with desired parameters
Notes

Author
C. Paullin 11/10/20
****************************************************************************/
void dotStar_Write(uint8_t bright1, uint8_t red1, uint8_t green1,
uint8_t blue1,uint8_t bright2, uint8_t red2, uint8_t green2 ,
uint8_t blue2)
{
uint32_t firstLED = 0; //initialize first LED bytes
uint32_t secondLED = 0; //initialize second LED bytes

firstLED = 0b111 << 29; //first three ones


firstLED |= bright1 << 24; //shift in global brightness

firstLED |= blue1 << 16; //shift in blue


firstLED |= green1 << 8; //shift in green
firstLED |= red1 << 0; //shift in red

secondLED = 0b111 << 29; //first three ones


secondLED |= bright2 << 24; //shift in global brightness

secondLED |= blue2 << 16; //shift in blue


secondLED |= green2 << 8; //shift in green
secondLED |= red2 << 0; //shift in red

spi2_Write(0); //start frame


spi2_Write(firstLED); //first LED white
spi2_Write(secondLED); //SECOND FULL RED
spi2_Write(0); //RESET FRAME
spi2_Write(0); //end frame

You might also like