Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

JoystickDevice.cpp

Go to the documentation of this file.
00001 
00003 //
00004 //  Copyright (c) 2002, Perry L. Miller IV
00005 //  All rights reserved.
00006 //  BSD License: http://www.opensource.org/licenses/bsd-license.html
00007 //
00009 
00011 //
00012 //  Wrapper class for two instances of gadget::AnalogInterface.
00013 //
00015 
00016 #include "vrjGaPrecompiled.h"
00017 #include "JoystickDevice.h"
00018 
00019 using namespace vrjGA;
00020 
00021 
00023 //
00024 //  Constructor.
00025 //
00027 
00028 JoystickDevice::JoystickDevice ( const std::string &h, const std::string &v ) : 
00029   BaseClass(),
00030   _cbm(),
00031   _tm(),
00032   _hai(),
00033   _vai(),
00034   _h ( 0 ),
00035   _v ( 0 ),
00036   _hs ( JOYSTICK_AT_CENTER ),
00037   _vs ( JOYSTICK_AT_CENTER )
00038 {
00039   // Initialize.
00040   _hai.init ( h );
00041   _vai.init ( v );
00042 
00043   // Fill up the transition map.
00044   _tm[MessagePair(JOYSTICK_AT_CENTER,JOYSTICK_AT_RIGHT) ] = MessagePair(JOYSTICK_LEAVING_CENTER,JOYSTICK_ENTERING_RIGHT );
00045   _tm[MessagePair(JOYSTICK_AT_CENTER,JOYSTICK_AT_LEFT)  ] = MessagePair(JOYSTICK_LEAVING_CENTER,JOYSTICK_ENTERING_LEFT  );
00046   _tm[MessagePair(JOYSTICK_AT_CENTER,JOYSTICK_AT_UP)    ] = MessagePair(JOYSTICK_LEAVING_CENTER,JOYSTICK_ENTERING_UP    );
00047   _tm[MessagePair(JOYSTICK_AT_CENTER,JOYSTICK_AT_DOWN)  ] = MessagePair(JOYSTICK_LEAVING_CENTER,JOYSTICK_ENTERING_DOWN  );
00048 
00049   // The other way...
00050   _tm[MessagePair(JOYSTICK_AT_RIGHT, JOYSTICK_AT_CENTER)] = MessagePair(JOYSTICK_LEAVING_RIGHT, JOYSTICK_ENTERING_CENTER);
00051   _tm[MessagePair(JOYSTICK_AT_LEFT,  JOYSTICK_AT_CENTER)] = MessagePair(JOYSTICK_LEAVING_LEFT,  JOYSTICK_ENTERING_CENTER);
00052   _tm[MessagePair(JOYSTICK_AT_UP,    JOYSTICK_AT_CENTER)] = MessagePair(JOYSTICK_LEAVING_UP,    JOYSTICK_ENTERING_CENTER);
00053   _tm[MessagePair(JOYSTICK_AT_DOWN,  JOYSTICK_AT_CENTER)] = MessagePair(JOYSTICK_LEAVING_DOWN,  JOYSTICK_ENTERING_CENTER);
00054 
00055   // Skipping center (if update-rate is slow)...
00056   _tm[MessagePair(JOYSTICK_AT_LEFT,  JOYSTICK_AT_RIGHT) ] = MessagePair(JOYSTICK_LEAVING_LEFT,  JOYSTICK_ENTERING_RIGHT );
00057   _tm[MessagePair(JOYSTICK_AT_RIGHT, JOYSTICK_AT_LEFT)  ] = MessagePair(JOYSTICK_LEAVING_RIGHT, JOYSTICK_ENTERING_LEFT  );
00058   _tm[MessagePair(JOYSTICK_AT_UP,    JOYSTICK_AT_DOWN)  ] = MessagePair(JOYSTICK_LEAVING_UP,    JOYSTICK_ENTERING_DOWN  );
00059   _tm[MessagePair(JOYSTICK_AT_DOWN,  JOYSTICK_AT_UP)    ] = MessagePair(JOYSTICK_LEAVING_DOWN,  JOYSTICK_ENTERING_UP    );
00060 }
00061 
00062 
00064 //
00065 //  Destructor.
00066 //
00068 
00069 JoystickDevice::~JoystickDevice()
00070 {
00071 }
00072 
00073 
00075 //
00076 //  Update the internal state.
00077 //
00079 
00080 void JoystickDevice::update()
00081 {
00082   _h = _hai->getData();
00083   _v = _vai->getData();
00084 }
00085 
00086 
00088 //
00089 //  Notify the client of any state changes.
00090 //
00092 
00093 void JoystickDevice::notify()
00094 {
00095   // Grab current state.
00096   Message hs ( _hs ), vs ( _vs );
00097 
00098   // Update the state.
00099   this->_updateState();
00100 
00101   // Notify for both directions.
00102   this->_notify ( hs, _hs );
00103   this->_notify ( vs, _vs );
00104 }
00105 
00106 
00108 //
00109 //  Notify the client of any state changes.
00110 //
00112 
00113 void JoystickDevice::_notify ( Message last, Message current )
00114 {
00115   // Look for a transition pair.
00116   Transitions::iterator i = _tm.find ( MessagePair ( last, current ) );
00117 
00118   // If we found one...
00119   if ( _tm.end() != i )
00120   {
00121     // Get the pair.
00122     MessagePair &mp = i->second;
00123 
00124     // Send the transitions.
00125     this->_sendMessage ( mp.first );
00126     this->_sendMessage ( mp.second );
00127   }
00128 
00129   // Always send the current state.
00130   this->_sendMessage ( current );
00131 }
00132 
00133 
00135 //
00136 //  Send the message to the client.
00137 //
00139 
00140 void JoystickDevice::_sendMessage ( Message m )
00141 {
00142   Callback *cb = this->callback ( m );
00143   if ( cb )
00144   {
00145     (*cb) ( m, this );
00146   }
00147 }
00148 
00149 
00151 //
00152 //  Set the callback.
00153 //
00155 
00156 void JoystickDevice::callback ( Message m, Callback::Ptr c )
00157 {
00158   _cbm[m] = c;
00159 }
00160 
00161 
00163 //
00164 //  Get the callback.
00165 //
00167 
00168 Callback *JoystickDevice::callback ( Message m )
00169 {
00170   Callbacks::iterator i = _cbm.find ( m );
00171   return ( _cbm.end() == i ) ? 0x0 : i->second.get();
00172 }
00173 
00174 
00176 //
00177 //  Update the current state.
00178 //
00180 
00181 void JoystickDevice::_updateState()
00182 {
00183   // The gadget library always gives us [0,1].
00184   const float high ( 0.75f );
00185   const float low  ( 0.25f );
00186 
00187   // Horizontal.
00188   if ( _h > high )
00189     _hs = JOYSTICK_AT_RIGHT;
00190   else if ( _h < low )
00191     _hs = JOYSTICK_AT_LEFT;
00192   else
00193     _hs = JOYSTICK_AT_CENTER;
00194 
00195   // Vertical.
00196   if ( _v > high )
00197     _vs = JOYSTICK_AT_UP;
00198   else if ( _v < low )
00199     _vs = JOYSTICK_AT_DOWN;
00200   else
00201     _vs = JOYSTICK_AT_CENTER;
00202 }

Generated on Thu Aug 12 16:42:14 2004 for vrjGA by doxygen 1.3.5