/****************************************************************************
 Module
   Gameplay_GameTasks_DumpTrashSM.c

 Revision
   2.0.1

 Description
   This is a sub state machine (3rd level) called DumpTrash within the
   GameTasks level within the Gameplay module for ME 218B project for Team 3.
   Implements a hierarchical state machine under the Gen2 Events and
   Services Framework.

 Notes


 History
 When           Who     What/Why
 -------------- ---     --------
 02/23/19       bibit   converted from template to project file
 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 this module's own header file
#include "Gameplay_GameTasks_DumpTrashSM.h"

// include the highest level header file for this HSM
#include "GameplayHSM.h"

// other modules
#include "MotorSM.h"
#include "ServoModule.h"
#include "BeaconSense.h"
#include "GameControlService.h"
#include "Gameplay_GameTasksSM.h"

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

#define ENTRY_STATE T_AligningWithRecycling

#define ReleaseTrashTime  5000
#define SquaringUpTime    2000
#define BackingUpTime     1000
#define ReAlignTime       2500
#define AngleAdjustmentTime 75

/*---------------------------- 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 DuringT_AligningWithRecycling(ES_Event_t Event);
static ES_Event_t DuringT_DrivingTowardsRecycling(ES_Event_t Event);
static ES_Event_t DuringT_BackingUp(ES_Event_t Event);
static ES_Event_t DuringT_AligningWithLandfill(ES_Event_t Event);
static ES_Event_t DuringT_DrivingTowardsLandfill(ES_Event_t Event);
static ES_Event_t DuringT_SquaringWithWallCW(ES_Event_t Event);
static ES_Event_t DuringT_SquaringWithWallCCW(ES_Event_t Event);
static ES_Event_t DuringReleasingTrash(ES_Event_t Event);
static ES_Event_t DuringT_BackingUp2(ES_Event_t Event);

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

/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
 Function
    RunDumpTrashSM

 Parameters
   ES_Event_t: the event to process

 Returns
   ES_Event_t: an event to return

 Description
   run function for the DumpTrash state machine

 Notes
   uses nested switch/case to implement the machine.

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
ES_Event_t RunDumpTrashSM(ES_Event_t CurrentEvent)
{
  bool              MakeTransition = false; // making a state transition?
  DumpTrashState_t  NextState = CurrentState;

  // default to normal entry to new state
  ES_Event_t  EntryEventKind = { ES_ENTRY, 0 };
  ES_Event_t  ReturnEvent = CurrentEvent; // assume we don't consume event

  switch (CurrentState)
  {
    case T_AligningWithRecycling:      // current state AligningWithRecycling
    {
      // Execute During function for AligningWithRecycling. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringT_AligningWithRecycling(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case BEACON_DETECTED: // if we detected the correct beacon in the
                                // rear detector
          {
            // check that it's the correct beacon (rear):
            if (CurrentEvent.EventParam == REAR)
            {
              // post MOTOR_COMMAND with EventParam BACKWARDS
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = BACKWARDS;
              PostMotorSM(PostEvent);

              // we want to move to T_DrivingTowardsRecycling state
              NextState = T_DrivingTowardsRecycling;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter T_DrivingTowardsRecycling with history,
              // so will leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for AligningWithRecycling

    case T_DrivingTowardsRecycling:  // current state DrivingTowardsRecycling
    {
      // Execute During function for DrivingTowardsRecycling. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringT_DrivingTowardsRecycling(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case REAR_LIMIT_HIT_L: // if we hit the wall in rear left corner
          {
            // we want to back away from the wall (will be forward since we
            // approach beacons by driving backwards)
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = FORWARD;
            PostMotorSM(PostEvent);

            // start a backing up timer
            ES_Timer_InitTimer(GameplayTimer, BackingUpTime);

            // we want to move to T_BackingUp state
            NextState = T_BackingUp;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter T_BackingUp with history, so will
            // leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case REAR_LIMIT_HIT_R: // if we hit the wall in rear right corner
          {
            // we want to back away from the wall (will be forward since we
            // approach beacons by driving backwards)
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = FORWARD;
            PostMotorSM(PostEvent);

            // start a backing up timer
            ES_Timer_InitTimer(GameplayTimer, BackingUpTime);

            // we want to move to T_BackingUp state
            NextState = T_BackingUp;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter T_BackingUp with history, so will
            // leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for DrivingTowardsRecycling

    case T_BackingUp:    // If current state is BackingUp
    {
      // Execute During function for BackingUp. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringT_BackingUp(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case ES_TIMEOUT: // if our backing up timer ran out
          {
            // make sure it is the right timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // turn CW so we can align with the beacon
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = CW;
              PostMotorSM(PostEvent);

              // set frequency to look for our assigned landfill
              SetBeaconRearFreq(GetOurLandfillFreq());

              // enable interrupts for rear beacon sensor
              EnableRearBeaconInterrupts();

              // we want to move to T_AligningWithLandfill state
              NextState = T_AligningWithLandfill;

              // we are making a transition
              MakeTransition = true;

              // we do not want to enter the other states with history, so
              // will leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for BackingUp

    case T_AligningWithLandfill:      // current state AligningWithLandfill
    {
      // Execute During function for AligningWithLandfill. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringT_AligningWithLandfill(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case BEACON_DETECTED: // if we detected the correct beacon in the
                                // rear detector
          {
            // check that it's the correct beacon detector (rear):
            if (CurrentEvent.EventParam == REAR)
            {
              // we want to turn a litle bit more since we are able to see
              // the beacon within a wide angle.  so start an angle
              // adjustment timer
              ES_Timer_InitTimer(GameplayTimer, AngleAdjustmentTime);

              // make sure the motors are still turning clockwise
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = CW;
              PostMotorSM(PostEvent);

              // we do not want to change states

              // we are not transitioning out of this state
              //MakeTransition = true;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;

          case ES_TIMEOUT:  // if our re-adjustment timer timed out
          {
            // check that it is the correct timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // start a timer for us to re-align with beacon after a bit
              ES_Timer_InitTimer(GameplayTimer, ReAlignTime);

              // post MOTOR_COMMAND with EventParam BACKWARDS
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = BACKWARDS;
              PostMotorSM(PostEvent);

              // we want to move to T_DrivingTowardsLandfill state
              NextState = T_DrivingTowardsLandfill;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter T_DrivingTowardsLandfill with
              // history, so will leave the default to ES_ENTRY (no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for AligningWithLandfill

    case T_DrivingTowardsLandfill:  // current state DrivingTowardsLandfill
    {
      // Execute During function for DrivingTowardsLandfill. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringT_DrivingTowardsLandfill(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case REAR_LIMIT_HIT_L: // if we hit the wall in rear left corner
          {
            // post MOTOR_COMMAND with EventParam LEFT_STOP_RIGHT_BACK
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = LEFT_STOP_RIGHT_BACK;
            PostMotorSM(PostEvent);

            // start a timer in case the other limit switch never hits
            ES_Timer_InitTimer(GameplayTimer, SquaringUpTime);

            // we want to move to T_SquaringWithWallCCW state
            NextState = T_SquaringWithWallCCW;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter T_SquaringWithWallCCW with history, so
            // will leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case REAR_LIMIT_HIT_R: // if we hit the wall in rear right corner
          {
            // post MOTOR_COMMAND with EventParam RIGHT_STOP_LEFT_BACK
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = RIGHT_STOP_LEFT_BACK;
            PostMotorSM(PostEvent);

            // start a timer in case the other limit switch never hits
            ES_Timer_InitTimer(GameplayTimer, SquaringUpTime);

            // we want to move to T_SquaringWithWallCW state
            NextState = T_SquaringWithWallCW;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter T_SquaringWithWallCW with history, so
            // will leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          // check if it is time for us to re-align bc we've driven a bit
          case ES_TIMEOUT:
          {
            // check it is from the correct timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // post MOTOR_COMMAND with EventParam CW
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = CW;
              PostMotorSM(PostEvent);

              // enable interrupts for rear beacon sensor
              EnableRearBeaconInterrupts();

              // we want to go back to T_AligningWithLandfill state
              NextState = T_AligningWithLandfill;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter AligningWithLandfill with history,
              // so will leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for DrivingTowardsLandfill

    case T_SquaringWithWallCW:      // If current state is SquaringWithWallCW
    {
      // Execute During function for SquaringWithWallCW. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringT_SquaringWithWallCW(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case REAR_LIMIT_HIT_L: // if we hit the wall in rear left corner
          {
            // post MOTOR_COMMAND with EventParam STOP
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = STOP;
            PostMotorSM(PostEvent);

            // set trash servo to open position
            OpenTrashChute();

            // set timer to allow balls to go into landfill
            ES_Timer_InitTimer(GameplayTimer, ReleaseTrashTime);

            // we want to move to ReleasingTrash state
            NextState = ReleasingTrash;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter ReleasingTrash with
            // history, so will leave the default to ES_ENTRY (make no
            // change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case ES_TIMEOUT: // if our squaring up timer timed out
          {
            // check that it is the correct timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // post MOTOR_COMMAND with EventParam STOP
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = STOP;
              PostMotorSM(PostEvent);

              // set trash servo to open position
              OpenTrashChute();

              // set timer to allow balls to go into landfill
              ES_Timer_InitTimer(GameplayTimer, ReleaseTrashTime);

              // we want to move to ReleasingTrash state
              NextState = ReleasingTrash;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter ReleasingTrash with
              // history, so will leave the default to ES_ENTRY (make no
              // change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for SquaringWithWallCW

    case T_SquaringWithWallCCW:    // If current state is SquaringWithWallCCW
    {
      // Execute During function for SquaringWithWallCCW. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringT_SquaringWithWallCCW(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case REAR_LIMIT_HIT_R: // if we hit the wall in rear right corner
          {
            // post MOTOR_COMMAND with EventParam STOP
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = STOP;
            PostMotorSM(PostEvent);

            // set trash servo to open position
            OpenTrashChute();

            // set timer to allow balls to go into landfill
            ES_Timer_InitTimer(GameplayTimer, ReleaseTrashTime);

            // we want to move to ReleasingTrash state
            NextState = ReleasingTrash;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter ReleasingTrash with
            // history, so will leave the default to ES_ENTRY (make no
            // change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case ES_TIMEOUT: // if our squaring up timer timed out
          {
            // check that it is the correct timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // post MOTOR_COMMAND with EventParam STOP
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = STOP;
              PostMotorSM(PostEvent);

              // set trash servo to open position
              OpenTrashChute();

              // set timer to allow balls to go into landfill
              ES_Timer_InitTimer(GameplayTimer, ReleaseTrashTime);

              // we want to move to ReleasingTrash state
              NextState = ReleasingTrash;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter ReleasingTrash with
              // history, so will leave the default to ES_ENTRY (make no
              // change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for SquaringWithWallCCW

    case ReleasingTrash:    // If current state is ReleasingTrash
    {
      // Execute During function for ReleasingTrash. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringReleasingTrash(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case ES_TIMEOUT:     // if our timer for dispensing balls timed out
          {
            // check that it's the correct timer (GameplayTimer):
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // set trash servo to closed position
              CloseTrashChute();

              // back away from the wall (will be forward since we approach
              // beacons by driving backwards)
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = FORWARD;
              PostMotorSM(PostEvent);

              // start a backing up timer
              ES_Timer_InitTimer(GameplayTimer, BackingUpTime);

              // set next state to BackingUp
              NextState = T_BackingUp;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter the other states with history, so will
              // leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for ReleasingTrash

    case T_BackingUp2:    // If current state is BackingUp2
    {
      // Execute During function for BackingUp2. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringT_BackingUp2(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case ES_TIMEOUT: // if our backing up timer ran out
          {
            // make sure it is the right timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // stop our motors
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = STOP;
              PostMotorSM(PostEvent);

              // post TRASH_EMPTY to Gameplay
              PostEvent.EventType = TRASH_EMPTY;
              PostGameplayMasterSM(PostEvent);

              // we don't need to change states

              // we are not making a transition
              //MakeTransition = true;

              // we do not want to enter the other states with history, so will
              // leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for BackingUp
  }

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

    CurrentState = NextState; // Modify state variable

    // Execute entry function for new state
    // this defaults to ES_ENTRY
    RunDumpTrashSM(EntryEventKind);
  }
  return ReturnEvent;
}

/****************************************************************************
 Function
     StartDumpTrashSM

 Parameters
     None

 Returns
     None

 Description
     Does any required initialization for this state machine

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
void StartDumpTrashSM(ES_Event_t CurrentEvent)
{
  // to implement entry to a history state or directly to a substate
  // you can modify the initialization of the CurrentState variable
  // otherwise just start in the entry state every time the state machine
  // is started
  if (CurrentEvent.EventType != ES_ENTRY_HISTORY)
  {
    CurrentState = ENTRY_STATE;
  }
  // call the entry function (if any) for the ENTRY_STATE
  RunDumpTrashSM(CurrentEvent);
}

/****************************************************************************
 Function
     QueryDumpTrashSM

 Parameters
     None

 Returns
     DumpTrashState_t The current state of the state machine

 Description
     returns the current state of the GameTasks state machine

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
DumpTrashState_t QueryDumpTrashSM(void)
{
  return CurrentState;
}

/***************************************************************************
 private functions
 ***************************************************************************/
