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

C:/Users/Michael/Ftc/2011/code/trclib/joybtn.h

Go to the documentation of this file.
00001 #if 0
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #endif
00014 
00015 #ifndef _JOYBTN_H
00016 #define _JOYBTN_H
00017 
00018 #pragma systemFile
00019 
00020 #ifdef MOD_ID
00021     #undef MOD_ID
00022 #endif
00023 #define MOD_ID                  MOD_JOYBTN
00024 
00025 //
00026 // Constants.
00027 //
00028 #ifndef NUM_BTNS
00029   #define NUM_BTNS              12
00030 #endif
00031 #define JOYBTNF_USER_MASK       0x00ff
00032 #define JOYBTNF_ENABLE_EVENTS   0x0001
00033 
00034 //
00035 // Macros.
00036 //
00037 #define Btn(n)                  (1 << (n))
00038 
00039 #define Logitech_Btn1           Btn(0)
00040 #define Logitech_Btn2           Btn(1)
00041 #define Logitech_Btn3           Btn(2)
00042 #define Logitech_Btn4           Btn(3)
00043 #define Logitech_LB5            Btn(4)
00044 #define Logitech_RB6            Btn(5)
00045 #define Logitech_LB7            Btn(6)
00046 #define Logitech_RB8            Btn(7)
00047 #define Logitech_Btn9           Btn(8)
00048 #define Logitech_Btn10          Btn(9)
00049 #define Logitech_LStick         Btn(10)
00050 #define Logitech_RStick         Btn(11)
00051 
00052 #define Xbox_A                  Btn(0)
00053 #define Xbox_B                  Btn(1)
00054 #define Xbox_X                  Btn(2)
00055 #define Xbox_Y                  Btn(3)
00056 #define Xbox_LB                 Btn(4)
00057 #define Xbox_RB                 Btn(5)
00058 #define Xbox_Back               Btn(6)
00059 #define Xbox_Start              Btn(7)
00060 #define Xbox_LStick             Btn(8)
00061 #define Xbox_RStick             Btn(9)
00062 
00063 //
00064 // Type definitions.
00065 //
00066 typedef struct
00067 {
00068     int  joystickID;
00069     int  buttonFlags;
00070     int  prevButtons;
00071     int  buttonMask;
00072     bool fPressed;
00073 } JOYBTN;
00074 
00075 //
00076 // Import function prototypes.
00077 //
00078 void
00079 JoyBtnEvent(
00080     __in JOYBTN &joybtn
00081     );
00082 
00090 void
00091 JoyBtnInit(
00092     __out JOYBTN &joybtn,
00093     __in  int joystickID,
00094     __in  int buttonFlags
00095     )
00096 {
00097     TFuncName("JoyBtnInit");
00098     TLevel(INIT);
00099     TEnter();
00100 
00101     joybtn.joystickID = joystickID;
00102     joybtn.buttonFlags = buttonFlags & JOYBTNF_USER_MASK;
00103     joybtn.prevButtons = 0;
00104     joybtn.buttonMask = 0;
00105     joybtn.fPressed = false;
00106 
00107     TExit();
00108     return;
00109 }   //JoyBtnInit
00110 
00117 void
00118 JoyBtnTask(
00119     __inout JOYBTN &joybtn
00120     )
00121 {
00122     int currButtons = (joybtn.joystickID == 1)? joystick.joy1_Buttons:
00123                                                 joystick.joy2_Buttons;
00124 
00125     TFuncName("JoyBtnTask");
00126     TLevel(TASK);
00127     TEnterMsg(("Prev=%x,Curr=%x", joybtn.prevButtons, currButtons));
00128 
00129     if (joybtn.buttonFlags & JOYBTNF_ENABLE_EVENTS)
00130     {
00131         int changedButtons = joybtn.prevButtons^currButtons;
00132 
00133         while (changedButtons != 0)
00134         {
00135             //
00136             // maskButton contains the least significant set bit.
00137             //
00138             joybtn.buttonMask = changedButtons &
00139                                 ~(changedButtons^-changedButtons);
00140             if ((currButtons & joybtn.buttonMask) != 0)
00141             {
00142                 //
00143                 // Button is pressed.
00144                 //
00145                 joybtn.fPressed = true;
00146                 JoyBtnEvent(joybtn);
00147             }
00148             else
00149             {
00150                 //
00151                 // Button is released.
00152                 //
00153                 joybtn.fPressed = false;
00154                 JoyBtnEvent(joybtn);
00155             }
00156             changedButtons &= ~joybtn.buttonMask;
00157         }
00158     }
00159     joybtn.prevButtons = currButtons;
00160 
00161     TExit();
00162     return;
00163 }   //JoyBtnTask
00164 
00165 #endif  //ifndef _JOYBTN_H
 All Data Structures Files Functions Variables Defines