Rev 229 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 219 | f9daq | 1 | //============================================================================== |
| 2 | // |
||
| 3 | // Title: RedPitaya.c |
||
| 4 | // Purpose: A short description of the implementation. |
||
| 5 | // |
||
| 6 | // Created on: 27. 01. 2017 at 13:29:33 by Samo Korpar. |
||
| 7 | // Copyright: . All Rights Reserved. |
||
| 8 | // |
||
| 9 | //============================================================================== |
||
| 10 | |||
| 11 | //============================================================================== |
||
| 12 | // Include files |
||
| 13 | |||
| 14 | //#include "RedPitaya.h" |
||
| 15 | #include "RedPitaya_ui.h" |
||
| 16 | #include <userint.h> |
||
| 17 | #include <ansi_c.h> |
||
| 18 | #include <utility.h> |
||
| 19 | #include <visa.h> |
||
| 20 | #include <cvirte.h> |
||
| 21 | |||
| 22 | //============================================================================== |
||
| 23 | // Constants |
||
| 24 | |||
| 25 | #define MAX_THREADS 10 |
||
| 26 | #define NSAMPLES 2048 |
||
| 27 | #define NBEFORE 80 |
||
| 28 | #define NGROUP 32 |
||
| 29 | #define MAXSAMPLES 16384 |
||
| 30 | #define MINTRGDELAY -8192 |
||
| 31 | #define MAXTDCCH 0x80 |
||
| 32 | |||
| 33 | //============================================================================== |
||
| 34 | // Types |
||
| 35 | |||
| 36 | //============================================================================== |
||
| 37 | // Static global variables |
||
| 38 | |||
| 39 | static int daq_on = 0; |
||
| 40 | static int ntics; |
||
| 41 | static int poolHandle = 0; |
||
| 42 | static int p1h, pID, rID, tfID; |
||
| 43 | static int ph_tdc, ph_wf; |
||
| 44 | static int dtdc[MAXTDCCH]; |
||
| 45 | static int debugOut = 0; |
||
| 222 | f9daq | 46 | static time_t t0; |
| 219 | f9daq | 47 | static ViStatus istat; |
| 48 | static ViSession RedPHandle,RMHandle; |
||
| 49 | |||
| 50 | //============================================================================== |
||
| 51 | // Static functions |
||
| 52 | |||
| 222 | f9daq | 53 | static int update_plots (void) { |
| 219 | f9daq | 54 | if (ph_tdc>0) DeleteGraphPlot (p1h, P1_TDC_G, ph_tdc, VAL_DELAYED_DRAW); |
| 55 | ph_tdc = PlotY (p1h, P1_TDC_G, &dtdc[0], MAXTDCCH, VAL_INTEGER, |
||
| 56 | VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); |
||
| 57 | return (0); |
||
| 58 | } |
||
| 59 | |||
| 222 | f9daq | 60 | |
| 61 | |||
| 62 | static int export_data (void) { |
||
| 63 | char filename[0xFF],rootcmd[0xFF]; |
||
| 64 | int type=0; |
||
| 65 | int neve; |
||
| 66 | GetCtrlVal(p1h,P1_FILENAME_EN,filename); |
||
| 67 | GetCtrlVal(p1h,P1_FILETYPE,&type); |
||
| 68 | unsigned int hdr[10]; |
||
| 69 | time_t t; |
||
| 70 | hdr[0] = 0x1; |
||
| 71 | hdr[1] = sizeof(int) * MAXTDCCH; |
||
| 72 | hdr[2] = t0; |
||
| 73 | GetCtrlVal (p1h, P1_CEVE_N, &neve); |
||
| 74 | hdr[3] = neve; |
||
| 75 | hdr[4] = (unsigned int)( time(&t)-t0); |
||
| 76 | FILE *fp; |
||
| 77 | switch (type) { |
||
| 78 | case 0: |
||
| 79 | case 1: |
||
| 80 | fp =fopen(filename,"wb"); |
||
| 81 | if (fp) { |
||
| 82 | fwrite(hdr,sizeof(unsigned int),5,fp); |
||
| 83 | fwrite(dtdc,sizeof(int),MAXTDCCH,fp); |
||
| 84 | fclose(fp); |
||
| 85 | if (type) { |
||
| 86 | sprintf(rootcmd ,"root.exe plottdc.cxx(\\\"%s\\\")", filename); |
||
| 87 | LaunchExecutable(rootcmd); |
||
| 88 | |||
| 89 | } |
||
| 90 | } |
||
| 91 | break; |
||
| 92 | case 2: |
||
| 93 | fp=fopen(filename,"w"); |
||
| 94 | if (fp) { |
||
| 95 | for (int i=0; i<MAXTDCCH; i++) fprintf(fp,"%d\n", dtdc[i]); |
||
| 96 | fclose(fp); |
||
| 97 | } |
||
| 98 | break; |
||
| 99 | |||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | return (0); |
||
| 104 | } |
||
| 105 | static int CVICALLBACK daq_run(void *functionData) { |
||
| 106 | int trgdly,neve,ieve,ndata; |
||
| 219 | f9daq | 107 | float data[10000]; |
| 108 | char datac[100000]; |
||
| 109 | char *ctoken_p; |
||
| 110 | char response[80]; |
||
| 222 | f9daq | 111 | time_t t; |
| 112 | response[0] = 0; |
||
| 113 | t0 = time(&t); |
||
| 114 | if (RedPHandle) { |
||
| 115 | istat = viPrintf (RedPHandle, "ACQ:RST\r\n"); |
||
| 116 | istat = viPrintf (RedPHandle, "ACQ:AVG OFF\r\n"); |
||
| 117 | istat = viPrintf (RedPHandle, "ACQ:DEC 1\r\n"); |
||
| 118 | istat = viPrintf (RedPHandle, "ACQ:TRIG:LEV -0.03\r\n"); |
||
| 119 | trgdly = MINTRGDELAY + NSAMPLES - NBEFORE + 1; |
||
| 120 | istat = viPrintf (RedPHandle, "ACQ:TRIG:DLY %0d\r\n", trgdly); |
||
| 121 | istat = viPrintf (RedPHandle, "ACQ:START\r\n"); |
||
| 122 | Delay(0.01); |
||
| 123 | istat = viPrintf (RedPHandle, "ACQ:TRIG CH1_NE\r\n"); |
||
| 219 | f9daq | 124 | // istat = viPrintf (RedPHandle, "ACQ:TRIG NOW\r\n"); |
| 222 | f9daq | 125 | } |
| 126 | GetCtrlVal (p1h, P1_NEVE_N, &neve); |
||
| 219 | f9daq | 127 | ieve=0; |
| 128 | do { |
||
| 129 | while (VI_TRUE) { |
||
| 222 | f9daq | 130 | if (RedPHandle) istat = viQueryf (RedPHandle, "ACQ:TRIG:STAT?\r\n", "%s",response); |
| 219 | f9daq | 131 | if (response[0]=='T') break; |
| 132 | if (!daq_on) break; |
||
| 133 | } |
||
| 134 | if (!daq_on) break; |
||
| 222 | f9daq | 135 | if (RedPHandle) { |
| 136 | istat = viQueryf (RedPHandle, "ACQ:SOUR1:DATA:LAT:N? %0d\r\n", "%s", NSAMPLES, datac); |
||
| 137 | istat = viPrintf (RedPHandle, "ACQ:START\r\n"); |
||
| 138 | Delay(0.001); |
||
| 139 | istat = viPrintf (RedPHandle, "ACQ:TRIG CH1_NE\r\n"); |
||
| 219 | f9daq | 140 | // istat = viPrintf (RedPHandle, "ACQ:TRIG NOW\r\n"); |
| 141 | // Delay(0.01); |
||
| 222 | f9daq | 142 | } |
| 219 | f9daq | 143 | ndata=0; |
| 144 | ctoken_p = strtok (datac, "{,}\r"); |
||
| 145 | while (ctoken_p) { |
||
| 146 | sscanf(ctoken_p,"%f",&data[ndata]); |
||
| 147 | if ((ndata)&&(data[ndata]<-0.015)&&(data[ndata-1]>=-0.015)) dtdc[ndata/NGROUP]++; |
||
| 148 | ctoken_p = strtok (NULL, "{,}\r"); |
||
| 149 | ndata++; |
||
| 150 | } |
||
| 151 | ph_wf = PlotY (p1h, P1_WF_G, data, ndata, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, VAL_BLUE); |
||
| 222 | f9daq | 152 | SetCtrlVal (p1h, P1_CEVE_N, ++ieve); |
| 219 | f9daq | 153 | if (debugOut) { |
| 154 | sprintf(response,"%d, %d",ieve,ndata); |
||
| 155 | istat = InsertTextBoxLine (p1h, P1_IO_TB, -1, response); |
||
| 156 | debugOut=0; |
||
| 157 | } |
||
| 158 | } while (ieve<neve); |
||
| 222 | f9daq | 159 | daq_on=0; |
| 160 | SetCtrlVal (p1h, P1_DAQ_S, daq_on); |
||
| 161 | SetCtrlVal (p1h, P1_CEVE_N, ieve); |
||
| 162 | return 0; |
||
| 219 | f9daq | 163 | } |
| 222 | f9daq | 164 | |
| 219 | f9daq | 165 | //============================================================================== |
| 166 | // Global variables |
||
| 167 | |||
| 168 | //============================================================================== |
||
| 169 | // Global functions |
||
| 170 | |||
| 171 | /// HIFN What does your function do? |
||
| 172 | /// HIPAR x/What inputs does your function expect? |
||
| 173 | /// HIRET What does your function return? |
||
| 174 | |||
| 175 | int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData, |
||
| 222 | f9daq | 176 | int eventData1, int eventData2) { |
| 219 | f9daq | 177 | QueueUserEvent (9000, p1h, P1_TIMER_T); |
| 178 | return (0); |
||
| 179 | } |
||
| 180 | |||
| 181 | int CVICALLBACK debug_pressed (int panel, int control, int event, |
||
| 182 | void *callbackData, int eventData1, int eventData2) { |
||
| 183 | switch (event) { |
||
| 184 | case EVENT_COMMIT: |
||
| 185 | debugOut=1; |
||
| 186 | break; |
||
| 187 | } |
||
| 188 | return 0; |
||
| 189 | } |
||
| 190 | |||
| 191 | int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, |
||
| 192 | LPSTR lpszCmdLine, int nCmdShow) { |
||
| 193 | int i,refon,dummy; |
||
| 194 | char response[80]; |
||
| 222 | f9daq | 195 | |
| 219 | f9daq | 196 | if (InitCVIRTE (hInstance, 0, 0) == 0) |
| 197 | return -1; /* out of memory */ |
||
| 198 | |||
| 199 | SetSleepPolicy(VAL_SLEEP_MORE); |
||
| 200 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
| 201 | |||
| 202 | SetStdioWindowOptions (1000, 0, 0); |
||
| 203 | SetStdioWindowSize (150, 600); |
||
| 204 | SetStdioWindowPosition (825, 20); |
||
| 205 | |||
| 206 | istat = viOpenDefaultRM (&RMHandle); |
||
| 229 | f9daq | 207 | if (RMHandle) istat = viOpen (RMHandle, "TCPIP0::178.172.43.91::5000::SOCKET", VI_NULL, VI_NULL, &RedPHandle); |
| 222 | f9daq | 208 | if (RedPHandle){ |
| 219 | f9daq | 209 | istat = viSetAttribute (RedPHandle, VI_ATTR_TCPIP_KEEPALIVE, VI_TRUE); |
| 210 | istat = viSetAttribute (RedPHandle, VI_ATTR_TCPIP_NODELAY, VI_TRUE); |
||
| 211 | istat = viSetAttribute (RedPHandle, VI_ATTR_TERMCHAR, '\n'); |
||
| 212 | istat = viSetAttribute (RedPHandle, VI_ATTR_TERMCHAR_EN, VI_TRUE); |
||
| 213 | istat = viSetAttribute (RedPHandle, VI_ATTR_TMO_VALUE, 1000); |
||
| 222 | f9daq | 214 | } |
| 219 | f9daq | 215 | if ((p1h = LoadPanel (0, "RedPitaya_ui.uir", P1)) < 0) return -1; |
| 216 | DisplayPanel (p1h); |
||
| 217 | istat = SetCtrlAttribute (p1h, P1_WF_G, ATTR_DATA_MODE, VAL_DISCARD); |
||
| 218 | |||
| 222 | f9daq | 219 | if (RedPHandle) istat = viQueryf (RedPHandle, "*IDN?\r\n", "%s",response); |
| 220 | else sprintf(response,"Connection to RedPitaya is missing"); |
||
| 219 | f9daq | 221 | istat = InsertTextBoxLine (p1h, P1_IO_TB, -1,response); |
| 222 | istat = ProcessDrawEvents (); |
||
| 223 | |||
| 224 | QueueUserEvent (1000, p1h, P1_RESET_PB); |
||
| 225 | |||
| 226 | do { |
||
| 227 | GetUserEvent (1, &pID, &rID); |
||
| 228 | switch (rID) { |
||
| 229 | case P1_TIMER_T: |
||
| 230 | ntics+=1; |
||
| 231 | GetCtrlVal (p1h, P1_REFON_CB, &refon); |
||
| 232 | if (refon) update_plots(); |
||
| 233 | break; |
||
| 234 | case P1_REFRESH_PB: |
||
| 235 | update_plots(); |
||
| 236 | break; |
||
| 237 | case P1_DAQ_S: |
||
| 238 | GetCtrlVal (p1h, P1_DAQ_S, &daq_on); |
||
| 239 | if (daq_on) { |
||
| 240 | CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID); |
||
| 222 | f9daq | 241 | } else { |
| 219 | f9daq | 242 | CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID, |
| 243 | OPT_TP_PROCESS_EVENTS_WHILE_WAITING); |
||
| 244 | CmtReleaseThreadPoolFunctionID (poolHandle, tfID); |
||
| 245 | } |
||
| 246 | break; |
||
| 247 | case P1_RESET_PB: |
||
| 248 | for (i=0; i<MAXTDCCH; i++) { |
||
| 249 | dtdc[i]=0; |
||
| 250 | } |
||
| 251 | update_plots(); |
||
| 252 | case P1_CLEAR_PB: |
||
| 253 | DeleteGraphPlot (p1h, P1_WF_G, -1, VAL_IMMEDIATE_DRAW); |
||
| 254 | break; |
||
| 222 | f9daq | 255 | |
| 256 | case P1_EXPORT_PB: |
||
| 257 | export_data(); |
||
| 258 | break; |
||
| 219 | f9daq | 259 | case P1_TDCLOG_S: |
| 260 | GetCtrlVal (p1h, P1_TDCLOG_S, &istat); |
||
| 261 | SetCtrlAttribute (p1h, P1_TDC_G, ATTR_YMAP_MODE, istat); |
||
| 262 | update_plots(); |
||
| 263 | break; |
||
| 264 | default: |
||
| 265 | Delay(1.); |
||
| 266 | } |
||
| 222 | f9daq | 267 | } while ((rID != P1_EXIT_PB)||daq_on); |
| 219 | f9daq | 268 | |
| 269 | CmtDiscardThreadPool (poolHandle); |
||
| 270 | DiscardPanel (p1h); |
||
| 271 | |||
| 222 | f9daq | 272 | if (RedPHandle) istat = viClose (RedPHandle); |
| 273 | if (RMHandle) istat = viClose (RMHandle); |
||
| 219 | f9daq | 274 | |
| 275 | return 0; |
||
| 276 | } |
||
| 277 |