/****************************************************************************
 Module
   ByteTransfer.c

 Revision
   2.0.1

 Description
   This is a file for implementing communication command state machine.

 Notes

 History
 When           Who     What/Why
 -------------- ---     --------
 02/16/17 3:33  ra       editing for ME218 project
 02/27/17 09:48 jec      another correction to re-assign both CurrentEvent
                         and ReturnEvent to the result of the During function
                         this eliminates the need for the prior fix and allows
                         the during function to-remap an event that will be
                         processed at a higher level.
 02/20/17 10:14 jec      correction to Run function to correctly assign 
                         ReturnEvent in the situation where a lower level
                         machine consumed an event.
 02/03/16 12:38 jec      updated comments to reflect changes made in '14 & '15
                         converted unsigned char to bool where appropriate
                         spelling changes on true (was True) to match standard
                         removed local var used for debugger visibility in 'C32
                         commented out references to Start & RunLowerLevelSM so
                         that this can compile. 
 02/07/13 21:00 jec      corrections to return variable (should have been
                         ReturnEvent, not CurrentEvent) and several EV_xxx
                         event names that were left over from the old version
 02/08/12 09:56 jec      revisions for the Events and Services Framework Gen2
 02/13/10 14:29 jec      revised Start and run to add new kind of entry function
                         to make implementing history entry cleaner
 02/13/10 12:29 jec      added NewEvent local variable to During function and
                         comments about using either it or Event as the return
 02/11/10 15:54 jec      more revised comments, removing last comment in during
                         function that belongs in the run function
 02/09/10 17:21 jec      updated comments about internal transitions on During funtion
 02/18/09 10:14 jec      removed redundant call to RunLowerlevelSM in EV_Entry
                         processing in During function
 02/20/07 21:37 jec      converted to use enumerated type for events & states
 02/13/05 19:38 jec      added support for self-transitions, reworked
                         to eliminate repeated transition code
 02/11/05 16:54 jec      converted to implment hierarchy explicitly
 02/25/03 10:32 jec      converted to take a passed event parameter
 02/18/99 10:19 jec      built template from MasterMachine.c
 02/14/99 10:34 jec      Began Coding
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
// Basic includes for a program using the Events and Services Framework
#include "ES_Configure.h"
#include "ES_Framework.h"

#include "ByteTransfer.h"
/* 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 "SPI.h"
//I do not think that the lower level SM need the .h file for the top level SM 

/*----------------------------- Module Defines ----------------------------*/
// define constants for the states for this machine
// and any other local defines

#define ENTRY_STATE BTWaiting4EOT
#define BYTE_TIME 2 //2ms

/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine, things like during
   functions, entry & exit functions.They should be functions relevant to the
   behavior of this state machine
*/
static ES_Event_t DuringBTWaiting4EOT( ES_Event_t Event);
static ES_Event_t DuringBTWaiting4Timeout( ES_Event_t Event);

/*---------------------------- Module Variables ---------------------------*/
// everybody needs a state variable, you may need others as well
static ByteTransfer_State_t CurrentState;

uint8_t command2ref = 0x3F; 

/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
 Function
    RunByteTransferSM

 Parameters
   ES_Event_t: the event to process

 Returns
   ES_Event_t: an event to return

 Description
	 Lowest Level of a hierarchical state machine that does actual transfer of bytes to te REF 
 Notes
   uses nested switch/case to implement the machine.
 Author
  R. Adenekan