/****************************************************************************
 Function
     DuringT_AligningWithRecycling

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the AligningWithRecycling state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringT_AligningWithRecycling(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into AligningWithRecycling
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from AligningWithRecycling
  }

  // this state doesn't have a lower state machine to run

  // return either Event, if you don't want to allow the lower level machine
  // to remap the current event, or ReturnEvent if you do want to allow it.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringT_DrivingTowardsRecycling

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the DrivingTowardsRecycling state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringT_DrivingTowardsRecycling(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into DrivingTowardsRecycling
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from DrivingTowardsRecycling
  }

  // this state doesn't have a lower state machine to run

  // return either Event, if you don't want to allow the lower level machine
  // to remap the current event, or ReturnEvent if you do want to allow it.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringT_BackingUp

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the BackingUp state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 3/05/2019
****************************************************************************/
static ES_Event_t DuringT_BackingUp(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into BackingUp
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from BackingUp
  }

  // this state doesn't have a lower state machine to run

  // return either Event, if you don't want to allow the lower level machine
  // to remap the current event, or ReturnEvent if you do want to allow it.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringT_AligningWithLandfill

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the AligningWithLandfill state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringT_AligningWithLandfill(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into AligningWithLandfill
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from AligningWithLandfill
  }

  // this state doesn't have a lower state machine to run

  // return either Event, if you don't want to allow the lower level machine
  // to remap the current event, or ReturnEvent if you do want to allow it.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringT_DrivingTowardsLandfill

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the DrivingTowardsLandfill state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringT_DrivingTowardsLandfill(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into DrivingTowardsLandfill
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from DrivingTowardsLandfill
  }

  // this state doesn't have a lower state machine to run

  // return either Event, if you don't want to allow the lower level machine
  // to remap the current event, or ReturnEvent if you do want to allow it.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringT_SquaringWithWallCW

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the SquaringWithWallCW state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringT_SquaringWithWallCW(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into SquaringWithWallCW
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from SquaringWithWallCW
  }

  // this state doesn't have a lower state machine to run

  // return either Event, if you don't want to allow the lower level machine
  // to remap the current event, or ReturnEvent if you do want to allow it.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringT_SquaringWithWallCCW

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the SquaringWithWallCCW state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringT_SquaringWithWallCCW(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into SquaringWithWallCCW
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from SquaringWithWallCCW
  }

  // this state doesn't have a lower state machine to run

  // return either Event, if you don't want to allow the lower level machine
  // to remap the current event, or ReturnEvent if you do want to allow it.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringReleasingTrash

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the ReleasingTrash state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringReleasingTrash(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into ReleasingTrash
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from ReleasingTrash
  }

  // this state doesn't have a lower state machine to run

  // return either Event, if you don't want to allow the lower level machine
  // to remap the current event, or ReturnEvent if you do want to allow it.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringT_BackingUp2

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the BackingUp2 state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 3/05/2019
****************************************************************************/
static ES_Event_t DuringT_BackingUp2(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into BackingUp2
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from BackingUp2
  }

  // this state doesn't have a lower state machine to run

  // return either Event, if you don't want to allow the lower level machine
  // to remap the current event, or ReturnEvent if you do want to allow it.
  return ReturnEvent;
}

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