/** digital_io.c
 *
 *  Author: N. Gissler
 *  Creation: 02.12.2014
 *  $LastChangedDate: 2014-12-15 16:34:39 +0100 (Mon, 15 Dec 2014) $
 *  $LastChangedRevision: 1328 $
 *
 *  Copyright (C) 2014 ADDI-DATA GmbH
 *
 *  ADDI-DATA GmbH
 *  Airpark Business Center
 *  Airport Boulevard B210
 *  77836 Rheinmünster
 *  Germany
 *  Tel: +49(0)7229/1847-0    |  Fax: +49(0)7229/1847-200
 *  http://www.addi-data-com  |  info@addi-data.com
 **/

/***********************************************/

#include <apci3120.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <apci3120-events.h>
#include "../lib/apci3120Lib.h"
/***********************************************/

int main(int argc, char *argv[])
{
    int          ret;
    uint8_t      boardCount;
    unsigned int boardIndex  = (unsigned int)-1;
    unsigned char digIn;

    /* Find how many boards are available */
    ret = i_APCI3120_GetNumberOfBoards(&boardCount);
    if (ret != 0)
    {
        fprintf(stderr, "i_APCI3120_GetNumberOfBoards: error %d\n", ret);
        return 1;
    }
    printf("Found %d board%s.\n", boardCount, boardCount > 1 ? "s" : "");
    if (boardCount == 0)
        return 2;

    /* Ask which board should be used */
    do
    {
        char inputBuffer[32];
        fprintf(stdout, "Select the board you would like to use (0 to %d): ", boardCount - 1);
        fflush(stdout);
        if (fgets(inputBuffer, sizeof(inputBuffer), stdin) != NULL && strlen(inputBuffer) > 1)
            boardIndex = (unsigned int)atoi(inputBuffer);
    } while (boardIndex >= boardCount);

    ret = i_APCI3120_Read4DigitalInputs(boardIndex, &digIn);
    if (ret != 0)
    { fprintf(stderr, "i_APCI3120_Read4DigitalInputs error %d\n", ret); goto end; } 
    
    fprintf (stdout, "Channel0 = %i Channel1 = %i Channel2 = %i Channel3 = %i.\n", (digIn&1), ((digIn&2)>>1), ((digIn&4)>>2), ((digIn&8)>>3));
    
end:
  return ret;
}
