/* ---------------------------------------------------------------------------
 * ledfunctions.c                                       (c) 2008 Micro-key bv
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AB Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl  
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: 
 * Some handy Functions for the status and the Digital Output LEDs
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Mar 11, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files
 * ---------------------------------------------------------------------------
 */
#include "lpc23xx.h"
#include "types.h"


/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "Task.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */ 
#include "leds.h"
#include "dio.h"

/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */


void LEDPauseFlash (t_led_ids LEDnum, UINT8 counts, INT16 delay_length)
{
    /* Local Variable Declaration */
	UINT16 loopcnt;                     /* loop index                       */
    
	for ( loopcnt=0; loopcnt <= (counts*2); loopcnt++)
	{
		if (LEDnum == LED0)             /* RED LED                          */
		{
			ledToggle (LED0);           /* Toggle LED Status                */
		    vTaskDelay (delay_length);  /* Wait for defined time            */
		}
		if (LEDnum == LED1)             /* GREEN LED                        */
		{
			ledToggle (LED1);
		    vTaskDelay (delay_length);
		}     
	}     
	if (ledGet (LED1) == FALSE)         /* GREEN LED should be switched on  */
	{
		ledToggle (LED1);
	}    
}


void LEDResult (BOOLEAN TestResult)
{
    if (TestResult == TRUE)             /* If Test was passed               */
    {
        ledSet (LED0, 1);               /* switch on RED LED                */
    }     
    if (TestResult == FALSE)            /* If test failed                   */
    {
        ledSet (LED0, 0);               /* Switch off RED LED               */
    }    
}

void LEDResultDIO (BOOLEAN TestResult, UINT16 dioNo)
{
    if (TestResult == TRUE)             /* If Test was passed               */
    {
        dioWrite (0, dioNo, TRUE);      /* switch on DIO LED                */
    }     
    if (TestResult == FALSE)            /* If test failed                   */
    {
        dioWrite (0, dioNo, FALSE);     /* Switch off DIO LED               */
    }    
}

void LEDToggleForever (void)
{
    ledSet (LED0, FALSE);               /* Set both LEDs to StartUp         */
    ledSet (LED1, TRUE);
    vTaskDelay (500);                   /* Wait for 500 ms                  */
    
	for (;;)                            /* endless loop                     */
        {
        ledToggle (LED0);               /* Toggle RED LED                   */
        ledToggle (LED1);               /* Toggle GREEN LED                 */
        
        vTaskDelay (500);
        }    
}

void LEDToggle (UINT16 cnts)
{
    /* Local Variable Declaration */
	UINT16 loopcnt;                     /* loop index                       */
	
    ledSet (LED0, FALSE);               /* Set both LEDs to StartUp         */
    ledSet (LED1, TRUE);
    vTaskDelay (500);                   /* Wait for 500 ms                  */
    
    for ( loopcnt = 0; loopcnt <= cnts; loopcnt++)
        {
        ledToggle(LED0);                /* Toggle RED LED                   */
        ledToggle(LED1);                /* Toggle GREEN LED                 */
        
        vTaskDelay (500);
        }
}

void LEDShowStatus (UINT16 status)
{
    /* Local Variable Declaration */ 
	UINT16 loopcnt;                     /* loop index                       */    
    
    if (ledGet (LED0) == TRUE)          /* If current LED Status is TRUE    */
    {
        ledSet( LED0, FALSE);           /* Switch of RED LED                */
        vTaskDelay (500);               /* Wait for 500 ms                  */
    }    

    for (loopcnt = 0; loopcnt <= (status*2); loopcnt++)
    {
        ledToggle (LED0);               /* Toggle RED LED                   */
        vTaskDelay (500);
    }
    vTaskDelay (1000);
}

void gLEDToggle (UINT16 time)
{
    for (;;)                            /* Endless loop                     */
    {
	    ledToggle (LED1);               /* Toggle GREEN LED                 */
        vTaskDelay (time);              /* Wait for defined time            */
    }
}

void rLEDToggle (UINT16 time)
{
    for (;;)                            /* Endless loop                     */
    {
	    ledToggle (LED0);               /* Toggle RED LED                   */
        vTaskDelay (time);              /* Wait for defined Time            */
    }
}

