Cleaned up mixing of spaces/tabs for indentation probably done by emacs.
[comedilib.git] / demo / choose_clock.c
1 /*
2  * INSN_CONFIG_SET_CLOCK_SRC example
3  * Part of Comedilib
4  *
5  * Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
6  * Copyright (c) 2007 Frank Mori Hess <fmhess@users.sourceforge.net>
7  *
8  * This file may be freely modified, distributed, and combined with
9  * other software, as long as proper attribution is given in the
10  * source code.
11  */
12 /*
13  * Requirements:  A board with a subdevice that supports
14  *    INSN_CONFIG_CLOCK_SRC
15  */
16
17 #define _GNU_SOURCE
18
19 #include <stdio.h>
20 #include <comedilib.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <ctype.h>
28 #include "examples.h"
29
30
31 comedi_t *device;
32
33 int main(int argc, char *argv[])
34 {
35         unsigned period_ns;
36         int retval;
37         lsampl_t clock_selection;
38         struct parsed_options options;
39
40         init_parsed_options(&options);
41         options.freq = 0.;
42         parse_options(&options, argc, argv);
43
44         device = comedi_open(options.filename);
45         if(!device){
46                 comedi_perror(options.filename);
47                 exit(-1);
48         }
49         if(options.freq > 0.)
50                 period_ns = 1e9 / options.freq;
51         else
52                 period_ns = 0;
53         clock_selection = options.value;
54         printf("Selecting master clock %d on subdevice %d.\n", clock_selection, options.subdevice);
55         if(period_ns)
56         {
57                 printf("Clock period = %d nanoseconds.\n", period_ns);
58         }else
59         {
60                 printf("Clock period unspecified.\n");
61         }
62         comedi_insn insn;
63         lsampl_t data[3];
64         memset(&insn, 0, sizeof(comedi_insn));
65         insn.insn = INSN_CONFIG;
66         insn.subdev = options.subdevice;
67         insn.data = data;
68         insn.n = sizeof(data) / sizeof(data[0]);
69         data[0] = INSN_CONFIG_SET_CLOCK_SRC;
70         data[1] = clock_selection;
71         data[2] = period_ns;
72
73         retval = comedi_do_insn(device, &insn);
74         if(retval < 0) comedi_perror("comedi_do_insn");
75         return retval;
76 }
77