ftclib/joybtn.h
author Michael Tsang
Sun Oct 02 04:55:09 2011 -0700 (2011-10-02)
changeset 6 741c5850cf79
parent 2 60be331af8fe
child 21 2659449ebab0
permissions -rw-r--r--
Added comments
     1 #if 0
     2 /// Copyright (c) Titan Robotics Club. All rights reserved.
     3 ///
     4 /// <module name="joybtn.h" />
     5 ///
     6 /// <summary>
     7 ///     This module contains the library functions for the joystick buttons.
     8 /// </summary>
     9 ///
    10 /// <remarks>
    11 ///     Environment: RobotC for Lego Mindstorms NXT.
    12 /// </remarks>
    13 #endif
    14 
    15 #ifndef _JOYBTN_H
    16 #define _JOYBTN_H
    17 
    18 #pragma systemFile
    19 
    20 #ifdef MOD_ID
    21     #undef MOD_ID
    22 #endif
    23 #define MOD_ID                  MOD_JOYBTN
    24 
    25 //
    26 // Constants.
    27 //
    28 #ifndef NUM_BTNS
    29   #define NUM_BTNS              12
    30 #endif
    31 #define JOYBTNF_USER_MASK       0x00ff
    32 #define JOYBTNF_ENABLE_EVENTS   0x0001
    33 
    34 #define JOYEVENT_BUTTON         0
    35 #define JOYEVENT_TOPHAT         1
    36 
    37 //
    38 // Macros.
    39 //
    40 #define Btn(n)                  (1 << (n))
    41 
    42 #define Logitech_Btn1           Btn(0)
    43 #define Logitech_Btn2           Btn(1)
    44 #define Logitech_Btn3           Btn(2)
    45 #define Logitech_Btn4           Btn(3)
    46 #define Logitech_LB5            Btn(4)
    47 #define Logitech_RB6            Btn(5)
    48 #define Logitech_LB7            Btn(6)
    49 #define Logitech_RB8            Btn(7)
    50 #define Logitech_Btn9           Btn(8)
    51 #define Logitech_Btn10          Btn(9)
    52 #define Logitech_LStick         Btn(10)
    53 #define Logitech_RStick         Btn(11)
    54 
    55 #define Xbox_A                  Btn(0)
    56 #define Xbox_B                  Btn(1)
    57 #define Xbox_X                  Btn(2)
    58 #define Xbox_Y                  Btn(3)
    59 #define Xbox_LB                 Btn(4)
    60 #define Xbox_RB                 Btn(5)
    61 #define Xbox_Back               Btn(6)
    62 #define Xbox_Start              Btn(7)
    63 #define Xbox_LStick             Btn(8)
    64 #define Xbox_RStick             Btn(9)
    65 
    66 //
    67 // Type definitions.
    68 //
    69 typedef struct
    70 {
    71     int  joystickID;
    72     int  buttonFlags;
    73     int  prevButtons;
    74     int  prevTopHat;
    75 } JOYBTN;
    76 
    77 //
    78 // Import function prototypes.
    79 //
    80 void
    81 JoyBtnEvent(
    82     __in int  joystickID,
    83     __in int  eventType,
    84     __in int  eventID,
    85     __in bool fPressed
    86     );
    87 
    88 /**
    89  *  This function initializes the joystick button system.
    90  *
    91  *  @param joybtn Points to the JOYBTN structure to be initialized.
    92  *  @param joystickID Specifies the joystick ID.
    93  *  @param buttonFlags Specifies the button flags.
    94  */
    95 void
    96 JoyBtnInit(
    97     __out JOYBTN &joybtn,
    98     __in  int joystickID,
    99     __in  int buttonFlags
   100     )
   101 {
   102     TFuncName("JoyBtnInit");
   103     TLevel(INIT);
   104     TEnter();
   105 
   106     joybtn.joystickID = joystickID;
   107     joybtn.buttonFlags = buttonFlags & JOYBTNF_USER_MASK;
   108     joybtn.prevButtons = 0;
   109     joybtn.prevTopHat = -1;
   110 
   111     TExit();
   112     return;
   113 }   //JoyBtnInit
   114 
   115 /**
   116  *  This function processes the changed buttons and sends button event
   117  *  notifications.
   118  *
   119  *  @param joybtn Points to the JOYBTN structure.
   120  */
   121 void
   122 JoyBtnTask(
   123     __inout JOYBTN &joybtn
   124     )
   125 {
   126 #if (_TARGET == "Robot")
   127     int currButtons = (joybtn.joystickID == 1)? joystick.joy1_Buttons:
   128                                                 joystick.joy2_Buttons;
   129     int currTopHat = (joybtn.joystickID == 1)? joystick.joy1_TopHat:
   130                                                joystick.joy2_TopHat;
   131 #else
   132     int currButtons = joystick.joy1_Buttons;
   133     int currTopHat = joystick.joy1_TopHat;
   134 #endif
   135 
   136     TFuncName("JoyBtnTask");
   137     TLevel(TASK);
   138     TEnterMsg(("Prev=%x,Curr=%x", joybtn.prevButtons, currButtons));
   139 
   140     if (joybtn.buttonFlags & JOYBTNF_ENABLE_EVENTS)
   141     {
   142         int changedButtons = currButtons^joybtn.prevButtons;
   143         int buttonMask;
   144 
   145         while (changedButtons != 0)
   146         {
   147             //
   148             // maskButton contains the least significant set bit.
   149             //
   150             buttonMask = changedButtons & ~(changedButtons^-changedButtons);
   151             if ((currButtons & buttonMask) != 0)
   152             {
   153                 //
   154                 // Button is pressed.
   155                 //
   156                 JoyBtnEvent(joybtn.joystickID,
   157                             JOYEVENT_BUTTON,
   158                             buttonMask,
   159                             true);
   160             }
   161             else
   162             {
   163                 //
   164                 // Button is released.
   165                 //
   166                 JoyBtnEvent(joybtn.joystickID,
   167                             JOYEVENT_BUTTON,
   168                             buttonMask,
   169                             false);
   170             }
   171             changedButtons &= ~buttonMask;
   172         }
   173 
   174         if (currTopHat != joybtn.prevTopHat)
   175         {
   176             JoyBtnEvent(joybtn.joystickID,
   177                         JOYEVENT_TOPHAT,
   178                         currTopHat,
   179                         currTopHat != -1);
   180         }
   181     }
   182     joybtn.prevButtons = currButtons;
   183     joybtn.prevTopHat = currTopHat;
   184 
   185     TExit();
   186     return;
   187 }   //JoyBtnTask
   188 
   189 #endif  //ifndef _JOYBTN_H