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

 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)

{

  ANSELBbits.ANSB13 = 0; //disable analog

  TRISBbits.TRISB13 = 1; //set as input

  LastCoinState = PORTBbits.RB13;

}


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

 Function

    CheckButtonEvents

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

bool CheckCoinEvents(void)

{

  bool ReturnVal = false;

  bool CurrentCoinState = PORTBbits.RB13;

  ES_Event_t CoinEvent;

  if (CurrentCoinState != LastCoinState)

  {

    ReturnVal = true;

    if (CurrentCoinState == false)

    {

      CoinEvent.EventType = COIN_INSERT;

      PostMasterService(CoinEvent);

    }

    LastCoinState = CurrentCoinState;

  }

  return ReturnVal;

}


bool CheckLeftButtonEvents(void)

{

  bool ReturnVal = false;

  bool CurrentLeftButtonState = PORTAbits.RA3;

  ES_Event_t LeftButtonEvent;

  if (CurrentLeftButtonState != LastLeftButtonState)

  {

    ReturnVal = true;

    if (CurrentLeftButtonState == false)

    {

      LeftButtonEvent.EventType = RAW_LEFT_DOWN;

      PostDebouncingService(LeftButtonEvent);

    }

    else if (CurrentLeftButtonState == true)

    {

      LeftButtonEvent.EventType = RAW_LEFT_UP;

      PostDebouncingService(LeftButtonEvent);

    }

    LastLeftButtonState = CurrentLeftButtonState;

  }

  return ReturnVal;

}


bool CheckRightButtonEvents(void)

{

  bool ReturnVal = false;

  bool CurrentRightButtonState = PORTAbits.RA4;

  ES_Event_t RightButtonEvent;

  if (CurrentRightButtonState != LastRightButtonState)

  {

    ReturnVal = true;

    if (CurrentRightButtonState == false)

    {

      RightButtonEvent.EventType = RAW_RIGHT_DOWN;

      PostDebouncingService(RightButtonEvent);

    }

    else if (CurrentRightButtonState == true)

    {

      RightButtonEvent.EventType = RAW_RIGHT_UP;

      PostDebouncingService(RightButtonEvent);

    }

    LastRightButtonState = CurrentRightButtonState;

  }

  return ReturnVal;

}