TOBI Interface A  0.1
/home/breidi/Dropbox/libtia/src/tia/datapacket/raw_mem3.cpp
Go to the documentation of this file.
00001 /*
00002     This file is part of the TOBI Interface A (TiA) library.
00003 
00004     Commercial Usage
00005     Licensees holding valid Graz University of Technology Commercial
00006     licenses may use this file in accordance with the Graz University
00007     of Technology Commercial License Agreement provided with the
00008     Software or, alternatively, in accordance with the terms contained in
00009     a written agreement between you and Graz University of Technology.
00010 
00011     --------------------------------------------------
00012 
00013     GNU Lesser General Public License Usage
00014     Alternatively, this file may be used under the terms of the GNU Lesser
00015     General Public License version 3.0 as published by the Free Software
00016     Foundation and appearing in the file lgpl.txt included in the
00017     packaging of this file.  Please review the following information to
00018     ensure the GNU General Public License version 3.0 requirements will be
00019     met: http://www.gnu.org/copyleft/lgpl.html.
00020 
00021     In case of GNU Lesser General Public License Usage ,the TiA library
00022     is distributed in the hope that it will be useful,
00023     but WITHOUT ANY WARRANTY; without even the implied warranty of
00024     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025     GNU General Public License for more details.
00026 
00027     You should have received a copy of the GNU Lesser General Public License
00028     along with the TiA library. If not, see <http://www.gnu.org/licenses/>.
00029 
00030     Copyright 2010 Graz University of Technology
00031     Contact: TiA@tobi-project.org
00032 */
00033 
00038 #include <iostream>
00039 #include <vector>
00040 
00041 #include <boost/numeric/conversion/cast.hpp>
00042 #include "tia-private/datapacket/raw_mem3.h"
00043 
00044 namespace tia
00045 {
00046 
00047 using boost::numeric_cast;
00048 using boost::numeric::bad_numeric_cast;
00049 using boost::numeric::positive_overflow;
00050 using boost::numeric::negative_overflow;
00051 
00052 using boost::uint16_t;
00053 using boost::uint32_t;
00054 using boost::uint64_t;
00055 
00056 using std::vector;
00057 using std::cerr;
00058 
00059 //-----------------------------------------------------------------------------
00060 
00061 RawMem3::RawMem3(uint8_t packet_version, uint32_t flags, uint64_t packet_id, uint64_t packet_nr, \
00062                 uint64_t timestamp, \
00063                 vector<uint16_t>& nr_channels, vector<uint16_t>& samples_per_channel,
00064                 vector<double>& data)
00065                   : size_(0)
00066 {
00067   size_ = sizeof(packet_version) + sizeof(size_)
00068           + sizeof(flags) + sizeof(packet_id) + sizeof(packet_nr) + sizeof(timestamp) \
00069           + nr_channels.size() * sizeof(boost::uint16_t) \
00070           + samples_per_channel.size() * sizeof(boost::uint16_t) \
00071           + data.size() * sizeof(float);  // FIXXXXXME  ...  hardcoded sizeof() !!!!
00072 
00073   mem_ = malloc(size_);
00074 
00075   uint8_t* ui8_ptr = reinterpret_cast<uint8_t*>(mem_);
00076   *ui8_ptr++ = packet_version;
00077 
00078   uint32_t* ui32_ptr = reinterpret_cast<uint32_t*>(ui8_ptr);
00079   *ui32_ptr++ = size_;
00080   *ui32_ptr++ = flags;
00081 
00082   uint64_t* ui64_ptr = reinterpret_cast<uint64_t*>(ui32_ptr);
00083   *ui64_ptr++ = packet_id;
00084   *ui64_ptr++ = packet_nr;
00085 
00086   uint64_t* time_ptr
00087   = reinterpret_cast<uint64_t*>(ui64_ptr);
00088   *time_ptr++ = timestamp;
00089 
00090   uint16_t* ui16_ptr = reinterpret_cast<uint16_t*>(time_ptr);
00091 
00092   for(unsigned int n = 0; n < nr_channels.size(); n++)
00093     *ui16_ptr++ = nr_channels[n];
00094   for(unsigned int n = 0; n < samples_per_channel.size(); n++)
00095     *ui16_ptr++ = samples_per_channel[n];
00096 
00097   try
00098   {
00099     float* flt_ptr = reinterpret_cast<float*>(ui16_ptr);
00100     for(unsigned int n = 0; n < data.size(); n++)
00101       *flt_ptr++ = numeric_cast<float>(data[n]);
00102   }
00103   catch(negative_overflow& e)
00104   {
00105     cerr << "RawMem -- Constructor: " << e.what();
00106   }
00107   catch(positive_overflow& e)
00108   {
00109     cerr << "RawMem -- Constructor: " <<  e.what();
00110   }
00111   catch(bad_numeric_cast& e)
00112   {
00113     cerr << "RawMem -- Constructor: " <<  e.what();
00114   }
00115 }
00116 
00117 } // Namespace tobiss
00118 
00119 //-----------------------------------------------------------------------------
 All Data Structures Files Functions Variables Typedefs Enumerations