test-JoyStick.C

Go to the documentation of this file.
00001 /*!@file AppDevices/test-JoyStick.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-JoyStick.C $
00035 // $Id: test-JoyStick.C 7880 2007-02-09 02:34:07Z itti $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Devices/JoyStick.H"
00040 #include "Util/Types.H"
00041 #include "Util/log.H"
00042 #include <unistd.h>
00043 
00044 #ifdef HAVE_LINUX_JOYSTICK_H
00045 
00046 //! A simple joystick listener
00047 class TestJoyStickListener : public JoyStickListener
00048 {
00049 public:
00050   virtual ~TestJoyStickListener() { }
00051 
00052   virtual void axis(const uint num, const int16 val)
00053   { LINFO("Axis[%d] = %d", num, val); }
00054 
00055   virtual void button(const uint num, const bool state)
00056   { LINFO("Button[%d] = %s", num, state?"true":"false"); }
00057 };
00058 
00059 #endif
00060 
00061 //! Test JoyStick code
00062 /*! Test Joystick code. */
00063 int main(const int argc, const char **argv)
00064 {
00065 #ifndef HAVE_LINUX_JOYSTICK_H
00066 
00067   LFATAL("<linux/joystick.h> must be installed to use this program");
00068 
00069 #else
00070 
00071   // get a manager going:
00072   ModelManager manager("JoyStick Manager");
00073 
00074   // instantiate our model components:
00075   nub::soft_ref<JoyStick> js(new JoyStick(manager) );
00076   manager.addSubComponent(js);
00077 
00078   // Parse command-line:
00079   if (manager.parseCommandLine(argc, argv, "[device]", 0, 1) == false)
00080     return(1);
00081 
00082   // let's configure our device:
00083   if (manager.numExtraArgs() > 0)
00084     js->setModelParamVal("JoyStickDevName",
00085                          manager.getExtraArg(0), MC_RECURSE);
00086 
00087   // register a listener:
00088   rutz::shared_ptr<TestJoyStickListener> lis(new TestJoyStickListener);
00089   rutz::shared_ptr<JoyStickListener> lis2; lis2.dynCastFrom(lis); // cast down
00090   js->setListener(lis2);
00091 
00092   // get started:
00093   manager.start();
00094 
00095   // Everything is event-driven so in our main loop here we just sleep:
00096   while(1) sleep(100);
00097 
00098   /*
00099   // Alternatively to the event-driven approach used above where a
00100   // JoyStickListener is activated each time an event is received from
00101   // the joystick, below is an example of how you would do a polling
00102   // driver. It is not recommended as you may miss some events if you
00103   // don't poll fast enough:
00104 
00105   // get our joystick's vitals:
00106   uint nax = js->getNumAxes();
00107   uint nbu = js->getNumButtons();
00108 
00109   char msg[2000], tmp[20];
00110 
00111   // main loop: display the values at regular time intervals
00112   for(;;)
00113     {
00114       // get the axes:
00115       sprintf(msg, "Axes:   ");
00116       for (uint i = 0; i < nax; i ++)
00117         {
00118           int16 val = js->getAxisValue(i);
00119           sprintf(tmp, " %-05d", val); strcat(msg, tmp);
00120         }
00121       LINFO(msg);
00122 
00123       // get the buttons:
00124       sprintf(msg, "Buttons:");
00125       for (uint i = 0; i < nbu; i ++)
00126         {
00127           bool val = js->getButtonState(i);
00128           if (val) strcat(msg, " 1"); else strcat(msg, " 0");
00129         }
00130       LINFO(msg);
00131 
00132       // sleep a bit:
00133       usleep(100000);
00134     }
00135   */
00136 
00137   // stop everything and exit:
00138   manager.stop();
00139   return 0;
00140 
00141 #endif // HAVE_LINUX_JOYSTICK_H
00142 
00143 }
00144 
00145 // ######################################################################
00146 /* So things look consistent in everyone's emacs... */
00147 /* Local Variables: */
00148 /* indent-tabs-mode: nil */
00149 /* End: */
Generated on Sun May 8 08:04:10 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3