root/juggler/branches/2.2/modules/gadgeteer/gadget/Type/Command.cpp

Revision 19729, 6.1 kB (checked in by patrick, 2 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  *************** <auto-copyright.pl END do not edit this line> ***************/
26
27 #include <gadget/gadgetConfig.h>
28 #include <boost/concept_check.hpp>
29 #include <vpr/Util/Debug.h>
30 #include <vpr/IO/ObjectWriter.h>
31 #include <vpr/IO/ObjectReader.h>
32 #include <gadget/Type/Command.h>
33
34 namespace gadget
35 {
36
37 const CommandData Command::getCommandData(int devNum)
38 {
39    SampleBuffer_t::buffer_t& stable_buffer = mCommandSamples.stableBuffer();
40
41    if ( (!stable_buffer.empty()) &&
42         (stable_buffer.back().size() > (unsigned)devNum) )  // If Have entry && devNum in range
43    {
44       return stable_buffer.back()[devNum];
45    }
46    else        // No data or request out of range, return default value
47    {
48       if ( stable_buffer.empty() )
49       {
50          vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
51             << "WARNING: [gadget::Command::getCommandData()] "
52             << "Stable buffer is empty.  If this is not the first "
53             << "read, then this is a problem.\n" << vprDEBUG_FLUSH;
54       }
55       else
56       {
57          vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL)
58             << "WARNING: [gadget::Command::getCommandData()] "
59             << "Requested devNum (" << devNum
60             << ") is not in the range available.  "
61             << "This is probably a configuration error.\n" << vprDEBUG_FLUSH;
62       }
63       return mDefaultValue;
64    }
65 }
66
67 void Command::writeObject(vpr::ObjectWriter* writer)
68 {
69    writer->beginTag(Command::getInputTypeName());
70    SampleBuffer_t::buffer_t& stable_buffer = mCommandSamples.stableBuffer();
71    writer->beginAttribute(gadget::tokens::DataTypeAttrib);
72       // Write out the data type so that we can assert if reading in wrong
73       // place.
74       writer->writeUint16(MSG_DATA_COMMAND);
75    writer->endAttribute();
76
77    writer->beginAttribute(gadget::tokens::SampleBufferLenAttrib);
78       // Write the # of vectors in the stable buffer.
79       writer->writeUint16(stable_buffer.size());
80    writer->endAttribute();
81
82    if ( !stable_buffer.empty() )
83    {
84       mCommandSamples.lock();
85       for ( unsigned j = 0; j < stable_buffer.size(); ++j )                               // For each vector in the stable buffer
86       {
87          writer->beginTag(gadget::tokens::BufferSampleTag);
88          writer->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
89             writer->writeUint16(stable_buffer[j].size());                           // Write the # of CommandDatas in the vector
90          writer->endAttribute();
91          for ( unsigned i = 0; i < stable_buffer[j].size(); ++i )                         // For each CommandData in the vector
92          {
93             writer->beginTag(gadget::tokens::DigitalValue);
94             writer->beginAttribute(gadget::tokens::TimeStamp);
95                writer->writeUint64(stable_buffer[j][i].getTime().usec());           // Write Time Stamp vpr::Uint64
96             writer->endAttribute();
97             writer->writeUint32((vpr::Uint32)stable_buffer[j][i].getDigital());  // Write Command Data(int)
98             writer->endTag();
99          }
100          writer->endTag();
101       }
102       mCommandSamples.unlock();
103    }
104    writer->endTag();
105 }
106
107 void Command::readObject(vpr::ObjectReader* reader)
108 {
109    vprASSERT(reader->attribExists("rim.timestamp.delta"));
110    vpr::Uint64 delta = reader->getAttrib<vpr::Uint64>("rim.timestamp.delta");
111
112       // ASSERT if this data is really not Command Data
113    reader->beginTag(Command::getInputTypeName());
114    reader->beginAttribute(gadget::tokens::DataTypeAttrib);
115       vpr::Uint16 temp = reader->readUint16();
116    reader->endAttribute();
117
118    // XXX: Should there be error checking for the case when vprASSERT()
119    // is compiled out?  -PH 8/21/2003
120    vprASSERT(temp==MSG_DATA_COMMAND && "[Remote Input Manager]Not Command Data");
121    boost::ignore_unused_variable_warning(temp);
122
123    std::vector<CommandData> dataSample;
124
125    unsigned numCommandDatas;
126    vpr::Uint32 value;
127    vpr::Uint64 timeStamp;
128    CommandData temp_command_data;
129
130    reader->beginAttribute(gadget::tokens::SampleBufferLenAttrib);
131       unsigned numVectors = reader->readUint16();
132    reader->endAttribute();
133
134    mCommandSamples.lock();
135    for ( unsigned i = 0; i < numVectors; ++i )
136    {
137       reader->beginTag(gadget::tokens::BufferSampleTag);
138       reader->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
139          numCommandDatas = reader->readUint16();
140       reader->endAttribute();
141
142       dataSample.clear();
143       for ( unsigned j = 0; j < numCommandDatas; ++j )
144       {
145          reader->beginTag(gadget::tokens::DigitalValue);
146          reader->beginAttribute(gadget::tokens::TimeStamp);
147             timeStamp = reader->readUint64();    // read Time Stamp vpr::Uint64
148          reader->endAttribute();
149          value = reader->readUint32();           // read Command Data(int)
150          reader->endTag();
151
152          temp_command_data.setDigital(value);
153          temp_command_data.setTime(vpr::Interval(timeStamp + delta,vpr::Interval::Usec));
154          dataSample.push_back(temp_command_data);
155       }
156       mCommandSamples.addSample(dataSample);
157       reader->endTag();
158    }
159    mCommandSamples.unlock();
160    mCommandSamples.swapBuffers();
161
162    reader->endTag();
163 }
164
165 } // End of gadget namespace
166
Note: See TracBrowser for help on using the browser.