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

 Module

   TemplateFSM.c


 Revision

   1.0.1


 Description

   This is a template file for implementing flat state machines under the

   Gen2 Events and Services Framework.


 Notes


 History

 When           Who     What/Why

 -------------- ---     --------

 01/15/12 11:12 jec      revisions for Gen2 framework

 11/07/11 11:26 jec      made the queue static

 10/30/11 17:59 jec      fixed references to CurrentEvent in RunTemplateSM()

 10/23/11 18:20 jec      began conversion from SMTemplate.c (02/20/07 rev)

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

/*----------------------------- 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 "LEDService.h"

#include "PIC32_SPI_HAL.h"

#include "ES_Configure.h"

#include "ES_Framework.h"

#include "TemplateFSM.h"

#include "DM_Display.h"

#include "FontStuff.h"

#include "ES_Events.h" // needed for definition of REENTRANT

#include "dbprintf.h"


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

#define ONE_SEC 1000

#define HALF_SEC (ONE_SEC /2)


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

/* prototypes for private functions for this machine.They should be functions

   relevant to the behavior of this state machine

*/


/*---------------------------- 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

static LEDServiceState_t CurrentState;


// with the introduction of Gen2, we need a module level Priority var as well

static uint8_t MyPriority;


static uint32_t col[] = {0x00000001, 0x00000001 << 1, 0x00000001 << 2, 0x00000001 << 3, 0x00000001 << 4, 0x00000001 << 5, 0x00000001 << 6, 0x00000001 << 7, 0x00000001 << 8,

                         0x00000001 << 9, 0x00000001 << 10, 0x00000001 << 11, 0x00000001 << 12, 0x00000001 << 13, 0x00000001 << 14, 0x00000001 << 15, 0x00000001 << 16,

                         0x00000001 << 17, 0x00000001 << 18, 0x00000001 << 19, 0x00000001 << 20, 0x00000001 << 21, 0x00000001 << 22, 0x00000001 << 23, 0x00000001 << 24,

                         0x00000001 << 25, 0x00000001 << 26, 0x00000001 << 27, 0x00000001 << 28, 0x00000001 << 29, 0x00000001 << 30, 0x00000001 << 31};

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

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

 Function

     InitTemplateFSM


 Parameters

     uint8_t : the priorty of this service


 Returns

     bool, false if error in initialization, true otherwise


 Description

     Saves away the priority, sets up the initial transition and does any

     other required initialization for this state machine

 Notes


 Author

     J. Edward Carryer, 10/23/11, 18:55

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

bool InitLEDService(uint8_t Priority)

{

    Declare a local variable ThisEvent of type ES_Event_t

    Initialize SPI module parameters:

        - Set module to SPI1

        - Set phase to SPI_SMP_MID


    Configure SPI settings:

        - Disable SPI for module

        - Apply basic configuration

        - Set leader mode with specified phase

        - Set bit time to 10000

        - Map SS and SD outputs to specific pins

        - Set clock idle state to low

        - Set active edge to first edge

        - Set transfer width to 16 bits

        - Enable enhanced buffer

        - Enable SPI for the module


    Clear any pending interrupt flag


    Initialize display module:

        - While DM_TakeInitDisplayStep() is not complete, wait

        - clear display buffer

        - While DM_TakeDisplayUpdateStep() is not complete, wait


    Store Priority in MyPriority

    Set CurrentState to InitState


    Create an initial transition event:

        - Set ThisEvent.EventType to ES_INIT

        - Post the event to the service using MyPriority


    If the event was posted successfully:

        Return true

    Otherwise:

        Return false

}


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

 Function

     PostTemplateFSM


 Parameters

     EF_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

     J. Edward Carryer, 10/23/11, 19:25

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

bool PostLEDService(ES_Event_t ThisEvent)

{

  return ES_PostToService(MyPriority, ThisEvent);

}


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

 Function

    RunTemplateFSM


 Parameters

   ES_Event_t : the event to process


 Returns

   ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise


 Description

   add your description here

 Notes

   uses nested switch/case to implement the machine.

 Author

   J. Edward Carryer, 01/15/12, 15:23

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

ES_Event_t RunLEDService(ES_Event_t ThisEvent)

{

    Declare ReturnEvent with EventType set to ES_NO_EVENT


    Switch on CurrentState:

        

        Case InitState:

            If ThisEvent is ES_INIT:

                Set CurrentState to Waiting

                clear LED buffer

                Set CurrentState to Updating

                Set ThisEvent to ES_DISPLAY_UPDATE_STEP

                Post ThisEvent to LED service


        Case Waiting:

            Switch on ThisEvent.EventType:


                Case ES_NEW_KEY:

                    Scroll display buffer by 4 units

                    Add character from ThisEvent.EventParam to the display buffer

                    Set CurrentState to Updating

                    Set ThisEvent to ES_DISPLAY_UPDATE_STEP

                    Post ThisEvent to LED service


                Case NEW_CREW_POSITION:

                    For each row from 0 to 7:

                        Update display buffer row with column data corresponding to ThisEvent.EventParam

                    Set CurrentState to Updating

                    Set ThisEvent.EventType to ES_DISPLAY_UPDATE_STEP

                    Post ThisEvent to LED service


                Case GAME_START:

                    Set CurrentState to InitPState

                    Set ThisEvent.EventType to ES_INIT

                    Post ThisEvent to LED service


                Default:

                    Do nothing


        Case Updating:

            If ThisEvent is ES_DISPLAY_UPDATE_STEP:

                If DM_TakeDisplayUpdateStep() is not complete:

                    Post ThisEvent to LED service

                Else:

                    Set CurrentState to Waiting


        Default:

            Do nothing


    Return ReturnEvent


}


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

 Function

     QueryTemplateSM


 Parameters

     None


 Returns

     TemplateState_t The current state of the Template state machine


 Description

     returns the current state of the Template state machine

 Notes


 Author

     J. Edward Carryer, 10/23/11, 19:21

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

TemplateState_t QueryTemplateFSM(void)

{

  return CurrentState;

}


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

 private functions

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