EventChecker.c

/****************************************************************************
 Module
     Event_Checker.c
 
 Description
     Contains the Event Checker for the main state machine
 
 Notes
 
 
 History
 When           Who     What/Why
 -------------- ---     --------
                NRM
****************************************************************************/
 
/*----------------------------- Include Files -----------------------------*/
#include <hidef.h>      /* common defines and macros */
#include <mc9s12e128.h>     /* derivative information */
#include <stdio.h>
#include <S12E128bits.h>
#include "ADS12.h"
#include "SCI.h"
#include "HC_Controller.h"
#include "EventChecker.h"
 
#pragma LINK_INFO DERIVATIVE "SampleS12"
/*----------------------------- Module Defines ----------------------------*/
#define _OFF_ 0
#define _ON_  1
#define _SET_ 1
/*---------------------------- Module Functions ---------------------------*/
Event_t Check_Events(void);
static char QueryCurrent_Switch_State(void);
/*---------------------------- Module Variables ---------------------------*/
static unsigned char Previous_Switch_State = _OFF_;
static unsigned char Current_Switch_State = _OFF_;
static Event_t PreviousEvent = NO_EVENT;
Event_t CurrentEvent = NO_EVENT;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
 Function
     Check_Events()
 
 Parameters
 
 
 Returns
     The event in Event_t type
 
 Description
 
 Notes
 
 
 Author
     Nick Musser
****************************************************************************/
Event_t Check_Events(void)
{
  char KeyStroke;
  char Key_Switch = QueryCurrent_Switch_State();
  Event_t CurrentEvent = NO_EVENT;
 
 
  //Test State machine with these manual (keyboard hit triggered) events
    if ( kbhit() != 0)       // there was a key pressed
    {
        KeyStroke = getchar();
        switch ( toupper(KeyStroke))
        {
            case 'A' : CurrentEvent = SWITCH_ON; break;
            case 'B' : CurrentEvent = CIPHER_RECEIVED; break;
            case 'C' : CurrentEvent = GO; break;
            case 'D' : CurrentEvent = SKIP_MODE_ON; break;
            case 'E' : CurrentEvent = NEW_MESSAGE; break;
            case 'F' : CurrentEvent = TIME_TO_SEND_INPUTS; break;
            case 'G' : CurrentEvent = NO_EVENT; break;
            case 'H' : CurrentEvent = SWITCH_OFF; break;
        }
        return(CurrentEvent);
    }
 
    if( Key_Switch == _ON_) // If game switch is on and it was off before
    {
      return SWITCH_ON;  // return switch on event
    }
 
    if( Key_Switch == _OFF_) // if game switch is off and it was on
    {
      return SWITCH_OFF;  // return switch OFF event
    }
 
    if(Query_Received_Cipher_Flag() == _SET_)  // if cipher flag is set
    {
       return CIPHER_RECEIVED;// return CIPHER_RECEIVED event
    }
 
    if(Query_GO_Flag() == _SET_)  // GO flag is set
    {
        return GO;// return GO event
    }
 
    if(Query_New_Message_Flag() == _SET_) // if New message flag is set
    {
        return NEW_MESSAGE;// return NEW MESSAGE event
    }
 
    if(Query_Time_To_Send_Inputs_Flag() == _SET_)  // if Time to send Inputs flag is set
    {
        return TIME_TO_SEND_INPUTS;  // retun TIME TO SEND INPUTS event
    }
 
    return CurrentEvent;
 
}
 
/****************************************************************************
 Function
     QueryCurrent_Switch_State()
 
 Parameters
 
  Returns
     0, 1, or 2
 
 Description
     Queries the current switch state.  Returns 1 if the switch has been
     turned on, returns a 0 if the switch has been turned off, and a 2 otherwise
 Notes
 
 
 Author
     Nick Musser
****************************************************************************/
char QueryCurrent_Switch_State(void)
{
  char Port_Value = (PTP & BIT2HI); // read switch port
  Previous_Switch_State = Current_Switch_State; // assign current port state to previous switch state
 
  if( Port_Value != _OFF_) // if switch port is high
  {
    Current_Switch_State = _ON_; // current switch state is high
  }
  if(Port_Value == _OFF_) // if switch port is low
  {
    Current_Switch_State = _OFF_; // current swtich state is low
  }
 
  if((Current_Switch_State == _ON_) && (Previous_Switch_State == _OFF_))  // If game switch is on and it was off before
    {
      return 1;  // indicate that a on change has occured
    }
  if((Current_Switch_State == _OFF_) && (Previous_Switch_State == _ON_))  // if game switch is off and it was on
    {
      return 0;  // indicate that an off change has occured
    }
 
  return 2; // no change has occurred.
} // end of function