void MainWindow::on_stopButton_clicked()
{
    int ULStat = cbStopBackground (0,AIFUNCTION);

    cbWinBufFree(MemHandle);
    exit(1); // exits background operation and exits gui application
}
Beispiel #2
0
void main()
    {
    /* Variable Declarations */
    int I, J, ULStat, BoardNum;
    int ChipNum, CounterNum;
    int FOutDivider, FOutSource, Compare1, Compare2, TimeOfDay;
    int GateControl, CounterEdge, CountSource, SpecialGate;
    int ReLoad, RecycleMode, BCDMode, CountDirection, OutputControl;
    int LoadValue;
    int RegName;
    int IntCount, NumCntrs;
    short Status;
    long CurCount, CurIndex, FirstPoint;
    short  CntrControl[MAXNUMCNTRS];
    char *StatusStr;
    WORD *DataBuffer;
    float    RevLevel = (float)CURRENTREVNUM;

  /* Declare UL Revision Level */
   ULStat = cbDeclareRevision(&RevLevel);

    /* Initiate error handling
    Parameters:
        PRINTALL :all warnings and errors encountered will be printed
        DONTSTOP :program will continue even if error occurs.
                     Note that STOPALL and STOPFATAL are only effective in 
                     Windows applications, not Console applications. 
   */
    cbErrHandling (PRINTALL, DONTSTOP);

    /* set up the display screen */
    ClearScreen();
    printf ("Demonstration of 9513 Counter Functions.\n\n");
    printf ("Press any key to quit.\n\n");

    /* Initialize the board level features  */
    BoardNum = 0;           /* Number used by CB.CFG to describe this board */
    ChipNum = 1;            /* chip being init'ed (1 for CTR5, 1 or 2 for CTR10) */
    FOutDivider = 10;       /* sets up OSC OUT for 10Hz signal which can be */
    FOutSource = FREQ5;     /* used as an interrrupt source for this example */
    Compare1 = DISABLED;    /* Status of comparator 1 */
    Compare2 = DISABLED;    /* Status of comparator 2 */
    TimeOfDay = DISABLED;   /* Time of day control mode */
    ULStat = cbC9513Init (BoardNum, ChipNum, FOutDivider, FOutSource, Compare1,
                          Compare2, TimeOfDay);

    /* Set the configurable operations of counters 1 and 2 */
    CounterNum = 1;         /* Counter to be configured (0-5) */
    GateControl = NOGATE;   /* Gate control value */
    CounterEdge = POSITIVEEDGE;    /* Which edge to count */
    CountSource = FREQ3;    /* Signal source for counter */
    SpecialGate = DISABLED; /* Status of special gate */
    ReLoad = LOADREG;       /* Method of reloading the counter */
    RecycleMode = RECYCLE;  /* Recycle mode */
    BCDMode = DISABLED;     /* Counting mode, BCD or binary */
    CountDirection = COUNTUP;  /* Counting direction (up or down) */
    OutputControl = ALWAYSLOW; /* Output signal type and level */
    ULStat = cbC9513Config (BoardNum, CounterNum , GateControl, CounterEdge,
                            CountSource, SpecialGate, ReLoad, RecycleMode,
                            BCDMode, CountDirection, OutputControl);
    CounterNum = 2;
    CountSource = FREQ5;
    ULStat = cbC9513Config (BoardNum, CounterNum , GateControl, CounterEdge,
                            CountSource, SpecialGate, ReLoad, RecycleMode,
                            BCDMode, CountDirection, OutputControl);

    /* Load the 2 counters with starting values of zero with cbCLoad()
        Parameters:
            BoardNum    :the number used by CB.CFG to describe this board
            RegName     :the counter to be loading with the starting value
            LoadValue   :the starting value to place in the counter */
    LoadValue = 0;
    for (RegName=LOADREG1; RegName<=LOADREG2; RegName++)
        ULStat = cbCLoad (BoardNum, RegName, LoadValue);

    /* set the counters to store their values upon an interrupt
        Parameters:
            BoardNum    :the number used by CB.CFG to describe this board
            IntCount    :maximum number of interrupts
            CntrControl[]:array which indicates the channels to be used
            DataBuffer[] :array that receives the count values for enabled
                          channels each time an interrupt occurs */
    IntCount = 100;

   
	/*
		DataBuffer must be able to hold  NumCntrs*IntCount  values, even if
		we're only enabling a few counters. We'll allocate enough space for
		MAXNUMCNTRS in case NumCntrs has not been updated.
	*/
	DataBuffer = (WORD *)cbWinBufAlloc( MAXNUMCNTRS*IntCount );

	/* 	
		The actual number of counters onboard.  
		UPDATE THIS	VALUE TO REFLECT THE NUMBER OF COUNTERS FOR THE INSTALLED MODEL.
	*/	
	NumCntrs = 10;			
    for (I = 0; I < NumCntrs; I++)
        CntrControl[I] = DISABLED;

	for (J = 0; J< IntCount; J++)
		for (I = 0; I<NumCntrs; I++)
			DataBuffer[J*NumCntrs + I] = 0;
        

    /* enable the channels to be monitored */
    CntrControl[0] = ENABLED;
    CntrControl[1] = ENABLED;

    ULStat = cbCStoreOnInt (BoardNum, IntCount, CntrControl, DataBuffer);

    MoveCursor (1, 14);
    printf ("When an interrupt is generated, the table below will be updated.");

    MoveCursor (5, 15);
    printf ("Counter   Status      Value ");

    Status = RUNNING;
    while (!kbhit() && Status==RUNNING)
        {
		/* We'll display the latest counts retrieved and the current INT count*/
		/* Check the status of the current background operation
        Parameters:
            BoardNum  :the number used by CB.CFG to describe this board
            Status    :current status of the operation (IDLE or RUNNING)
            CurCount  :current number of samples collected
            CurIndex  :index to the last data value transferred 
            FunctionType: A/D operation (CTRFUNCTION)*/
        cbGetStatus (BoardNum, &Status, &CurCount, &CurIndex,CTRFUNCTION);

        for (I = 0; I < 5; I++)
            {
            if (CntrControl[I] == DISABLED)
                StatusStr = "DISABLED";
            else
                StatusStr = "ENABLED";

            MoveCursor (5, 16 + I);
			
			FirstPoint=0;

			/* 
				The latest set of data in the data buffer is starts at
					FirstPoint = NumCntrs*CurIndex;
			*/
			if (CurIndex>0)
				{
				FirstPoint = NumCntrs*CurIndex;
				}	

			printf (" %2u       %8s    %5u\n", I, StatusStr, DataBuffer[FirstPoint + I]);
            }

        
        printf ("Number of interrupts so far=%-ld\n", CurCount);
        }

        /* the BACKGROUND operation must be explicitly stopped
        Parameters:
             BoardNum    :the number used by CB.CFG to describe this board 
             FunctionType: counter operation (CTRFUNCTION)*/  
    ULStat = cbStopBackground (BoardNum,CTRFUNCTION);

	cbWinBufFree((int)DataBuffer);

    MoveCursor (1, 22);
    printf ("\n");

    }
