/* ---------------------------------------------------------------------------
 * test_digital.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: 
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Dez 15, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

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

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "Task.h"
#include "semphr.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */ 
#include "test_digital.h"
#include "remote_digital.h"

#include "dio.h"
#include "remote_tests.h"
#include "SerOut.h"
#include "protocolfunctions.h"
#include "BusProtocol.h"
#include "ledfunctions.h"


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

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

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

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

BOOLEAN remoteDigitalLinetestMBExecute (void)
{
    BOOLEAN returnValue;
    BOOLEAN remoteDigitalOutput;
    BOOLEAN remoteDigitalInput;
    
    
    remoteDigitalOutput = remoteDigitalOutputTest_MB();
    sendString (SerOutPort, TRUE, resultMessage,
            "remote digital_mb output test: ", f_tab, BoolRestoStr(remoteDigitalOutput));
    remoteDigitalInput = remoteDigitalInputTest_MB();
    sendString (SerOutPort, TRUE, resultMessage,
            "remote digital_mb input test: ", f_tab, BoolRestoStr(remoteDigitalInput));
    
    if ((remoteDigitalOutput == TRUE) && (remoteDigitalInput == TRUE))
        {
        returnValue = TRUE;
        }
    else
        {
        returnValue = FALSE;
        }
    
    return (returnValue);
}

BOOLEAN remoteDigitalLinetestEBExecute (void)
{
// \MARK NEW PINSETTINGS FOR TESTER (2)
#if (PINSET_TESTER == 2)  
    BOOLEAN returnValue;
    BOOLEAN remoteDigitalOutput;
    BOOLEAN remoteDigitalInput;      
   
    remoteDigitalOutput = remoteDigitalOutputTest_EB();
    sendString (SerOutPort, TRUE, resultMessage,
            "remote digital_eb output test: ", f_tab, BoolRestoStr(remoteDigitalOutput));
    
    remoteDigitalInput = remoteDigitalInputTest_EB();
    sendString (SerOutPort, TRUE, resultMessage,
            "remote digital_eb input test: ", f_tab, BoolRestoStr(remoteDigitalInput));
    
    if ((remoteDigitalOutput == TRUE) && (remoteDigitalInput == TRUE))
        {
        returnValue = TRUE;
        }
    else
        {
        returnValue = FALSE;
        }
    
    return (returnValue);
#else
    sendString (SerOutPort, TRUE, importantMessage,
            "Extension Board test not available on this device", Dummy, Dummy);
    
    return (FALSE);
#endif
}


BOOLEAN remoteDigitalOutputTest_MB (void)
{
    UINT32 channelcnt;
    UINT32 loopcnt;
    
    BOOLEAN returnResult = TRUE;
    BOOLEAN Low_Test = TRUE;
    BOOLEAN High_Test = TRUE;
    BOOLEAN AllOther_Test = TRUE;
    
    /* Reset all remote digital outputs to LOW/OFF                          */
    remoteDioWriteAll(remoteDeviceNumber, digital_mb, FALSE);
    vTaskDelay (200);
    
    for (channelcnt = 0; channelcnt < NUMBER_OF_DO_MB; channelcnt++)
        {
        
        /* Read remote digital channel value for LOW test                   */
        if (dioRead(thisDeviceNumber, channelcnt) == FALSE)
            {
            /* Read back a LOW Value, which is correct - LOW test passed    */
            Low_Test = TRUE;
            }
        else
            {
            /* read back a HIGH Value, which is incorrect - LOW test failed */
            Low_Test = FALSE;
            }
        
        /* Drive testchannel to HIGH Output value                           */
        remoteDioWrite(remoteDeviceNumber, channelcnt, TRUE);
        vTaskDelay (200);                   /* Assure command was executed  */
        
        if (dioRead(thisDeviceNumber, channelcnt) == TRUE)
            {
            /* Read back a HIGH Value, which is correct - HIGH test passed  */
            High_Test = TRUE;
            }
        else
            {
            /* read back a LOW Value, which is incorrect - HIGH test failed */
            High_Test = FALSE;
            }
        
        vTaskDelay (200);
        /* Check all other channels for remaining at LOW                    */
        for (loopcnt = 0; loopcnt < NUMBER_OF_DO_MB; loopcnt++)
            {
            if (loopcnt == channelcnt)
                {
                /* Skip test channel                                        */
                } 
            else
                {
                /* Check actual channel to remain LOW                       */
                if (dioRead(thisDeviceNumber, loopcnt) == TRUE)
                    {
                    /* Channel should remain LOW - AllOther test failed     */
                    AllOther_Test = FALSE;
                    break;                  /* Skip rest of loop            */
                    }
                else
                    {
                    AllOther_Test = TRUE;
                    }
                }
            }
        
        remoteDioWrite(remoteDeviceNumber, channelcnt, FALSE);
        
        /* Message out Test Results                                         */
        sendString (SerOutPort, TRUE, testMessage,
                "\tLinetest for Digital Input ", ItoDStr(channelcnt), ": ");
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (Low_Test));
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (AllOther_Test));
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (High_Test));
        vTaskDelay (50);
        
        if ((Low_Test == FALSE) || (High_Test == FALSE) || (AllOther_Test == FALSE))
            {
            returnResult = FALSE;
            }
        }
    
    return (returnResult);
}

