/**
 *  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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <apci3120.h>
#include <apci3120-events.h>
#include "../lib/apci3120Lib.h"
/***********************************************/

#define NB_READ 100
#define MAX_CHANNEL_NBR 16
#define SEQUENCE_ARRAY_SIZE 4
#define NUMBER_OF_SEQUENCE 120
#define NUMBER_OF_ACQUISITON SEQUENCE_ARRAY_SIZE * NUMBER_OF_SEQUENCE

uint8_t volatile stopProgram = 1;

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

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

static void process_signal(int signal)
{
  // (void)signal; /* Unused */
  stopProgram = 0;
}

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

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

  // 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;
  }
  fprintf(stdout, "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);

  // Initialize the elements of the sequence
  struct apci3120_sequence_acquisition_data sequence_data;

  sequence_data.sequence_array_size = 4;
  for ( cpt = 0; cpt < sequence_data.sequence_array_size; cpt++ )
  {
    sequence_data.sequence_channels[cpt] = cpt;
    sequence_data.gain[cpt] = APCI3120_1_GAIN;
    sequence_data.polarity[cpt] = APCI3120_BIPOLAR;
  }

  sequence_data.number_of_acquisition = NUMBER_OF_ACQUISITON;
  sequence_data.dma_mode              = APCI3120_DMA_USED;
  sequence_data.extern_trigger        = APCI3120_DISABLE;
  sequence_data.acquisition_mode      = APCI3120_SIMPLE_MODUS;
  sequence_data.acquisition_cycle     = APCI3120_CONTINUOUS;
  sequence_data.delay_timing          = 0;
  sequence_data.acquisition_timing    = 2500;

  fprintf(stdout, "Setting up the interrupt routine... \n");
  ret = i_APCI3120_SetBoardInterruptRoutine(boardIndex);
  if (ret != 0)
  {
    fprintf(stderr, "i_APCI3120_SetBoardInterruptRoutine error %d\n", ret);
    goto end;
  }

  usleep(1000000);

  fprintf(stdout, "launching the init analog input sequence acquisition...\n");
  //error = ioctl (fd, CMD_APCI3120_InitAnalogInputSequenceAcquisition, &sequence_data);
  ret = i_APCI3120_InitAnalogInputSequenceAcquisition(boardIndex, &sequence_data);
  if (ret != 0)
  {
    fprintf(stderr, "i_APCI3120_InitAnalogInputSequenceAcquisition error %d\n", ret);
    goto end;
  }


  usleep(1000000);

  ret = i_APCI3120_StartAnalogInputSequenceAcquisition(boardIndex, &sequence_data);
  if (ret != 0)
  {
    fprintf(stderr, "i_APCI3120_StartAnalogInputSequenceAcquisition error\n");
    goto end;
  }


  // Handle CTRL+C
  if (signal( SIGINT, &process_signal))
  { fprintf(stderr, "signal ctrl+c error "); goto end; }

  // Empty the event buffer
  printf("Empty the event buffer \n");
  {
    ret = i_APCI3120_EventEmpty(boardIndex);
    if (ret != 0)
    {
      fprintf(stderr, "i_APCI3120_EventEmpty error\n");
      goto end;
    }
  }

  fprintf(stderr, "Press ctrl+c to stop the sample\n");

  //Endless while, only interruptible by CTRL+C
  while(stopProgram)
  {
    apci3120_event_t evt[NUMBER_OF_SEQUENCE];
    ret = i_APCI3120_EventWait(boardIndex, evt);
    if (ret != 0)
    {
      fprintf(stderr, "i_APCI3120_EventWait error\n");
      goto end;
    }
    for (int y = 0; y < NUMBER_OF_SEQUENCE; y++) {
			printf("Event %d, Source: %d\n",y,evt[y].source);
      fprintf(stdout, "Content: \n");
      for ( i = 0; i < sequence_data.sequence_array_size; i++)
      {
        fprintf(stdout, "Channel[%d]: %d \t", i, evt[y].param1[i]);
      }
      fprintf(stdout, "\n");
    }
  }


end:
  fprintf(stdout, "Stop the sequence acquisition, reset the interrupt routine, close the board.\n");
  i_APCI3120_StopAnalogInputSequenceAcquisition(boardIndex);
  i_APCI3120_ResetBoardInterruptRoutine(boardIndex);
  i_APCI3120_CloseBoard(boardIndex);
  return ret;
}

