Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

GamepadController.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_GamepadController_h_
00003 #define INCLUDED_GamepadController_h_
00004 
00005 #include "Behaviors/BehaviorBase.h"
00006 #include "Events/EventBase.h"
00007 #include "Shared/Config.h"
00008 #include "Shared/Gamepad.h"
00009 #include "Wireless/Wireless.h"
00010 
00011 //! Listens to Gamepad events coming in from the gamepad port
00012 /*! The communication protocol is a simple, 5-byte group. The first byte
00013  *  indicates which button/joystick had a state change, and the next 4 bytes
00014  *  indicate the new state value for that input:
00015  *
00016  *  - <@c char: input indicator>
00017  *  - <@c float: value>
00018  *
00019  */
00020 class GamepadController : public BehaviorBase {
00021   public:
00022     //! Points to the one GamepadController object that the input stream is
00023     //! talking to.
00024     static GamepadController *theOne;
00025     static int input_callback(char *buf, int bytes); //!< Called when new data comes in over wireless
00026 
00027   protected:
00028     //! The input indicator values
00029     enum {
00030       X_BUTTON=1,
00031       Y_BUTTON,
00032       A_BUTTON,
00033       B_BUTTON,
00034       START_BUTTON,
00035       SELECT_BUTTON,
00036       LJOY_X,
00037       LJOY_Y,
00038       RJOY_X,
00039       RJOY_Y,
00040       DPAD_X,
00041       DPAD_Y,
00042       L_BUMPER,
00043       R_BUMPER
00044     };
00045 
00046   private:
00047     //! The last GamepadController that was theOne.
00048     GamepadController *theLastOne;
00049 
00050     //! The input stream socket
00051     Socket *inputsock;
00052     //! The actual socket
00053     int sock;
00054 
00055     //! Closes the server socket
00056     void shutdown();
00057 
00058     //! Processes a received packet
00059     void processInput(unsigned char* buf);
00060 
00061     void checkInputMapping(int thisVal, int enumVal);
00062 
00063     GamepadController(const GamepadController&); //!< don't call
00064     GamepadController operator=(const GamepadController&); //!< don't call
00065 
00066   public:
00067     GamepadController() :
00068       BehaviorBase("GamepadController"),
00069       theLastOne(theOne),
00070       inputsock(NULL), sock(-1)
00071     {
00072       //Make sure the assumptions of the class hold true
00073       //XXX: these should be static assertions
00074       checkInputMapping(X_BUTTON, GamepadSrcID::gamepadXButtonSrcID);
00075       checkInputMapping(Y_BUTTON, GamepadSrcID::gamepadYButtonSrcID);
00076       checkInputMapping(A_BUTTON, GamepadSrcID::gamepadAButtonSrcID);
00077       checkInputMapping(B_BUTTON, GamepadSrcID::gamepadBButtonSrcID);
00078       checkInputMapping(START_BUTTON, GamepadSrcID::gamepadStartButtonSrcID);
00079       checkInputMapping(SELECT_BUTTON, GamepadSrcID::gamepadSelectButtonSrcID);
00080       checkInputMapping(LJOY_X, GamepadSrcID::gamepadLeftJoyXSrcID);
00081       checkInputMapping(LJOY_Y, GamepadSrcID::gamepadLeftJoyYSrcID);
00082       checkInputMapping(RJOY_X, GamepadSrcID::gamepadRightJoyXSrcID);
00083       checkInputMapping(RJOY_Y, GamepadSrcID::gamepadRightJoyYSrcID);
00084       checkInputMapping(DPAD_X, GamepadSrcID::gamepadDPadXSrcID);
00085       checkInputMapping(DPAD_Y, GamepadSrcID::gamepadDPadYSrcID);
00086       checkInputMapping(L_BUMPER, GamepadSrcID::gamepadLeftBumperSrcID);
00087       checkInputMapping(R_BUMPER, GamepadSrcID::gamepadRightBumperSrcID);
00088     }
00089 
00090     virtual ~GamepadController() { theOne = theLastOne; }
00091 
00092     virtual void start();
00093 
00094     virtual void stop();
00095 
00096     virtual void doEvent() {
00097       if (event->getGeneratorID() == EventBase::runtimeEGID &&
00098           event->getTypeID() == EventBase::deactivateETID) {
00099         shutdown();
00100         BehaviorBase::stop();
00101       }
00102     }
00103 
00104     static std::string getClassDescription() {
00105       char tmp[20];
00106       sprintf(tmp,"%d",*config->main.gamepadControl_port);
00107       return std::string("Listens to gamepad inputs coming in from port ")+tmp;
00108     }
00109     virtual std::string getDescription() const { return getClassDescription(); }
00110 };
00111 
00112 /*! @file
00113  * @brief Describes GamepadController, listens to inputs and fires events appropriately
00114  * @author asf
00115  * @author med
00116  */
00117 
00118 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:41 2016 by Doxygen 1.6.3