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

 Module

   DebouncingService.c


 Revision

   1.0.1


 Description

   This is a template file for implementing a simple service under the

   Gen2 Events and Services Framework.


 Notes


 History

 When           Who     What/Why

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

 01/16/12 09:58 jec      began conversion from TemplateFSM.c

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

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

*/

//This Module

#include "DebouncingService.h"


#include "ES_Configure.h"

#include "ES_Framework.h"

#include "dbprintf.h"

#include "CrewService.h"

#include "LEDService.h"


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

#define ONE_SEC 1000

#define DEBOUNCING_SEC (ONE_SEC /10)

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

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

   relevant to the behavior of this service

*/


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

// with the introduction of Gen2, we need a module level Priority variable

static uint8_t MyPriority;

DebouncingState_t CurrentLeftState = Debounce_Waiting;

DebouncingState_t CurrentRightState = Debounce_Waiting;

DebouncingState_t NextRightState = Debounce_Waiting;

DebouncingState_t NextLeftState = Debounce_Waiting;


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

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

 Function

     InitDebouncingService


 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

     J. Edward Carryer, 01/16/12, 10:00

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

bool InitDebouncingService(uint8_t Priority)

{

    Declare ThisEvent as ES_Event_t

    Set MyPriority to Priority


    Set ThisEvent.EventType to ES_INIT

    If ES_PostToService(MyPriority, ThisEvent) returns true:

        Return true

    Else:

        Return false

    EndIf

}


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

 Function

     PostDebouncingService


 Parameters

     EF_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

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

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

bool PostDebouncingService(ES_Event_t ThisEvent)

{

  return ES_PostToService(MyPriority, ThisEvent);

}


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

 Function

    RunDebouncingService


 Parameters

   ES_Event_t : the event to process


 Returns

   ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise


 Description

   add your description here

 Notes


 Author

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

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

ES_Event_t RunDebouncingService(ES_Event_t ThisEvent)

{

    Declare ReturnEvent as ES_Event_t

    Set ReturnEvent to ES_NO_EVENT (assume no errors)

    Declare RightDBEvent and LeftDBEvent as ES_Event_t


    /********** Right Button Debouncing **********/


    Switch CurrentRightState:

        Case Debounce_Waiting:

            If ThisEvent is RAW_RIGHT_DOWN:

                Set NextRightState to Raw_Right_Down

                Start RIGHT_DEBOUNCING_TIMER with DEBOUNCING_SEC

            ElseIf ThisEvent is RAW_RIGHT_UP:

                Set NextRightState to Raw_Right_Up

                Start RIGHT_DEBOUNCING_TIMER with DEBOUNCING_SEC

            EndIf

        EndCase


        Case Raw_Right_Down:

            If ThisEvent.EventType is RAW_RIGHT_UP:

                Set NextRightState to Debounce_Waiting

            ElseIf ThisEvent is ES_TIMEOUT AND Timer is RIGHT_DEBOUNCING_TIMER:

                Set NextRightState to Debounce_Waiting

                Set RightDBEvent to RIGHT_DOWN

                Post RightDBEvent to CrewService and LEDService

            EndIf

        EndCase


        Case Raw_Right_Up:

            If ThisEvent is RAW_RIGHT_DOWN:

                Set NextRightState to Debounce_Waiting

            ElseIf ThisEvent is ES_TIMEOUT AND Timer is RIGHT_DEBOUNCING_TIMER:

                Set NextRightState to Debounce_Waiting

                Set RightDBEvent to RIGHT_UP

                Post RightDBEvent to CrewService

            EndIf

        EndCase


    EndSwitch


    /********** Left Button Debouncing **********/


    Switch CurrentLeftState:

        Case Debounce_Waiting:

            If ThisEvent is RAW_LEFT_DOWN:

                Set NextLeftState to Raw_Left_Down

                Start LEFT_DEBOUNCING_TIMER with DEBOUNCING_SEC

            ElseIf ThisEvent is RAW_LEFT_UP:

                Set NextLeftState to Raw_Left_Up

                Start LEFT_DEBOUNCING_TIMER with DEBOUNCING_SEC

            EndIf

        EndCase


        Case Raw_Left_Down:

            If ThisEvent is RAW_LEFT_UP:

                Set NextLeftState to Debounce_Waiting

            ElseIf ThisEvent is ES_TIMEOUT AND Timer is LEFT_DEBOUNCING_TIMER:

                Set NextLeftState to Debounce_Waiting

                Set LeftDBEvent to LEFT_DOWN

                Post LeftDBEvent to CrewService and LEDService

            EndIf

        EndCase


        Case Raw_Left_Up:

            If ThisEvent.EventType is RAW_LEFT_DOWN:

                Set NextLeftState to Debounce_Waiting

            ElseIf ThisEvent is ES_TIMEOUT AND Timer is LEFT_DEBOUNCING_TIMER:

                Set NextLeftState to Debounce_Waiting

                Set LeftDBEvent.EventType to LEFT_UP

                Post LeftDBEvent to CrewService and LEDService

            EndIf

        EndCase


    EndSwitch


    Update CurrentRightState to NextRightState

    Update CurrentLeftState to NextLeftState


    Return ReturnEvent

}


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

 private functions

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



/*------------------------------- Footnotes -------------------------------*/

/*------------------------------ End of file ------------------------------*/