doudou / orocos-rtt forked from psoetens/orocos-rtt

Orocos Real-Time Toolkit

Janosch Machowinski (author)
Wed Nov 26 07:50:12 -0800 2008
Sylvain Joyeux (committer)
Fri Dec 19 07:31:38 -0800 2008
orocos-rtt / tests / FakeDigitalDevice.hpp
100644 141 lines (116 sloc) 4.815 kb
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
132
133
134
135
136
137
138
139
140
141
/***************************************************************************
tag: Peter Soetens Thu Apr 22 20:40:58 CEST 2004 FakeDigitalDevice.hpp
 
FakeDigitalDevice.hpp - description
-------------------
begin : Thu April 22 2004
copyright : (C) 2004 Peter Soetens
email : peter.soetens@mech.kuleuven.ac.be
 
***************************************************************************
* 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 FAKEDIGITALDEVICE_HPP
#define FAKEDIGITALDEVICE_HPP
 
#include "dev/DigitalInInterface.hpp"
#include "dev/DigitalOutInterface.hpp"
#include <vector>
 
namespace RTT
{
    /**
* A Fake (Simulated) Digital Input/Output Device which replicates the inputs
* on its outputs.
*/
    class FakeDigitalDevice
        : public DigitalInInterface,
          public DigitalOutInterface
    {
    public:
        std::vector<bool> mchannels;
 
        FakeDigitalDevice(unsigned int channels=32)
            : DigitalInInterface("FakeDigitalDevice"),
              DigitalOutInterface("FakeDigitalDevice"),
              mchannels(channels, false)
        {}
 
        virtual void switchOn( unsigned int n )
        {
            if ( n < mchannels.size() )
                mchannels[n] = true;
        }
 
        virtual void switchOff( unsigned int n )
        {
            if ( n < mchannels.size() )
                mchannels[n] = false;
        }
 
        virtual void setBit( unsigned int bit, bool value )
        {
            if ( bit < mchannels.size() )
                mchannels[bit] = value;
        }
 
        virtual void setSequence(unsigned int start_bit, unsigned int stop_bit, unsigned int value)
        {
            if ( start_bit < mchannels.size() && stop_bit < mchannels.size() )
                for (unsigned int i = start_bit; i <= stop_bit; ++i)
                    mchannels[i] = value & ( 1<<( i - start_bit ) );
        }
 
        virtual bool checkBit(unsigned int n) const
        {
            if ( n < mchannels.size() )
                return mchannels[n];
            return false;
        }
 
 
        virtual unsigned int checkSequence( unsigned int start_bit, unsigned int stop_bit ) const
        {
            unsigned int result = 0;
            if ( start_bit < mchannels.size() && stop_bit < mchannels.size() )
                for (unsigned int i = start_bit; i <= stop_bit; ++i)
                    result += (mchannels[i] & 1)<<i;
            return result;
        }
 
        virtual unsigned int nbOfOutputs() const
        {
            return mchannels.size();
        }
 
        virtual unsigned int nbOfInputs() const
        {
            return mchannels.size();
        }
 
        virtual bool isOn( unsigned int bit = 0) const
        {
            if ( bit < mchannels.size() )
                return mchannels[bit];
            return false;
        }
 
        virtual bool isOff( unsigned int bit = 0) const
        {
            if ( bit < mchannels.size() )
                return !mchannels[bit];
            return true;
        }
 
        virtual bool readBit( unsigned int bit = 0) const
        {
            if ( bit < mchannels.size() )
                return mchannels[bit];
            return false;
        }
 
        virtual unsigned int readSequence(unsigned int start_bit, unsigned int stop_bit) const
        {
            if ( start_bit < mchannels.size() && stop_bit < mchannels.size() )
                return checkSequence(start_bit, stop_bit);
            return 0;
        }
 
    };
 
 
}
 
 
#endif