/* ---------------------------------------------------------------------------
 * remote_relay.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 16, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

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

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */

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

#include "remote_relay.h"

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

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

/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */
BOOLEAN remoteRelayInitialised = FALSE;
 
/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */

void remoteRelayInit(void)
{
    UINT32 loopcnt;
    RESULT set_mb;
    RESULT set_eb;
    
    remoteRelayInitialised = TRUE;

    sendString(SerOutPort, TRUE, importantMessage, NewLine,
            "Initialise relay remote buffers...", Dummy);
    
    for (loopcnt = 0; loopcnt < NUMBER_OF_TOTAL_RLY; loopcnt++)
        {
        remoteRelayInputs[loopcnt] = 0;
        }
    sendString(SerOutPort, FALSE, importantMessage, "Done", Dummy, Dummy);
    
    
    vSemaphoreCreateBinary (remoteRelaySemaphore);
    sendString (SerOutPort, TRUE, importantMessage,
                "Take relay semaphore... ", Dummy, Dummy);
        
    if (xSemaphoreTake(remoteRelaySemaphore, 0) == pdTRUE)
        {
        sendString (SerOutPort, FALSE, importantMessage,
                "Done", Dummy, Dummy);
        }
    else
        {
        sendString (SerOutPort, FALSE, importantMessage,
                "Failed", Dummy, Dummy);
        }
    
    
    sendString(SerOutPort, TRUE, importantMessage,
            "reset remote relays...", Dummy, Dummy);
    set_mb = remoteRelaySetAll (remoteDeviceNumber, relay_mb, FALSE);
    set_eb = remoteRelaySetAll (remoteDeviceNumber, relay_eb, FALSE);
    if ((set_mb == OK) && (set_eb == OK))
        {
        sendString(SerOutPort, FALSE, importantMessage, "Done", Dummy, Dummy);
        }
    else
        {
        sendString(SerOutPort, FALSE, importantMessage, "Failed", Dummy, Dummy);
        }
    
}

RESULT remoteRelaySet (UINT8 device, UINT8 channel, BOOLEAN value)
{
    INT32 sendArray[2];
    RESULT returnValue;
    
    if (remoteRelayInitialised == FALSE)
        {
        /* Remote digital driver is not initialised                         */
        sendString (SerOutPort, TRUE, importantMessage,
                "\t\trelay not intialised", Dummy, Dummy);
        return (ERROR);
        }

    
    sendArray[0] = (INT32)channel;
    sendArray[1] = (INT32)value;
    
    bpSendCallRpc(handleBus1, device, 28, 2, sendArray);
    
    if (xSemaphoreTake(remoteRelaySemaphore, 3000) != pdTRUE)
        {
        sendString (SerOutPort, TRUE, importantMessage,
                s_tab, "relay set was not successful", Dummy);
        
        returnValue = ERROR;
        }
    else
        {
        remoteRelayInputs[channel] = value;
        
        returnValue = OK;
        }
        
        return (returnValue);

}


BOOLEAN remoteRelayRead (UINT8 device, UINT8 channel)
{
    
    if (remoteRelayInitialised == FALSE)
        {
        /* Remote digital driver is not initialised                         */
        sendString (SerOutPort, TRUE, importantMessage,
                "\t\trelay not intialised", Dummy, Dummy);
        return;
        }

}


RESULT remoteRelaySetAll(UINT8 device, t_boardtype_relay board, BOOLEAN value)
{
    INT32 sendArray[2];
    UINT32 loopcnt = 0;
    UINT32 loopend = 0;
    RESULT returnValue;
    
    if (remoteRelayInitialised == FALSE)
        {
        /* Remote digital driver is not initialised                         */
        sendString (SerOutPort, TRUE, importantMessage,
                "\t\trelay not intialised", Dummy, Dummy);
        return (ERROR);
        }

    sendArray[0] = (INT32)board;
    sendArray[1] = (INT32)value;

    bpSendCallRpc(handleBus1, device, 29, 2, sendArray);

    if (xSemaphoreTake(remoteRelaySemaphore, 3000) != pdTRUE)
        {
        sendString(SerOutPort, TRUE, importantMessage, s_tab,
                "relay set was not successful", Dummy);

        returnValue = ERROR;
        } 
    else
        {

        switch (board)
            {
            case relay_mb:
                loopcnt = 0;
                loopend = NUMBER_OF_RLY_MB;
                break;

            case relay_eb:
                loopcnt = NUMBER_OF_RLY_MB;
                loopend = NUMBER_OF_TOTAL_RLY;
                break;
            }

        for (loopcnt; loopcnt < loopend; loopcnt++)
            {
            remoteRelayInputs[loopcnt] = value;
            }
        returnValue = OK;
        }

    return (returnValue);
}


void remoteRelaySemaphoreRelease (UINT8 requestNr, UINT8 nrOfResults, UINT32 *results)
{
    xSemaphoreGive (remoteRelaySemaphore);
}

