//==============================================================================
//
// Title: RedPitaya.c
// Purpose: A short description of the implementation.
//
// Created on: 27. 01. 2017 at 13:29:33 by Samo Korpar.
// Copyright: . All Rights Reserved.
//
//==============================================================================
//==============================================================================
// Include files
//#include "RedPitaya.h"
#include "RedPitaya_ui.h"
#include <userint.h>
#include <ansi_c.h>
#include <utility.h>
#include <visa.h>
#include <cvirte.h>
//==============================================================================
// Constants
#define MAX_THREADS 10
#define NSAMPLES 2048
#define NBEFORE 80
#define NGROUP 32
#define MAXSAMPLES 16384
#define MINTRGDELAY -8192
#define MAXTDCCH 0x80
//==============================================================================
// Types
//==============================================================================
// Static global variables
static int daq_on = 0;
static int ntics;
static int poolHandle = 0;
static int p1h, pID, rID, tfID;
static int ph_tdc, ph_wf;
static int dtdc[MAXTDCCH];
static int debugOut = 0;
static ViStatus istat;
static ViSession RedPHandle,RMHandle;
//==============================================================================
// Static functions
static int update_plots (void)
{
if (ph_tdc>0) DeleteGraphPlot (p1h, P1_TDC_G, ph_tdc, VAL_DELAYED_DRAW);
ph_tdc = PlotY (p1h, P1_TDC_G, &dtdc[0], MAXTDCCH, VAL_INTEGER,
VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
return (0);
}
static int CVICALLBACK daq_run(void *functionData)
{
int i,trgdly,neve,ieve,ndata;
float data[10000];
char datac[100000];
char *ctoken_p;
char response[80];
istat = viPrintf (RedPHandle, "ACQ:RST\r\n");
istat = viPrintf (RedPHandle, "ACQ:AVG OFF\r\n");
istat = viPrintf (RedPHandle, "ACQ:DEC 1\r\n");
istat = viPrintf (RedPHandle, "ACQ:TRIG:LEV -0.03\r\n");
trgdly = MINTRGDELAY + NSAMPLES - NBEFORE + 1;
istat = viPrintf (RedPHandle, "ACQ:TRIG:DLY %0d\r\n", trgdly);
istat = viPrintf (RedPHandle, "ACQ:START\r\n");
Delay(0.01);
istat = viPrintf (RedPHandle, "ACQ:TRIG CH1_NE\r\n");
// istat = viPrintf (RedPHandle, "ACQ:TRIG NOW\r\n");
GetCtrlVal (p1h, P1_NEVE_N, &neve);
ieve=0;
do {
while (VI_TRUE) {
istat = viQueryf (RedPHandle, "ACQ:TRIG:STAT?\r\n", "%s",response);
if (response[0]=='T') break;
if (!daq_on) break;
}
if (!daq_on) break;
istat = viQueryf (RedPHandle, "ACQ:SOUR1:DATA:LAT:N? %0d\r\n", "%s", NSAMPLES, datac);
istat = viPrintf (RedPHandle, "ACQ:START\r\n");
Delay(0.001);
istat = viPrintf (RedPHandle, "ACQ:TRIG CH1_NE\r\n");
// istat = viPrintf (RedPHandle, "ACQ:TRIG NOW\r\n");
// Delay(0.01);
ndata=0;
ctoken_p
= strtok (datac
, "{,}\r");
while (ctoken_p) {
sscanf(ctoken_p
,"%f",&data
[ndata
]);
if ((ndata)&&(data[ndata]<-0.015)&&(data[ndata-1]>=-0.015)) dtdc[ndata/NGROUP]++;
ctoken_p
= strtok (NULL
, "{,}\r");
ndata++;
}
ph_wf = PlotY (p1h, P1_WF_G, data, ndata, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, VAL_BLUE);
SetCtrlVal (p1h, P1_CEVE_N, ++ieve);
if (debugOut) {
sprintf(response
,"%d, %d",ieve
,ndata
);
istat = InsertTextBoxLine (p1h, P1_IO_TB, -1, response);
debugOut=0;
}
} while (ieve<neve);
daq_on=0;
SetCtrlVal (p1h, P1_DAQ_S, daq_on);
SetCtrlVal (p1h, P1_CEVE_N, ieve);
return 0;
}
//==============================================================================
// Global variables
//==============================================================================
// Global functions
/// HIFN What does your function do?
/// HIPAR x/What inputs does your function expect?
/// HIRET What does your function return?
int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
QueueUserEvent (9000, p1h, P1_TIMER_T);
return (0);
}
int CVICALLBACK debug_pressed (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:
debugOut=1;
break;
}
return 0;
}
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow) {
int i,refon,dummy;
char response[80];
if (InitCVIRTE (hInstance, 0, 0) == 0)
return -1; /* out of memory */
SetSleepPolicy(VAL_SLEEP_MORE);
CmtNewThreadPool (MAX_THREADS, &poolHandle);
SetStdioWindowOptions (1000, 0, 0);
SetStdioWindowSize (150, 600);
SetStdioWindowPosition (825, 20);
istat = viOpenDefaultRM (&RMHandle);
istat = viOpen (RMHandle, "TCPIP0::178.172.43.73::5000::SOCKET", VI_NULL, VI_NULL, &RedPHandle);
istat = viSetAttribute (RedPHandle, VI_ATTR_TCPIP_KEEPALIVE, VI_TRUE);
istat = viSetAttribute (RedPHandle, VI_ATTR_TCPIP_NODELAY, VI_TRUE);
istat = viSetAttribute (RedPHandle, VI_ATTR_TERMCHAR, '\n');
istat = viSetAttribute (RedPHandle, VI_ATTR_TERMCHAR_EN, VI_TRUE);
istat = viSetAttribute (RedPHandle, VI_ATTR_TMO_VALUE, 1000);
if ((p1h = LoadPanel (0, "RedPitaya_ui.uir", P1)) < 0) return -1;
DisplayPanel (p1h);
istat = SetCtrlAttribute (p1h, P1_WF_G, ATTR_DATA_MODE, VAL_DISCARD);
istat = viQueryf (RedPHandle, "*IDN?\r\n", "%s",response);
istat = InsertTextBoxLine (p1h, P1_IO_TB, -1,response);
istat = ProcessDrawEvents ();
QueueUserEvent (1000, p1h, P1_RESET_PB);
do {
GetUserEvent (1, &pID, &rID);
switch (rID) {
case P1_TIMER_T:
ntics+=1;
GetCtrlVal (p1h, P1_REFON_CB, &refon);
if (refon) update_plots();
break;
case P1_REFRESH_PB:
update_plots();
break;
case P1_DAQ_S:
GetCtrlVal (p1h, P1_DAQ_S, &daq_on);
if (daq_on) {
CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID);
}
else {
CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID,
OPT_TP_PROCESS_EVENTS_WHILE_WAITING);
CmtReleaseThreadPoolFunctionID (poolHandle, tfID);
}
break;
case P1_RESET_PB:
for (i=0; i<MAXTDCCH; i++) {
dtdc[i]=0;
}
update_plots();
case P1_CLEAR_PB:
DeleteGraphPlot (p1h, P1_WF_G, -1, VAL_IMMEDIATE_DRAW);
break;
case P1_TDCLOG_S:
GetCtrlVal (p1h, P1_TDCLOG_S, &istat);
SetCtrlAttribute (p1h, P1_TDC_G, ATTR_YMAP_MODE, istat);
update_plots();
break;
default:
Delay(1.);
}
}
while ((rID != P1_EXIT_PB)||daq_on);
CmtDiscardThreadPool (poolHandle);
DiscardPanel (p1h);
istat = viClose (RedPHandle);
istat = viClose (RMHandle);
return 0;
}