/* ---------------------------------------------------------------------------
 * digital_test.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: Test file for the digital connections
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Dez 11, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files
 * ---------------------------------------------------------------------------
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */ 
#include "digital_test.h"

#include "smc4000io.h"

#include "BusProtocol.h"
#include "protocolfunctions.h"
/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */

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

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

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

void digitalWrite (UINT8 senderId, UINT8 targetId, UINT8 requestNr, 
                     UINT8 functionId, UINT8 nrOfParams, UINT32 *params) 
{
    /* nrOfParams = 2
     * params[0]: digital output channel number
     * params[1]: digital output value (0: LOW, !0: HIGH)
     */
    
    int result;
    UINT32 channel;
    UINT16 doutValue;
    
    printf ("\nCalled function: digitalWrite\n");
    printf ("params[0]: %i\n", params[0]);
    printf ("params[1]: %i\n", params[1]);

    channel = params[0];
    
    if (params[1] != 0)
        {
        params[1] = 1;
        }
    
    if (channel < NUMBEROFDO_MB)
        {
        /* Channel is located in mainboard                                  */
        din0_7Read (IOCTL_interface, & doutValue); /* Get actual DoutValue  */
        doutValue &=~(1 << channel);        /* Reset channel bit to Zero    */
        doutValue |= (params[1] << channel);/* Set channel bit              */
        
        /* Write built Bitmask to IO-Control Processor                      */
        result = dout0_15Write (IOCTL_interface, & doutValue);
        }
    else if ((channel >= NUMBEROFDO_MB) && (channel < NUMBEROFTOTALDO))
        {
        /* Channel is located in extensionboard                             */
        dinext0_3Read (IOCTL_interface, &doutValue);
        doutValue &=~(1 << channel);        /* Reset channel bit to Zero    */
        doutValue |= (params[1] << channel);/* Set channel bit              */
        
        /* Write built Bitmask to IO-Control Processor                      */
        result = doutext0_3Write (IOCTL_interface, & doutValue);
        }

    /* Release digital semaphore on calling device with result function     */
    bpSendRpcResult(bushandler, REMOTEDEVICENUMBER, 20, 1, 0, NULL); 
    
    printf ("dout_Write returned: %d\n", result);
    
}

 void digitalWriteAll (UINT8 senderId, UINT8 targetId, UINT8 requestNr, 
                      UINT8 functionId, UINT8 nrOfParams, UINT32 *params) 

{
    /* nrOfParams = 2
     * params[0]: board type (0: MB, 1: EB)
     * params[1]: digital output value (0: LOW, !0: HIGH)
     */
    
    int result;
    UINT16 doutValue = 9;

    
    if (params[1] == 0)
        {
        /* All effected Outputs should be written to LOW                    */
        doutValue = 0x0000;
        }
    else if (params[1] == 1)
        {
        /* All effected Outputs should be written to HIGH                   */
        doutValue = 0x00FF;
        printf ("params[1] recognised as 1");
        }
    
    if (params[0] == 0)
        {
        /* Maindboard Outputs should be effected                            */
        result = dout0_15Write (IOCTL_interface, & doutValue);
        }
    else if (params[0] == 1)
        {
        /* Extensionboard Outputs should be effected                        */      
        result = doutext0_3Write (IOCTL_interface, & doutValue);        
        }

    /* Release digital semaphore on calling device with result function     */
    bpSendRpcResult(bushandler, REMOTEDEVICENUMBER, 20, 1, 0, NULL);
    
    printf ("dout_Write returned: %d\n", result);
}


void digitalRead (UINT8 senderId, UINT8 targetId, UINT8 requestNr, 
                     UINT8 functionId, UINT8 nrOfParams, UINT32 *params) 
{
    int result;
    INT32 dioReadResult;
    UINT32 channel;
    UINT16 doutValue;
    
    /* nrOfParams = 1
     * params[0]: digital input channel number
     */
    
    channel = params[0];
    printf ("\nCalled function: digitalRead\n");
    
    if (channel < NUMBEROFDI_MB)
        {
        /* work on digital input of Mainboard                               */
        result = din0_7Read (IOCTL_interface, & doutValue);
        printf ("digital read: %h\n", doutValue);
        dioReadResult = (doutValue & (0x0000 | (1 << channel)));
        }
    else if ((channel >= NUMBEROFDI_MB) && (channel < NUMBEROFTOTALDI))
        {
        /* Work on digital input of Extensionboard                          */
        result = doutext0_3Write (IOCTL_interface, & doutValue);
        printf ("digital read: %h\n", doutValue);
        }
    
    printf ("dout_Write returned: %d\n", result);

    
    bpSendRpcResult(bushandler, MASTERDEVICENUMBER, 11, 1, 1, &dioReadResult);  
}

void digitalReadAll (UINT8 senderId, UINT8 targetId, UINT8 requestNr, 
                     UINT8 functionId, UINT8 nrOfParams, UINT32 *params)
{
    /* nrOfParams = 1
     * params[0]: board type (0: MB, 1: EB)
     */
    
    int result;
    UINT32 doutValue[2];
    UINT8 channelcnt;
    
    printf ("Called function: digitalReadAll\n");
    printf ("params[0]: %i\n", params[0]);
    fflush( stdout );
    
    if (params[0] == 0)
        {     
        doutValue[0] = params[0];        
        result = din0_7Read (IOCTL_interface, & doutValue[1]); 
        printf ("digital read: %x\n", doutValue[1]);
        }
    else if (params[0] == 1)
        {
        doutValue[0] = params[0];
        result = doutext0_3Write (IOCTL_interface, & doutValue[1]);
        printf ("digital read: %x\n", doutValue[1]);
        }
    
    
    printf ("digitalReadAll returned: %d\n", result);

    bpSendRpcResult(bushandler, REMOTEDEVICENUMBER, 13, 1, 2, &doutValue);
    
}


void digitalMB_test_execute (void)
{
   printf ("THIS IS THE DIGITAL TEST SEQUENCE");

}
 
void digitalEB_test_execute (void)
{
   printf ("THIS IS THE DIGITAL TEST SEQUENCE");
 
}
 