Beispiel #3
0
void main ()
    {
    /* Variable Declarations */
    int Row, Col;
    int PrnNum=1000;
    int BoardNum = 0;
    int ULStat = 0;
    int LowChan = 0;
    int HighChan = 0;
    int Gain = BIP5VOLTS;
    short Status = 0;
    long CurCount;
    long CurIndex;
    int Count = 10000;
    long Rate = 3125;
    unsigned Options;
    WORD *ADData;
   float    RevLevel = (float)CURRENTREVNUM;

  /* Declare UL Revision Level */
   ULStat = cbDeclareRevision(&RevLevel);

    ADData = (WORD*)cbWinBufAlloc(10000);
    if (!ADData)    /* Make sure it is a valid pointer */
        {
        printf("\nout of memory\n");
        exit(1);
        }

    /* Initiate error handling
        Parameters:
            PRINTALL :all warnings and errors encountered will be printed
            DONTSTOP :program will continue even if error occurs.
                     Note that STOPALL and STOPFATAL are only effective in 
                     Windows applications, not Console applications. 
   */
    cbErrHandling (PRINTALL, DONTSTOP);

    /* set up the display screen */
    ClearScreen();
    printf ("Demonstration of cbAInScan() in BACKGROUND mode\n\n");
    printf ("%d data points are being collected in the background. This program\n", Count);
    printf ("could be doing many other things right now, but it will display every\n");
    printf ("1000th data value collected.\n\n");
    printf ("10000 Points Being Collected:\n\n");
    printf ("Press any key to quit.\n\n");
    printf ("Point #   Value\n");
    printf ("-------   ------\n");
    GetTextCursor (&Col, &Row);

    /* Collect the values with cbAInScan() in BACKGROUND mode
        Parameters:
             BoardNum    :the number used by CB.CFG to describe this board
             LowChan     :low channel of the scan
             HighChan    :high channel of the scan
             Count       :the total number of A/D samples to collect
             Rate        :sample rate in samples per second
             Gain        :the gain for the board
             ADData[]    :the array for the collected data values
             Options     :data collection options */
    Options = CONVERTDATA + BACKGROUND + SINGLEIO;
    ULStat = cbAInScan (BoardNum, LowChan, HighChan, Count, &Rate,
                                            Gain, ADData, Options);

    /* During the BACKGROUND operation, check the status, print every ten values */
    Status = RUNNING;
    while (!kbhit() && Status==RUNNING)
        {
        /* Check the status of the current background operation
        Parameters:
            BoardNum  :the number used by CB.CFG to describe this board
            Status    :current status of the operation (IDLE or RUNNING)
            CurCount  :current number of samples collected
            CurIndex  :index to the last data value transferred 
            FunctionType: A/D operation (AIFUNCTIOM)*/
        ULStat = cbGetStatus (BoardNum, &Status, &CurCount, &CurIndex,AIFUNCTION);
        
        /* Check if there are data in the buffer */
        if (CurCount > 0)
            {
            MoveCursor (Col + 25, Row - 4);
            printf ("%ld", CurCount);
            if (CurCount > PrnNum)    /* display every 100th data point */
                {
                if (CurCount > 0)
                    {
                    MoveCursor (Col, Row - 1 + (PrnNum/ 1000));
                    printf ("  %4d    %5u",  PrnNum, ADData[PrnNum]);
                    }
                PrnNum = PrnNum+1000;
                }
            }
        }
    MoveCursor (Col + 30, Row - 4);
    if (Status == IDLE)
        printf ("  Data Collection finished.");
    else
        printf ("Data collection interrupted by user.");

    /* the BACKGROUND operation must be explicitly stopped
        Parameters:
             BoardNum    :the number used by CB.CFG to describe this board 
             FunctionType: A/D operation (AIFUNCTIOM)*/  
    ULStat = cbStopBackground (BoardNum,AIFUNCTION);

    MoveCursor (1, 22);
    printf ("\n");

    GetTextCursor (&Col, &Row);
    cbWinBufFree((int)ADData);
}
Beispiel #4
0
void main ()
    {
    /* Variable Declarations */
    int Row, Col;
    int BoardNum = 0;
    int NumChan = 2;
    int ULStat = 0;
    int LowChan, HighChan;
    int Options;
    int Gain = BIP5VOLTS;
    double DAData[NUMCHANS * NUMPOINTS];
    long Count, Rate;
	long CurCount, CurIndex;
	short Status = IDLE;
    float    RevLevel = (float)CURRENTREVNUM;

  /* Declare UL Revision Level */
   ULStat = cbDeclareRevision(&RevLevel);

    /* Initiate error handling
        Parameters:
            PRINTALL :all warnings and errors encountered will be printed
            DONTSTOP :program will continue even if error occurs.
                     Note that STOPALL and STOPFATAL are only effective in 
                     Windows applications, not Console applications. 
   */
    cbErrHandling (PRINTALL, DONTSTOP);

    /* set up the display screen */
    ClearScreen();
    printf ("Demonstration of cbAOutScan() with SCALEDATA option\n\n");
    printf ("Press any key to quit.\n\n\n");
    GetTextCursor (&Col, &Row);

	/* load the output array with values */

	// output (low)
	DAData[0] = 0.0;        
	DAData[1] = 0.0;

	// output (high))
	DAData[2] = 5.0;
	DAData[3] = 5.0;

	LowChan = 0;
    HighChan = 1;

    /* send the output values to the D/A range using cbAOutScan() */
    /* Parameters:
            BoardNum    :the number used by CB.CFG to describe this board
            LowChan     :the lower channel of the scan
            HighChan    :the upper channel of the scan
            Count       :the number of D/A values to send
            Rate        :send rate in values/second
            Gain        :the gain of the D/A
            ADData[]    :array of values to send to the scanned channels
            Options     :data send options  */
    Count = NUMPOINTS * NUMCHANS;
    Rate = 1000;
    Options = BACKGROUND | CONTINUOUS | SCALEDATA;
    ULStat = cbAOutScan (BoardNum, LowChan, HighChan, Count, &Rate, Gain, DAData,
                         Options);

    /* show the results */
    printf ("Here are the output values \n\n");

	printf ("Output(low):   %.8f       %.8f   \n",DAData[0],DAData[1]);
	printf ("Output(high):  %.8f       %.8f   \n\n",DAData[2],DAData[3]);

	GetTextCursor (&Col, &Row);

	if(ULStat == NOERRORS)
		Status = RUNNING;

	/* During the BACKGROUND operation, check the status */
    while (!kbhit() && Status==RUNNING)
        {
        /* Check the status of the current background operation
        Parameters:
            BoardNum  :the number used by CB.CFG to describe this board
            Status    :current status of the operation (IDLE or RUNNING)
            CurCount  :current number of samples collected
            CurIndex  :index to the last data value transferred 
            FunctionType: A/D operation (AOFUNCTION)*/
        ULStat = cbGetStatus (BoardNum, &Status, &CurCount, &CurIndex,AOFUNCTION);
		printf ("Current Count: %u \n", CurCount);
		printf ("Current Index: %d \n", CurIndex);
		printf ("\nPress any key to quit.\n");
		MoveCursor (Col, Row);
		}
	MoveCursor (Col, Row + 4);
	printf ("\nData output terminated.");

	/* The BACKGROUND operation must be explicitly stopped
        Parameters:
             BoardNum    :the number used by CB.CFG to describe this board 
             FunctionType: A/D operation (AOFUNCTION)*/  
    ULStat = cbStopBackground (BoardNum,AOFUNCTION);;

}
Beispiel #5
0
void main ()
    {
    /* Variable Declarations */
    int Row, Col, K;
    int BoardNum = 0;
    int ULStat = 0;
    int LowChan = 0;
    int HighChan = 0;
    int Gain = BIP5VOLTS;
    short Status = 0;
    long CurCount, LastCount;
    long CurIndex;
    long NumPoints = 10;
    long Count = 10;
    long Rate = 3125;
    WORD  ADData[10];
    unsigned  Options;
    float     revision = (float)CURRENTREVNUM;

   /* Declare Revision level of the Universal Library */
    ULStat = cbDeclareRevision(&revision);

   /* Initiate error handling
        Parameters:
            PRINTALL :all warnings and errors encountered will be printed
            DONTSTOP :program will continue even if error occurs.
                     Note that STOPALL and STOPFATAL are only effective in 
                     Windows applications, not Console applications. 
   */
    cbErrHandling (PRINTALL, DONTSTOP);

    /* set up the display screen */
    ClearScreen();
    printf ("Demonstration of cbAInScan() in BACKGROUND mode followed by data conversion\n\n");
    printf ("Collecting %ld data points.   Press any key to quit.\n\n", Count);
    printf ("Value: the 16-bit integer read from the board.\n");
    printf ("Converted value: the 12-bit value after the 4 channel bits have been removed.\n\n");
    GetTextCursor (&Col, &Row);
    printf ("Collecting data");

    /* Collect the values with cbAInScan() in BACKGROUND mode
        Parameters:
             BoardNum    :the number used by CB.CFG to describe this board
             LowChan     :low channel of the scan
             HighChan    :high channel of the scan
             Count       :the total number of A/D samples to collect
             Rate        :sample rate in samples per second
             Gain        :the gain for the board
             ADData[]    :the array for the collected data values
             Options     :data collection options */
    Options = NOCONVERTDATA + BACKGROUND;
    ULStat = cbAInScan (BoardNum, LowChan, HighChan, Count, &Rate,
                                            Gain, ADData, Options);

    /* During the BACKGROUND operation, check status */
    Status = RUNNING;
    while (!kbhit() && Status==RUNNING)
        {
        /* Check the status of the current background operation
        Parameters:
            BoardNum  :the number used by CB.CFG to describe this board
            Status    :current status of the operation (IDLE or RUNNING)
            CurCount  :current number of samples collected
            CurIndex  :index to the last data value transferred 
            FunctionType: A/D operation (AIFUNCTIOM)*/
        ULStat = cbGetStatus (BoardNum, &Status, &CurCount, &CurIndex,AIFUNCTION);

        /* check if there is data in the buffer */
        if (CurCount != LastCount)
            {
            printf (".");
            LastCount = CurCount;
            }
        }

    if (Status == IDLE)
        printf (" Data Collection finished.");
    else
        printf (" Data collection terminated by the user.");

    /* the BACKGROUND operation must be explicitly stopped
       Parameters:
          BoardNum    :the number used by CB.CFG to describe this board
          FunctionType: A/D operation (AIFUNCTIOM)*/  
    ULStat = cbStopBackground (BoardNum,AIFUNCTION);

    /* show the collected data, then show the converted data */
    MoveCursor (Col, Row + 2);
    printf ("Point#    Value \n");
    printf ("-------   -------");

    /* show the 16-bit values that were collected */
    for (K = 0; K < (int)Count; K++)
        {
        MoveCursor (Col, Row + 4 + K);
        printf ("  %3u     %5u", K, ADData[K]);
        }

    /* use cbAConvertData to convert the 16-bit values to 12-bit values
    Parameters:
            NumPoints   :the number of data values to convert
            ADData[]    :the array holding the 16-bit values, 12-bit values
                         are returned in the same array
            NULL        :channel tags not returned */
    ULStat = cbAConvertData (BoardNum, NumPoints, ADData, NULL);

    /* display the converted data */
    MoveCursor (Col + 22, Row + 2);
    printf ("Converted value\n");
    MoveCursor (Col + 22, Row + 3);
    printf ("---------------");

    for (K = 0; K < (int)NumPoints; K++)
        {
        MoveCursor (Col + 25, Row + 4 + K);
        printf ("  %u", ADData[K]);
        }

    MoveCursor (Col, Row + 15);
    printf ("Data conversion completed with cbConvertData.");

    printf ("\n");
}
Beispiel #6
0
void main ()
    {
    /* Variable Declarations */
    int Row, Col;
	int  BoardNum = 0;
	int Options;
    long PreTrigCount, TotalCount, Rate, ChanCount;
	short ChanArray[NUMCHANS];
	short ChanTypeArray[NUMCHANS];
    short GainArray[NUMCHANS];
    int ULStat = 0;
    short Status = IDLE;
    long CurCount;
    long CurIndex, DataIndex;
	int PortNum, Direction, CounterNum;
    WORD *ADData;
    float    RevLevel = (float)CURRENTREVNUM;

  /* Declare UL Revision Level */
   ULStat = cbDeclareRevision(&RevLevel);

    ADData = (WORD*)cbWinBufAlloc(NUMPOINTS * NUMCHANS);
    if (!ADData)    /* Make sure it is a valid pointer */
        {
        printf("\nout of memory\n");
        exit(1);
        }

    /* Initiate error handling
        Parameters:
            PRINTALL :all warnings and errors encountered will be printed
            DONTSTOP :program will continue even if error occurs.
                     Note that STOPALL and STOPFATAL are only effective in 
                     Windows applications, not Console applications. 
   */
    cbErrHandling (PRINTALL, DONTSTOP);

    /* set up the display screen */
    ClearScreen();

    printf ("Demonstration of cbDaqInScan()\n\n");
    printf ("Data are being collected in the BACKGROUND, CONTINUOUSLY\n");


	/* load the arrays with values */
    ChanArray[0] = 0;
	ChanTypeArray[0] = ANALOG;
    GainArray[0] = BIP10VOLTS;

    ChanArray[1] = FIRSTPORTA;
	ChanTypeArray[1] = DIGITAL16;
    GainArray[1] = NOTUSED;

	ChanArray[2] = 0;
	ChanTypeArray[2] = CTR32LOW;
    GainArray[2] = NOTUSED;

	ChanArray[3] = 0;
	ChanTypeArray[3] = CTR32HIGH;
    GainArray[3] = NOTUSED;

	/* configure FIRSTPORTA and FIRSTPORTB for digital input */
	
	PortNum = FIRSTPORTA;
    Direction = DIGITALIN;
    ULStat = cbDConfigPort (BoardNum, PortNum, Direction);

	PortNum = FIRSTPORTB;
	ULStat = cbDConfigPort (BoardNum, PortNum, Direction);

	// configure counter 0
	CounterNum = ChanArray[2];
    ULStat = cbCConfigScan(BoardNum, CounterNum, ROLLOVER, CTR_DEBOUNCE_NONE, 0, CTR_RISING_EDGE, 0, CounterNum);

    /* Collect the values with cbDaqInScan() in BACKGROUND mode, CONTINUOUSLY
        Parameters:
            BoardNum    :the number used by CB.CFG to describe this board
            ChanArray[] :array of channel values
			ChanTypeArray[] : array of channel types
            GainArray[] :array of gain values
            ChanCount    :the number of channels
            Rate        :sample rate in samples per second
			PretrigCount:number of pre-trigger A/D samples to collect
            TotalCount  :the total number of A/D samples to collect
            ADData[]    :the array for the collected data values
            Options     :data collection options  */

    ChanCount = NUMCHANS;
	PreTrigCount =0;
	TotalCount = NUMPOINTS * NUMCHANS;
	Rate = 1000;								             /* sampling rate (samples per second) */
	Options = CONVERTDATA + BACKGROUND + CONTINUOUS;         /* data collection options */

	ULStat = cbDaqInScan(BoardNum, ChanArray, ChanTypeArray, GainArray, ChanCount, &Rate, &PreTrigCount, &TotalCount, ADData, Options);

    printf ("--------------------------------------------------------------------------------");
    printf ("| Your program could be doing something useful here while data are collected...|");
    printf ("--------------------------------------------------------------------------------");
    printf ("\nCollecting data...\n\n");
    printf ("Press any key to quit.\n\n");

    GetTextCursor (&Col, &Row);

	if(ULStat == NOERRORS)
		Status = RUNNING;

    /* During the BACKGROUND operation, check the status */
    while (!kbhit() && Status==RUNNING)
        {
        /* Check the status of the current background operation
        Parameters:
            BoardNum  :the number used by CB.CFG to describe this board
            Status    :current status of the operation (IDLE or RUNNING)
            CurCount  :current number of samples collected
            CurIndex  :index to the last data value transferred 
            FunctionType: A/D operation (DAQIFUNCTION)*/
        ULStat = cbGetStatus (BoardNum, &Status, &CurCount, &CurIndex,DAQIFUNCTION);

        /* check the current status of the background operation */
        if (Status == RUNNING)
            {
			DataIndex = CurIndex -  CurIndex % NUMCHANS - NUMCHANS;
			if(DataIndex>0)
				{
					MoveCursor (Col, Row);
					printf ("Channel 0   Data point: %3ld   ", DataIndex);
					printf ("  Value: %d  \n",ADData[DataIndex]);
					DataIndex++;
					printf ("FIRSTPORTA  Data point: %3ld   ", DataIndex);
					printf ("  Value: %d  \n",ADData[DataIndex]);
					DataIndex++;
					printf ("Counter 0   Data point: %3ld   ", DataIndex);
					printf ("  Value: %u  ",ADData[DataIndex] + (ADData[DataIndex+1]<<16));   // 32-bit counter
				}
            }
        }
    printf ("\n");
    MoveCursor (Col, Row + 4);
    printf ("Data collection terminated.");

    /* The BACKGROUND operation must be explicitly stopped
        Parameters:
             BoardNum    :the number used by CB.CFG to describe this board 
             FunctionType: A/D operation (DAQIFUNCTION)*/  
    ULStat = cbStopBackground (BoardNum,DAQIFUNCTION);

    cbWinBufFree((int)ADData);
    MoveCursor (1, 22);
    printf ("\n");
}
void main ()
    {
    /* Variable Declarations */
    int Row, Col;
	int BoardNum = 0;
	int Options;
    long PreTrigCount, TotalCount, Rate, ChanCount;
	short ChanArray[NUMCHANS];
	short ChanTypeArray[NUMCHANS];
    short GainArray[NUMCHANS];
    int ULStat = 0;
    short Status = RUNNING;
    int TrigSource, TrigSense, TrigChan, ChanType, Gain, TrigEvent;
	float Level, Variance;
	
    long CurCount;
    long CurIndex, DataIndex;
	int PortNum, Direction;
    WORD *ADData;
    float    RevLevel = (float)CURRENTREVNUM;

  /* Declare UL Revision Level */
   ULStat = cbDeclareRevision(&RevLevel);

    ADData = (WORD*)cbWinBufAlloc(NUMPOINTS * NUMCHANS);
    if (!ADData)    /* Make sure it is a valid pointer */
        {
        printf("\nout of memory\n");
        exit(1);
        }

    /* Initiate error handling
        Parameters:
            PRINTALL :all warnings and errors encountered will be printed
            DONTSTOP :program will continue even if error occurs.
                     Note that STOPALL and STOPFATAL are only effective in 
                     Windows applications, not Console applications. 
   */
    cbErrHandling (PRINTALL, DONTSTOP);

    /* set up the display screen */
    ClearScreen();

    printf ("Demonstration of cbDaqSetTrigger()\n\n");


	/* load the arrays with values */
    ChanArray[0] = 0;
	ChanTypeArray[0] = ANALOG;
    GainArray[0] = BIP10VOLTS;

    ChanArray[1] = FIRSTPORTA;
	ChanTypeArray[1] = DIGITAL8;
    GainArray[1] = NOTUSED;

	ChanArray[2] = 0;
	ChanTypeArray[2] = CTR16;
    GainArray[2] = NOTUSED;


	/* configure FIRSTPORTA for digital input */
	
	PortNum = FIRSTPORTA;
    Direction = DIGITALIN;
    ULStat = cbDConfigPort (BoardNum, PortNum, Direction);

	/* Set Triggers 
		Parameters:
			BoardNum    :the number used by CB.CFG to describe this board
			TrigSource	:trigger source
			TrigSense	:trigger sensitivity
			TrigChan	:trigger channel
			ChanType	:trigger channel type
			Gain		:trigger channel gain
			Level		:trigger level
			Variance	:trigger variance
			TrigEvent	:trigger event type */

	/* Start trigger settings.
	   AD conversions are enabled when analog channel 0 makes a transition from below 2 V to above.*/
	TrigSource = TRIG_ANALOG_SW;
	TrigSense = RISING_EDGE;
	TrigChan = ChanArray[0];
	ChanType = ChanTypeArray[0];
	Gain = GainArray[0];
	Level = 2.0;
	Variance = 0;
	TrigEvent = START_EVENT;

	/* Set start trigger */
	ULStat = cbDaqSetTrigger(BoardNum, TrigSource, TrigSense, TrigChan, ChanType , Gain, Level, Variance, TrigEvent);

	/* Stop trigger settings. 
	   AD conversions are terminated when counter 0 reaches 100 counts.*/
	TrigSource = TRIG_COUNTER;
	TrigSense = ABOVE_LEVEL;
	TrigChan = ChanArray[2];
	ChanType = ChanTypeArray[2];
	Gain = GainArray[2];
	Level = 100;
	Variance = 0;
	TrigEvent = STOP_EVENT;

	/* Set stop trigger */
	ULStat = cbDaqSetTrigger(BoardNum, TrigSource, TrigSense, TrigChan, ChanType , Gain, Level, Variance, TrigEvent);


    /* Collect the values with cbDaqInScan() in BACKGROUND mode, CONTINUOUSLY
        Parameters:
            BoardNum    :the number used by CB.CFG to describe this board
            ChanArray[] :array of channel values
			ChanTypeArray[] : array of channel types
            GainArray[] :array of gain values
            ChanCount    :the number of channels
            Rate        :sample rate in samples per second
			PretrigCount:number of pre-trigger A/D samples to collect
            TotalCount  :the total number of A/D samples to collect
            ADData[]    :the array for the collected data values
            Options     :data collection options  */
    
	ChanCount = NUMCHANS;
	PreTrigCount = 0;
	TotalCount = NUMPOINTS * NUMCHANS;
	Rate = 1000;								             /* sampling rate (samples per second) */
	Options = CONVERTDATA + BACKGROUND + CONTINUOUS + EXTTRIGGER;         /* data collection options */

	ULStat = cbDaqInScan(BoardNum, ChanArray, ChanTypeArray, GainArray, ChanCount, &Rate, &PreTrigCount, &TotalCount, ADData, Options);

	if (ULStat==NOERRORS)
		{

		printf ("\nTrigger signals are needed for this sample.\nSignals required: ACH0 - signal that transitions from below 2V to above.\nCNT0 should have a TTL signal applied.");
	    
		printf ("\n\nStart Trigger Source: Channel 0\n");
		printf ("Waiting for start trigger....\n\n");
		
		printf ("Press any key to quit.\n\n");
   
		GetTextCursor (&Col, &Row);

		/* During the BACKGROUND operation, check the status */
		while (!kbhit() && Status==RUNNING)
			{
			/* Check the status of the current background operation
			Parameters:
				BoardNum  :the number used by CB.CFG to describe this board
				Status    :current status of the operation (IDLE or RUNNING)
				CurCount  :current number of samples collected
				CurIndex  :index to the last data value transferred 
				FunctionType: A/D operation (DAQIFUNCTION)*/
			ULStat = cbGetStatus (BoardNum, &Status, &CurCount, &CurIndex,DAQIFUNCTION);

			/* check the current status of the background operation */
		
			DataIndex = CurIndex;
			if(DataIndex>=0)
				{
					MoveCursor (Col, Row);
					printf ("Triggered!\n\n");
					printf ("\nCollecting data...\n\n");
					printf ("Channel 0   Data point: %3ld   ", DataIndex);
					printf ("  Value: %d  \n",ADData[DataIndex]);
					DataIndex++;
					printf ("FIRSTPORTA  Data point: %3ld   ", DataIndex);
					printf ("  Value: %d  \n",ADData[DataIndex]);
					DataIndex++;
					printf ("Counter 0   Data point: %3ld   ", DataIndex);
					printf ("  Value: %d  ",ADData[DataIndex]);
					
					printf ("\n\nStop Trigger Source: Counter 0 \n");
					if (Status==RUNNING)
						printf ("Waiting for stop trigger...\n");
					else
						printf ("\n\nTriggered!");
				}
			
/*			else
				{
					MoveCursor (Col, Row + 11);
					printf ("\nTriggered!");
				}*/
			
			}

		printf ("\n");
		MoveCursor (Col, Row + 13);
		printf ("Data collection terminated.");
		}

    /* The BACKGROUND operation must be explicitly stopped
        Parameters:
             BoardNum    :the number used by CB.CFG to describe this board 
             FunctionType: A/D operation (DAQIFUNCTION)*/  
    ULStat = cbStopBackground (BoardNum,DAQIFUNCTION);

    cbWinBufFree((int)ADData);
    MoveCursor (1, 22);
    printf ("\n");
}
void MainWindow::on_startButton_clicked()
{
    /* Variable Declarations */
    int BoardNum = 0;
    int ULStat = 0;
    int LowChan = 0;
    int HighChan = 0;
    int Gain = BIP5VOLTS;
    short Status = RUNNING;
    long CurCount;
    long CurIndex;
    long Count = 100000;
    long Rate = 4000;
    HANDLE MemHandle = 0;
    WORD *ADData;
    DWORD *ADData32;
    unsigned Options;
    float revision = (float)CURRENTREVNUM;
    BOOL HighResAD = FALSE;
    int ADRes;
    SamplingThread  samplingThread;
    samplingThread.setInterval( .25 );

   /* Declare Revision level of the Universal Library */
    ULStat = cbDeclareRevision(&revision);

    /* Initiate error handling
        Parameters:
            PRINTALL :all warnings and errors encountered will be printed
            DONTSTOP :program will continue even if error occurs.
                     Note that STOPALL and STOPFATAL are only effective in
                     Windows applications, not Console applications.
   */
    cbErrHandling (PRINTALL, DONTSTOP);

    /* Get the resolution of A/D */
    cbGetConfig(BOARDINFO, BoardNum, 0, BIADRES, &ADRes);

    /* check If the resolution of A/D is higher than 16 bit.
       If it is, then the A/D is high resolution. */
    if(ADRes > 16)
        HighResAD = TRUE;

    /*  set aside memory to hold data */
    if(HighResAD)
    {
        MemHandle = cbWinBufAlloc32(Count);
        ADData32 = (DWORD*) MemHandle;
    }
    else
    {
        MemHandle = cbWinBufAlloc(Count);
        ADData = (WORD*) MemHandle;
    }

    /* Make sure it is a valid pointer */
    if (!MemHandle)
        exit(1); /* out of memory */
    samplingThread.setADData(ADData);
    /* set up the display screen */
    /* Demonstration of cbAInScan() */
    /* Data are being collected in the BACKGROUND, CONTINUOUSLY */

    /* Collect the values with cbAInScan() in BACKGROUND mode, CONTINUOUSLY
        Parameters:
             BoardNum    :the number used by CB.CFG to describe this board
             LowChan     :low channel of the scan
             HighChan    :high channel of the scan
             Count       :the total number of A/D samples to collect
             Rate        :sample rate in samples per second
             Gain        :the gain for the board
             ADData[]    :the array for the collected data values
             Options     :data collection options */
    Options = CONVERTDATA + BACKGROUND + CONTINUOUS;
    ULStat = cbAInScan (BoardNum, LowChan, HighChan, Count, &Rate, Gain,
                        MemHandle, Options);
    samplingThread.start();
    d_plot->start();

    /* Your program could be doing something useful here while data are collected */

    /* During the BACKGROUND operation, check the status */

    while (Status==RUNNING)
    {
        qApp->processEvents();
//        // Check the status of the current background operation
//        /*Parameters:
//            BoardNum  :the number used by CB.CFG to describe this board
//            Status    :current status of the operation (IDLE or RUNNING)
//            CurCount  :current number of samples collected
//            CurIndex  :index to the last data value transferred
//            FunctionType: A/D operation (AIFUNCTIOM)*/
        ULStat = cbGetStatus (BoardNum, &Status, &CurCount, &CurIndex,AIFUNCTION);
//        // check the current status of the background operation */

        if ((Status == RUNNING) && CurCount > 0)
        {
            emit dataPointChanged(CurIndex);
            if(HighResAD)
                emit longDataValueChanged(ADData32[CurIndex]);
            else
                emit shortDataValueChanged(ADData[CurIndex]);
        }
    }




    /* Data collection terminated */

    /* The BACKGROUND operation must be explicitly stopped
        Parameters:
             BoardNum    :the number used by CB.CFG to describe this board
             FunctionType: A/D operation (AIFUNCTIOM)*/
    ULStat = cbStopBackground (BoardNum,AIFUNCTION);

    cbWinBufFree(MemHandle);
}