| 1 | /* |
| 2 | * Instruction example |
| 3 | * Part of Comedilib |
| 4 | * |
| 5 | * Copyright (c) 1999,2000 David A. Schleef <[email protected]> |
| 6 | * |
| 7 | * This file may be freely modified, distributed, and combined with |
| 8 | * other software, as long as proper attribution is given in the |
| 9 | * source code. |
| 10 | */ |
| 11 | /* |
| 12 | This example shows how to use instructions, i.e., comedi_insns. |
| 13 | |
| 14 | Using instructions directly, as in this example, is not recommended |
| 15 | for the beginner. Use the higher-level functions such as |
| 16 | comedi_data_read(), comedi_data_write(), etc., as demonstrated |
| 17 | in the inp, outp, and dio examples. Then, if you need the |
| 18 | additional flexibility that using instructions directly |
| 19 | provides, study this example and the implementations of |
| 20 | comedi_data_read(), etc. |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <comedilib.h>cxx . . $ `<file>/include/comedilib.h`/ |
| 25 | #include <fcntl.h> |
| 26 | #include <unistd.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <errno.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <unistd.h> |
| 31 | #include "examples.h"cxx . . $ `<file>/demo/examples.h`/ |
| 32 | |
| 33 | /* |
| 34 | * This example does 3 instructions in one system call. It does |
| 35 | * a gettimeofday() call, then reads N_SAMPLES samples from an |
| 36 | * analog input, and the another gettimeofday() call. |
| 37 | * |
| 38 | * (Note: The gettimeofday() value is obtained using an INSN_GTOD |
| 39 | * instruction, which places the seconds value in data[0] and the |
| 40 | * microseconds in data[1], so the seconds value is limited to |
| 41 | * 32-bits even on 64-bit systems.) |
| 42 | */ |
| 43 | |
| 44 | #define MAX_SAMPLEScxx . . $ `demo/insn.c:44:9`! 128 |
| 45 | |
| 46 | comedi_tcxx . . $ comedi_t# *devicecxx . . $ device.; |
| 47 | |
| 48 | int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 49 | { |
| 50 | int ret,i; |
| 51 | comedi_insncxx . . $ comedi_insn# insn[3]; |
| 52 | comedi_insnlistcxx . . $ comedi_insnlist# il; |
| 53 | lsampl_tcxx . . $ lsampl_t# t1[2], t2[2]; |
| 54 | lsampl_tcxx . . $ lsampl_t# data[MAX_SAMPLEScxx . . $ `demo/insn.c:44:9`!]; |
| 55 | long diffus; |
| 56 | struct parsed_optionscxx . . $ parsed_options# options; |
| 57 | |
| 58 | init_parsed_optionscxx . . $ init_parsed_options(8caace97c9c42d8f).(&options); |
| 59 | options.n_scancxx . . $ parsed_options#n_scan. = 10; /* override default n_scan value to something more suitable */ |
| 60 | parse_optionscxx . . $ parse_options(b6aabb1543a6f628).(&options, argc, argv); |
| 61 | if(options.n_scancxx . . $ parsed_options#n_scan. > MAX_SAMPLEScxx . . $ `demo/insn.c:44:9`! ){ |
| 62 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).( stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Requested too many samples, reducing to %i\n", MAX_SAMPLEScxx . . $ `demo/insn.c:44:9`! ); |
| 63 | options.n_scancxx . . $ parsed_options#n_scan. = MAX_SAMPLEScxx . . $ `demo/insn.c:44:9`!; |
| 64 | } |
| 65 | |
| 66 | devicecxx . . $ device. = comedi_opencxx . . $ comedi_open(d67864c6c007fe39).(options.filenamecxx . . $ parsed_options#filename.); |
| 67 | if(!devicecxx . . $ device.){ |
| 68 | comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).(options.filenamecxx . . $ parsed_options#filename.); |
| 69 | exitcxx . . $ exit(3ca292c338df9bb3).(-1); |
| 70 | } |
| 71 | |
| 72 | if(options.verbosecxx . . $ parsed_options#verbose.){ |
| 73 | printfcxx . . $ printf(581fddd72a8db8fc).("measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", |
| 74 | options.filenamecxx . . $ parsed_options#filename., options.subdevicecxx . . $ parsed_options#subdevice., options.channelcxx . . $ parsed_options#channel., options.rangecxx . . $ parsed_options#range., options.arefcxx . . $ parsed_options#aref.); |
| 75 | } |
| 76 | |
| 77 | /* Set up a the "instruction list", which is just a pointer |
| 78 | * to the array of instructions and the number of instructions. |
| 79 | */ |
| 80 | il.n_insnscxx . . $ comedi_insnlist_struct#n_insns.=3; |
| 81 | il.insnscxx . . $ comedi_insnlist_struct#insns.=insn; |
| 82 | |
| 83 | /* Instruction 0: perform a gettimeofday() */ |
| 84 | insn[0].insncxx . . $ comedi_insn_struct#insn.=INSN_GTODcxx . . $ `include/comedi.h:133:9`!; |
| 85 | insn[0].ncxx . . $ comedi_insn_struct#n.=2; |
| 86 | insn[0].datacxx . . $ comedi_insn_struct#data.=t1; |
| 87 | |
| 88 | /* Instruction 1: do 10 analog input reads */ |
| 89 | insn[1].insncxx . . $ comedi_insn_struct#insn.=INSN_READcxx . . $ `include/comedi.h:129:9`!; |
| 90 | insn[1].ncxx . . $ comedi_insn_struct#n. = options.n_scancxx . . $ parsed_options#n_scan.; |
| 91 | insn[1].datacxx . . $ comedi_insn_struct#data.=data; |
| 92 | insn[1].subdevcxx . . $ comedi_insn_struct#subdev. = options.subdevicecxx . . $ parsed_options#subdevice.; |
| 93 | insn[1].chanspeccxx . . $ comedi_insn_struct#chanspec.=CR_PACKcxx . . $ `include/comedi.h:79:9`!(options.channel, options.range, options.aref); |
| 94 | |
| 95 | /* Instruction 2: perform a gettimeofday() */ |
| 96 | insn[2].insncxx . . $ comedi_insn_struct#insn.=INSN_GTODcxx . . $ `include/comedi.h:133:9`!; |
| 97 | insn[2].ncxx . . $ comedi_insn_struct#n.=2; |
| 98 | insn[2].datacxx . . $ comedi_insn_struct#data.=t2; |
| 99 | |
| 100 | ret=comedi_do_insnlistcxx . . $ comedi_do_insnlist(7d76e6bd0e6cef21).(devicecxx . . $ device.,&il); |
| 101 | if(ret<0){ |
| 102 | comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).(options.filenamecxx . . $ parsed_options#filename.); |
| 103 | exitcxx . . $ exit(3ca292c338df9bb3).(-1); |
| 104 | } |
| 105 | |
| 106 | printfcxx . . $ printf(581fddd72a8db8fc).("initial time: %u.%06u\n", t1[0], t1[1]); |
| 107 | for(i = 0; i < options.n_scancxx . . $ parsed_options#n_scan.; i++){ |
| 108 | printfcxx . . $ printf(581fddd72a8db8fc).("%d\n", data[i]); |
| 109 | } |
| 110 | printfcxx . . $ printf(581fddd72a8db8fc).("final time: %u.%06u\n", t2[0], t2[1]); |
| 111 | |
| 112 | if (t2[0] >= t1[0]) { |
| 113 | diffus = (long)(t2[0] - t1[0]); |
| 114 | } else { |
| 115 | diffus = -(long)(t1[0] - t2[0]); |
| 116 | } |
| 117 | diffus *= 1000000; |
| 118 | if (t2[1] >= t1[1]) { |
| 119 | diffus += (long)(t2[1] - t1[1]); |
| 120 | } else { |
| 121 | diffus -= (long)(t1[1] - t2[1]); |
| 122 | } |
| 123 | printfcxx . . $ printf(581fddd72a8db8fc).("difference (us): %ld\n", diffus); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 |