/** ###################################################################
**     Filename    : Events.c
**     Project     : ProcessorExpert
**     Processor   : MKL25Z128VLK4
**     Component   : Events
**     Version     : Driver 01.00
**     Compiler    : GNU C Compiler
**     Date/Time   : 2012-11-16, 16:20, # CodeGen: 0
**     Abstract    :
**         This is user's event module.
**         Put your event handler code here.
**     Settings    :
**     Contents    :
**         Cpu_OnNMIINT - void Cpu_OnNMIINT(void);
**
** ###################################################################*/
/* MODULE Events */

#include "Cpu.h"
#include "Events.h"


/* User includes (#include below this line is not maintained by Processor Expert) */

extern int TI1casovac;
bool FlagTI1Int=FALSE;
bool FlagADC1Dokonceno=FALSE;
extern volatile bool FlagReceive;
extern volatile bool DataTransmittedFlg;
extern volatile bool DataReceivedFlg;
extern void Timer_Interrupt_CB(void);
/*PRO UART-BLUETOOTH*/
extern volatile bool blockReceived;
extern volatile bool blockSent;
extern volatile int LWpocitadloKroku;
extern volatile int RWpocitadloKroku;
extern volatile bool PWMGeneratedFlag;
extern bool LWA;
extern bool LWB;
extern bool RWA;
extern bool RWB;
extern void SetPWM(void);
unsigned int RW;
unsigned int LW;
/*
** ===================================================================
**     Event       :  Cpu_OnNMIINT (module Events)
**
**     Component   :  Cpu [MKL25Z128LK4]
**     Description :
**         This event is called when the Non maskable interrupt had
**         occurred. This event is automatically enabled when the <NMI
**         interrrupt> property is set to 'Enabled'.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void Cpu_OnNMIINT(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  Cpu_OnNMIINT0 (module Events)
**
**     Component   :  Cpu [MKL25Z128LK4]
**     Description :
**         This event is called when the Non maskable interrupt had
**         occurred. This event is automatically enabled when the <NMI
**         interrrupt> property is set to 'Enabled'.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void Cpu_OnNMIINT0(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  TI1_OnInterrupt (module Events)
**
**     Component   :  TI1 [TimerInt_LDD]
**     Description :
**         Called if periodic event occur. Component and OnInterrupt
**         event must be enabled. See <SetEventMask> and <GetEventMask>
**         methods. This event is available only if a <Interrupt
**         service/event> is enabled.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. The pointer passed as
**                           the parameter of Init method.
**     Returns     : Nothing
** ===================================================================
*/
void TI1_OnInterrupt(LDD_TUserData *UserDataPtr)
{
	TI1casovac++;
	Timer_Interrupt_CB();
	if(FlagReceive==TRUE)
	{
		if(FlagTI1Int==FALSE)
		{
			FlagTI1Int=TRUE;
			FlagReceive=FALSE;
		}
	}
	else FlagReceive=TRUE;
}

/*
** ===================================================================
**     Event       :  AD2_OnEnd (module Events)
**
**     Component   :  AD2 [ADC]
**     Description :
**         This event is called after the measurement (which consists
**         of <1 or more conversions>) is/are finished.
**         The event is available only when the <Interrupt
**         service/event> property is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void AD2_OnEnd(void)
{
	FlagADC1Dokonceno=TRUE;
	//Timer_Interrupt_CB();
}

/*
** ===================================================================
**     Event       :  AD2_OnCalibrationEnd (module Events)
**
**     Component   :  AD2 [ADC]
**     Description :
**         This event is called when the calibration has been finished.
**         User should check if the calibration pass or fail by
**         Calibration status method./nThis event is enabled only if
**         the <Interrupt service/event> property is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void AD2_OnCalibrationEnd(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  I2C2_OnMasterBlockSent (module Events)
**
**     Component   :  I2C2 [I2C_LDD]
**     Description :
**         This event is called when I2C in master mode finishes the
**         transmission of the data successfully. This event is not
**         available for the SLAVE mode and if MasterSendBlock is
**         disabled. 
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. This pointer is passed
**                           as the parameter of Init method.
**     Returns     : Nothing
** ===================================================================
*/
void I2C2_OnMasterBlockSent(LDD_TUserData *UserDataPtr)
{
  /* Write your code here ... */
	DataTransmittedFlg = TRUE;
}

