TRC Library for FRC (2011) 2011
TRC Library Code Project

C:/Users/Michael/Frc/2011/code/trclib/TrcJoystick.h

Go to the documentation of this file.
00001 #if 0
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 #endif
00015 
00016 #ifndef _TRCJOYSTICK_H
00017 #define _TRCJOYSTICK_H
00018 
00019 #ifdef MOD_ID
00020     #undef MOD_ID
00021 #endif
00022 #define MOD_ID                  MOD_JOYSTICK
00023 #ifdef MOD_NAME
00024     #undef MOD_NAME
00025 #endif
00026 #define MOD_NAME                "TrcJoystick"
00027 
00028 //
00029 // Joystick input macros.
00030 //
00031 #ifndef DEADBAND_INPUT_THRESHOLD
00032   #define DEADBAND_INPUT_THRESHOLD  0.10
00033 #endif
00034 #define DEADBAND_INPUT(n)       DEADBAND(n, DEADBAND_INPUT_THRESHOLD)
00035 //
00036 // Macros.
00037 //
00038 #define Btn(n)                  (1 << (n))
00039 //
00040 // Logitech Joystick:
00041 // UsagePage=0x01, Usage=0x04
00042 //
00043 #define Logitech_Trigger        Btn(0)
00044 #define Logitech_Btn2           Btn(1)
00045 #define Logitech_Btn3           Btn(2)
00046 #define Logitech_Btn4           Btn(3)
00047 #define Logitech_Btn5           Btn(4)
00048 #define Logitech_Btn6           Btn(5)
00049 #define Logitech_Btn7           Btn(6)
00050 #define Logitech_Btn8           Btn(7)
00051 #define Logitech_Btn9           Btn(8)
00052 #define Logitech_Btn10          Btn(9)
00053 #define Logitech_Btn11          Btn(10)
00054 #define Logitech_Btn12          Btn(11)
00055 //
00056 // Microsoft SideWinder Joystick:
00057 // UsagePage=0x01, Usage=0x04
00058 //
00059 #define SideWinder_Trigger      Btn(0)
00060 #define SideWinder_Btn2         Btn(1)
00061 #define SideWinder_Btn3         Btn(2)
00062 #define SideWinder_Btn4         Btn(3)
00063 #define SideWinder_BtnA         Btn(4)
00064 #define SideWinder_BtnB         Btn(5)
00065 #define SideWinder_BtnC         Btn(6)
00066 #define SideWinder_BtnD         Btn(7)
00067 #define SideWinder_Btn9         Btn(8)
00068 //
00069 // Microsoft Xbox Game Controller:
00070 // UsagePage=0x01, Usage=0x05
00071 //
00072 #define Xbox_BtnA               Btn(0)
00073 #define Xbox_BtnB               Btn(1)
00074 #define Xbox_BtnX               Btn(2)
00075 #define Xbox_BtnY               Btn(3)
00076 #define Xbox_LB                 Btn(4)
00077 #define Xbox_RB                 Btn(5)
00078 #define Xbox_Back               Btn(6)
00079 #define Xbox_Start              Btn(7)
00080 
00087 class ButtonNotify
00088 {
00089 public:
00099     virtual
00100     void
00101     NotifyButton(
00102         __in UINT32 port,
00103         __in UINT16 btnMask,
00104         __in bool   fPressed
00105         ) = 0;
00106 };  //class ButtonNotify
00107 
00114 class TrcJoystick: public Joystick
00115 {
00116 private:
00117     UINT32          m_port;
00118     ButtonNotify   *m_notify;
00119     DriverStation  *m_ds;
00120     UINT16          m_prevBtn;
00121 
00122 public:
00130     TrcJoystick(
00131         __in UINT32        port,
00132         __in ButtonNotify *notify = NULL
00133         ): Joystick(port),
00134            m_port(port),
00135            m_notify(notify)
00136     {
00137         TLevel(INIT);
00138         TEnterMsg(("Joystick=%d,notify=%p", port, notify));
00139 
00140         m_ds = DriverStation::GetInstance();
00141         m_prevBtn = m_ds->GetStickButtons(port);
00142 
00143         TExit();
00144     }   //TrcJoystick
00145 
00149     ~TrcJoystick(
00150         void
00151         )
00152     {
00153         TLevel(INIT);
00154         TEnter();
00155         TExit();
00156     }   //~TrcJoystick
00157 
00165     float
00166     GetXWithDeadband(
00167         __in float        threshold = DEADBAND_INPUT_THRESHOLD,
00168         __in JoystickHand hand = kRightHand
00169         )
00170     {
00171         float value;
00172 
00173         TLevel(API);
00174         TEnterMsg(("threshold=%f,hand=%d", threshold, hand));
00175 
00176         value = DEADBAND(GetX(hand), threshold);
00177 
00178         TExitMsg(("=%f", value));
00179         return value;
00180     }   //GetXWithDeadband
00181 
00189     float
00190     GetYWithDeadband(
00191         __in float        threshold = DEADBAND_INPUT_THRESHOLD,
00192         __in JoystickHand hand = kRightHand
00193         )
00194     {
00195         float value;
00196 
00197         TLevel(API);
00198         TEnterMsg(("threshold=%f,hand=%d", threshold, hand));
00199 
00200         value = DEADBAND(GetY(hand), threshold);
00201 
00202         TExitMsg(("=%f", value));
00203         return value;
00204     }   //GetYWithDeadband
00205 
00211     float
00212     GetZWithDeadband(
00213         __in float threshold = DEADBAND_INPUT_THRESHOLD
00214         )
00215     {
00216         float value;
00217 
00218         TLevel(API);
00219         TEnterMsg(("threshold=%f", threshold));
00220 
00221         value = DEADBAND(GetZ(), threshold);
00222 
00223         TExitMsg(("=%f", value));
00224         return value;
00225     }   //GetZWithDeadband
00226 
00232     float
00233     GetTwistWithDeadband(
00234         __in float threshold = DEADBAND_INPUT_THRESHOLD
00235         )
00236     {
00237         float value;
00238 
00239         TLevel(API);
00240         TEnterMsg(("threshold=%f", threshold));
00241 
00242         value = DEADBAND(GetTwist(), threshold);
00243 
00244         TExitMsg(("=%f", value));
00245         return value;
00246     }   //GetTwistWithDeadband
00247 
00254     float
00255     GetMagnitudeWithDeadband(
00256         __in float threshold = DEADBAND_INPUT_THRESHOLD
00257         )
00258     {
00259         float value;
00260 
00261         TLevel(API);
00262         TEnterMsg(("threshold=%f", threshold));
00263 
00264         value = MAGNITUDE(GetXWithDeadband(threshold),
00265                           GetYWithDeadband(threshold));
00266 
00267         TExitMsg(("=%f", value));
00268         return value;
00269     }   //GetMagnitudeWithDeadband
00270 
00277     float
00278     GetRadiansWithDeadband(
00279         __in float threshold = DEADBAND_INPUT_THRESHOLD
00280         )
00281     {
00282         float value;
00283 
00284         TLevel(API);
00285         TEnterMsg(("threshold=%f", threshold));
00286 
00287         value = DIR_RADIANS(GetXWithDeadband(threshold),
00288                             -GetYWithDeadband(threshold));
00289 
00290         TExitMsg(("=%f", value));
00291         return value;
00292     }   //GetRadiansWithDeadband
00293 
00300     float
00301     GetDegreesWithDeadband(
00302         __in float threshold = DEADBAND_INPUT_THRESHOLD
00303         )
00304     {
00305         float value;
00306 
00307         TLevel(API);
00308         TEnterMsg(("threshold=%f", threshold));
00309 
00310         value = RADIANS_TO_DEGREES(GetRadiansWithDeadband(threshold));
00311 
00312         TExitMsg(("=%f", value));
00313         return value;
00314     }   //GetDegreesWithDeadband
00315 
00320     void
00321     ButtonTask(
00322         void
00323         )
00324     {
00325         TLevel(HIFREQ);
00326         TEnter();
00327 
00328         UINT16 currBtn = m_ds->GetStickButtons(m_port);
00329         if (m_notify != NULL)
00330         {
00331             UINT16 changedBtn = m_prevBtn^currBtn;
00332             UINT16 btnMask;
00333             while (changedBtn != 0)
00334             {
00335                 //
00336                 // maskButton contains the least significant set bit.
00337                 //
00338                 btnMask = changedBtn & ~(changedBtn^-changedBtn);
00339                 if ((currBtn & btnMask) != 0)
00340                 {
00341                     //
00342                     // Button is pressed.
00343                     //
00344                     m_notify->NotifyButton(m_port, btnMask, true);
00345                 }
00346                 else
00347                 {
00348                     //
00349                     // Button is released.
00350                     //
00351                     m_notify->NotifyButton(m_port, btnMask, false);
00352                 }
00353                 //
00354                 // Clear the least significant set bit.
00355                 //
00356                 changedBtn &= ~btnMask;
00357             }
00358         }
00359         m_prevBtn = currBtn;
00360 
00361         TExit();
00362     }   //ButtonTask
00363 };  //class TrcJoystick
00364 
00365 #endif  //ifndef _TRCJOYSTICK_H
 All Classes Files Functions Variables Defines