TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/src/hardware/mouse.cpp
00001 /*
00002     This file is part of the TOBI SignalServer.
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 General Public License Usage
00014     Alternatively, this file may be used under the terms of the GNU
00015     General Public License version 3.0 as published by the Free Software
00016     Foundation and appearing in the file gpl.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/gpl.html.
00020 
00021     In case of GNU General Public License Usage ,the TOBI SignalServer
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 General Public License
00028     along with the TOBI SignalServer. If not, see <http://www.gnu.org/licenses/>.
00029 
00030     Copyright 2010 Graz University of Technology
00031     Contact: SignalServer@tobi-project.org
00032 */
00033 
00034 #include "hardware/mouse.h"
00035 
00036 namespace tobiss
00037 {
00038 using boost::lexical_cast;
00039 using boost::bad_lexical_cast;
00040 
00041 using std::vector;
00042 using std::string;
00043 using std::pair;
00044 using std::cout;
00045 using std::endl;
00046 using std::set;
00047 
00048 set<boost::uint16_t> MouseBase::used_ids_;
00049 
00050 const string MouseBase::str_hw_vid_("vendorid");
00051 const string MouseBase::str_hw_pid_("productid");
00052 const string MouseBase::str_usb_port_("usb_port");
00053 
00054 //-----------------------------------------------------------------------------
00055 MouseBase::MouseBase(ticpp::Iterator<ticpp::Element> hw)
00056         : HWThread()
00057 {
00058   #ifdef DEBUG
00059     cout << "MouseBase: Constructor" << endl;
00060   #endif
00061 
00062   checkMandatoryHardwareTags(hw);
00063   if(mode_ != APERIODIC)
00064     throw(std::invalid_argument("Mouse has to be started as aperiodic device!"));
00065   initMouse();
00066 
00067   ticpp::Iterator<ticpp::Element> ds(hw->FirstChildElement(hw_devset_, true));
00068   setDeviceSettings(ds);
00069 //  DS = ds;
00070   data_.init(1, channel_types_.size() , channel_types_);
00071 
00072   vector<boost::uint32_t> v;
00073   empty_block_.init(0,0, v);
00074 
00075   // cout << " * Mouse sucessfully initialized -- running as aperiodic: ";
00076   // cout << (mode_ == APERIODIC) << ";  ";
00077 }
00078 
00079 //-----------------------------------------------------------------------------
00080 
00081 MouseBase::~MouseBase()
00082 {
00083 }
00084 
00085 //---------------------------------------------------------------------------------------
00086 
00087 void MouseBase::setVendorId(ticpp::Iterator<ticpp::Element>const &elem)
00088 {
00089   #ifdef DEBUG
00090     cout << "HWThread: setVendorId" << endl;
00091   #endif
00092 
00093   try
00094   {
00095     vid_ = giveDecimalRepresentation(elem->GetText(true));
00096   }
00097   catch(bad_lexical_cast &)
00098   {
00099     string ex_str(type_ + " -- VendorId: value is not a decimal or hxadecimal number!");
00100     throw(std::invalid_argument(ex_str));
00101   }
00102   if(blocks_ == 0)
00103   {
00104     string ex_str(type_ + " -- VendorId: value is 0!");
00105     throw(std::invalid_argument(ex_str));
00106   }
00107 }
00108 //-----------------------------------------------------------------------------
00109 
00110 void MouseBase::setProductId(ticpp::Iterator<ticpp::Element>const &elem)
00111 {
00112   #ifdef DEBUG
00113     cout << "HWThread: setProductId" << endl;
00114   #endif
00115 
00116   try
00117   {
00118     pid_ = giveDecimalRepresentation( elem->GetText(true) );
00119   }
00120   catch(bad_lexical_cast &)
00121   {
00122     string ex_str(type_ + " -- ProductId: value is not a decimal or hxadecimal number!");
00123     throw(std::invalid_argument(ex_str));
00124   }
00125   if(blocks_ == 0)
00126   {
00127     string ex_str(type_ + " -- ProductId: value is 0!");
00128     throw(std::invalid_argument(ex_str));
00129   }
00130 }
00131 
00132 //-----------------------------------------------------------------------------
00133 
00134 void MouseBase::setUsbPort(ticpp::Iterator<ticpp::Element>const &elem)
00135 {
00136   #ifdef DEBUG
00137     cout << "HWThread: setUsbPort" << endl;
00138   #endif
00139 
00140   try
00141   {
00142     usb_port_ = giveDecimalRepresentation( elem->GetText(true) );
00143   }
00144   catch(bad_lexical_cast &)
00145   {
00146     string ex_str(type_ + " -- Usb port: value is not a decimal or hxadecimal number!");
00147     throw(std::invalid_argument(ex_str));
00148   }
00149   if(blocks_ == 0)
00150   {
00151     string ex_str(type_ + " -- Usb port: value is 0!");
00152     throw(std::invalid_argument(ex_str));
00153   }
00154 }
00155 
00156 //-----------------------------------------------------------------------------
00157 
00158 void MouseBase::setDeviceSettings(ticpp::Iterator<ticpp::Element>const& father)
00159 {
00160   #ifdef DEBUG
00161     cout << "MouseBase: setDeviceSettings" << endl;
00162   #endif
00163 
00164   ticpp::Iterator<ticpp::Element> elem(father->FirstChildElement(str_hw_vid_,true));
00165   setVendorId(elem);
00166 
00167   ticpp::Iterator<ticpp::Element> elem2(father->FirstChildElement(str_hw_pid_,true));
00168   setProductId(elem2);
00169 
00170   ticpp::Iterator<ticpp::Element> elem3(father->FirstChildElement(str_usb_port_,true));
00171   setUsbPort(elem3);
00172 
00173   string naming;
00174   string type;
00175 
00176   if(buttons_)
00177     channel_types_.push_back(SIG_MBUTTON);
00178   for(boost::uint32_t n = 0; n < buttons_; n++)
00179     channel_types_.push_back(SIG_MBUTTON);
00180 
00181   if(axes_)
00182     channel_types_.push_back(SIG_MOUSE);
00183   for(boost::uint32_t n = 0; n < axes_; n++)
00184    channel_types_.push_back(SIG_MOUSE);
00185   nr_ch_= channel_types_.size();
00186 
00187   boost::uint16_t n = 1;
00188   if(buttons_)
00189     for( ; n <= buttons_ +1; n++)
00190       channel_info_.insert(pair<boost::uint16_t, pair<string, boost::uint32_t> >(n, pair<string, boost::uint32_t>(naming, SIG_MBUTTON)));
00191 
00192   if(axes_)
00193     for( ; n <= axes_ + buttons_ +2; n++)
00194       channel_info_.insert(pair<boost::uint16_t, pair<string, boost::uint32_t> >(n, pair<string, boost::uint32_t>(naming, SIG_MOUSE)));
00195 
00196 
00197   homogenous_signal_type_ = 0;
00198 
00199 }
00200 
00201 //---------------------------------------------------------------------------------------
00202 
00203 void MouseBase::setChannelSettings(ticpp::Iterator<ticpp::Element>const& )
00204 {
00205   #ifdef DEBUG
00206     std::cout << "MouseBase: setChannelSettings" << std::endl;
00207   #endif
00208 
00209 }
00210 
00211 //---------------------------------------------------------------------------------------
00212 
00213 SampleBlock<double> MouseBase::getAsyncData()
00214 {
00215   #ifdef DEBUG
00216   cout << "MouseBase: getAsyncData" << endl;
00217   #endif
00218 
00219   if(!running_)
00220     return(empty_block_);
00221 
00222   bool dirty = 0;
00223 
00224   for(unsigned int n = 0; n < buttons_values_.size(); n++)
00225   {
00226     bool value = 0;
00227     int state_n = (async_data_buttons_ & static_cast<int>( 1 << n ));
00228 
00229     if (state_n != value)
00230       value = 1;
00231 
00232     if( value != buttons_values_[n])
00233     {
00234       dirty = 1;
00235       buttons_values_[n] = value;
00236     }
00237   }
00238 
00239   if(async_data_x_!=0 || async_data_y_!=0)
00240   {
00241     dirty = 1;
00242     axes_values_[0]+=async_data_x_;
00243     axes_values_[1]+=async_data_y_;
00244 
00245   }
00246 
00247   if(!dirty)
00248     return(empty_block_);
00249 
00250   vector<double> v;
00251 
00252   if(buttons_)
00253     v.push_back(id_);
00254   for(boost::uint8_t n = 0; n < buttons_values_.size(); n++)
00255     v.push_back(buttons_values_[n]);
00256 
00257   if(axes_)
00258     v.push_back(id_);
00259   for(boost::uint8_t n = 0; n < axes_values_.size(); n++)
00260     v.push_back(axes_values_[n]);
00261 
00262   data_.setSamples(v);
00263 
00264   return(data_);
00265 }
00266 
00267 //-----------------------------------------------------------------------------
00268 
00269 void MouseBase::run()
00270 {
00271   if(mode_ == APERIODIC)
00272   {
00273     async_acqu_thread_ = new boost::thread( boost::bind(&MouseBase::acquireData, this) );
00274   }
00275   running_ = true;
00276 }
00277 
00278 //-----------------------------------------------------------------------------
00279 
00280 void MouseBase::stop() {
00281   running_ =false;
00282 }
00283 
00284 //-----------------------------------------------------------------------------
00285 
00286 void MouseBase::initMouse()
00287 {
00288   set<boost::uint16_t>::iterator it(used_ids_.begin());
00289 
00290   axes_    = 2;
00291   buttons_ = 3;
00292 
00293   axes_values_.resize(axes_,0);
00294   buttons_values_.resize(buttons_,0);
00295 
00296   axes_values_[0] = 0;
00297   axes_values_[1] = 0;
00298 
00299   async_data_x_ = 0;
00300   async_data_y_ = 0;
00301   async_data_buttons_ = 0;
00302 }
00303 
00304 //-----------------------------------------------------------------------------
00305 
00306 
00307 int MouseBase::giveDecimalRepresentation(std::string str)
00308 {
00309   if( !str.compare(0, 2, "0x") )
00310   {
00311     int val = 0;
00312     if(!fromString(val, str.substr( 2, std::string::npos ),  std::hex))
00313       throw boost::bad_lexical_cast();
00314 
00315     return(val);
00316   }
00317   else
00318      return( lexical_cast<int>(str) );
00319 }
00320 
00321 //-----------------------------------------------------------------------------
00322 
00323 
00324 
00325 
00326 } // Namespace tobiss
 All Data Structures Files Functions Variables