-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFakeAnalogDevice.hpp
131 lines (109 loc) · 4.37 KB
/
FakeAnalogDevice.hpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/***************************************************************************
tag: Peter Soetens Thu Apr 22 20:40:59 CEST 2004 FakeAnalogDevice.hpp
FakeAnalogDevice.hpp - description
-------------------
begin : Thu April 22 2004
copyright : (C) 2004 Peter Soetens
email : [email protected]
***************************************************************************
* 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; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* 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 *
* *
***************************************************************************/
#ifndef FAKEANALOGDEVICE_HPP
#define FAKEANALOGDEVICE_HPP
#include <extras/dev/AnalogInInterface.hpp>
#include <extras/dev/AnalogOutInterface.hpp>
namespace RTT
{
using namespace dev;
/**
* A test class which replaces a real device driver.
* It reproduces on the output what it gets on the input.
*/
struct FakeAnalogDevice :
public AnalogInInterface,
public AnalogOutInterface
{
unsigned int nbofchans;
int* mchannels;
unsigned int mbin_range;
double mlowest, mhighest;
FakeAnalogDevice(unsigned int channels=32, unsigned int bin_range=4096, double lowest = -5.0, double highest = +5.0)
: AnalogInInterface("FakeAnalogDevice"),
AnalogOutInterface("FakeAnalogDevice"),
nbofchans(channels),
mchannels( new int[channels] ),
mbin_range( bin_range),
mlowest( lowest),
mhighest( highest)
{}
~FakeAnalogDevice() {
delete[] mchannels;
}
virtual void rangeSet(unsigned int /*chan*/,
unsigned int /*range*/) {}
virtual void arefSet(unsigned int /*chan*/,
unsigned int /*aref*/) {}
virtual unsigned int nbOfChannels() const {
return nbofchans;
}
virtual int read( unsigned int chan, double& value )
{
if (chan < nbofchans) {
value = mchannels[chan] / resolution(chan) + mlowest;
return 0;
}
return -1;
}
virtual int write( unsigned int chan, double value ) {
if (chan < nbofchans) {
mchannels[chan] = (unsigned int)((value - mlowest) * resolution(chan));
return 0;
}
return -1;
}
virtual int rawRead( unsigned int chan, int & value )
{
if (chan < nbofchans) {
value = mchannels[chan];
return 0;
}
return -1;
}
virtual int rawWrite( unsigned int chan, int value ) {
if (chan < nbofchans)
mchannels[chan] = value;
return 0;
}
virtual unsigned int rawRange() const
{
return mbin_range;
}
virtual double lowest(unsigned int /*chan*/) const
{
return mlowest;
}
virtual double highest(unsigned int /*chan*/) const
{
return mhighest;
}
virtual double resolution(unsigned int /*chan*/) const
{
return mbin_range/(mhighest-mlowest);
}
};
}
#endif