Subversion Repositories f9daq

Rev

Rev 222 | Go to most recent revision | Details | 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;
46
 
47
static ViStatus istat;
48
static ViSession RedPHandle,RMHandle;
49
 
50
//==============================================================================
51
// Static functions
52
 
53
static int update_plots (void)
54
{
55
  if (ph_tdc>0) DeleteGraphPlot (p1h, P1_TDC_G, ph_tdc, VAL_DELAYED_DRAW);
56
  ph_tdc = PlotY (p1h, P1_TDC_G, &dtdc[0], MAXTDCCH, VAL_INTEGER,
57
                  VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
58
  return (0);
59
}
60
 
61
static int CVICALLBACK daq_run(void *functionData)
62
{
63
  int i,trgdly,neve,ieve,ndata;
64
  float data[10000];
65
  char datac[100000];
66
  char *ctoken_p;
67
  char response[80];
68
 
69
  istat = viPrintf (RedPHandle, "ACQ:RST\r\n");
70
  istat = viPrintf (RedPHandle, "ACQ:AVG OFF\r\n");
71
  istat = viPrintf (RedPHandle, "ACQ:DEC 1\r\n");
72
  istat = viPrintf (RedPHandle, "ACQ:TRIG:LEV -0.03\r\n");
73
  trgdly = MINTRGDELAY + NSAMPLES - NBEFORE + 1;
74
  istat = viPrintf (RedPHandle, "ACQ:TRIG:DLY %0d\r\n", trgdly);
75
  istat = viPrintf (RedPHandle, "ACQ:START\r\n");
76
  Delay(0.01);
77
  istat = viPrintf (RedPHandle, "ACQ:TRIG CH1_NE\r\n");
78
//  istat = viPrintf (RedPHandle, "ACQ:TRIG NOW\r\n");
79
 
80
        GetCtrlVal (p1h, P1_NEVE_N, &neve);
81
  ieve=0;
82
  do {
83
    while (VI_TRUE) {
84
      istat = viQueryf (RedPHandle, "ACQ:TRIG:STAT?\r\n", "%s",response);
85
      if (response[0]=='T') break;
86
      if (!daq_on) break;
87
    }
88
    if (!daq_on) break;
89
    istat = viQueryf (RedPHandle, "ACQ:SOUR1:DATA:LAT:N? %0d\r\n", "%s", NSAMPLES, datac);
90
    istat = viPrintf (RedPHandle, "ACQ:START\r\n");
91
    Delay(0.001);
92
    istat = viPrintf (RedPHandle, "ACQ:TRIG CH1_NE\r\n");
93
//    istat = viPrintf (RedPHandle, "ACQ:TRIG NOW\r\n");
94
//    Delay(0.01);
95
    ndata=0;
96
    ctoken_p = strtok (datac, "{,}\r");
97
    while (ctoken_p) {
98
      sscanf(ctoken_p,"%f",&data[ndata]);
99
      if ((ndata)&&(data[ndata]<-0.015)&&(data[ndata-1]>=-0.015)) dtdc[ndata/NGROUP]++;
100
      ctoken_p = strtok (NULL, "{,}\r");
101
      ndata++;
102
    }
103
    ph_wf = PlotY (p1h, P1_WF_G, data, ndata, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, VAL_BLUE);
104
        SetCtrlVal (p1h, P1_CEVE_N, ++ieve);
105
    if (debugOut) {
106
      sprintf(response,"%d, %d",ieve,ndata);
107
      istat = InsertTextBoxLine (p1h, P1_IO_TB, -1, response);
108
      debugOut=0;
109
    }
110
  } while (ieve<neve);
111
  daq_on=0;    
112
        SetCtrlVal (p1h, P1_DAQ_S, daq_on);
113
        SetCtrlVal (p1h, P1_CEVE_N, ieve);
114
        return 0;
115
}
116
 
117
//==============================================================================
118
// Global variables
119
 
120
//==============================================================================
121
// Global functions
122
 
123
/// HIFN  What does your function do?
124
/// HIPAR x/What inputs does your function expect?
125
/// HIRET What does your function return?
126
 
127
int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData,
128
                           int eventData1, int eventData2)
129
{
130
  QueueUserEvent (9000, p1h, P1_TIMER_T);
131
  return (0);
132
}
133
 