****************************************************************************/
ES_Event_t RunByteTransferSM( ES_Event_t CurrentEvent )
{
   bool MakeTransition = false;/* we are not making a state transition */
	 //Set the NextState to be the CurrentState
  ByteTransfer_State_t  NextState = CurrentState;
   ES_Event_t EntryEventKind = { ES_ENTRY, 0 };// default to normal entry to new state
   ES_Event_t ReturnEvent = CurrentEvent; // assume we are not consuming event
 
   switch ( CurrentState )
   {
         
       case BTWaiting4EOT:       // If current state BTWating4EOT
       //No During function for BTWaiting4EOT
       CurrentEvent = DuringBTWaiting4EOT(CurrentEvent);
         //process any events
         if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
         {
            switch (CurrentEvent.EventType)
            {
               case ES_EOTREF : //If event is ES_EOTREF
                  NextState = BTWaiting4Timeout; //Set the NextState to BTWaiting4Timeout
                  MakeTransition = true; //mark that we are taking a transition
									//NOTE: The ISR EOT Response is in SPI.c and it stores the Ref Response
                  ReturnEvent.EventType = ES_NO_EVENT;
                  break;
            }
         }
       break;

       case BTWaiting4Timeout:       // If current state is BTWaiting4Timeout
       //No During function for BTWaiting4Timeout
       CurrentEvent = DuringBTWaiting4Timeout(CurrentEvent);
         //process any events
         if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
         {
            switch (CurrentEvent.EventType)
            {
               case (ES_TIMEOUT) : //If event is ES_TIMEOUT
                 if (CurrentEvent.EventParam == BYTE_TIMER)
                 {
                  // restart BYTE_TIME to query itself
                  ES_Timer_InitTimer(BYTE_TIMER,BYTE_TIME);  //reset 100ms timer
									//Change the ReturnEvent to ES_READY2WRITE
                  ReturnEvent.EventType = ES_READY2WRITE;
                  break;
                 }
            }
         }
       break;

    }
    //If we are making a state transition
    if (MakeTransition == true)
    {
       //Execute exit function for current state
       CurrentEvent.EventType = ES_EXIT;
       RunByteTransferSM(CurrentEvent);

       CurrentState = NextState; //Modify state variable

       //   Execute entry function for ByteTransfer state machine
       // this defaults to ES_ENTRY
       RunByteTransferSM(EntryEventKind);
     }
     return(ReturnEvent);
}
/****************************************************************************
 Function
     StartByteTransferSM

 Parameters
     ES_Event_t: the event to process

 Returns
     None

 Description
     Does any required initialization for this state machine
 Notes

 Author
     R. Adenekan
****************************************************************************/
void StartByteTransferSM ( ES_Event_t CurrentEvent )
{
   if ( ES_ENTRY_HISTORY != CurrentEvent.EventType )
   {
        CurrentState = ENTRY_STATE;
   }
   //Run the ByteTransfer state machine
   RunByteTransferSM(CurrentEvent);
}

/****************************************************************************
 Function
     QueryByteTransferSM 

 Parameters
     None

 Returns
     ByteTransferState_t The current state of the ByteTransfer state machine

 Description
     returns the current state of the ByteTransfer state machine
 Notes

 Author
     R. Adenekan
****************************************************************************/
ByteTransfer_State_t QueryTemplateSM ( void )
{
   return(CurrentState);
}

/***************************************************************************
 private functions
 ***************************************************************************/

static ES_Event_t DuringBTWaiting4EOT( ES_Event_t Event)
{
    ES_Event_t ReturnEvent = Event; // assme no re-mapping or comsumption
    // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
    if ( (Event.EventType == ES_ENTRY) ||
         (Event.EventType == ES_ENTRY_HISTORY) )
    {
			//Run the SendData function with the proper command to the REF
      SendData(command2ref);
    }
    else if ( Event.EventType == ES_EXIT )
    {   
    }else
    {=
    }
    return(ReturnEvent);
}

static ES_Event_t DuringBTWaiting4Timeout( ES_Event_t Event)
{
    ES_Event_t ReturnEvent = Event; // assme no re-mapping or comsumption

    // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
    if ( (Event.EventType == ES_ENTRY) ||
         (Event.EventType == ES_ENTRY_HISTORY) )
    {     
    }
    else if ( Event.EventType == ES_EXIT )
    {      
    }else
    {
    }
    return(ReturnEvent);
}