-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyusbdux.i
315 lines (263 loc) · 7.22 KB
/
pyusbdux.i
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
%module(directors="1") pyusbdux
%{
#define SWIG_FILE_WITH_INIT
%}
%feature("director") Callback;
%include exception.i
%exception {
try {
$action
} catch (const char* e) {
PyErr_SetString(PyExc_RuntimeError, e);
return NULL;
}
}
%{
#include <stdio.h>
#include <comedilib.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <array>
#include <fcntl.h>
#define N_CHANS 16
#define BUFSZ N_CHANS*sizeof(long int)
%}
%include "std_array.i"
namespace std {
%template(FloatArray) array<float,N_CHANS>;
%template(DoubleArray) array<double,N_CHANS>;
}
%inline %{
class Callback{
public:
virtual void hasSample(const std::array<float,N_CHANS> &data) = 0;
virtual ~Callback() {};
Callback() {};
};
%}
%{
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];
float samples[N_CHANS];
unsigned int chanlist[N_CHANS];
comedi_range* range_info[N_CHANS];
lsampl_t maxdata[N_CHANS];
comedi_cmd cmd;
std::thread* thr = nullptr;
bool running = false;
static const char errorDevNotOpen[] = "Comedi device not open. Use open() first.";
static const char errorDisconnect[] = "Device error. Possible disconnect.";
%}
%inline %{
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));
}
// autodetect
void open() {
for(int i = 0;i<16;i++) {
try {
open(i);
return;
} catch (const char*) {
}
}
throw "No USBDUX board found.";
}
%}
%{
int getSampleFromBuffer(float sample[N_CHANS]) {
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;
}
int hasSampleAvailable() {
if (dev == NULL) throw errorDevNotOpen;
int ret = comedi_get_buffer_contents(dev,subdevice);
if (ret < 0) throw errorDisconnect;
return ret > 0;
}
void readWorker(Callback* cb) {
std::array<float,N_CHANS> sample;
running = true;
while (running) {
float tmp[N_CHANS];
int n = getSampleFromBuffer(tmp);
if (n > 0) {
for(int i = 0; i < N_CHANS; i++) {
sample[i] = tmp[i];
}
//printf("%f\n",sample[0]);
cb->hasSample(sample);
}
if (n < 0) {
throw errorDisconnect;
}
}
}
%}
%inline %{
void start(Callback* cb, int n_channels, double fs) {
if (dev == NULL) throw errorDevNotOpen;
n_chan = n_channels;
freq = fs;
for(int i=0;i<N_CHANS;i++) {
samples[i] = 0;
}
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 = new std::thread(readWorker,cb);
}
void start(Callback* cb, int nChan) {
start(cb, nChan, 250);
}
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;
}
void stop() {
if (dev == NULL) throw errorDevNotOpen;
running = false;
thr->join();
comedi_cancel(dev,subdevice);
thr = nullptr;
memset(&cmd,0,sizeof(comedi_cmd));
}
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;
}
}
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;
}
void analogue_out(int channel, int value) {
if (dev == NULL) throw errorDevNotOpen;
int subdevice = comedi_find_subdevice_by_type(dev,COMEDI_SUBD_AO,0);
int ret = comedi_data_write(dev,subdevice,channel,0,0, value);
if (ret<0) throw errorDisconnect;
}
int get_analogue_out_max_raw_value() {
if (dev == NULL) throw errorDevNotOpen;
int subdevice = comedi_find_subdevice_by_type(dev,COMEDI_SUBD_AO,0);
int ret = comedi_get_maxdata(dev, subdevice, 0);
if (ret < 0) throw errorDisconnect;
return ret;
}
const char* get_board_name() {
if (dev == NULL) throw errorDevNotOpen;
return comedi_get_board_name(dev);
}
void close() {
if (dev == NULL) throw errorDevNotOpen;
comedi_close(dev);
dev = NULL;
memset(&cmd,0,sizeof(comedi_cmd));
}
%}