-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcpp-usbdux.h
298 lines (256 loc) · 7.06 KB
/
cpp-usbdux.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#ifndef _CPP_USBDUX_H
#define _CPP_USBDUX_H
#include <stdio.h>
#include <comedilib.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>
#include <fcntl.h>
#include <math.h>
#define N_CHANS 16
#define BUFSZ N_CHANS*sizeof(long int)
const char errorDevNotOpen[] = "Comedi device not open. Use open() first.";
const char errorDisconnect[] = "Device error. Possible disconnect.";
class CppUSBDUX {
public:
/**
* Abstract Callback which needs to be implemented by the main program
**/
struct Callback{
virtual void hasSample(const std::vector<float> &data) = 0;
};
/**
* Opens a specific comedi device with device number /dev/comedi%d
**/
void open(int comediDeviceNumber) {
char filename[256];
sprintf(filename,"/dev/comedi%d",comediDeviceNumber);
/* open the device */
dev = comedi_open(filename);
if(!dev){
throw "Could not open comedi device.";
}
if (strstr(comedi_get_board_name(dev),"usbdux") == NULL) {
throw "Not a USBDUX board.";
}
memset(&cmd,0,sizeof(comedi_cmd));
}
/**
* Searches for a usbdux device and opens it
**/
void open() {
for(int i = 0;i<16;i++) {
try {
open(i);
return;
} catch (const char*) {
}
}
throw "No USBDUX board found.";
}
/**
* Starts data acquisition and the Callback is
* called with the number of _nchannels at the sampling
* rate fs.
**/
void start(Callback* cb, int n_channels, double fs) {
if (dev == NULL) throw errorDevNotOpen;
n_chan = n_channels;
freq = fs;
subdevice = comedi_find_subdevice_by_type(dev,COMEDI_SUBD_AI,0);
// Print numbers for clipped inputs
comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER);
/* Set up channel list */
for(int i = 0; i < n_chan; i++){
chanlist[i] = CR_PACK(i, range, aref);
range_info[i] = comedi_get_range(dev, subdevice, 0, range);
maxdata[i] = comedi_get_maxdata(dev, subdevice, 0);
}
subdev_flags = comedi_get_subdevice_flags(dev,subdevice);
if(subdev_flags & SDF_LSAMPL) {
bytes_per_sample = sizeof(lsampl_t);
} else {
bytes_per_sample = sizeof(sampl_t);
}
memset(&cmd,0,sizeof(comedi_cmd));
int ret = comedi_get_cmd_generic_timed(dev, subdevice, &cmd, n_chan, 1e9 / freq);
if(ret<0){
throw "Failed to generate async timing information"
"based on sampling rate and number of channels.";
}
cmd.chanlist = chanlist;
cmd.chanlist_len = n_chan;
cmd.stop_src = TRIG_NONE;
cmd.stop_arg = 0;
cmd.scan_end_arg = n_chan;
// twice!
ret = comedi_command_test(dev, &cmd);
ret = comedi_command_test(dev, &cmd);
if(ret!=0){
throw "Error preparing async analogue in. Check sampling rate / number of channels.";
}
/* start the command */
ret = comedi_command(dev, &cmd);
if(ret < 0){
throw "Async data acquisition could not be started.";
}
thr = std::thread([this](Callback* cb){readWorker(cb);},cb);
}
/**
* Starts the usbdux at a sampling rate of 1kHz
**/
void start(Callback* cb, int nChan) {
start(cb, nChan, 1000);
}
/**
* Gets the actual sampling rate
**/
float getSamplingRate() {
float sampling_rate = 0;
// the timing is done channel by channel
// this means that the actual sampling rate is divided by
// number of channels
if ((cmd.convert_src == TRIG_TIMER)&&(cmd.convert_arg)) {
sampling_rate=((1E9 / cmd.convert_arg)/n_chan);
}
// the timing is done scan by scan (all channels at once)
// the sampling rate is equivalent of the scan_begin_arg
if ((cmd.scan_begin_src == TRIG_TIMER)&&(cmd.scan_begin_arg)) {
sampling_rate=1E9 / cmd.scan_begin_arg;
}
return sampling_rate;
}
/**
* Stops the background acquisition
**/
void stop() {
if (dev == NULL) throw errorDevNotOpen;
if (!running) return;
running = false;
thr.join();
comedi_cancel(dev,subdevice);
memset(&cmd,0,sizeof(comedi_cmd));
}
/**
* Changes value at a digital port
**/
void digital_out(int channel, int value) {
if (dev == NULL) throw errorDevNotOpen;
int subdevice = 2;
int ret = comedi_dio_config(dev,subdevice,channel,COMEDI_OUTPUT);
if(ret < 0){
throw errorDisconnect;
}
ret = comedi_dio_write(dev,subdevice,channel,value);
if (ret < 0) {
throw errorDisconnect;
}
}
/**
* Gets the status of a digital input port
**/
int digital_in(int channel) {
if (dev == NULL) throw errorDevNotOpen;
int subdevice = 2;
int ret = comedi_dio_config(dev,subdevice,channel,COMEDI_INPUT);
if (ret < 0) throw errorDisconnect;
unsigned int value;
ret = comedi_dio_read(dev,subdevice,channel,&value);
if (ret < 0) throw errorDisconnect;
return (int)value;
}
/**
* Sets the value of an analogue output channel between 0..1
**/
void analogue_out(int channel, float value) {
if (dev == NULL) throw errorDevNotOpen;
int subdevice = comedi_find_subdevice_by_type(dev,COMEDI_SUBD_AO,0);
int max = comedi_get_maxdata(dev, subdevice, 0);
int ret = comedi_data_write(dev,subdevice,channel,0,0, (int)round(value*max));
if (ret<0) throw errorDisconnect;
}
/**
* Gets the boardname
**/
const char* get_board_name() {
if (dev == NULL) throw errorDevNotOpen;
return comedi_get_board_name(dev);
}
/**
* Closes the comedi device
**/
void close() {
if (dev == NULL) throw errorDevNotOpen;
comedi_close(dev);
dev = NULL;
memset(&cmd,0,sizeof(comedi_cmd));
}
~CppUSBDUX() {
close();
}
private:
int getSampleFromBuffer(std::vector<float>& sample) {
if (dev == NULL) throw errorDevNotOpen;
fd_set rdset;
FD_ZERO(&rdset);
FD_SET(comedi_fileno(dev),&rdset);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 500000;
int ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
if (ret < 0) return ret;
ret = read(comedi_fileno(dev),buffer,bytes_per_sample * n_chan);
if (ret == 0) {
return ret;
}
if (ret < 0) {
return ret;
} else if (ret > 0) {
int subdev_flags = comedi_get_subdevice_flags(dev,subdevice);
for(int i = 0; i < n_chan; i++) {
int raw;
if(subdev_flags & SDF_LSAMPL) {
raw = ((lsampl_t *)buffer)[i];
} else {
raw = ((sampl_t *)buffer)[i];
}
sample[i] = comedi_to_phys(raw,
range_info[i],
maxdata[i]);
}
}
return 1;
}
void readWorker(Callback* cb) {
std::vector<float> sample;
sample.resize(n_chan);
running = true;
while (running) {
int n = getSampleFromBuffer(sample);
if (n < 0) {
throw errorDisconnect;
}
cb->hasSample(sample);
}
}
private:
comedi_t *dev = NULL;
int range = 0;
int subdevice = 0;
int aref = AREF_GROUND;
int n_chan = 0;
double freq = 0;
int bytes_per_sample = 0;
int subdev_flags = 0;
char buffer[BUFSZ];
unsigned int chanlist[N_CHANS];
comedi_range* range_info[N_CHANS];
lsampl_t maxdata[N_CHANS];
comedi_cmd cmd;
std::thread thr;
bool running = false;
};
#endif