BOOLEAN remoteDigitalInputTest_MB (void)
{
    UINT32 channelcnt;
    UINT32 loopcnt;
    
    BOOLEAN returnResult = TRUE;
    BOOLEAN Low_Test = TRUE;
    BOOLEAN High_Test = TRUE;
    BOOLEAN AllOther_Test = TRUE;
    
    dioClean();                             /* Clean local digital outputs  */
    vTaskDelay (500);
    
    for (channelcnt = 0; channelcnt < NUMBER_OF_DI_MB; channelcnt++)
        {
        if (remoteDioRead(remoteDeviceNumber, channelcnt) == FALSE)
            {
            /* Read back a LOW Value, which is correct - LOW test passed    */
            Low_Test = TRUE;
            }
        else
            {
            /* read back a HIGH Value, which is incorrect - LOW test failed */
            Low_Test = FALSE;
            }
        
        vTaskDelay (200);
        dioWrite(thisDeviceNumber, channelcnt, TRUE);
        vTaskDelay(200);
        
        if (remoteDioRead(remoteDeviceNumber, channelcnt) == TRUE)
            {
            /* Read back a HIGH Value, which is correct - HIGH test passed  */
            vTaskDelay(500);
            High_Test = TRUE;
            }
        else
            {
            /* read back a LOW Value, which is incorrect - HIGH test failed */
            vTaskDelay(500);
            High_Test = FALSE;
            }
        
        remoteDioReadAll(remoteDeviceNumber, digital_mb);
        vTaskDelay (200);
        
        for (loopcnt = 0; loopcnt < NUMBER_OF_DI_MB; loopcnt++)
            {
            if (loopcnt == channelcnt)
                {
                /* Skip test channel                                        */
                } 
            else
                {
                /* Check actual channel to remain LOW                       */
                if (remoteDigitalInputs[loopcnt] == TRUE)
                    {
                    /* Channel should remain LOW - AllOther test failed     */
                    AllOther_Test = FALSE;
                    break;                  /* Skip rest of loop            */
                    }
                else
                    {
                    AllOther_Test = TRUE;
                    }
                }
            }
        /* Return to LOW channel value                                      */
        dioWrite (thisDeviceNumber, channelcnt, FALSE);

        /* Message out Test Results                                         */
        sendString (SerOutPort, TRUE, testMessage,
                "\tLinetest for Digital Input ", ItoDStr(channelcnt), ": ");
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (Low_Test));
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (AllOther_Test));
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (High_Test));
        vTaskDelay (50);
        
        if ((Low_Test == FALSE) || (High_Test == FALSE) || (AllOther_Test == FALSE))
            {
            returnResult = FALSE;
            }
        }
    
    return (returnResult);
}


BOOLEAN remoteDigitalOutputTest_EB (void)
{
    UINT32 channelcnt;
    UINT32 loopcnt;
    
    BOOLEAN returnResult = TRUE;
    BOOLEAN Low_Test = TRUE;
    BOOLEAN High_Test = TRUE;
    BOOLEAN AllOther_Test = TRUE;
    
// \MARK NEW PINSETTINGS FOR TESTER (2)
#if (PINSET_TESTER == 2)
    dio_inMuxEn (TRUE);                     /* Switch dio to Extension Board*/
#endif
    
    /* Reset all remote digital outputs to LOW/OFF                          */
    remoteDioWriteAll(remoteDeviceNumber, digital_eb, FALSE);
    vTaskDelay (200);
    
    for (channelcnt = NUMBER_OF_DO_MB; channelcnt < NUMBER_OF_TOTAL_DO; channelcnt++)
        {
        if (dioRead(thisDeviceNumber, (channelcnt - NUMBER_OF_DO_MB)) == FALSE)
            {
            /* Read back a LOW Value, which is correct - LOW test passed    */
            Low_Test = TRUE;
            }
        else
            {
            /* read back a HIGH Value, which is incorrect - LOW test failed */
            Low_Test = FALSE;
            }
        
        /* Drive testchannel to HIGH Output value                           */
        remoteDioWrite(remoteDeviceNumber, channelcnt, TRUE);
        vTaskDelay (200);                   /* Assure command was executed  */
        
        if (dioRead(remoteDeviceNumber, (channelcnt - NUMBER_OF_DO_MB)) == TRUE)
            {
            /* Read back a HIGH Value, which is correct - HIGH test passed  */
            High_Test = TRUE;
            }
        else
            {
            /* read back a LOW Value, which is incorrect - HIGH test failed */
            High_Test = FALSE;
            }
        
        vTaskDelay (200);
        for (loopcnt = 0; loopcnt < NUMBER_OF_DO_EB; loopcnt++)
            {
            if (loopcnt == channelcnt)
                {
                /* Skip test channel                                        */
                } 
            else
                {
                /* Check actual channel to remain LOW                       */
                if (dioRead(thisDeviceNumber, loopcnt) == TRUE)
                    {
                    /* Channel should remain LOW - AllOther test failed     */
                    AllOther_Test = FALSE;
                    break;                  /* Skip rest of loop            */
                    }
                else
                    {
                    AllOther_Test = TRUE;
                    }
                }
            }
        /* Message out Test Results                                         */
        sendString (SerOutPort, TRUE, testMessage,
                "\tLinetest for Digital Input ", ItoDStr(channelcnt), ": ");
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (Low_Test));
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (AllOther_Test));
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (High_Test));
        vTaskDelay (50);
        
        if ((Low_Test == FALSE) || (High_Test == FALSE) || (AllOther_Test == FALSE))
            {
            returnResult = FALSE;
            }
        }
    
