/* ---------------------------------------------------------------------------
 * test_analogue.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_analogue.h"
#include "remote_analogue.h"
#include "adc.h"
#include "dac.h"

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


/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */
#define cTestvalue           15000
#define cNullvalue           0

#define ciTolerance_null     50
#define ciTolerance_uncal    3500
#define ciTolerance_cal      50

#define coTolerance_null     50
#define coTolerance_uncal    3500
#define coTolerance_cal      50
/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */

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

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

BOOLEAN remoteAnalogueLinetestMBExecute (void)
{
    BOOLEAN returnValue;
    BOOLEAN remoteAnalogueOutput;
    BOOLEAN remoteAnalogueInput;
    
    remoteAnalogueOutput = remoteAnalogueOutputTest_MB();
    sendString (SerOutPort, TRUE, resultMessage,
            "remote analogue output test: ", f_tab, BoolRestoStr(remoteAnalogueOutput));
    
    remoteAnalogueInput = remoteAnalogueInputTest_MB();
    sendString (SerOutPort, TRUE, resultMessage,
            "remote analogue input test: ", f_tab, BoolRestoStr(remoteAnalogueInput));
    
    if ((remoteAnalogueOutput == TRUE) && (remoteAnalogueInput == TRUE))
        {
        returnValue = TRUE;
        }
    else
        {
        returnValue = FALSE;
        }
    
    return (returnValue);
    
}


