Debian Codegraph @

c++/include/comedilib.hpp

raw symbols SCIP
1 /*
2 A C++ binding for comedilib. Requires the Boost C++ libraries.
3 Copyright (C) 2006-2007 Frank Mori Hess <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef _COMEDILIB_WRAPPER_HPP
21 #define _COMEDILIB_WRAPPER_HPP
22
23 #include <boost/shared_ptr.hpp>
24 #include <comedilib.h>
25 #include <cstring>
26 #include <iostream>
27 #include <sstream>
28 #include <stdexcept>
29 #include <string>
30 #include <vector>
31
32 namespace comedi
33 {
34 class subdevice;
35
36 // wrapper for comedi_to_physical()
37 class to_physical
38 {
39 public:
40 to_physical()
41 {
42 memset(&_polynomial, 0, sizeof(_polynomial));
43 }
44 to_physical(const comedi_polynomial_t &polynomial):
45 _polynomial(polynomial)
46 {}
47 double operator()(lsampl_t data) const
48 {
49 return comedi_to_physical(data, &_polynomial);
50 }
51 private:
52 comedi_polynomial_t _polynomial;
53 };
54
55 // wrapper for comedi_from_physical()
56 class from_physical
57 {
58 public:
59 from_physical()
60 {
61 memset(&_polynomial, 0, sizeof(_polynomial));
62 }
63 from_physical(const comedi_polynomial_t &polynomial):
64 _polynomial(polynomial)
65 {}
66 lsampl_t operator()(double physical_value) const
67 {
68 return comedi_from_physical(physical_value, &_polynomial);
69 }
70 private:
71 comedi_polynomial_t _polynomial;
72 };
73
74 class device {
75 public:
76 device() {}
77 device(const std::string &device_file)
78 {
79 this->open(device_file);
80 }
81 std::string board_name() const
82 {
83 const char *name = comedi_get_board_name(comedi_handle());
84 if(name == 0)
85 {
86 std::ostringstream message;
87 message << __PRETTY_FUNCTION__ << ": comedi_get_board_name() failed.";
88 std::cerr << message.str() << std::endl;
89 comedi_perror("comedi_get_board_name");
90 throw std::runtime_error(message.str());
91 }
92 return name;
93 }
94 int command_test(comedi_cmd *cmd) const
95 {
96 int retval = comedi_command_test(comedi_handle(), cmd);
97 if(retval < 0)
98 {
99 std::ostringstream message;
100 message << __PRETTY_FUNCTION__ << ": comedi_command_test() returned error.";
101 std::cerr << message.str() << std::endl;
102 comedi_perror("comedi_command_test");
103 throw std::runtime_error(message.str());
104 }
105 return retval;
106 }
107 void command(comedi_cmd *cmd) const
108 {
109 int retval = comedi_command(comedi_handle(), cmd);
110 if(retval < 0)
111 {
112 std::ostringstream message;
113 message << __PRETTY_FUNCTION__ << ": comedi_command() failed, return value=" << retval << " .";
114 std::cerr << message.str() << std::endl;
115 comedi_perror("comedi_command");
116 throw std::runtime_error(message.str());
117 }
118 }
119 std::string default_calibration_path() const
120 {
121 char *c_string_file_path = comedi_get_default_calibration_path(comedi_handle());
122 if(c_string_file_path == NULL)
123 {
124 comedi_perror("comedi_get_default_calibration_path");
125 throw std::runtime_error(__PRETTY_FUNCTION__);
126 }
127 std::string file_path = c_string_file_path;
128 free(c_string_file_path);
129 return file_path;
130 }
131 void do_insn(comedi_insn *instruction) const
132 {
133 int retval = comedi_do_insn(comedi_handle(), instruction);
134 if(retval < 0)
135 {
136 std::ostringstream message;
137 message << __PRETTY_FUNCTION__ << ": comedi_do_insn() failed.";
138 std::cerr << message.str() << std::endl;
139 comedi_perror("comedi_do_insn");
140 throw std::runtime_error(message.str());
141 }
142 }
143 void do_insnlist(comedi_insnlist *insnlist) const
144 {
145 int retval = comedi_do_insnlist(comedi_handle(), insnlist);
146 {
147 std::ostringstream message;
148 message << __PRETTY_FUNCTION__ << ": comedi_do_insnlist() failed.";
149 std::cerr << message.str() << std::endl;
150 comedi_perror("comedi_do_insnlist");
151 throw std::runtime_error(message.str());
152 }
153 }
154 std::string driver_name() const
155 {
156 const char *name = comedi_get_driver_name(comedi_handle());
157 if(name == 0)
158 {
159 std::ostringstream message;
160 message << __PRETTY_FUNCTION__ << ": comedi_get_driver_name() failed.";
161 std::cerr << message.str() << std::endl;
162 comedi_perror("comedi_get_driver_name");
163 throw std::runtime_error(message.str());
164 }
165 return name;
166 }
167 int fileno() const
168 {
169 int fd = comedi_fileno(comedi_handle());
170 if(fd < 0)
171 {
172 std::ostringstream message;
173 message << __PRETTY_FUNCTION__ << ": comedi_fileno() failed.";
174 std::cerr << message.str() << std::endl;
175 comedi_perror("comedi_fileno");
176 throw std::runtime_error(message.str());
177 }
178 return fd;
179 }
180 subdevice find_subdevice_by_type(int type, const subdevice *start_subdevice = 0) const;
181 subdevice get_read_subdevice() const;
182 subdevice get_write_subdevice() const;
183 unsigned n_subdevices() const
184 {
185 int retval = comedi_get_n_subdevices(comedi_handle());
186 if(retval < 0)
187 {
188 std::ostringstream message;
189 message << __PRETTY_FUNCTION__ << ": comedi_get_n_subdevices() failed.";
190 std::cerr << message.str() << std::endl;
191 comedi_perror("comedi_get_n_subdevices");
192 throw std::runtime_error(message.str());
193 }
194 return retval;
195 }
196 void open(const std::string &device_file)
197 {
198 comedi_t *dev = comedi_open(device_file.c_str());
199 if(dev == 0)
200 {
201 std::ostringstream message;
202 message << __PRETTY_FUNCTION__ << ": comedi_open() failed, with device file name: \"" << device_file << "\".";
203 std::cerr << message.str() << std::endl;
204 comedi_perror("comedi_open");
205 throw std::runtime_error(message.str().c_str());
206 }
207 _comedi_handle.reset(dev, &comedi_close);
208 }
209 private:
210 friend class subdevice;
211
212 comedi_t* comedi_handle() const
213 {
214 if(_comedi_handle == 0)
215 {
216 std::ostringstream message;
217 message << __PRETTY_FUNCTION__ << ": device not open.";
218 std::cerr << message.str() << std::endl;
219 throw std::invalid_argument(message.str());
220 }
221 return _comedi_handle.get();
222 }
223
224 boost::shared_ptr<comedi_t> _comedi_handle;
225 };
226
227 class calibration
228 {
229 public:
230 calibration() {}
231 calibration(const device &dev)
232 {
233 init(dev.default_calibration_path());
234 }
235 calibration(const std::string &file_path)
236 {
237 init(file_path);
238 }
239 const comedi_calibration_t* c_calibration() const
240 {
241 return _c_calibration.get();
242 }
243 private:
244 void init(const std::string &file_path)
245 {
246 comedi_calibration_t *cal = comedi_parse_calibration_file(file_path.c_str());
247 if(cal == NULL)
248 {
249 std::ostringstream message;
250 message << __PRETTY_FUNCTION__ << ": comedi_parse_calibration_file() failed.";
251 std::cerr << message.str() << std::endl;
252 comedi_perror("comedi_parse_calibration_file");
253 throw std::runtime_error(message.str());
254 }
255 _c_calibration.reset(cal, &comedi_cleanup_calibration);
256 }
257 boost::shared_ptr<comedi_calibration_t> _c_calibration;
258 };
259
260 class subdevice
261 {
262 public:
263 subdevice(): _index(-1)
264 {}
265 subdevice(const device &dev, unsigned subdevice_index):
266 _device(dev), _index(subdevice_index)
267 {
268 if(_index >= _device.n_subdevices())
269 {
270 std::ostringstream message;
271 message << __PRETTY_FUNCTION__ << ": invalid subdevice index.";
272 std::cerr << message.str() << std::endl;
273 throw std::invalid_argument(message.str());
274 }
275 }
276 void apply_hard_calibration(unsigned channel, unsigned range, unsigned aref, const calibration &cal)
277 {
278 if(cal.c_calibration() == 0) throw std::invalid_argument(__PRETTY_FUNCTION__);
279 int retval = comedi_apply_parsed_calibration(comedi_handle(), index(),
280 channel, range, aref, cal.c_calibration());
281 if(retval < 0)
282 {
283 comedi_perror("comedi_apply_parsed_calibration");
284 throw std::runtime_error(__PRETTY_FUNCTION__);
285 }
286 }
287 void apply_hard_calibration(unsigned channel, unsigned range, unsigned aref)
288 {
289 apply_hard_calibration(channel, range, aref, calibration(dev()));
290 }
291 void arm(unsigned source) const
292 {
293 int retval = comedi_arm(comedi_handle(), index(), source);
294 if (retval < 0)
295 {
296 std::ostringstream message;
297 message << __PRETTY_FUNCTION__ << ": comedi_arm() failed.";
298 std::cerr << message.str() << std::endl;
299 comedi_perror("comedi_arm");
300 throw std::runtime_error(message.str());
301 }
302 }
303 void arm_channel(unsigned channel, unsigned source) const
304 {
305 int retval = comedi_arm_channel(comedi_handle(), index(), channel, source);
306 if (retval < 0)
307 {
308 std::ostringstream message;
309 message << __PRETTY_FUNCTION__ << ": comedi_arm_channel() failed.";
310 std::cerr << message.str() << std::endl;
311 comedi_perror("comedi_arm_channel");
312 throw std::runtime_error(message.str());
313 }
314 }
315 unsigned buffer_size() const
316 {
317 int retval = comedi_get_buffer_size(comedi_handle(), index());
318 if(retval < 0)
319 {
320 std::ostringstream message;
321 message << __PRETTY_FUNCTION__ << ": comedi_get_buffer_size() failed.";
322 std::cerr << message.str() << std::endl;
323 comedi_perror("comedi_get_buffer_size");
324 throw std::runtime_error(message.str());
325 }
326 return retval;
327 }
328 void cancel() const
329 {
330 int retval = comedi_cancel(comedi_handle(), index());
331 if(retval < 0)
332 {
333 std::ostringstream message;
334 message << __PRETTY_FUNCTION__ << ": comedi_cancel() failed, return value=" << retval << " .";
335 std::cerr << message.str() << std::endl;
336 comedi_perror("comedi_cancel");
337 throw std::runtime_error(message.str());
338 }
339 }
340 lsampl_t data_read(unsigned channel, unsigned range, unsigned aref) const
341 {
342 lsampl_t value;
343 int retval = comedi_data_read(comedi_handle(), index(), channel, range, aref, &value);
344 if(retval < 0)
345 {
346 std::ostringstream message;
347 message << __PRETTY_FUNCTION__ << ": comedi_data_read() failed, return value=" << retval << " .";
348 std::cerr << message.str() << std::endl;
349 comedi_perror("comedi_data_read");
350 throw std::runtime_error(message.str());
351 }
352 return value;
353 }
354 std::vector<lsampl_t> data_read_n(unsigned channel, unsigned range, unsigned aref, unsigned num_samples) const
355 {
356 std::vector<lsampl_t> values(num_samples);
357 int retval = comedi_data_read_n(comedi_handle(), index(), channel, range, aref, &values.at(0), values.size());
358 if(retval < 0)
359 {
360 std::ostringstream message;
361 message << __PRETTY_FUNCTION__ << ": comedi_data_read_n() failed, return value=" << retval << " .";
362 std::cerr << message.str() << std::endl;
363 comedi_perror("comedi_data_read_n");
364 throw std::runtime_error(message.str());
365 }
366 return values;
367 }
368 lsampl_t data_read_delayed(unsigned channel, unsigned range, unsigned aref, unsigned nano_sec) const
369 {
370 lsampl_t value;
371 int retval = comedi_data_read_delayed(comedi_handle(), index(), channel, range, aref, &value, nano_sec);
372 if(retval < 0)
373 {
374 std::ostringstream message;
375 message << __PRETTY_FUNCTION__ << ": comedi_data_read_delayed() failed, return value=" << retval << " .";
376 std::cerr << message.str() << std::endl;
377 comedi_perror("comedi_data_read_delayed");
378 throw std::runtime_error(message.str());
379 }
380 return value;
381 }
382 void data_read_hint(unsigned channel, unsigned range, unsigned aref) const
383 {
384 int ret = comedi_data_read_hint(comedi_handle(), index(), channel, range, aref);
385 if(ret < 0)
386 {
387 std::ostringstream message;
388 message << __PRETTY_FUNCTION__ << ": comedi_data_read_hint() failed, return value = " << ret << " .";
389 std::cerr << message.str() << std::endl;
390 comedi_perror("comedi_data_read_hint");
391 throw std::runtime_error(message.str());
392 }
393 }
394 void data_write(unsigned channel, unsigned range, unsigned aref, lsampl_t data) const
395 {
396 int retval = comedi_data_write(comedi_handle(), index(), channel, range, aref, data);
397 if(retval < 0)
398 {
399 std::ostringstream message;
400 message << __PRETTY_FUNCTION__ << ": comedi_data_write() failed.";
401 std::cerr << message.str() << std::endl;
402 comedi_perror("comedi_data_write");
403 throw std::runtime_error(message.str());
404 }
405 }
406 device dev() const {return _device;}
407 void digital_trigger_disable(unsigned trigger_id) const
408 {
409 int retval = comedi_digital_trigger_disable(comedi_handle(), index(), trigger_id);
410 if (retval < 0)
411 {
412 std::ostringstream message;
413 message << __PRETTY_FUNCTION__ << ": comedi_digital_trigger_disable() failed.";
414 std::cerr << message.str() << std::endl;
415 comedi_perror("comedi_digital_trigger_disable");
416 throw std::runtime_error(message.str());
417 }
418 }
419 void digital_trigger_enable_edges(unsigned trigger_id, unsigned base_input, unsigned rising_edge_inputs, unsigned falling_edge_inputs) const
420 {
421 int retval = comedi_digital_trigger_enable_edges(comedi_handle(), index(), trigger_id, base_input, rising_edge_inputs, falling_edge_inputs);
422 if (retval < 0)
423 {
424 std::ostringstream message;
425 message << __PRETTY_FUNCTION__ << ": comedi_digital_trigger_enable_edges() failed.";
426 std::cerr << message.str() << std::endl;
427 comedi_perror("comedi_digital_trigger_enable_edges");
428 throw std::runtime_error(message.str());
429 }
430 }
431 void digital_trigger_enable_levels(unsigned trigger_id, unsigned base_input, unsigned high_level_inputs, unsigned low_level_inputs) const
432 {
433 int retval = comedi_digital_trigger_enable_levels(comedi_handle(), index(), trigger_id, base_input, high_level_inputs, low_level_inputs);
434 if (retval < 0)
435 {
436 std::ostringstream message;
437 message << __PRETTY_FUNCTION__ << ": comedi_digital_trigger_enable_levels() failed.";
438 std::cerr << message.str() << std::endl;
439 comedi_perror("comedi_digital_trigger_enable_levels");
440 throw std::runtime_error(message.str());
441 }
442 }
443 void dio_bitfield(unsigned write_mask, unsigned *bits) const
444 {
445 dio_bitfield2(write_mask, bits, 0);
446 }
447 void dio_bitfield2(unsigned write_mask, unsigned *bits, unsigned base_channel) const
448 {
449 int retval = comedi_dio_bitfield2(comedi_handle(), index(),
450 write_mask, bits, base_channel);
451 if(retval < 0)
452 {
453 std::ostringstream message;
454 message << __PRETTY_FUNCTION__ << ": comedi_dio_bitfield2() failed.";
455 std::cerr << message.str() << std::endl;
456 comedi_perror("comedi_dio_bitfield2");
457 throw std::runtime_error(message.str());
458 }
459 }
460 void dio_config(unsigned channel, enum comedi_io_direction direction) const
461 {
462 int retval = comedi_dio_config(comedi_handle(), index(), channel, direction);
463 if(retval < 0)
464 {
465 std::ostringstream message;
466 message << __PRETTY_FUNCTION__ << ": comedi_dio_config() failed.";
467 std::cerr << message.str() << std::endl;
468 comedi_perror("comedi_dio_config");
469 throw std::runtime_error(message.str());
470 }
471 }
472 enum comedi_io_direction dio_get_config(unsigned channel) const
473 {
474 unsigned dir;
475 int retval = comedi_dio_get_config(comedi_handle(), index(), channel, &dir);
476 if (retval < 0)
477 {
478 std::ostringstream message;
479 message << __PRETTY_FUNCTION__ << ": comedi_dio_get_config() failed.";
480 std::cerr << message.str() << std::endl;
481 comedi_perror("comedi_dio_get_config");
482 throw std::runtime_error(message.str());
483 }
484 return (enum comedi_io_direction)dir;
485 }
486 unsigned dio_read(unsigned channel) const
487 {
488 unsigned bit;
489 int retval = comedi_dio_read(comedi_handle(), index(), channel, &bit);
490 if (retval < 0)
491 {
492 std::ostringstream message;
493 message << __PRETTY_FUNCTION__ << ": comedi_dio_read() failed.";
494 std::cerr << message.str() << std::endl;
495 comedi_perror("comedi_dio_read");
496 throw std::runtime_error(message.str());
497 }
498 return bit;
499 }
500 void dio_write(unsigned channel, unsigned bit) const
501 {
502 int retval = comedi_dio_write(comedi_handle(), index(), channel, bit);
503 if (retval < 0)
504 {
505 std::ostringstream message;
506 message << __PRETTY_FUNCTION__ << ": comedi_dio_read() failed.";
507 std::cerr << message.str() << std::endl;
508 comedi_perror("comedi_dio_read");
509 throw std::runtime_error(message.str());
510 }
511 }
512 void disarm() const
513 {
514 int retval = comedi_disarm(comedi_handle(), index());
515 if (retval < 0)
516 {
517 std::ostringstream message;
518 message << __PRETTY_FUNCTION__ << ": comedi_disarm() failed.";
519 std::cerr << message.str() << std::endl;
520 comedi_perror("comedi_disarm");
521 throw std::runtime_error(message.str());
522 }
523 }
524 void disarm_channel(unsigned channel) const
525 {
526 int retval = comedi_disarm_channel(comedi_handle(), index(), channel);
527 if (retval < 0)
528 {
529 std::ostringstream message;
530 message << __PRETTY_FUNCTION__ << ": comedi_disarm_channel() failed.";
531 std::cerr << message.str() << std::endl;
532 comedi_perror("comedi_disarm_channel");
533 throw std::runtime_error(message.str());
534 }
535 }
536 unsigned find_range(unsigned channel, unsigned unit, double min, double max) const
537 {
538 int retval = comedi_find_range(comedi_handle(), index(), channel,
539 unit, min, max);
540 if(retval < 0)
541 {
542 std::ostringstream message;
543 message << __PRETTY_FUNCTION__ << ": comedi_find_range() failed.";
544 std::cerr << message.str() << std::endl;
545 comedi_perror("comedi_find_range");
546 throw std::runtime_error(message.str());
547 }
548 return retval;
549 }
550 unsigned flags() const
551 {
552 int retval = comedi_get_subdevice_flags(comedi_handle(), index());
553 if(retval < 0)
554 {
555 std::ostringstream message;
556 message << __PRETTY_FUNCTION__ << ": comedi_get_subdevice_flags() failed.";
557 std::cerr << message.str() << std::endl;
558 comedi_perror("comedi_get_subdevice_flags");
559 throw std::runtime_error(message.str());
560 }
561 return retval;
562 }
563 unsigned get_buffer_contents() const
564 {
565 int retval = comedi_get_buffer_contents(comedi_handle(), index());
566 if(retval < 0)
567 {
568 std::ostringstream message;
569 message << __PRETTY_FUNCTION__ << ": comedi_get_buffer_contents() failed.";
570 std::cerr << message.str() << std::endl;
571 comedi_perror("comedi_get_buffer_contents");
572 throw std::runtime_error(message.str());
573 }
574 return retval;
575 }
576 unsigned get_buffer_read_count() const
577 {
578 unsigned count;
579 int retval = comedi_get_buffer_read_count(comedi_handle(), index(), &count);
580 if (retval < 0)
581 {
582 std::ostringstream message;
583 message << __PRETTY_FUNCTION__ << ": comedi_get_buffer_read_count() failed.";
584 std::cerr << message.str() << std::endl;
585 comedi_perror("comedi_get_buffer_read_count");
586 throw std::runtime_error(message.str());
587 }
588 return count;
589 }
590 unsigned get_buffer_read_offset() const
591 {
592 int offset = comedi_get_buffer_read_offset(comedi_handle(), index());
593 if (offset < 0)
594 {
595 std::ostringstream message;
596 message << __PRETTY_FUNCTION__ << ": comedi_get_buffer_read_offset() failed.";
597 std::cerr << message.str() << std::endl;
598 comedi_perror("comedi_get_buffer_read_offset");
599 throw std::runtime_error(message.str());
600 }
601 return offset;
602 }
603 unsigned get_buffer_write_count() const
604 {
605 unsigned count;
606 int retval = comedi_get_buffer_write_count(comedi_handle(), index(), &count);
607 if (retval < 0)
608 {
609 std::ostringstream message;
610 message << __PRETTY_FUNCTION__ << ": comedi_get_buffer_write_count() failed.";
611 std::cerr << message.str() << std::endl;
612 comedi_perror("comedi_get_buffer_write_count");
613 throw std::runtime_error(message.str());
614 }
615 return count;
616 }
617 unsigned get_buffer_write_offset() const
618 {
619 int offset = comedi_get_buffer_write_offset(comedi_handle(), index());
620 if (offset < 0)
621 {
622 std::ostringstream message;
623 message << __PRETTY_FUNCTION__ << ": comedi_get_buffer_write_offset() failed.";
624 std::cerr << message.str() << std::endl;
625 comedi_perror("comedi_get_buffer_write_offset");
626 throw std::runtime_error(message.str());
627 }
628 return offset;
629 }
630 void get_clock_source(unsigned channel, unsigned *clock, unsigned *period_ns) const
631 {
632 int retval = comedi_get_clock_source(comedi_handle(), index(), channel, clock, period_ns);
633 if(retval < 0)
634 {
635 std::ostringstream message;
636 message << __PRETTY_FUNCTION__ << ": comedi_get_clock_source() failed.";
637 std::cerr << message.str() << std::endl;
638 comedi_perror("comedi_get_clock_source");
639 throw std::runtime_error(message.str());
640 }
641 }
642 void get_cmd_generic_timed(comedi_cmd *cmd, unsigned chanlist_len, unsigned scan_period) const
643 {
644 int retval = comedi_get_cmd_generic_timed(comedi_handle(), index(), cmd, chanlist_len, scan_period);
645 if (retval < 0)
646 {
647 std::ostringstream message;
648 message << __PRETTY_FUNCTION__ << ": comedi_get_cmd_generic_timed() failed.";
649 std::cerr << message.str() << std::endl;
650 comedi_perror("comedi_get_cmd_generic_timed");
651 throw std::runtime_error(message.str());
652 }
653 }
654 void get_cmd_src_mask(comedi_cmd *cmd) const
655 {
656 int retval = comedi_get_cmd_src_mask(comedi_handle(), index(), cmd);
657 if (retval < 0)
658 {
659 std::ostringstream message;
660 message << __PRETTY_FUNCTION__ << ": comedi_get_cmd_src_mask() failed.";
661 std::cerr << message.str() << std::endl;
662 comedi_perror("comedi_get_cmd_src_mask");
663 throw std::runtime_error(message.str());
664 }
665 }
666 void get_gate_source(unsigned channel, unsigned gate_index, unsigned *gate_source) const
667 {
668 int retval = comedi_get_gate_source(comedi_handle(), index(), channel, gate_index, gate_source);
669 if (retval < 0)
670 {
671 std::ostringstream message;
672 message << __PRETTY_FUNCTION__ << ": comedi_get_gate_source() failed.";
673 std::cerr << message.str() << std::endl;
674 comedi_perror("comedi_get_gate_source");
675 throw std::runtime_error(message.str());
676 }
677 }
678 unsigned get_routing(unsigned channel) const
679 {
680 unsigned routing = 0;
681 int retval = comedi_get_routing(comedi_handle(), index(), channel, &routing);
682 if(retval < 0)
683 {
684 std::ostringstream message;
685 message << __PRETTY_FUNCTION__ << ": comedi_get_routing() failed.";
686 std::cerr << message.str() << std::endl;
687 comedi_perror("comedi_get_routing");
688 throw std::runtime_error(message.str());
689 }
690 return routing;
691 }
692 comedi_polynomial_t hardcal_converter(unsigned channel, unsigned range,
693 enum comedi_conversion_direction direction) const
694 {
695 comedi_polynomial_t result;
696 int retval = comedi_get_hardcal_converter(comedi_handle(), index(),
697 channel, range, direction, &result);
698 if(retval < 0)
699 {
700 comedi_perror("comedi_get_hardcal_converter");
701 throw std::runtime_error(__PRETTY_FUNCTION__);
702 }
703 return result;
704 }
705 unsigned hardware_buffer_size(enum comedi_io_direction direction) const
706 {
707 int retval = comedi_get_hardware_buffer_size(comedi_handle(), index(), direction);
708 if(retval < 0)
709 {
710 std::ostringstream message;
711 message << __PRETTY_FUNCTION__ << ": comedi_get_hardware_buffer_size() failed.";
712 std::cerr << message.str() << std::endl;
713 comedi_perror("comedi_get_hardware_buffer_size");
714 throw std::runtime_error(message.str());
715 }
716 return retval;
717 }
718 unsigned index() const {return _index;};
719 void internal_trigger(unsigned trig_num) const
720 {
721 int retval = comedi_internal_trigger(comedi_handle(), index(), trig_num);
722 if(retval < 0)
723 {
724 std::ostringstream message;
725 message << __PRETTY_FUNCTION__ << ": comedi_internal_trigger() failed.";
726 std::cerr << message.str() << std::endl;
727 comedi_perror("comedi_internal_trigger");
728 throw std::runtime_error(message.str());
729 }
730 }
731 void lock() const
732 {
733 int retval = comedi_lock(comedi_handle(), index());
734 if (retval < 0)
735 {
736 std::ostringstream message;
737 message << __PRETTY_FUNCTION__ << ": comedi_lock() failed.";
738 std::cerr << message.str() << std::endl;
739 comedi_perror("comedi_lock");
740 throw std::runtime_error(message.str());
741 }
742 }
743 unsigned mark_buffer_read(unsigned bytes) const
744 {
745 int marked = comedi_mark_buffer_read(comedi_handle(), index(), bytes);
746 if (marked < 0)
747 {
748 std::ostringstream message;
749 message << __PRETTY_FUNCTION__ << ": comedi_mark_buffer_read() failed.";
750 std::cerr << message.str() << std::endl;
751 comedi_perror("comedi_mark_buffer_read");
752 throw std::runtime_error(message.str());
753 }
754 return marked;
755 }
756 unsigned mark_buffer_written(unsigned bytes) const
757 {
758 int marked = comedi_mark_buffer_written(comedi_handle(), index(), bytes);
759 if (marked < 0)
760 {
761 std::ostringstream message;
762 message << __PRETTY_FUNCTION__ << ": comedi_mark_buffer_written() failed.";
763 std::cerr << message.str() << std::endl;
764 comedi_perror("comedi_mark_buffer_written");
765 throw std::runtime_error(message.str());
766 }
767 return marked;
768 }
769 unsigned max_buffer_size() const
770 {
771 int retval = comedi_get_max_buffer_size(comedi_handle(), index());
772 if(retval < 0)
773 {
774 std::ostringstream message;
775 message << __PRETTY_FUNCTION__ << ": comedi_get_max_buffer_size() failed.";
776 std::cerr << message.str() << std::endl;
777 comedi_perror("comedi_get_max_buffer_size");
778 throw std::runtime_error(message.str());
779 }
780 return retval;
781 }
782 lsampl_t max_data(unsigned channel = 0) const
783 {
784 lsampl_t value = comedi_get_maxdata(comedi_handle(), index(), channel);
785 if(value == 0)
786 {
787 std::ostringstream message;
788 message << __PRETTY_FUNCTION__ << ": comedi_get_maxdata() failed.";
789 std::cerr << message.str() << std::endl;
790 comedi_perror("comedi_get_maxdata");
791 throw std::runtime_error(message.str());
792 }
793 return value;
794 }
795 bool max_data_is_chan_specific() const
796 {
797 int retval = comedi_maxdata_is_chan_specific(comedi_handle(), index());
798 if (retval < 0)
799 {
800 std::ostringstream message;
801 message << __PRETTY_FUNCTION__ << ": comedi_maxdata_is_chan_specific() failed.";
802 std::cerr << message.str() << std::endl;
803 comedi_perror("comedi_maxdata_is_chan_specific");
804 throw std::runtime_error(message.str());
805 }
806 return retval != 0;
807 }
808 unsigned n_channels() const
809 {
810 int retval = comedi_get_n_channels(comedi_handle(), index());
811 if(retval < 0)
812 {
813 std::ostringstream message;
814 message << __PRETTY_FUNCTION__ << ": comedi_get_n_channels() failed.";
815 std::cerr << message.str() << std::endl;
816 comedi_perror("comedi_get_n_channels");
817 throw std::runtime_error(message.str());
818 }
819 return retval;
820 }
821 unsigned n_ranges(unsigned channel = 0) const
822 {
823 int retval = comedi_get_n_ranges(comedi_handle(), index(), channel);
824 if(retval < 0)
825 {
826 std::ostringstream message;
827 message << __PRETTY_FUNCTION__ << ": comedi_get_n_ranges() failed.";
828 std::cerr << message.str() << std::endl;
829 comedi_perror("comedi_get_n_ranges");
830 throw std::runtime_error(message.str());
831 }
832 return retval;
833 }
834 unsigned poll() const
835 {
836 int amount = comedi_poll(comedi_handle(), index());
837 if (amount < 0)
838 {
839 std::ostringstream message;
840 message << __PRETTY_FUNCTION__ << ": comedi_poll() failed.";
841 std::cerr << message.str() << std::endl;
842 comedi_perror("comedi_poll");
843 throw std::runtime_error(message.str());
844 }
845 return amount;
846 }
847 const comedi_range* range(unsigned channel, unsigned range_index) const
848 {
849 comedi_range *cRange = comedi_get_range(comedi_handle(), index(), channel, range_index);
850 if(cRange == 0)
851 {
852 std::ostringstream message;
853 message << __PRETTY_FUNCTION__ << ": comedi_get_range() failed.";
854 std::cerr << message.str() << std::endl;
855 comedi_perror("comedi_get_range");
856 throw std::runtime_error(message.str());
857 }
858 return cRange;
859 }
860 bool range_is_chan_specific() const
861 {
862 int retval = comedi_range_is_chan_specific(comedi_handle(), index());
863 if (retval < 0)
864 {
865 std::ostringstream message;
866 message << __PRETTY_FUNCTION__ << ": comedi_range_is_chan_specific() failed.";
867 std::cerr << message.str() << std::endl;
868 comedi_perror("comedi_range_is_chan_specific");
869 throw std::runtime_error(message.str());
870 }
871 return retval != 0;
872 }
873 void reset() const
874 {
875 int retval = comedi_reset(comedi_handle(), index());
876 if(retval < 0)
877 {
878 std::ostringstream message;
879 message << __PRETTY_FUNCTION__ << ": comedi_reset() failed.";
880 std::cerr << message.str() << std::endl;
881 comedi_perror("comedi_reset");
882 throw std::runtime_error(message.str());
883 }
884 }
885 void reset_channel(unsigned channel) const
886 {
887 int retval = comedi_reset_channel(comedi_handle(), index(), channel);
888 if(retval < 0)
889 {
890 std::ostringstream message;
891 message << __PRETTY_FUNCTION__ << ": comedi_reset_channel() failed.";
892 std::cerr << message.str() << std::endl;
893 comedi_perror("comedi_reset_channel");
894 throw std::runtime_error(message.str());
895 }
896 }
897 void set_as_read_subdevice() const
898 {
899 int retval = comedi_set_read_subdevice(comedi_handle(), index());
900 if (retval < 0)
901 {
902 std::ostringstream message;
903 message << __PRETTY_FUNCTION__ << ": comedi_set_read_subdevice() failed.";
904 std::cerr << message.str() << std::endl;
905 comedi_perror("comedi_set_read_subdevice");
906 throw std::runtime_error(message.str());
907 }
908 }
909 void set_as_write_subdevice() const
910 {
911 int retval = comedi_set_write_subdevice(comedi_handle(), index());
912 if (retval < 0)
913 {
914 std::ostringstream message;
915 message << __PRETTY_FUNCTION__ << ": comedi_set_write_subdevice() failed.";
916 std::cerr << message.str() << std::endl;
917 comedi_perror("comedi_set_write_subdevice");
918 throw std::runtime_error(message.str());
919 }
920 }
921 void set_buffer_size(unsigned num_bytes) const
922 {
923 int retval = comedi_set_buffer_size(comedi_handle(), index(), num_bytes);
924 if(retval < 0)
925 {
926 std::ostringstream message;
927 message << __PRETTY_FUNCTION__ << ": comedi_set_buffer_size() failed.";
928 std::cerr << message.str() << std::endl;
929 comedi_perror("comedi_set_buffer_size");
930 throw std::runtime_error(message.str());
931 }
932 }
933 void set_clock_source(unsigned channel, unsigned clock, unsigned period_ns) const
934 {
935 int retval = comedi_set_clock_source(comedi_handle(), index(), channel, clock, period_ns);
936 if(retval < 0)
937 {
938 std::ostringstream message;
939 message << __PRETTY_FUNCTION__ << ": comedi_set_clock_source() failed.";
940 std::cerr << message.str() << std::endl;
941 comedi_perror("comedi_set_clock_source");
942 throw std::runtime_error(message.str());
943 }
944 }
945 void set_counter_mode(unsigned channel, unsigned mode_bits) const
946 {
947 int retval = comedi_set_counter_mode(comedi_handle(), index(), channel, mode_bits);
948 if(retval < 0)
949 {
950 std::ostringstream message;
951 message << __PRETTY_FUNCTION__ << ": comedi_set_counter_mode() failed.";
952 std::cerr << message.str() << std::endl;
953 comedi_perror("comedi_set_counter_mode");
954 throw std::runtime_error(message.str());
955 }
956 }
957 void set_filter(unsigned channel, unsigned filter) const
958 {
959 int retval = comedi_set_filter(comedi_handle(), index(), channel, filter);
960 if (retval < 0)
961 {
962 std::ostringstream message;
963 message << __PRETTY_FUNCTION__ << ": comedi_set_filter() failed.";
964 std::cerr << message.str() << std::endl;
965 comedi_perror("comedi_set_filter");
966 throw std::runtime_error(message.str());
967 }
968 }
969 void set_gate_source(unsigned channel, unsigned gate_index, unsigned gate_source) const
970 {
971 int retval = comedi_set_gate_source(comedi_handle(), index(), channel, gate_index, gate_source);
972 if(retval < 0)
973 {
974 std::ostringstream message;
975 message << __PRETTY_FUNCTION__ << ": comedi_set_gate_source() failed.";
976 std::cerr << message.str() << std::endl;
977 comedi_perror("comedi_set_gate_source");
978 throw std::runtime_error(message.str());
979 }
980 }
981 void set_max_buffer_size(unsigned num_bytes) const
982 {
983 int retval = comedi_set_max_buffer_size(comedi_handle(), index(), num_bytes);
984 if(retval < 0)
985 {
986 std::ostringstream message;
987 message << __PRETTY_FUNCTION__ << ": comedi_set_max_buffer_size() failed.";
988 std::cerr << message.str() << std::endl;
989 comedi_perror("comedi_set_max_buffer_size");
990 throw std::runtime_error(message.str());
991 }
992 }
993 void set_other_source(unsigned channel, unsigned other, unsigned source) const
994 {
995 int retval = comedi_set_other_source(comedi_handle(), index(), channel, other, source);
996 if (retval < 0)
997 {
998 std::ostringstream message;
999 message << __PRETTY_FUNCTION__ << ": comedi_set_other_source() failed.";
1000 std::cerr << message.str() << std::endl;
1001 comedi_perror("comedi_set_other_source");
1002 throw std::runtime_error(message.str());
1003 }
1004 }
1005 void set_routing(unsigned channel, unsigned routing) const
1006 {
1007 int retval = comedi_set_routing(comedi_handle(), index(), channel, routing);
1008 if(retval < 0)
1009 {
1010 std::ostringstream message;
1011 message << __PRETTY_FUNCTION__ << ": comedi_set_routing() failed.";
1012 std::cerr << message.str() << std::endl;
1013 comedi_perror("comedi_set_routing");
1014 throw std::runtime_error(message.str());
1015 }
1016 }
1017 comedi_polynomial_t softcal_converter(unsigned channel, unsigned range,
1018 enum comedi_conversion_direction direction, const calibration &cal) const
1019 {
1020 if(cal.c_calibration() == 0) throw std::invalid_argument(__PRETTY_FUNCTION__);
1021 comedi_polynomial_t result;
1022 int retval = comedi_get_softcal_converter(index(),
1023 channel, range, direction, cal.c_calibration(), &result);
1024 if(retval < 0)
1025 {
1026 comedi_perror("comedi_get_softcal_converter");
1027 throw std::runtime_error(__PRETTY_FUNCTION__);
1028 }
1029 return result;
1030 }
1031 comedi_subdevice_type subdevice_type() const
1032 {
1033 int retval = comedi_get_subdevice_type(comedi_handle(), index());
1034 if(retval < 0)
1035 {
1036 std::ostringstream message;
1037 message << __PRETTY_FUNCTION__ << ": comedi_get_subdevice_type() failed.";
1038 std::cerr << message.str() << std::endl;
1039 comedi_perror("comedi_get_subdevice_type");
1040 throw std::runtime_error(message.str());
1041 }
1042 return comedi_subdevice_type(retval);
1043 }
1044 void unlock() const
1045 {
1046 int retval = comedi_unlock(comedi_handle(), index());
1047 if (retval < 0)
1048 {
1049 std::ostringstream message;
1050 message << __PRETTY_FUNCTION__ << ": comedi_unlock() failed.";
1051 std::cerr << message.str() << std::endl;
1052 comedi_perror("comedi_unlock");
1053 throw std::runtime_error(message.str());
1054 }
1055 }
1056 private:
1057 comedi_t* comedi_handle() const {return dev().comedi_handle();}
1058
1059 device _device;
1060 unsigned _index;
1061 };
1062
1063 inline subdevice device::find_subdevice_by_type(int type, const subdevice *start_subdevice) const
1064 {
1065 unsigned start_index;
1066 if(start_subdevice) start_index = start_subdevice->index() + 1;
1067 else start_index = 0;
1068 int subdev = comedi_find_subdevice_by_type(comedi_handle(), type, start_index);
1069 if(subdev < 0)
1070 {
1071 std::ostringstream message;
1072 message << __PRETTY_FUNCTION__ << ": failed to find subdevice of type " << type << " .";
1073 std::cerr << message.str() << std::endl;
1074 comedi_perror("comedi_find_subdevice_by_type");
1075 throw std::runtime_error(message.str());
1076 }
1077 return subdevice(*this, subdev);
1078 }
1079
1080 inline subdevice device::get_read_subdevice() const
1081 {
1082 int subdev = comedi_get_read_subdevice(comedi_handle());
1083 if (subdev < 0)
1084 {
1085 std::ostringstream message;
1086 message << __PRETTY_FUNCTION__ << ": comedi_get_read_subdevice() failed.";
1087 std::cerr << message.str() << std::endl;
1088 comedi_perror("comedi_get_read_subdevice");
1089 throw std::runtime_error(message.str());
1090 }
1091 return subdevice(*this, subdev);
1092 }
1093
1094 inline subdevice device::get_write_subdevice() const
1095 {
1096 int subdev = comedi_get_write_subdevice(comedi_handle());
1097 if (subdev < 0)
1098 {
1099 std::ostringstream message;
1100 message << __PRETTY_FUNCTION__ << ": comedi_get_write_subdevice() failed.";
1101 std::cerr << message.str() << std::endl;
1102 comedi_perror("comedi_get_write_subdevice");
1103 throw std::runtime_error(message.str());
1104 }
1105 return subdevice(*this, subdev);
1106 }
1107 };
1108
1109 #endif // _COMEDILIB_WRAPPER_HPP