Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Drum LED FSM

Data private to this module:

#define ONE_SEC 1000

#define TWO_SEC 2000

static LEDFSMState_t CurrentState;

static uint8_t MyPriority;

static uint8_t MinBrightness = 1;

static uint8_t MaxBrightness = 31;

extern uint32_t NumPiezoIntensities;

static uint32_t HitIntensityToBrightness[9+1] = {

0,

10,

14,

17,

20,

23,

26,

29,

30,

31 // 9th intensity

};

static Colors_t NextWelcomingColor;


// --------------------------------------------------------------------

This function initializes the Drum LED FSM

Input: Priority number of the service

Output: True if initialization is successful

False otherwise

// --------------------------------------------------------------------

bool InitDRUM_LEDFSM(Priority)

create Event variable

set priority

current state is init state

set the MUX to left drum

clear the left drum leds

set left drum leds to minimum brightness

clear the bottom drum leds

set bottom drum leds to minimum brightness

clear the right drum leds

set right drum leds to minimum brightness

set the next welcoming mode color

post ES_INIT

}
// --------------------------------------------------------------------

This function posts an event to the Drum LED FSM

Input: Event to be posted

Output: True if post is successful

False otherwise

// --------------------------------------------------------------------

bool PostDRUM_LEDFSM(ThisEvent)

post the passed event to this FSM

// --------------------------------------------------------------------

This function returns the state of the Drum LED FSM

Input: N/A

Output: State of the FSM

// --------------------------------------------------------------------

LEDFSMState_t QueryDRUM_LEDFSM

return current state of the FSM

// --------------------------------------------------------------------

This function runs the Drum LED FSM

Input: Event that is posted to this FSM

Output: ES_NO_ERROR if there are no errors


// --------------------------------------------------------------------

ES_Event_t RunDRUM_LEDFSM(ThisEvent)

create return event

set return event to no errors

switch on current state

case InitPState_Drums

if ES_INIT event

current state is set to welcoming state

set MUX to left drums

set all drum leds to the next welcoming color

set all drum leds to minimum intensity

next welcoming color is purple

post updating LED event to this FSM

} break

case WelcomingState_Drums: {

switch(ThisEvent.EventType){

case ES_TIMEOUT: {

if LED_REFRESH_TIMER

set MUX to left drums

write the next welcoming colors to all the drum LEDs


post updating LED event to this FSM

update the next welcoming color

else if FADE_TIMER

set intensity of all drums to GetFadeIntensity()

break

case ES_UPDATING_LED: {

set MUX output to the drum we are currently updating

if still updating the LED

post updating LED to this FSM

else if have not updated all the drums

Event parameter is incremented

post updating LED to this FSM

break

case ES_IR_COVERED: {

set current state to IRCoveredState_Drums

// set the new color

set MUX output to left drum

set all the drums to gold

set all the drums' intensities to GetFadeIntensity()


post updating LED to this FSM

break

break

case IRCoveredState_Drums: {

switch(ThisEvent.EventType){

case ES_UPDATING_LED: {

set MUX output to the drum we are currently updating

if still updating the LED

post updating LED to this FSM

else if have not updated all the drums

Event parameter is incremented

post updating LED to this FSM

break

case ES_TIMEOUT: {

if FADE_TIMER

set intensity of all drums to GetFadeIntensity()

post updating LED to this FSM

break

case ES_IR_UNCOVERED: {

go back to welcoming state


set MUX to left drums

write the next welcoming color to all the drum LEDs

write minimum intensity to all drum LEDs

post updating LED event to this FSM

update the next welcoming color

break

case ES_ENTER_GAME: {

set current state to PlayingState_Drums;

clear all the drum LEDs

set the intensity of all the drum LEDs to minimum brightness

post updating LED to this FSM

break

break

case PlayingState_Drums: {

switch(ThisEvent.EventType){

case ES_TIMEOUT: {

if INTERACTION_TIMER

go back to welcoming state

write the next welcoming color to all the drum LEDs

write minimum intensity to all drum LEDs


post updating LED to this FSM

else if NOTE_WINDOW_TIMER

clear all the drum LED strips

set the intensity of all the drums to the minimum intensity

post updating LED to this FSM

break

case ES_NOTE_WINDOW: { // waiting for a drum to be hit

stay in PlayingState_Drums state

light up the active drum to pink

set the intensity of the active drum to 5

post updating LED to this FSM

break

case ES_HIT_INTENSITY_CORRECT_ONLY: {

get the hit intensity of the correct drum

if left drum intensity > 0

light the left drum pink and with hit intensity bucket as the brightness

if bottom drum intensity > 0

light the bottom drum pink and with hit intensity bucket as the brightness

if right bottom drum intensity > 0


light the right drum pink and with hit intensity bucket as the brightness

break

case ES_ENTER_ZEN: {

set current state to ZenState_Drums

set the color of all the drum LEDs to white

set the intensity of all the drum LEDs to 5

post updating LEDs to this FSM

break

case ES_UPDATING_LED: {

set MUX output to the drum we are currently updating

if still updating the LED

post updating LED to this FSM

else if have not updated all the drums

Event parameter is incremented

post updating LED to this FSM

break

break

case ZenState_Drums: {
switch(ThisEvent.EventType){

case ES_UPDATING_LED: {

set MUX output to the drum we are currently updating

if still updating the LED

post updating LED to this FSM

else if have not updated all the drums

Event parameter is incremented

post updating LED to this FSM

break;

case ES_TIMEOUT: {

if ZEN_TIMER

set current state to WelcomingState_Drums

set all the drum LEDs to the next welcoming color

set all the drum LEDs to the minimum intensity

post updating LEDs to this FSM

break

break

return the return event

}
// --------------------------------------------------------------------

This function sets the color and intensity of a drum and updates it

Input: Drum: which drum to light

Color: color to light up the drum

Brightness: intensity of the drum

Output: N/A

// --------------------------------------------------------------------

void LightDrum(Drum, Color, Brightness) {

set all the LEDs on the passed drum to the passed color

set the intensity of the passed drum to the passed intensity

post updating LED to this FSM

// --------------------------------------------------------------------

This function updates the intensity variable

Input: NewIntensity: intensity to set

Output: True if valid intensity

False otherwise

// --------------------------------------------------------------------

bool Update_DrumIntensity(NewIntensity){

return value is true

if invalid intensity

return value is false


else valid intensity

set the minimum brightness to the passed intensity

return the return value

You might also like