// \MARK NEW PINSETTINGS FOR TESTER (2)
#if (PINSET_TESTER == 2)
    dio_inMuxEn (FALSE);                    /* Switch dio back to main Board*/
#endif
    
    return (returnResult);
}


BOOLEAN remoteDigitalInputTest_EB (void)
{
    UINT32 channelcnt;
    UINT32 loopcnt;
    
    BOOLEAN returnResult = TRUE;
    BOOLEAN Low_Test = TRUE;
    BOOLEAN High_Test = TRUE;
    BOOLEAN AllOther_Test = TRUE;
    
// \MARK NEW PINSETTINGS FOR TESTER (2)
#if (PINSET_TESTER == 2)
    dio_outMuxEn (TRUE);                     /* Switch dio to Extension Board*/
#endif
    
    dioClean();                             /* Clean local digital outputs  */
    vTaskDelay (500);
    
    for (channelcnt = 0; channelcnt < NUMBER_OF_DI_EB; channelcnt++)
        {
        if (remoteDioRead(remoteDeviceNumber, (channelcnt + NUMBER_OF_DI_MB)) == FALSE)
            {
            /* Read back a LOW Value, which is correct - LOW test passed    */
            Low_Test = TRUE;
            }
        else
            {
            /* read back a HIGH Value, which is incorrect - LOW test failed */
            Low_Test = FALSE;
            }
        
        vTaskDelay (200);
        dioWrite(thisDeviceNumber, channelcnt, TRUE);
        
        if (remoteDioRead(remoteDeviceNumber, (channelcnt + NUMBER_OF_DI_MB)) == TRUE)
            {
            /* Read back a HIGH Value, which is correct - HIGH test passed  */
            High_Test = TRUE;
            }
        else
            {
            /* read back a LOW Value, which is incorrect - HIGH test failed */
            High_Test = FALSE;
            }
        
        remoteDioReadAll(remoteDeviceNumber, digital_eb);
        vTaskDelay (200);
        
        for (loopcnt = 0; loopcnt < NUMBER_OF_DI_EB; loopcnt++)
            {
            if (loopcnt == channelcnt)
                {
                /* Skip test channel                                        */
                } 
            else
                {
                /* Check actual channel to remain LOW                       */
                if (remoteDigitalInputs[(loopcnt + NUMBER_OF_DI_MB)] == TRUE)
                    {
                    /* Channel should remain LOW - AllOther test failed     */
                    AllOther_Test = FALSE;
                    break;                  /* Skip rest of loop            */
                    }
                else
                    {
                    AllOther_Test = TRUE;
                    }
                }
            }
        /* Return to LOW channel value                                      */
        dioWrite (thisDeviceNumber, channelcnt, FALSE);

        /* Message out Test Results                                         */
        sendString (SerOutPort, TRUE, testMessage,
                "\tLinetest for Digital Input ", ItoDStr(channelcnt), ": ");
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (Low_Test));
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (AllOther_Test));
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, BoolRestoStr (High_Test));
        vTaskDelay (50);
        
        if ((Low_Test == FALSE) || (High_Test == FALSE) || (AllOther_Test == FALSE))
            {
            returnResult = FALSE;
            }
        }
    
// \MARK NEW PINSETTINGS FOR TESTER (2)
#if (PINSET_TESTER == 2)
    dio_outMuxEn (FALSE);                   /* Switch dio back to main Board*/
#endif
    
    return (returnResult);
}