BOOLEAN remoteAnalogueLinetestEBExecute (void)
{
// \MARK NEW PINSETTINGS FOR TESTER (2)
#if (PINSET_TESTER == 2) 
    BOOLEAN remoteAnalogueOutput;
    BOOLEAN remoteAnalogueInput;
    
    remoteAnalogueOutput = remoteAnalogueOutputTest_MB();
    sendString (SerOutPort, TRUE, resultMessage,
            "remote analogue output test: ", f_tab, BoolRestoStr(remoteAnalogueOutput));
    
    remoteAnalogueInput = remoteAnalogueInputTest_MB();
    sendString (SerOutPort, TRUE, resultMessage,
            "remote analogue input test: ", f_tab, BoolRestoStr(remoteAnalogueInput));
    
    if ((remoteAnalogueOutput == TRUE) && (remoteAnalogueInput == 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 remoteAnalogueOutputTest_MB (void)
{
    UINT32 loopcnt;
    UINT32 channelcnt;
    UINT32 testTolerance;
    UINT32 lowRead;
    UINT32 highRead;
    
    BOOLEAN returnResult = TRUE;
    BOOLEAN Low_Test = TRUE;
    BOOLEAN High_Test = TRUE;
    BOOLEAN AllOther_Test = TRUE;
    
    
    // \TODO SET TEST TOLERANCE DEPENDING ON THE CALIBRATION STATUS
    testTolerance = coTolerance_uncal;
    
    adcModeAll(adcCURRENT);                 /* Set local ADC to mode CURRENT*/
    remoteAioWriteAll(remoteDeviceNumber, analogue_mb, cNullvalue);
    vTaskDelay (500);
    
    sendString (SerOutPort, FALSE, testMessage, NewLine,
            "\tCurrent Test Value: ", ItoDStr (cTestvalue));
    sendString (SerOutPort, FALSE, testMessage,
            "\t\tTolerance: ", ItoDStr (testTolerance), Dummy);
    
    for (channelcnt = 0; channelcnt < NUMBER_OF_AO_MB; channelcnt++)
        {
        lowRead = adcRead(thisDeviceNumber, channelcnt);
        if (lowRead < (cNullvalue + coTolerance_null))
            {
            /* Read a null-like value - LOW test passed                     */
            }
        else
            {
            /* Read a too high value - LOW test failed                      */
            Low_Test = FALSE;
            }
        
        remoteAioWrite(remoteDeviceNumber, channelcnt, cTestvalue);
        vTaskDelay (500);
        highRead = adcRead(thisDeviceNumber, channelcnt);
        
        if ((highRead < (cTestvalue + testTolerance)) && (highRead > (cTestvalue - testTolerance)))
            {
            /* Read a value within the test tolerances - High test passed   */
            }
        else
            {
            /* read a value out of the test tolerances - High test failed   */
            High_Test = FALSE;
            }
        
        for (loopcnt = 0; loopcnt < NUMBER_OF_AO_MB; loopcnt++)
            {
            if (loopcnt == channelcnt)
                {
                /* Skip actual test channel                                 */
                }
            else
                {
                /* Check channel for remaining LOW                          */
                if (adcRead(thisDeviceNumber, loopcnt) >= (cNullvalue + coTolerance_null))
                    {
                    /* Actual channels value too high, AllOther test failed */
                    AllOther_Test = FALSE;
                    }
                }
            }
        
        remoteAioWrite(remoteDeviceNumber, channelcnt, cNullvalue);
        
        /* Message out Test Results                                         */
        sendString (SerOutPort, TRUE, testMessage,
                "\tCurrent Test for Analogue Input ", ItoDStr(channelcnt), ": ");
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, ItoDStr (lowRead));
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, ItoDStr (highRead));
        sendString (SerOutPort, FALSE, testMessage,
                "  All Other: ", Dummy, BoolRestoStr (AllOther_Test));

        if ((Low_Test == FALSE) || (High_Test == FALSE) || (AllOther_Test == FALSE))
            {
            returnResult = FALSE;
            }
        }
    
    return (returnResult);
}



BOOLEAN remoteAnalogueInputTest_MB (void)
{
    UINT32 loopcnt;
    UINT32 channelcnt;
    UINT32 testTolerance;
    UINT32 lowRead;
    UINT32 highRead;
    
    BOOLEAN returnResult = TRUE;
    BOOLEAN Low_Test = TRUE;
    BOOLEAN High_Test = TRUE;
    BOOLEAN AllOther_Test = TRUE;
    
    // \TODO SET TEST TOLERANCE DEPENDING ON THE CALIBRATION STATUS
    testTolerance = coTolerance_uncal;
    
    dacModeAll(dacCURRENT);                 /* Set local DAC to mode CURRENT*/
    /* Reset all local DAC channels to zero ampere                          */
    for (loopcnt = 0; loopcnt < maxDAC_Channels; loopcnt++)
        {
        dacWrite(thisDeviceNumber, loopcnt, cNullvalue);
        }

    vTaskDelay (500);
    
    sendString (SerOutPort, FALSE, testMessage, NewLine,
            "\tCurrent Test Value: ", ItoDStr (cTestvalue));
    sendString (SerOutPort, FALSE, testMessage,
            "\t\tTolerance: ", ItoDStr (testTolerance), Dummy);
    
    for (channelcnt = 0; channelcnt < NUMBER_OF_AI_MB; channelcnt++)
        {
        lowRead = remoteAioRead(remoteDeviceNumber, channelcnt);
        if (lowRead < (cNullvalue + testTolerance))
            {
            /* Read a null-like value - LOW test passed                     */
            Low_Test = TRUE;
            }
        else
            {
            /* Read a too high value - LOW test failed                      */
            Low_Test = FALSE;
            }
        
        dacWrite(thisDeviceNumber, channelcnt, cTestvalue);
        vTaskDelay (200);
        highRead = remoteAioRead(remoteDeviceNumber, channelcnt);
        
        if ((highRead < (cTestvalue + testTolerance)) && (highRead > (cTestvalue - testTolerance)))
            {
            /* Read a value within the test tolerances - High test passed   */
            High_Test = TRUE;
            }
        else
            {
            /* read a value out of the test tolerances - High test failed   */
            High_Test = FALSE;
            }
        
        remoteAioReadAll(remoteDeviceNumber, analogue_mb);
        vTaskDelay (200);
        
        for (loopcnt = 0; loopcnt < NUMBER_OF_AO_MB; loopcnt++)
            {
            if (loopcnt == channelcnt)
                {
                /* Skip test channel                                        */
                } 
            else
                {
                /* Check actual channel to remain LOW                       */
                if (remoteAnalogueOutputs[loopcnt] >= (cNullvalue + ciTolerance_null))
                    {
                    /* Channel should remain LOW - AllOther test failed     */
                    AllOther_Test = FALSE;
                    break;                  /* Skip rest of loop            */
                    }
                else
                    {
                    AllOther_Test = TRUE;
                    }
                }
            }
        
        dacWrite(thisDeviceNumber, channelcnt, cNullvalue);
        
        /* Message out Test Results                                         */
        sendString (SerOutPort, TRUE, testMessage,
                "\tCurrent Test for Analogue Input ", ItoDStr(channelcnt), ": ");
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, ItoDStr (lowRead));
        sendString (SerOutPort, FALSE, testMessage,
                "  ", Dummy, ItoDStr (highRead));
        sendString (SerOutPort, FALSE, testMessage,
                "  All Other: ", Dummy, BoolRestoStr (AllOther_Test));

        if ((Low_Test == FALSE) || (High_Test == FALSE) || (AllOther_Test == FALSE))
            {
            returnResult = FALSE;
            }
        }
    
    return (returnResult);
    
    
}



BOOLEAN remoteAnalogueOutputTest_EB (void)
{

}
