/****************************************************************************
Module
   Idle.c
 Description
   This is the TODO service for the Test Harness under the
   Gen2 Events and Services Framework.
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for the framework and this service
*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"
#include   "inc/hw_memmap.h"
#include   "inc/hw_types.h"
#include   "inc/hw_gpio.h"
#include   "inc/hw_sysctl.h"
#include   "driverlib/sysctl.h"
#include   "driverlib/pin_map.h"    // Define PART_TM4C123GH6PM in project
#include   "driverlib/gpio.h"
#include   "termio.h"
#include   "Attack.h"
#include   "LEDRegisterWrite.h"
#include   "AudioRegisterWrite.h"
#include   "PWM16Tiva.h"
#include   "ADMulti.h"
#include   "TRex.h"
#include   "MusicSequence.h"
#include   "Idle.h"
/*----------------------------- Module Defines ----------------------------*/
// these times assume a 1.000mS/tick timing
#define ONE_SEC 976
#define HALF_SEC (ONE_SEC / 2)
#define TWO_SEC (ONE_SEC * 2)
#define FIVE_SEC (ONE_SEC * 5)
#define IDLE_TIME (ONE_SEC * 30)
/*---------------------------- 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 consts want to control type
// just static
static uint8_t MyPriority;
static IdleState_t CurrentState = InitializeIdle;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
 Function
     InitIdle
 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
****************************************************************************/
bool InitIdle(uint8_t Priority)
{
  ES_Event ThisEvent;
     MyPriority = Priority;
     //Put us into the initial pseudo-state to set up for the initial transition
     CurrentState = InitializeIdle;
     // post the initial transition event
     ThisEvent.EventType = ES_INIT;
     if (ES_PostToService(MyPriority, ThisEvent) == true)
     {
       return true;
     }
     else
     {
       return false;
     }
}
/****************************************************************************
 Function
     PostIdle
    Parameters
        EF_Event 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
****************************************************************************/
bool PostIdle(ES_Event ThisEvent)
{
  return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
 Function
    RunIdle
    Parameters
      ES_Event : the event to process
    Returns
      ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
    Description
      Resets the system if idle for 30sec
****************************************************************************/
ES_Event RunIdle(ES_Event ThisEvent)
{
  ES_Event ReturnEvent;
  ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
     switch (CurrentState)
     {
       case InitializeIdle:
       {
         if (ThisEvent.EventType == ES_INIT)
         {
           CurrentState = Interacting;
         }
       }
       break;
      case Interacting:
      {
        if ((ThisEvent.EventType == CORRECT_NOTE) ||
            (ThisEvent.EventType == MOVE_DOWN) || (ThisEvent.EventType == MOVE_UP) ||
            (ThisEvent.EventType == FULLY_DOWN) || (ThisEvent.EventType == FULLY_UP)
||
             (ThisEvent.EventType == CAR_PLACED))
        {
            // start/reset idle timer
            ES_Timer_InitTimer(IdleTimer, IDLE_TIME);
      }
      else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
IdleTimer))
      {
        ES_Event NewEvent;
        NewEvent.EventType = EARLY_RESET;
        ES_PostAll(NewEvent);
        CurrentState = Idling;
      }
      else if ((ThisEvent.EventType == HEADPHONES_RESET) ||
           ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
MainTimer)))
      {
        ES_Timer_StopTimer(IdleTimer);
        CurrentState = Idling;
      }
    }
    break;
      case Idling:
      {
        if (ThisEvent.EventType == CAR_PLACED)
        {
          // start idle timer
          ES_Timer_InitTimer(IdleTimer, IDLE_TIME);
          CurrentState = Interacting;
        }
      }
      break;
     }
     return ReturnEvent;
}
/*------------------------------ End of file ------------------------------*/