/** 
 *  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 <signal.h>
#include <apci3120.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <apci3120-events.h>
#include "../lib/apci3120Lib.h"
/***********************************************/

#define NB_READ 100
#define MAX_CHANNEL_NBR 16


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

int main(int argc, char *argv[])
{
    int          ret, i, j;
    uint8_t      boardCount;
    unsigned int boardIndex  = (unsigned int)-1;
    unsigned int arg[5] = {0};
 //   unsigned int channelIndex = 0;
    long long int average = 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);
    
    // Loop over all the analog channels to retrieve an averaged value per channel
    for (j = 0; j < MAX_CHANNEL_NBR; j++) 
    {
      average = 0;
      // Compute average values per channel
      for (i = 0; i < NB_READ; i++)
      {
        // Fill options
        arg[0] = j;     // Channel number (1 to 16)
        arg[1] = APCI3120_1_GAIN;  // Gain to use (APCI3120_1_GAIN, APCI3120_2_GAIN, APCI3120_5_GAIN and APCI3120_10_GAIN)
        arg[2] = APCI3120_BIPOLAR; // Polarity can be APCI3120_UNIPOLAR or APCI3120_BIPOLAR
        arg[3] = 50;               // Selection of the conversion time from 10us to 32767us
        arg[4] = APCI3120_DISABLE; // Interrupt flag (NOT IMPLEMENTED)
        ret = i_APCI3120_Read1AnalogInput(boardIndex, arg);
        if (ret != 0)
        { fprintf(stderr, "i_APCI3120_Read1AnalogInput error %d\n", ret); goto end; } 
        average += arg[0];
      }
      average = average / NB_READ;
      // gain x1 : full_scale = 10. gain x10 : full_scale = 1
      fprintf(stdout, "Channel %d, average : %lld (%f V)\n", j+1, average, computeVoltage(average, 10));  

      usleep(100000);
    }
  
    fprintf(stdout, "As stated in the documentation, Due to the very high impedance of the analog inputs, the measurement result is undefined, i.e. variable at inputs that are not connected (open). To minimise interfering factors, all the inputs that are not required should be connected with “Analog input GND” (see pin assignment of the documentation).");
end:
  return ret;
}