void dioToggle (UINT16 LEDnr)
{
    UINT16 time = 100;                  /* LED Toggle-time definition       */
	
    for (;;)                            /* Endless loop                     */
    {
        dioWrite (0, LEDnr, TRUE);      /* Write defined digitalOut to HIGH */
        vTaskDelay (time);              /* Wait for defined Toggle-time     */
        dioWrite (0, LEDnr, FALSE);     /* Write defined digitalOut to LOW  */
        vTaskDelay (time);
    }
}

void dioToggleNC (UINT16 LEDnr)
{
    UINT16 time = 1000;                 /* LED Toggle-time definition       */
	
    for (;;)                            /* Endless loop                     */
    {
        dioWrite (0, LEDnr, TRUE);      /* Write defined digitalOut to HIGH */
        vTaskDelay (time);              /* Wait for defined Toggle-time     */
        dioWrite (0, LEDnr, FALSE);     /* Write defined digitalOut to LOW  */
        vTaskDelay (time);
    }
}

void dio2x4 (UINT32 ToggleTime)
{

    for (;;)                            /* endless loop                     */
        {
        dioWrite (0,0,TRUE);            /* ON-OFF-ON-OFF-ON-OFF-ON-OFF      */
        dioWrite (0,1,FALSE);
        dioWrite (0,2,TRUE);
        dioWrite (0,3,FALSE);
        dioWrite (0,4,TRUE);
        dioWrite (0,5,FALSE);
        dioWrite (0,6,TRUE);
        dioWrite (0,7,FALSE);
        
        vTaskDelay(ToggleTime);         /* Wait Toggle Time                 */ 
        
        dioWrite (0,0,FALSE);           /* OFF-ON-OFF-ON-OFF-ON-OFF-ON      */
        dioWrite (0,1,TRUE);
        dioWrite (0,2,FALSE);
        dioWrite (0,3,TRUE);
        dioWrite (0,4,FALSE);
        dioWrite (0,5,TRUE);
        dioWrite (0,6,FALSE);
        dioWrite (0,7,TRUE);
        
        vTaskDelay (ToggleTime);
        }
}

void dioClean (void)
{
    dioWrite (0,0,FALSE);               /* Write digital Output to LOW      */
    dioWrite (0,1,FALSE);
    dioWrite (0,2,FALSE);
    dioWrite (0,3,FALSE);
    dioWrite (0,4,FALSE);
    dioWrite (0,5,FALSE);
    dioWrite (0,6,FALSE);
    dioWrite (0,7,FALSE);
}
UINT8 dioCapture (void)
{
    /* Local Variable Declaration */
	UINT8 dioCaptureResult = 0;
	
    dioCaptureResult |= (dioReadBack (0,0) << 0); /*Save current DIO Status */
    dioCaptureResult |= (dioReadBack (0,1) << 1);
    dioCaptureResult |= (dioReadBack (0,2) << 2);
    dioCaptureResult |= (dioReadBack (0,3) << 3);
    dioCaptureResult |= (dioReadBack (0,4) << 4);
    dioCaptureResult |= (dioReadBack (0,5) << 5);
    dioCaptureResult |= (dioReadBack (0,6) << 6);
    dioCaptureResult |= (dioReadBack (0,7) << 7);
    
    return dioCaptureResult;            /* Return saved DIO Status          */
}

void dioResume (UINT8 dioResumeValue)
{
    dioWrite (0,0,((dioResumeValue >> 0) & 1)); /* Set DIO to defined Value */
    dioWrite (0,1,((dioResumeValue >> 1) & 1));
    dioWrite (0,2,((dioResumeValue >> 2) & 1));
    dioWrite (0,3,((dioResumeValue >> 3) & 1));
    dioWrite (0,4,((dioResumeValue >> 4) & 1));
    dioWrite (0,5,((dioResumeValue >> 5) & 1));
    dioWrite (0,6,((dioResumeValue >> 6) & 1));
    dioWrite (0,7,((dioResumeValue >> 7) & 1));
    
}

