/** 
 *  Authors: 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 long digOut;
    uint8_t action = 0;
    // 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);

    // Handling of the digital outputs 
  
    // Activate the memorisation of the digital outputs values  
    ret = i_APCI3120_SetDigitalOutputMemoryOn(boardIndex);
    if (ret != 0)
    { fprintf(stderr, "i_APCI3120_SetDigitalOutputMemoryOn error %d\n", ret); goto end; } 

    // Ask the user what to do with the digital outputs
    do
    {
      do
      {
        char inputBuffer[32];
        printf("Do you want to activate (1), desactivate (2) the digital outputs or quit the program (0) ?\n");
        fflush(stdout);
        if (fgets(inputBuffer, sizeof(inputBuffer), stdin) != NULL && strlen(inputBuffer) > 1)
          action = (uint8_t)atoi(inputBuffer);
      } while (action > 2);   
 
      switch(action)
      {
        case 0:
          break;

        case 1:
          digOut = 0;
          do
          {
            char inputBuffer[32];
            printf("Select which digital output(s) you want to set to 1 : bit 0 is DigOut0, bit 1 is Digout1, etc. (0 to 15): ");
            fflush(stdout);
            if (fgets(inputBuffer, sizeof(inputBuffer), stdin) != NULL && strlen(inputBuffer) > 1)
              digOut = (unsigned long)atoi(inputBuffer);
          } while (digOut > 15);   

          ret = i_APCI3120_Set4DigitalOutputsOn(boardIndex, digOut);
          if (ret != 0)
          { fprintf(stderr, "i_APCI3120_Set4DigitalOutputsOn error %d\n", ret); goto end; } 
          
          fprintf(stdout, "Digital outputs activated with 0x%lx\n", digOut);   
          break;

        case 2:
          digOut = 0;
          do
          {
            char inputBuffer[32];
            printf("Select which digital output(s) you want to set to 0 : bit 0 is DigOut0, bit 1 is Digout1, etc. (0 to 15): ");
            fflush(stdout);
            if (fgets(inputBuffer, sizeof(inputBuffer), stdin) != NULL && strlen(inputBuffer) > 1)
              digOut = (unsigned long)atoi(inputBuffer);
          } while (digOut > 15);   

          ret = i_APCI3120_Set4DigitalOutputsOff(boardIndex, digOut);
          if (ret != 0)
          { fprintf(stderr, "i_APCI3120_Set4DigitalOutputsOff error %d\n", ret); goto end; } 
          
          fprintf(stdout, "Digital outputs disactivated with 0x%lx\n", digOut);   
          break;

        default:
          break;
      }
    } while (action != 0);
 
end:
    ret = i_APCI3120_SetDigitalOutputMemoryOff(boardIndex);
    if (ret != 0)
    { fprintf(stderr, "i_APCI3120_SetDigitalOutputMemoryOff error %d\n", ret); } 
  return ret;
}
