#include "klavesnica.h"
#include "uart1.h"
#include "printf-stdarg.h"
portTickType xTimeLast;
/*
 ** ===================================================================
 **     Method      :  Klavesnica_Enable (component ExtInt)
 **
 **     Description :
 **         Enable the component - the external events are accepted.
 **         This method is available only if HW module allows
 **         enable/disable of the interrupt.
 **     Parameters  : None
 **     Returns     : Nothing
 ** ===================================================================
 */
void Klavesnica_Enable(void) {
	setReg8(EPFR0,Klavesnica_PIN_MASK); /* Clear flag */
	setReg8Bits(EPIER0,Klavesnica_PIN_MASK); /* Enable interrupt */
}

/*
 ** ===================================================================
 **     Method      :  Klavesnica_Disable (component ExtInt)
 **
 **     Description :
 **         Disable the component - the external events are not accepted.
 **         This method is available only if HW module allows
 **         enable/disable of the interrupt.
 **     Parameters  : None
 **     Returns     : Nothing
 ** ===================================================================
 */
/*
 void Klavesnica_Disable(void)

 **  This method is implemented as a macro. See Klavesnica.h file.  **

 */
/*
 ** ===================================================================
 **     Method      :  KlavesnicaLow_GetVal (component BitsIO)
 **
 **     Description :
 **         This method returns an input value.
 **           a) direction = Input  : reads the input value from the
 **                                   pins and returns it
 **           b) direction = Output : returns the last written value
 **         Note: This component is set to work in Input direction only.
 **     Parameters  : None
 **     Returns     :
 **         ---        - Input value (0 to 3)
 ** ===================================================================
 */
byte KlavesnicaValue_GetVal(void) {
	return (byte) ((getReg8(SETUA) & 0x0C) >> 2) | (byte) ((getReg8(SETUB)
			& 0x0C)); /* Return port input data */

}

/*
 ** ===================================================================
 **     Method      :  Klavesnica_Interrupt (component ExtInt)
 **
 **     Description :
 **         The method services the interrupt of the selected peripheral(s)
 **         and invokes the components event.
 **         This method is internal. It is used by Processor Expert only.
 ** ===================================================================
 */
void __attribute__ ((interrupt)) __cs3_isr_interrupt_68(void) {
	portTickType xTimeAkt;
	portBASE_TYPE xHigherPriorityTask=pdFALSE;
	setReg8(EPFR0,Klavesnica_PIN_MASK); /* Clear flag */

	xTimeAkt = xTaskGetTickCount();
	if ((xTimeAkt - xTimeLast) > klavesnicaTimeOut) {
		xSemaphoreGiveFromISR(klavesnicaGetKeyMutex,&xHigherPriorityTask);
		xHigherPriorityTask(xHigherPriorityTask);
	}
	xTimeLast=xTimeAkt;
}
