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

 Module

   ButtonCkecker.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

*/

// This module

#include "ButtonChecker.h"


// Hardware

#include <xc.h>

//#include <proc/p32mx170f256b.h>


// Event & Services Framework

#include "ES_Configure.h"

#include "ES_Framework.h"

#include "dbprintf.h"

#include "DM_Display.h"

// #include "PIC32_SPI_HAL.h"

#include "CrewService.h"

#include "MasterService.h"

#include "DebouncingService.h"


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


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



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

static uint8_t MyPriority;

static bool LastCoinState;

static bool LastLeftButtonState = true;

static bool LastRightButtonState = true;

static bool DebouncingFlag = 0;


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

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

 Function

     InitMorseElementsService


 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 InitButtonChecker(void)

{

    Disable analog functionality for pin RB13

    Set pin RB13 as input

    Set LastCoinState to the current state of pin RB13

}


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

 Function

    CheckButtonEvents

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

bool CheckCoinEvents(void)

{

    Initialize ReturnVal to false

    Set CurrentCoinState to the state of pin RB13

    Declare CoinEvent as ES_Event_t


    If CurrentCoinState is not equal to LastCoinState:

        Set ReturnVal to true

        If CurrentCoinState is false:

            Set CoinEvent to COIN_INSERT

            Post CoinEvent to MasterService

        EndIf

        Update LastCoinState to CurrentCoinState

    EndIf


    Return ReturnVal

}


bool CheckLeftButtonEvents(void)

{

    Initialize ReturnVal to false

    Set CurrentLeftButtonState to the state of pin RA3

    Declare LeftButtonEvent as ES_Event_t


    If CurrentLeftButtonState is not equal to LastLeftButtonState:

        Set ReturnVal to true

        If CurrentLeftButtonState is false:

            Set LeftButtonEvent to RAW_LEFT_DOWN

            Post LeftButtonEvent to DebouncingService

        ElseIf CurrentLeftButtonState is true:

            Set LeftButtonEvent to RAW_LEFT_UP

            Post LeftButtonEvent to DebouncingService

        EndIf

        Update LastLeftButtonState to CurrentLeftButtonState

    EndIf


    Return ReturnVal

}


bool CheckRightButtonEvents(void)

{

    Initialize ReturnVal to false

    Set CurrentRightButtonState to the state of pin RA4

    Declare RightButtonEvent as ES_Event_t


    If CurrentRightButtonState is not equal to LastRightButtonState:

        Set ReturnVal to true

        If CurrentRightButtonState is false:

            Set RightButtonEvent to RAW_RIGHT_DOWN

            Post RightButtonEvent to DebouncingService

        ElseIf CurrentRightButtonState is true:

            Set RightButtonEvent to RAW_RIGHT_UP

            Post RightButtonEvent to DebouncingService

        EndIf

        Update LastRightButtonState to CurrentRightButtonState

    EndIf


    Return ReturnVal

}