00001 /*!@file Devices/JoyStick.H Driver for a linux joystick */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003 // 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/Devices/JoyStick.H $ 00035 // $Id: JoyStick.H 13071 2010-03-28 16:24:18Z beobot $ 00036 // 00037 00038 #ifndef JOYSTICK_H_DEFINED 00039 #define JOYSTICK_H_DEFINED 00040 00041 #include "Component/ModelComponent.H" 00042 #include "Component/ModelParam.H" 00043 #include "Util/Types.H" 00044 00045 #include <pthread.h> 00046 #include <vector> 00047 00048 //! Simple listener for JoyStick events 00049 /*! The JoyStickListener gets called each time an event is received 00050 from the JoyStick. By default, there is no listener, and users can 00051 just asynchronously query the JoyStick for its current internal 00052 state. If those queries come in too slowly, however, they may miss 00053 some events (e.g., a briefly pressed button). So, production code 00054 should define a derived class for JoyStickListener and register it 00055 with the JoyStick object, so that it will take action as soon as an 00056 event is received. */ 00057 class JoyStickListener { 00058 public: 00059 //! Destructor 00060 virtual ~JoyStickListener(); 00061 00062 //! An axis changed 00063 /*! @param num axis number 00064 @param val new axis value */ 00065 virtual void axis(const uint num, const int16 val) = 0; 00066 00067 //! A button changed 00068 /*! @param num button number 00069 @param state new button state (true = pressed) */ 00070 virtual void button(const uint num, const bool state) = 0; 00071 }; 00072 00073 00074 #ifdef HAVE_LINUX_JOYSTICK_H 00075 00076 //! Driver for a Linux joystick 00077 /*! This is a simple driver for a Linux joystick. There are basically 00078 two modes of operation: you can either get the current JoyStick 00079 status at any time using getAxisValue() and getButtonState(), or you 00080 can register a JoyStickListener using setListener, and the listener 00081 will be called each time some change occurs in any of the axes or 00082 buttons. Beware that your listener will be called from a thread 00083 running in parallel with your main thread. */ 00084 class JoyStick : public ModelComponent 00085 { 00086 public: 00087 //! Constructor 00088 JoyStick(OptionManager& mgr, 00089 const std::string& descrName = "JoyStick", 00090 const std::string& tagName = "JoyStick", 00091 const char *dev = "/dev/input/js0"); 00092 00093 //! Add a listener 00094 void setListener(rutz::shared_ptr<JoyStickListener>& listener); 00095 00096 //! Destructor 00097 ~JoyStick(); 00098 00099 //! Get number of axes 00100 uint getNumAxes() const; 00101 00102 //! Get number of buttons 00103 uint getNumButtons() const; 00104 00105 //! Get value of a given axis 00106 int16 getAxisValue(const uint num) const; 00107 00108 //! Get state of a given button 00109 bool getButtonState(const uint num) const; 00110 00111 //! Should not call this directly -- running in a thread 00112 void run(); 00113 00114 bool joystickPlugged() 00115 { 00116 return itsPlugged; 00117 } 00118 00119 protected: 00120 NModelParam<std::string> itsDevName; //!< Name of our device 00121 00122 void start1(); //!< get started 00123 void stop1(); //!< get stopped 00124 00125 int itsFd; //!< file descriptor for our device 00126 bool itsPlugged; 00127 bool itsKeepGoing; //!< tell our thread to keep going 00128 std::vector<int16> itsAxes; //!< data from our axes 00129 std::vector<bool> itsButtons; //!< data from our buttons 00130 00131 private: 00132 rutz::shared_ptr<JoyStickListener> itsListener; 00133 pthread_mutex_t itsMutex; // mutex to protect access to the data 00134 pthread_t itsRunner; // thread to poll the joystick 00135 }; 00136 00137 #endif // HAVE_LINUX_JOYSTICK_H 00138 00139 // ###################################################################### 00140 /* So things look consistent in everyone's emacs... */ 00141 /* Local Variables: */ 00142 /* indent-tabs-mode: nil */ 00143 /* End: */ 00144 00145 #endif