/** ###################################################################
**     Filename  : Events.C
**     Project   : vga2scart
**     Processor : MC9S08QD4CPC
**     Component : Events
**     Version   : Driver 01.02
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 5.7.2010, 1:47
**     Abstract  :
**         This is user's event module.
**         Put your event handler code here.
**     Settings  :
**     Contents  :
**         HSyncTimeout_OnInterrupt - void HSyncTimeout_OnInterrupt(void);
**         HSync_OnCapture          - void HSync_OnCapture(void);
**
** ###################################################################*/
/* MODULE Events */


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

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

volatile byte gState = 0;


void EnableVideo(bool en)
{
    RgbStatus_PutVal(en);
    CSyncEn_SetDir(!en);
}

/*
** ===================================================================
**     Event       :  HSync_OnCapture (module Events)
**
**     Component   :  HSync [Capture]
**     Description :
**         This event is called on capturing of Timer/Counter actual
**         value (only when the bean is enabled - <Enable> and the
**         events are enabled - <EnableEvent>.This event is available
**         only if a <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void HSync_OnCapture(void)
{
    static word LastCap = 0;
    
    word Cap, Diff;

    if (HSync_GetCaptureValue(&Cap) == ERR_OK)
    {
        Diff = (Cap >= LastCap) ? Cap - LastCap : ~LastCap + Cap+1;
        
        if (gState == 0)
        {
            gState++;
        }
        else if ((Diff > HSync_COUNTER_INPUT_CLOCK_HZ/17000) &&
                 (Diff < HSync_COUNTER_INPUT_CLOCK_HZ/13000))
        {
            if (gState < 100)
                gState++;
            else
                EnableVideo(TRUE);
        }
        else
        {
            EnableVideo(FALSE);
            gState = 1;
        }

        HSyncTimeout_Reset();
        LastCap = Cap;
    }
}

/*
** ===================================================================
**     Event       :  HSyncTimeout_OnInterrupt (module Events)
**
**     Component   :  HSyncTimeout [FreeCntr]
**     Description :
**         This event is called when a compare matches the counter
**         value (if compare or reload is selected as a interrupt
**         source) or a counter overflows (for free-running devices).
**         It is valid only when the component is enabled - <"Enable">
**         and the events are enabled - <"EnableEvent">. The event is
**         available only if <Interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void HSyncTimeout_OnInterrupt(void)
{
    EnableVideo(FALSE);
    gState = 0;
    
    HSyncTimeout_Reset();
}

/* END Events */

/*
** ###################################################################
**
**     This file was created by Processor Expert 3.07 [04.34]
**     for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/
