include typemaps.i
[comedilib.git] / swig / ruby / demo / cmd
1 #!/usr/bin/env ruby
2
3 require 'comedi'
4 require 'getoptlong'
5 require 'common'
6
7 include Comedi
8
9 cmdtest_messages = [ "success", "invalid source", "source conflict",
10                     "invalid argument", "argument conflict",
11                     "invalid chanlist" ]
12
13 class SWIG::TYPE_p_comedi_t
14
15     def prepare_cmd_lib(subdevice, freq, cmd)
16         ret, cmd = get_cmd_generic_timed(subdevice, cmd,
17             1000000000.0 / freq)
18
19         if ret < 0
20             printf("comedi_get_cmd_generic_timed failed\n")
21             return ret, cmd
22         end
23
24         cmd.chanlist = $chanlist
25         cmd.chanlist_len = $n_chan
26
27         cmd.scan_end_arg = $n_chan
28         cmd.stop_arg = $n_scan if cmd.stop_src == TRIG_COUNT
29
30         return 0, cmd
31     end
32
33 end
34
35 parse_options($ARGV)
36
37 begin
38     dev = Comedi::open($filename)
39 rescue
40     comedi_perror($filename)
41     exit 1
42 end
43
44 $chanlist = Chanlist.new($n_chan)
45 0.upto($n_chan - 1) do |i|
46     $chanlist[i] = cr_pack($channel + i, $range, $aref)
47 end
48
49 ret, cmd = dev.prepare_cmd_lib($subdevice, $freq, Comedi_cmd_struct.new)
50
51 $stderr.printf("command before testing:\n")
52 dump_cmd($stderr, cmd)
53
54 ret, cmd = dev.command_test(cmd)
55 if ret < 0
56     comedi_perror("comedi_command_test")
57     exit 1
58 end
59 $stderr.printf("first test returned %d (%s)\n", ret, cmdtest_messages[ret])
60 dump_cmd($stderr, cmd)
61
62 ret, cmd = dev.command_test(cmd)
63 if ret < 0
64     comedi_perror("comedi_command_test")
65     exit 1
66 end
67 $stderr.printf("second test returned %d (%s)\n", ret, cmdtest_messages[ret])
68 dump_cmd($stderr, cmd)
69
70 tstart = Time.new
71 $stderr.printf("start time: %d.%06d\n", tstart.tv_sec, tstart.tv_usec)
72
73 ret = dev.command(cmd)
74 if ret < 0
75     comedi_perror("comedi_command")
76     exit 1
77 end
78
79 while line = dev.ios.read(2 * $n_chan)
80     data = line.unpack('S*')
81     data.each do |d|
82         printf("%d ", d)
83     end
84     puts
85 end
86
87 tend = Time.new
88 $stderr.printf("end time: %d.%06d\n", tend.tv_sec, tend.tv_usec)
89
90 tdiff = tend - tstart
91 $stderr.printf("time: %.6f\n", tdiff)