/*
** ===================================================================
**     Event       :  I2C2_OnMasterBlockReceived (module Events)
**
**     Component   :  I2C2 [I2C_LDD]
**     Description :
**         This event is called when I2C is in master mode and finishes
**         the reception of the data successfully. This event is not
**         available for the SLAVE mode and if MasterReceiveBlock is
**         disabled.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. This pointer is passed
**                           as the parameter of Init method.
**     Returns     : Nothing
** ===================================================================
*/
void I2C2_OnMasterBlockReceived(LDD_TUserData *UserDataPtr)
{
  /* Write your code here ... */
	DataReceivedFlg = TRUE;
}

/*
** ===================================================================
**     Event       :  I2C2_OnError (module Events)
**
**     Component   :  I2C2 [I2C_LDD]
**     Description :
**         This event is called when an error (e.g. Arbitration lost)
**         occurs. The errors can be read with GetError method.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. This pointer is passed
**                           as the parameter of Init method.
**     Returns     : Nothing
** ===================================================================
*/
void I2C2_OnError(LDD_TUserData *UserDataPtr)
{
  /* Write your code here ... */
}

void uart_putchar (int uart_port_num, char ch)
{
	if (uart_port_num == 0)
	{
      /* Wait until space is available in the FIFO */
      while(!(UART0_S1 & UART_S1_TDRE_MASK));
    
      /* Send the character */
      UART0_D = (uint8)ch;
	}
	else if (uart_port_num == 1)
	{
	  /* Wait until space is available in the FIFO */
	  while(!(UART1_S1 & UART_S1_TDRE_MASK));
	   
	  /* Send the character */
	  UART1_D = (uint8)ch;
	}
	else
	{
	  /* Wait until space is available in the FIFO */
	  while(!(UART2_S1 & UART_S1_TDRE_MASK));
	  
	  /* Send the character */
	  UART2_D = (uint8)ch;
	}
 }

/*
** ===================================================================
**     Function    : uart_getchar
**
**     Description :
**         This function gets one character to the desired UART port.
**         It was written to patch a CodeWarrior bug with the getchar
**         function.
**     Parameters  :
**         NAME            - DESCRIPTION
**         uart_port_num   - Specifies which UART to get the 
**         					 character from.   
**                           
**     Returns     : character (UARTx_D)
** ===================================================================
*/
char uart_getchar (int uart_port_num)
{
	if (uart_port_num == 0)
	{
      /* Wait until space is available in the FIFO */
      while(!(UART0_S1 & UART_S1_RDRF_MASK));
    
      /* Send the character */
      return (uint8)UART0_D;
	}
	else if (uart_port_num == 1)
	{
	  /* Wait until space is available in the FIFO */
	  while(!(UART1_S1 & UART_S1_RDRF_MASK));
	   
	  /* Send the character */
	  return (uint8)UART1_D;
	}
	else
	{
	  /* Wait until space is available in the FIFO */
	  while(!(UART2_S1 & UART_S1_RDRF_MASK));
	  
	  /* Send the character */
	  return (uint8)UART2_D;
	}
 }

/*
** ===================================================================
**     Event       :  BT_OnBlockReceived (module Events)
**
**     Component   :  BT [Serial_LDD]
**     Description :
**         This event is called when the requested number of data is
**         moved to the input buffer.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. This pointer is passed
**                           as the parameter of Init method.
**     Returns     : Nothing
** ===================================================================
*/
void BT_OnBlockReceived(LDD_TUserData *UserDataPtr)
{
	blockReceived = TRUE;     /* set the flag */
}

