You are on page 1of 11

Clock FSM

Data private to this module:

#define ONE_SEC 1000

#define HALF_SEC (ONE_SEC/2)

#define TWO_SEC (2 * ONE_SEC)

#define FIVE_SEC (5 * ONE_SEC)

#define SIX_SEC (6 * ONE_SEC)

#define TEN_SEC (10 * ONE_SEC)

#define MAX_FADE 10

#define FADE_TIME (ONE_SEC/10)

#define NUM_TIMER_LED 12

static ClockFSMState_t CurrentState;

static uint8_t MyPriority;

static uint8_t LEDIntensity = 1;

static uint8_t FadeIntensity = 1;

static Colors_t NextWelcomingColor;

static uint8_t Clock_idx = 0;

static uint16_t TimeToNextLED = (uint16_t)(49./NUM_TIMER_LED*ONE_SEC);

// --------------------------------------------------------------------
This function initializes the Clock FSM

Input: Priority number of the service

Output: True if initialization is successful

False otherwise

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

bool InitClockFSM(Priority)

create a new event

set my priority

set current state to InitPState_ClockFSM

set up SPI with 1000 SCK and 32 bit transfer width

// set up the MUX pins

set the MUX pins as digital output

set the MUX output to the clock LEDs

clear the clock LEDs

set the clock LEDs to minimum brightness

set the next welcoming color to turquoise

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

This function posts an event to the ClockFSM

Input: Event to be posted

Output: True if post is successful

False otherwise
// --------------------------------------------------------------------

bool PostClockFSM(ThisEvent)

post to this FSM

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

This function returns the state of the Clock FSM

Input: N/A

Output: State of the FSM

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

ClockFSMState_t QueryClockFSM()

return the current state

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

This function runs the Clock FSM

Input: Event that is posted to this FSM

Output: ES_NO_ERROR if there are no errors

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

ES_Event_t RunClockFSM(ThisEvent)

create return event

set return event to no errors

switch on the current state


{

case InitPState_ClockFSM

if ES_INIT

set current state to WelcomeState_ClockFSM

set MUX output to clock LEDs

set the clock LEDs to the next welcoming color

set the clock LEDs to the predefined intensity

set the next welcoming color to purple;

post updating LED to this FSM

break

case WelcomeState_ClockFSM: {

set the clock index to 1

switch on the Event Type

case ES_TIMEOUT: {

set MUX output to clock LEDs

set the clock LEDs to the next welcoming color

set the clock LEDs to the predefined intensity

post updating LEDs to this FSM

if LED_REFRESH_TIMER

update the next welcoming color

break

case ES_UPDATING_LED: {

set MUX output to the clock


if still updating the LED

post updating LEDs to this FSM

else

start a 2 second timer to change welcoming color

break

case ES_IR_COVERED: {

stop the refresh timer

begin the fade timer

set current state to IRCovered_ClockFSM

set MUX output to clock LEDs

set the clock LEDs to gold

set the clock LEDs to UpdateFadeIntensity()

post updating LEDs to this FSM

break

break

case IRCovered_ClockFSM: {

switch(ThisEvent.EventType){

case ES_UPDATING_LED: {

set MUX output to the clock

if still updating the LED


post updating LEDs to this FSM

else

start a 2 second timer to change welcoming color

break

case ES_TIMEOUT: {

if FADE_TIMER

set clock LED intensity to UpdateFadeIntensity()

post fade event to this FSM

start fade timer

break

case ES_IR_UNCOVERED: {

set current state to WelcomeState_ClockFSM

stop the fade timer

reset the fade intensity

set MUX output to clock LEDs

set the clock LEDs to the next welcoming color

set the clock LEDs to the predefined intensity

break

case ES_ENTER_GAME: {

set current state to PlayingGameState_ClockFSM


clear the timer LEDs

post updating LEDs to this FSM

Init time elapsed timer

break

break

case PlayingGameState_ClockFSM: {

switch(ThisEvent.EventType){

case ES_UPDATING_LED: {

set MUX output to the clock

if still updating the LED

post updating LEDs to this FSM

else

start a 2 second timer to change welcoming color

break

case ES_CORRECT_HIT: {

clear the clock LEDs

set the clock LEDs up until the current index to gold

set the clock intensity to 30

post updating LEDs to this FSM

}
break

case ES_ENTER_ZEN: {

set the current state to ZenState_ClockFSM

set the LED intensity to 12

clear the clock LED strip

set the color of all the clock LEDs to white

set the clock LED intensity to the value above

post updating LEDs to this FSM

break

case ES_TIMEOUT: {

if INTERACTION_TIMER

set the current state to WelcomeState_ClockFSM

set MUX output to clock LEDs

set the clock LEDs to the next welcoming color

set the clock LEDs to the predefined intensity

post updating LED to this FSM

else if MOTOR_TIMER

clear the clock LED strip

set the clock LED strip to pink


post updating LEDs to this FSM

else if TIME_ELAPSED_TIMER

set current state to PlayingGameState_ClockFSM

set MUX output to clock LEDs

set clock LEDs until clock index to pink

set clock LED intensity to 10

increment the clock index

if clock index is greater than number of clock LEDs

set clock index to the number of LEDs

post updating LEDs to this FSM

restart time elapsed timer

break

break

case ZenState_ClockFSM: {

switch(ThisEvent.EventType){

case ES_UPDATING_LED: {

set MUX output to the clock

if still updating the LED

post updating LEDs to this FSM


else

start a 2 second timer to change welcoming color

break

case ES_TIMEOUT: {

if ZEN_TIMER

set current state to WelcomeState_ClockFSM

set all the clock LEDs to the next welcoming color

set the clock LEDs intensity to LED intensity

post updating LEDs to this FSM

else if LED_REFRESH_TIMER

set all the clock LEDs to the next welcoming color

set LED intensity to 1

set clock LED intensity to above value

post updating LEDs to this FSM

break

break

return the return event

// --------------------------------------------------------------------
This function

Input: NumLEDs: number of LEDs to light up on the Clock

WhichColor: color to set the LEDs

Output: N/A

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

void SetLEDs(NumLEDs, WhichColor){

if valid number of LEDs to light

loop from 1 to number of LEDs

set that LED to the passed color

You might also like