test-BeoJoyStick.C

Go to the documentation of this file.
00001 /*!@file AppDevices/test-BeoJoyStick.C test Linux JoyStick interfacing */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2002   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the 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 iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppDevices/test-BeoJoyStick.C $
00035 // $Id: test-BeoJoyStick.C 8267 2007-04-18 18:24:24Z rjpeters $
00036 
00037 #include "Beowulf/Beowulf.H"
00038 #include "Beowulf/BeowulfOpts.H"
00039 #include "Component/ModelManager.H"
00040 #include "Devices/JoyStick.H"
00041 #include "Image/Image.H"
00042 
00043 //! A simple joystick listener
00044 class TestJoyStickListener : public JoyStickListener
00045 {
00046 public:
00047   TestJoyStickListener(const nub::soft_ref<Beowulf> b) :
00048     data(6, 1, ZEROS), beo(b)
00049   { }
00050 
00051   virtual ~TestJoyStickListener() { }
00052 
00053   virtual void axis(const uint num, const int16 val)
00054   {
00055     LINFO("Axis[%d] = %d", num, val);
00056 
00057     bool changed = false;
00058 
00059     switch(num)
00060       {
00061       case 0:
00062         // left-right
00063         data.setVal(1, val);
00064         changed = true;
00065         break;
00066 
00067       case 1:
00068         // front-back
00069         data.setVal(0, val);
00070         changed = true;
00071         break;
00072 
00073       case 2:
00074         // rotation of the handle
00075         break;
00076 
00077       case 3:
00078         // little accessory slider
00079         data.setVal(2, val);
00080         changed = true;
00081         break;
00082       }
00083 
00084     if (changed) sendData();
00085   }
00086 
00087   virtual void button(const uint num, const bool state)
00088   {
00089     LINFO("Button[%d] = %s", num, state?"pressed":"released");
00090 
00091     bool changed = false;
00092 
00093     switch(num)
00094       {
00095       case 5:
00096         // third largest button on the base
00097         data.setVal(3, state ? 1.0F : 0.0F);
00098         changed = true;
00099         break;
00100       case 6:
00101         // second largest button on base
00102         data.setVal(4, state ? 1.0F : 0.0F);
00103         changed = true;
00104         break;
00105 
00106       case 7:
00107         // largest button on base
00108         data.setVal(5, state ? 1.0F : 0.0F);
00109         changed = true;
00110         break;
00111       }
00112 
00113     if (changed) sendData();
00114   }
00115 
00116 virtual void sendData()
00117 {
00118   TCPmessage smsg(0, 0);
00119   smsg.addImage(data);
00120   beo->send(0, smsg);
00121 }
00122 
00123 Image<float> data;
00124 nub::soft_ref<Beowulf> beo;
00125 };
00126 
00127 //! Test JoyStick code
00128 /*! Test Joystick code. */
00129 int main(const int argc, const char **argv)
00130 {
00131 #ifndef HAVE_LINUX_JOYSTICK_H
00132 
00133   LFATAL("<linux/joystick.h> must be installed to use this program");
00134 
00135 #else
00136 
00137   // get a manager going:
00138   ModelManager manager("JoyStick Manager");
00139 
00140   // instantiate our model components:
00141   nub::soft_ref<JoyStick> js(new JoyStick(manager));
00142   manager.addSubComponent(js);
00143 
00144   nub::soft_ref<Beowulf> beow(new Beowulf(manager, "Beowulf Master",
00145                                           "BeowulfMaster", true));
00146   manager.addSubComponent(beow);
00147 
00148   manager.setOptionValString(&OPT_BeowulfSlaveNames, "192.168.0.215");
00149 
00150   // Parse command-line:
00151   if (manager.parseCommandLine(argc, argv, "[device]", 0, 1) == false)
00152     return(1);
00153 
00154   // let's configure our device:
00155   if (manager.numExtraArgs() > 0)
00156     js->setModelParamVal("JoyStickDevName",
00157                          manager.getExtraArg(0), MC_RECURSE);
00158 
00159   // get started:
00160   manager.start();
00161 
00162   // register a listener:
00163   rutz::shared_ptr<TestJoyStickListener> lis(new TestJoyStickListener(beow));
00164   rutz::shared_ptr<JoyStickListener> lis2; lis2.dynCastFrom(lis); // cast down
00165   js->setListener(lis2);
00166 
00167   // Everything is event-driven so in our main loop here we just sleep:
00168   while(1) sleep(100);
00169 
00170   // stop everything and exit:
00171   manager.stop();
00172 
00173   return 0;
00174 
00175 #endif // HAVE_LINUX_JOYSTICK_H
00176 
00177 }
00178 
00179 // ######################################################################
00180 /* So things look consistent in everyone's emacs... */
00181 /* Local Variables: */
00182 /* indent-tabs-mode: nil */
00183 /* End: */
Generated on Sun May 8 08:04:10 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3