/*
** ===================================================================
**     Event       :  BT_OnBlockSent (module Events)
**
**     Component   :  BT [Serial_LDD]
**     Description :
**         This event is called after the last character from the
**         output buffer is moved to the transmitter. 
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. This pointer is passed
**                           as the parameter of Init method.
**     Returns     : Nothing
** ===================================================================
*/
void BT_OnBlockSent(LDD_TUserData *UserDataPtr)
{
	blockSent = TRUE;     /* set the flag */
}

/*
** ===================================================================
**     Event       :  RWExt_OnInterrupt (module Events)
**
**     Component   :  RWExt [ExtInt]
**     Description :
**         This event is called when an active signal edge/level has
**         occurred.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void RWExt_OnInterrupt(void)
{
	RW=GPIOD_PDIR & 0b11000000; 
	if((RW==64) || (RW==128))
					{
						RWpocitadloKroku--;
					}
	else if((RW==0) || (RW==192))
					{
						RWpocitadloKroku++;
					}
}

/*
** ===================================================================
**     Event       :  RWExt_OnInterrupt (module Events)
**
**     Component   :  RWExt [ExtInt]
**     Description :
**         This event is called when an active signal edge/level has
**         occurred.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/

/*
** ===================================================================
**     Event       :  LWExt_OnInterrupt (module Events)
**
**     Component   :  LWExt [ExtInt]
**     Description :
**         This event is called when an active signal edge/level has
**         occurred.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void LWExt_OnInterrupt(void)
{
		LW=GPIOA_PDIR & 0b000000011000000000000; 
		if((LW==8192) || (LW==4096))
					{
						LWpocitadloKroku++;
					}
					else if((LW==0) || (LW==12288))
					{
						LWpocitadloKroku--;
					}
		
//		LWA=LWExt_GetVal();
//		LWB=LWExt2_GetVal();
//		
//		if((LWA==TRUE && LWB==FALSE) || (LWA==FALSE && LWB==TRUE))
//			{
//				LWpocitadloKroku++;
//			}
//			else if((LWA==FALSE && LWB==FALSE) || (LWA==TRUE && LWB==TRUE))
//			{
//				LWpocitadloKroku--;
//			}
			
}

/*
** ===================================================================
**     Event       :  LWExt2_OnInterrupt (module Events)
**
**     Component   :  LWExt2 [ExtInt]
**     Description :
**         This event is called when an active signal edge/level has
**         occurred.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void LWExt2_OnInterrupt(void)
{
			LW=GPIOA_PDIR & 0b000000011000000000000; 
			if((LW==12288) || (LW==0))
							{
								LWpocitadloKroku++;
							}
							else if((LW==4096) || (LW==8192))
							{
								LWpocitadloKroku--;
							}
//			LWA=LWExt_GetVal();
//			LWB=LWExt2_GetVal();
//			
//			if((LWB==TRUE && LWA==TRUE) || (LWB==FALSE && LWA==FALSE))
//				{
//					LWpocitadloKroku++;
//				}
//				else if((LWB==FALSE && LWA==TRUE) || (LWB==TRUE && LWA==FALSE))
//				{
//					LWpocitadloKroku--;
//				}
				
}

/*
** ===================================================================
**     Event       :  RWExt2_OnInterrupt (module Events)
**
**     Component   :  RWExt2 [ExtInt]
**     Description :
**         This event is called when an active signal edge/level has
**         occurred.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void RWExt2_OnInterrupt(void)
{
	RW=GPIOD_PDIR & 0b11000000; 
	if((RW==192) || (RW==0))
							{
								RWpocitadloKroku--;
							}
	else if((RW==128) || (RW==64))
							{
								RWpocitadloKroku++;
							}
}

/* END Events */

/*
** ###################################################################
**
**     This file was created by Processor Expert 10.0 [05.03]
**     for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/