134
int CVICALLBACK debug_pressed (int panel, int control, int event,
135
                               void *callbackData, int eventData1, int eventData2) {
136
  switch (event) {
137
    case EVENT_COMMIT:
138
      debugOut=1;
139
      break;
140
  }
141
  return 0;
142
}
143
 
144
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
145
                       LPSTR lpszCmdLine, int nCmdShow) {
146
  int i,refon,dummy;
147
  char response[80];
148
 
149
  if (InitCVIRTE (hInstance, 0, 0) == 0)
150
    return -1;    /* out of memory */
151
 
152
  SetSleepPolicy(VAL_SLEEP_MORE);
153
  CmtNewThreadPool (MAX_THREADS, &poolHandle);
154
 
155
  SetStdioWindowOptions (1000, 0, 0);
156
  SetStdioWindowSize (150, 600);
157
  SetStdioWindowPosition (825, 20);
158
 
159
  istat = viOpenDefaultRM (&RMHandle);
160
  istat = viOpen (RMHandle, "TCPIP0::178.172.43.73::5000::SOCKET", VI_NULL, VI_NULL, &RedPHandle);
161
  istat = viSetAttribute (RedPHandle, VI_ATTR_TCPIP_KEEPALIVE, VI_TRUE);
162
  istat = viSetAttribute (RedPHandle, VI_ATTR_TCPIP_NODELAY, VI_TRUE);
163
  istat = viSetAttribute (RedPHandle, VI_ATTR_TERMCHAR, '\n');
164
  istat = viSetAttribute (RedPHandle, VI_ATTR_TERMCHAR_EN, VI_TRUE);
165
  istat = viSetAttribute (RedPHandle, VI_ATTR_TMO_VALUE, 1000);
166
 
167
  if ((p1h = LoadPanel (0, "RedPitaya_ui.uir", P1)) < 0) return -1;
168
  DisplayPanel (p1h);
169
  istat = SetCtrlAttribute (p1h, P1_WF_G, ATTR_DATA_MODE, VAL_DISCARD);
170
 
171
  istat = viQueryf (RedPHandle, "*IDN?\r\n", "%s",response);
172
  istat = InsertTextBoxLine (p1h, P1_IO_TB, -1,response);
173
  istat = ProcessDrawEvents ();
174
 
175
  QueueUserEvent (1000, p1h, P1_RESET_PB);
176
 
177
  do {
178
    GetUserEvent (1, &pID, &rID);
179
    switch (rID) {
180
      case P1_TIMER_T:
181
        ntics+=1;
182
        GetCtrlVal (p1h, P1_REFON_CB, &refon);
183
        if (refon) update_plots();
184
        break;
185
      case P1_REFRESH_PB:
186
        update_plots();
187
        break;
188
      case P1_DAQ_S:
189
        GetCtrlVal (p1h, P1_DAQ_S, &daq_on);
190
        if (daq_on) {
191
          CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID);
192
        }
193
        else {
194
          CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID,
195
                                                  OPT_TP_PROCESS_EVENTS_WHILE_WAITING);
196
          CmtReleaseThreadPoolFunctionID (poolHandle, tfID);
197
        }
198
        break;
199
      case P1_RESET_PB:
200
        for (i=0; i<MAXTDCCH; i++) {
201
          dtdc[i]=0;
202
        }
203
        update_plots();
204
      case P1_CLEAR_PB:
205
        DeleteGraphPlot (p1h, P1_WF_G, -1, VAL_IMMEDIATE_DRAW);
206
        break;
207
      case P1_TDCLOG_S:
208
        GetCtrlVal (p1h, P1_TDCLOG_S, &istat);
209
        SetCtrlAttribute (p1h, P1_TDC_G, ATTR_YMAP_MODE, istat);
210
        update_plots();
211
        break;
212
      default:
213
        Delay(1.);
214
    }
215
  }
216
  while ((rID != P1_EXIT_PB)||daq_on);
217
 
218
  CmtDiscardThreadPool (poolHandle);
219
  DiscardPanel (p1h);
220
 
221
  istat = viClose (RedPHandle);
222
  istat = viClose (RMHandle);
223
 
224
  return 0;
225
}
226