~ubuntu-branches/ubuntu/feisty/comedilib/feisty

« back to all changes in this revision

Viewing changes to demo/insn.c

  • Committer: Bazaar Package Importer
  • Author(s): David Schleef
  • Date: 2003-09-23 18:11:12 UTC
  • Revision ID: james.westby@ubuntu.com-20030923181112-sat05jyh702rb1at
Tags: upstream-0.7.21
ImportĀ upstreamĀ versionĀ 0.7.21

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Instruction example
 
3
 * Part of Comedilib
 
4
 *
 
5
 * Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
 
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>
 
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"
 
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
 
 
39
#define MAX_SAMPLES 128
 
40
 
 
41
comedi_t *device;
 
42
 
 
43
int main(int argc, char *argv[])
 
44
{
 
45
        int ret,i;
 
46
        comedi_insn insn[3];
 
47
        comedi_insnlist il;
 
48
        struct timeval t1,t2;
 
49
        lsampl_t data[MAX_SAMPLES];
 
50
 
 
51
        n_scan = 10;    /* override default n_scan value to something more suitable */
 
52
        parse_options(argc,argv);
 
53
        if( n_scan > MAX_SAMPLES ){
 
54
                fprintf( stderr, "Requested too many samples, reducing to %i\n", MAX_SAMPLES );
 
55
                n_scan = MAX_SAMPLES;
 
56
        }
 
57
 
 
58
        device=comedi_open(filename);
 
59
        if(!device){
 
60
                comedi_perror(filename);
 
61
                exit(0);
 
62
        }
 
63
 
 
64
        if(verbose){
 
65
                printf("measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d\n",
 
66
                        filename,subdevice,channel,range,aref);
 
67
        }
 
68
 
 
69
        /* Set up a the "instruction list", which is just a pointer
 
70
         * to the array of instructions and the number of instructions.
 
71
         */
 
72
        il.n_insns=3;
 
73
        il.insns=insn;
 
74
 
 
75
        /* Instruction 0: perform a gettimeofday() */
 
76
        insn[0].insn=INSN_GTOD;
 
77
        insn[0].n=2;
 
78
        insn[0].data=(void *)&t1;
 
79
 
 
80
        /* Instruction 1: do 10 analog input reads */
 
81
        insn[1].insn=INSN_READ;
 
82
        insn[1].n=n_scan;
 
83
        insn[1].data=data;
 
84
        insn[1].subdev=subdevice;
 
85
        insn[1].chanspec=CR_PACK(channel,range,aref);
 
86
 
 
87
        /* Instruction 2: perform a gettimeofday() */
 
88
        insn[2].insn=INSN_GTOD;
 
89
        insn[2].n=2;
 
90
        insn[2].data=(void *)&t2;
 
91
 
 
92
        ret=comedi_do_insnlist(device,&il);
 
93
        if(ret<0){
 
94
                comedi_perror(filename);
 
95
                exit(0);
 
96
        }
 
97
 
 
98
        printf("initial time: %ld.%06ld\n",t1.tv_sec,t1.tv_usec);
 
99
        for(i=0;i<n_scan;i++){
 
100
                printf("%d\n",data[i]);
 
101
        }
 
102
        printf("final time: %ld.%06ld\n",t2.tv_sec,t2.tv_usec);
 
103
 
 
104
        printf("difference (us): %ld\n",(t2.tv_sec-t1.tv_sec)*1000000+
 
105
                        (t2.tv_usec-t1.tv_usec));
 
106
 
 
107
        return 0;
 
108
}
 
109