PixelLightAPI  .
WiiMote.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: WiiMote.h                                      *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLINPUT_WIIMOTE_H__
00024 #define __PLINPUT_WIIMOTE_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include <PLMath/Vector2.h>
00032 #include <PLMath/Vector3.h>
00033 #include <PLCore/Base/Event/EventHandler.h>
00034 #include "PLInput/Input/Controls/Button.h"
00035 #include "PLInput/Input/Controls/Axis.h"
00036 #include "PLInput/Input/Controls/LED.h"
00037 #include "PLInput/Input/Controls/Effect.h"
00038 #include "PLInput/Input/Devices/Device.h"
00039 
00040 
00041 //[-------------------------------------------------------]
00042 //[ Namespace                                             ]
00043 //[-------------------------------------------------------]
00044 namespace PLInput {
00045 
00046 
00047 //[-------------------------------------------------------]
00048 //[ Forward declarations                                  ]
00049 //[-------------------------------------------------------]
00050 class ConnectionDevice;
00051 
00052 
00053 //[-------------------------------------------------------]
00054 //[ Classes                                               ]
00055 //[-------------------------------------------------------]
00056 /**
00057 *  @brief
00058 *    WiiMote input device
00059 *
00060 *  @remarks
00061 *    This class supports the following device backend types:
00062 *    - ConnectionDevice
00063 */
00064 class WiiMote : public Device {
00065 
00066 
00067     //[-------------------------------------------------------]
00068     //[ Class definition                                      ]
00069     //[-------------------------------------------------------]
00070     pl_class(PLINPUT_RTTI_EXPORT, WiiMote, "PLInput", PLInput::Device, "WiiMote input controller")
00071     pl_class_end
00072 
00073 
00074     //[-------------------------------------------------------]
00075     //[ Controller definition                                 ]
00076     //[-------------------------------------------------------]
00077     public:
00078         // LEDs and effects
00079         LED     LEDs;               /**< LEDs */
00080         Effect  Rumble1;            /**< Rumble effect (motor #1) */
00081 
00082         // Main buttons
00083         Button  Button1;            /**< Button '1' */
00084         Button  Button2;            /**< Button '2' */
00085         Button  ButtonA;            /**< Button 'A' */
00086         Button  ButtonB;            /**< Button 'B' */
00087         Button  ButtonMinus;        /**< Button 'Minus' */
00088         Button  ButtonPlus;         /**< Button 'Plus' */
00089         Button  ButtonHome;         /**< Button 'Home' */
00090         Button  ButtonLeft;         /**< Button 'Left' */
00091         Button  ButtonRight;        /**< Button 'Right' */
00092         Button  ButtonUp;           /**< Button 'Up' */
00093         Button  ButtonDown;         /**< Button 'Down' */
00094 
00095         // Main values
00096         Axis    AccX;               /**< Acceleration (X) */
00097         Axis    AccY;               /**< Acceleration (Y) */
00098         Axis    AccZ;               /**< Acceleration (Z) */
00099         Axis    OrientX;            /**< Orientation (X) */
00100         Axis    OrientY;            /**< Orientation (Y) */
00101         Axis    OrientZ;            /**< Orientation (Z) */
00102         Axis    Roll;               /**< Rotation (roll) */
00103         Axis    Pitch;              /**< Rotation (pitch) */
00104         Axis    PointerX;           /**< Pointer (X) */
00105         Axis    PointerY;           /**< Pointer (Y) */
00106 
00107         // Nunchuk
00108         Button  NunchukButtonC;     /**< Nunchuk Button 'C' */
00109         Button  NunchukButtonZ;     /**< Nunchuk Button 'Z' */
00110         Axis    NunchukAccX;        /**< Nunchuk Acceleration (X) */
00111         Axis    NunchukAccY;        /**< Nunchuk Acceleration (Y) */
00112         Axis    NunchukAccZ;        /**< Nunchuk Acceleration (Z) */
00113         Axis    NunchukOrientX;     /**< Nunchuk Orientation (X) */
00114         Axis    NunchukOrientY;     /**< Nunchuk Orientation (Y) */
00115         Axis    NunchukOrientZ;     /**< Nunchuk Orientation (Z) */
00116         Axis    NunchukRoll;        /**< Nunchuk Rotation (roll) */
00117         Axis    NunchukPitch;       /**< Nunchuk Rotation (pitch) */
00118         Axis    NunchukX;           /**< Nunchuk joystick (X) */
00119         Axis    NunchukY;           /**< Nunchuk joystick (Y) */
00120 
00121 
00122     //[-------------------------------------------------------]
00123     //[ Public data types                                     ]
00124     //[-------------------------------------------------------]
00125     public:
00126         // Definitions
00127         enum EProductID {
00128             // HID definitions
00129             VendorID  = 0x057e,     /**< 'Nintendo' */
00130             ProductID = 0x0306,     /**< 'Wiimote' */
00131 
00132             // Bluetooth definitions
00133             DeviceClass0 = 0x04,
00134             DeviceClass1 = 0x25,
00135             DeviceClass2 = 0x00
00136         };
00137 
00138         /**
00139         *  @brief
00140         *    Report type
00141         */
00142         enum EReport {
00143             ReportStatus            = 0x20,     /**< Status report */
00144             ReportReadMemory        = 0x21,     /**< Data from memory */
00145             ReportButtons           = 0x30,     /**< Buttons */
00146             ReportButtonsAccel      = 0x31,     /**< Buttons and acceleration */
00147             ReportButtonsAccelIR    = 0x33,     /**< Buttons, acceleration and IR (extended) */
00148             ReportButtonsAccelExt   = 0x35,     /**< Buttons, acceleration and extension */
00149             ReportButtonsAccelIRExt = 0x37      /**< Buttons, acceleration, IR (basic) and extension */
00150         };
00151 
00152         /**
00153         *  @brief
00154         *    Infrared sensor mode
00155         */
00156         enum EIRMode {
00157             IROff                   = 0x00,     /**< IR off */
00158             IRBasic                 = 0x01,     /**< Basic IR mode */
00159             IRExtended              = 0x03,     /**< Extended IR mode */
00160             IRFull                  = 0x05      /**< Full IR mode */
00161         };
00162 
00163         /**
00164         *  @brief
00165         *    WiiMote extension
00166         */
00167         enum EExtension {
00168             ExtNone                 = 0x0000,   /**< No extension */
00169             ExtNunchuk              = 0xfefe,   /**< Nunchuk */
00170             ExtClassic              = 0xfdfd,   /**< Classic controller */
00171             ExtPartiallyInserted    = 0xffff    /**< Extension not inserted correctly */
00172         };
00173 
00174 
00175     //[-------------------------------------------------------]
00176     //[ Public functions                                      ]
00177     //[-------------------------------------------------------]
00178     public:
00179         /**
00180         *  @brief
00181         *    Constructor
00182         *
00183         *  @param[in] sName
00184         *    Device name
00185         *  @param[in] pImpl
00186         *    System specific device implementation, must be valid!
00187         */
00188         PLINPUT_API WiiMote(const PLCore::String &sName, DeviceImpl *pImpl);
00189 
00190         /**
00191         *  @brief
00192         *    Destructor
00193         */
00194         PLINPUT_API virtual ~WiiMote();
00195 
00196         /**
00197         *  @brief
00198         *    Get report mode
00199         *
00200         *  @return
00201         *    Report mode
00202         */
00203         PLINPUT_API EReport GetReportMode() const;
00204 
00205         /**
00206         *  @brief
00207         *    Set report mode
00208         *
00209         *  @param[in] nReportMode
00210         *    Report mode
00211         *  @param[in] bContinuous
00212         *    Continuous updates?
00213         */
00214         PLINPUT_API void SetReportMode(EReport nReportMode, bool bContinuous = false);
00215 
00216         /**
00217         *  @brief
00218         *    Get infrared mode
00219         *
00220         *  @return
00221         *    Infrared mode
00222         */
00223         PLINPUT_API EIRMode GetIRMode() const;
00224 
00225         /**
00226         *  @brief
00227         *    Set infrared mode
00228         *
00229         *  @param[in] nIRMode
00230         *    Infrared mode
00231         */
00232         PLINPUT_API void SetIRMode(EIRMode nIRMode);
00233 
00234         /**
00235         *  @brief
00236         *    Get extension
00237         *
00238         *  @return
00239         *    Extension type
00240         */
00241         PLINPUT_API EExtension GetExtension() const;
00242 
00243         /**
00244         *  @brief
00245         *    Get battery state
00246         *
00247         *  @return
00248         *    Battery state
00249         */
00250         PLINPUT_API PLCore::uint8 GetBattery() const;
00251 
00252         /**
00253         *  @brief
00254         *    Calibrate device
00255         */
00256         PLINPUT_API void Calibrate();
00257 
00258 
00259     //[-------------------------------------------------------]
00260     //[ Public virtual Controller functions                   ]
00261     //[-------------------------------------------------------]
00262     public:
00263         PLINPUT_API virtual void UpdateOutputControl(Control *pControl) override;
00264 
00265 
00266     //[-------------------------------------------------------]
00267     //[ Private data types                                    ]
00268     //[-------------------------------------------------------]
00269     private:
00270         /**
00271         *  @brief
00272         *    Acceleration data
00273         */
00274         struct SAcceleration {
00275             // Status
00276             int nUpdateNearG;   /**< Update counter when acceleration is near 1G */
00277 
00278             // Status
00279             float fAccX;        /**< Acceleration: X */
00280             float fAccY;        /**< Acceleration: Y */
00281             float fAccZ;        /**< Acceleration: Z */
00282             float fOrientX;     /**< Orientation:  X */
00283             float fOrientY;     /**< Orientation:  Y */
00284             float fOrientZ;     /**< Orientation:  Z */
00285             float fRoll;        /**< Angle:        Roll */
00286             float fPitch;       /**< Angle:        Pitch */
00287 
00288             // Calibration
00289             PLCore::uint8 nX0;  /**< Calibration: X0 */
00290             PLCore::uint8 nY0;  /**< Calibration: Y0 */
00291             PLCore::uint8 nZ0;  /**< Calibration: Z0 */
00292             PLCore::uint8 nXG;  /**< Calibration: XG */
00293             PLCore::uint8 nYG;  /**< Calibration: YG */
00294             PLCore::uint8 nZG;  /**< Calibration: ZG */
00295 
00296             // Calculate orientation from acceleration data
00297             void CalculateOrientation();
00298         };
00299 
00300         /**
00301         *  @brief
00302         *    Nunchuk joystick data
00303         */
00304         struct SJoystick {
00305             // Status
00306             float fX;               /**< X position */
00307             float fY;               /**< Y position */
00308 
00309             // Calibration
00310             PLCore::uint8 nMinX;    /**< Calibration: Minimum X */
00311             PLCore::uint8 nMidX;    /**< Calibration: Middle  X */
00312             PLCore::uint8 nMaxX;    /**< Calibration: Maximum X */
00313             PLCore::uint8 nMinY;    /**< Calibration: Minimum Y */
00314             PLCore::uint8 nMidY;    /**< Calibration: Middle  Y */
00315             PLCore::uint8 nMaxY;    /**< Calibration: Maximum Y */
00316         };
00317 
00318         /**
00319         *  @brief
00320         *    IR sensor dot
00321         */
00322         struct SDot {
00323             bool  bFound;   /**< The dot has been found */
00324             int   nRawX;    /**< Raw X position */
00325             int   nRawY;    /**< Raw Y position */
00326             float fX;       /**< X position (0..1) */
00327             float fY;       /**< Y position (0..1) */
00328             int   nSize;    /**< Dot size */
00329         };
00330 
00331 
00332     //[-------------------------------------------------------]
00333     //[ Private functions                                     ]
00334     //[-------------------------------------------------------]
00335     private:
00336         /**
00337         *  @brief
00338         *    Called when the HID device has been connected
00339         */
00340         void OnDeviceConnect();
00341 
00342         /**
00343         *  @brief
00344         *    Called when the HID device has been disconnected
00345         */
00346         void OnDeviceDisconnect();
00347 
00348         /**
00349         *  @brief
00350         *    Called when the HID device has read some data
00351         */
00352         void OnDeviceRead();
00353 
00354         /**
00355         *  @brief
00356         *    Data from the WiiMote has been received
00357         */
00358         void OnReadData();
00359 
00360         /**
00361         *  @brief
00362         *    Data from memory received
00363         */
00364         void OnReadMemory();
00365 
00366         /**
00367         *  @brief
00368         *    Calibration data received
00369         */
00370         void OnReadCalibration();
00371 
00372         /**
00373         *  @brief
00374         *    Extension information received
00375         */
00376         void OnReadExtensionType();
00377 
00378         /**
00379         *  @brief
00380         *    Nunchuk calibration data received
00381         */
00382         void OnReadNunchukCalibration();
00383 
00384         /**
00385         *  @brief
00386         *    Classic calibration data received
00387         */
00388         void OnReadClassicCalibration();
00389 
00390         /**
00391         *  @brief
00392         *    Status information received
00393         */
00394         void OnReadStatus();
00395 
00396         /**
00397         *  @brief
00398         *    Button status received
00399         */
00400         void OnReadButtons();
00401 
00402         /**
00403         *  @brief
00404         *    Acceleration status received
00405         */
00406         void OnReadAccel();
00407 
00408         /**
00409         *  @brief
00410         *    IR status received
00411         */
00412         void OnReadIR();
00413 
00414         /**
00415         *  @brief
00416         *    Extension status received
00417         */
00418         void OnReadExtension(PLCore::uint32 nOffset);
00419 
00420         /**
00421         *  @brief
00422         *    Nunchuk status received
00423         */
00424         void OnReadNunchuk(PLCore::uint32 nOffset);
00425 
00426         /**
00427         *  @brief
00428         *    Classic-controller status received
00429         */
00430         void OnReadClassic(PLCore::uint32 nOffset);
00431 
00432         /**
00433         *  @brief
00434         *    Read from WiiMote memory
00435         *
00436         *  @param[in] nAddress
00437         *    Address to read from
00438         *  @param[in] nSize
00439         *    Size to read
00440         */
00441         void ReadMemory(int nAddress, PLCore::uint8 nSize);
00442 
00443         /**
00444         *  @brief
00445         *    Write to WiiMote memory
00446         *
00447         *  @param[in] nAddress
00448         *    Address to write to
00449         *  @param[in] pBuffer
00450         *    Buffer containing the data
00451         *  @param[in] nSize
00452         *    Size to write
00453         */
00454         void WriteMemory(int nAddress, const PLCore::uint8* pBuffer, PLCore::uint8 nSize);
00455 
00456         /**
00457         *  @brief
00458         *    Write a single byte to WiiMote memory
00459         *
00460         *  @param[in] nAddress
00461         *    Address to write to
00462         *  @param[in] nData
00463         *    Byte to write
00464         */
00465         void WriteMemory(int nAddress, PLCore::uint8 nData);
00466 
00467         /**
00468         *  @brief
00469         *    Clear output report
00470         */
00471         void ClearReport();
00472 
00473         /**
00474         *  @brief
00475         *    Send data to WiiMote device
00476         *
00477         *  @param[in] pBuffer
00478         *    Pointer to buffer (must be valid!)
00479         *  @param[in] nSize
00480         *    Size of buffer
00481         */
00482         void Send(PLCore::uint8 *pBuffer, PLCore::uint32 nSize);
00483 
00484         /**
00485         *  @brief
00486         *    Decrypt data
00487         *
00488         *  @param[in] nOffset
00489         *    Start address inside m_nWriteBuffer
00490         *  @param[in] nSize
00491         *    Size inside m_nWriteBuffer
00492         */
00493         void DecryptBuffer(PLCore::uint32 nOffset, PLCore::uint32 nSize);
00494 
00495         /**
00496         *  @brief
00497         *    Get WiiMote status
00498         */
00499         void SendStatusRequest();
00500 
00501         /**
00502         *  @brief
00503         *    Get calibration information
00504         */
00505         void SendCalibrationRequest();
00506 
00507 
00508     //[-------------------------------------------------------]
00509     //[ Private data                                          ]
00510     //[-------------------------------------------------------]
00511     private:
00512         // Event handlers
00513         PLCore::EventHandler<> EventHandlerOnDeviceConnect;
00514         PLCore::EventHandler<> EventHandlerOnDeviceDisconnect;
00515         PLCore::EventHandler<> EventHandlerOnDeviceRead;
00516 
00517         // HID connection
00518         ConnectionDevice    *m_pConnectionDevice;   /**< Connection device, always valid! */
00519         PLCore::uint8       *m_pInputBuffer;        /**< Input buffer */
00520         PLCore::uint8       *m_pOutputBuffer;       /**< Output buffer */
00521 
00522         // WiiMote options
00523         EReport              m_nReportMode;         /**< Report mode */
00524         EIRMode              m_nIRMode;             /**< IR mode */
00525         EExtension           m_nExtension;          /**< Extension type */
00526 
00527         // WiiMote status
00528         PLCore::uint8        m_nBattery;            /**< Battery (percent) */
00529         PLCore::uint8        m_nLEDs;               /**< LEDs status */
00530         PLCore::uint8        m_nRumble;             /**< Rumble state (1=on, 0=off) */
00531         PLCore::uint16       m_nButtons;            /**< WiiMote Buttons */
00532         SAcceleration        m_sAcc;                /**< Acceleration sensor */
00533         SDot                 m_sDots[2];            /**< IR dots */
00534         PLMath::Vector2      m_vIRPos;              /**< IR position (X, Y between 0..1) */
00535 
00536         // Nunchuk status
00537         PLCore::uint16       m_nNunchukButtons;     /**< Nunchuk buttons */
00538         SAcceleration        m_sNunchukAcc;         /**< Nunchuk acceleration sensor */
00539         SJoystick            m_sNunchukJoy;         /**< Nunchuk joystick */
00540 
00541 
00542 };
00543 
00544 
00545 //[-------------------------------------------------------]
00546 //[ Namespace                                             ]
00547 //[-------------------------------------------------------]
00548 } // PLInput
00549 
00550 
00551 #endif // __PLINPUT_WIIMOTE_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:09:01
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported