//==============================================================================
//
// 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 time_t t0;
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 export_data (void) {
char filename[0xFF],rootcmd[0xFF];
int type=0;
int neve;
GetCtrlVal(p1h,P1_FILENAME_EN,filename);
GetCtrlVal(p1h,P1_FILETYPE,&type);
unsigned int hdr[10];
time_t t;
hdr[0] = 0x1;
hdr[1] = sizeof(int) * MAXTDCCH;
hdr[2] = t0;
GetCtrlVal (p1h, P1_CEVE_N, &neve);
hdr[3] = neve;
hdr
[4] = (unsigned int)( time(&t
)-t0
);
FILE *fp;
switch (type) {
case 0:
case 1:
fp
=fopen(filename
,"wb");
if (fp) {
fwrite(hdr
,sizeof(unsigned int),5,fp
);
fwrite(dtdc
,sizeof(int),MAXTDCCH
,fp
);
if (type) {
sprintf(rootcmd
,"root.exe plottdc.cxx(\\\"%s\\\")", filename
);
LaunchExecutable(rootcmd);
}
}
break;
case 2:
if (fp) {
for (int i
=0; i
<MAXTDCCH
; i
++) fprintf(fp
,"%d\n", dtdc
[i
]);
}
break;
}
return (0);
}
static int CVICALLBACK daq_run(void *functionData) {
int trgdly,neve,ieve,ndata;
float data[10000];
char datac[100000];
char *ctoken_p;
char response[80];
time_t t;
response[0] = 0;
if (RedPHandle) {
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) {
if (RedPHandle) istat = viQueryf (RedPHandle, "ACQ:TRIG:STAT?\r\n", "%s",response);
if (response[0]=='T') break;
if (!daq_on) break;
}
if (!daq_on) break;
if (RedPHandle) {
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);
if (RMHandle) istat = viOpen (RMHandle, "TCPIP0::178.172.43.91::5000::SOCKET", VI_NULL, VI_NULL, &RedPHandle);
if (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);
if (RedPHandle) istat = viQueryf (RedPHandle, "*IDN?\r\n", "%s",response);
else sprintf(response
,"Connection to RedPitaya is missing");
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_EXPORT_PB:
export_data();
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);
if (RedPHandle) istat = viClose (RedPHandle);
if (RMHandle) istat = viClose (RMHandle);
return 0;
}