File: timed.c

package info (click to toggle)
comedilib 0.11.0%2B5-1.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,544 kB
  • sloc: xml: 19,779; ansic: 14,719; sh: 5,679; cpp: 2,211; ruby: 1,658; perl: 700; makefile: 596; yacc: 439; lex: 86; python: 17
file content (94 lines) | stat: -rw-r--r-- 2,244 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
    lib/timed.c
    description

    COMEDILIB - Linux Control and Measurement Device Interface Library
    Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation, version 2.1
    of the License.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
    USA.
*/

#include "libinternal.h"

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>


#define BUFSZ 100

EXPORT_ALIAS_DEFAULT(_comedi_timed_1chan,comedi_timed_1chan,0.7.18);
int _comedi_timed_1chan(comedi_t *dev,unsigned int subd,unsigned int chan,unsigned int range,
	unsigned int aref,double freq,unsigned int n_samples,double *data)
{
	comedi_trig t;
	double act_freq;
	sampl_t *buffer;
	comedi_range *the_range;
	unsigned int maxdata;
	int i,n,m;
	
	if(!valid_chan(dev,subd,chan))return -1;
	if(!data)return -1;
	
	memset(&t,0,sizeof(t));
	
	/* check range */

	the_range=comedi_get_range(dev,subd,chan,range);
	maxdata=comedi_get_maxdata(dev,subd,chan);

	chan=CR_PACK(chan,range,aref);

	t.subdev=subd;
	t.mode=2;
	t.n_chan=1;
	t.chanlist=&chan;
	t.n=n_samples;
	comedi_get_timer(dev,subd,freq,&t.trigvar,&act_freq);
	t.trigvar1=1;
	
	buffer=malloc(sizeof(sampl_t)*BUFSZ);
	if(!buffer)return -1;

	comedi_trigger(dev,&t);
	n=0;
	while(n<n_samples){
		m=n_samples-n;
		if(m>BUFSZ)m=BUFSZ;
		if((m=read(dev->fd,buffer,m*sizeof(sampl_t)))<0){
			/* ack! */
			return -1;
		}
		m/=sizeof(sampl_t);
		for(i=0;i<m;i++){
			data[n+i]=comedi_to_phys(buffer[i],the_range,maxdata);
		}
		n+=m;
	}

	free(buffer);

	return 0;
}