oculus1

diff libovr/Src/OVR_SensorImpl.h @ 1:e2f9e4603129

added LibOVR and started a simple vr wrapper.
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Sep 2013 16:14:59 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/OVR_SensorImpl.h	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,208 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +Filename    :   OVR_SensorImpl.h
     1.7 +Content     :   Sensor device specific implementation.
     1.8 +Created     :   March 7, 2013
     1.9 +Authors     :   Lee Cooper
    1.10 +
    1.11 +Copyright   :   Copyright 2013 Oculus VR, Inc. All Rights reserved.
    1.12 +
    1.13 +Use of this software is subject to the terms of the Oculus license
    1.14 +agreement provided at the time of installation or download, or which
    1.15 +otherwise accompanies this software in either electronic or hard copy form.
    1.16 +
    1.17 +*************************************************************************************/
    1.18 +
    1.19 +#ifndef OVR_SensorImpl_h
    1.20 +#define OVR_SensorImpl_h
    1.21 +
    1.22 +#include "OVR_HIDDeviceImpl.h"
    1.23 +
    1.24 +namespace OVR {
    1.25 +    
    1.26 +struct TrackerMessage;
    1.27 +class ExternalVisitor;
    1.28 +
    1.29 +//-------------------------------------------------------------------------------------
    1.30 +// SensorDeviceFactory enumerates Oculus Sensor devices.
    1.31 +class SensorDeviceFactory : public DeviceFactory
    1.32 +{
    1.33 +public:
    1.34 +    static SensorDeviceFactory Instance;
    1.35 +
    1.36 +    // Enumerates devices, creating and destroying relevant objects in manager.
    1.37 +    virtual void EnumerateDevices(EnumerateVisitor& visitor);
    1.38 +
    1.39 +    virtual bool MatchVendorProduct(UInt16 vendorId, UInt16 productId) const;
    1.40 +    virtual bool DetectHIDDevice(DeviceManager* pdevMgr, const HIDDeviceDesc& desc);
    1.41 +protected:
    1.42 +    DeviceManager* getManager() const { return (DeviceManager*) pManager; }   
    1.43 +};
    1.44 +
    1.45 +
    1.46 +// Describes a single a Oculus Sensor device and supports creating its instance.
    1.47 +class SensorDeviceCreateDesc : public HIDDeviceCreateDesc
    1.48 +{
    1.49 +public:
    1.50 +    SensorDeviceCreateDesc(DeviceFactory* factory, const HIDDeviceDesc& hidDesc)
    1.51 +        : HIDDeviceCreateDesc(factory, Device_Sensor, hidDesc) { }
    1.52 +    
    1.53 +    virtual DeviceCreateDesc* Clone() const
    1.54 +    {
    1.55 +        return new SensorDeviceCreateDesc(*this);
    1.56 +    }
    1.57 +
    1.58 +    virtual DeviceBase* NewDeviceInstance();
    1.59 +
    1.60 +    virtual MatchResult MatchDevice(const DeviceCreateDesc& other,
    1.61 +                                    DeviceCreateDesc**) const
    1.62 +    {
    1.63 +        if ((other.Type == Device_Sensor) && (pFactory == other.pFactory))
    1.64 +        {
    1.65 +            const SensorDeviceCreateDesc& s2 = (const SensorDeviceCreateDesc&) other;
    1.66 +            if (MatchHIDDevice(s2.HIDDesc))
    1.67 +                return Match_Found;
    1.68 +        }
    1.69 +        return Match_None;
    1.70 +    }
    1.71 +
    1.72 +    virtual bool MatchHIDDevice(const HIDDeviceDesc& hidDesc) const
    1.73 +    {
    1.74 +        // should paths comparison be case insensitive?
    1.75 +        return ((HIDDesc.Path.CompareNoCase(hidDesc.Path) == 0) &&
    1.76 +                (HIDDesc.SerialNumber == hidDesc.SerialNumber));
    1.77 +    }
    1.78 +
    1.79 +    virtual bool        GetDeviceInfo(DeviceInfo* info) const;
    1.80 +};
    1.81 +
    1.82 +
    1.83 +//-------------------------------------------------------------------------------------
    1.84 +// ***** OVR::SensorDisplayInfoImpl
    1.85 +
    1.86 +// DisplayInfo obtained from sensor; these values are used to report distortion
    1.87 +// settings and other coefficients.
    1.88 +// Older SensorDisplayInfo will have all zeros, causing the library to apply hard-coded defaults.
    1.89 +// Currently, only resolutions and sizes are used.
    1.90 +struct SensorDisplayInfoImpl
    1.91 +{
    1.92 +    enum  { PacketSize = 56 };
    1.93 +    UByte   Buffer[PacketSize];
    1.94 +
    1.95 +    enum
    1.96 +    {
    1.97 +        Mask_BaseFmt    = 0x0f,
    1.98 +        Mask_OptionFmts = 0xf0,
    1.99 +        Base_None       = 0,
   1.100 +        Base_ScreenOnly = 1,
   1.101 +        Base_Distortion = 2,
   1.102 +    };
   1.103 +
   1.104 +    UInt16  CommandId;
   1.105 +    UByte   DistortionType;    
   1.106 +    UInt16  HResolution, VResolution;
   1.107 +    float   HScreenSize, VScreenSize;
   1.108 +    float   VCenter;
   1.109 +    float   LensSeparation;
   1.110 +    float   EyeToScreenDistance[2];
   1.111 +    float   DistortionK[6];
   1.112 +
   1.113 +    SensorDisplayInfoImpl();
   1.114 +
   1.115 +    void Unpack();
   1.116 +};
   1.117 +
   1.118 +
   1.119 +//-------------------------------------------------------------------------------------
   1.120 +// ***** OVR::SensorDeviceImpl
   1.121 +
   1.122 +// Oculus Sensor interface.
   1.123 +
   1.124 +class SensorDeviceImpl : public HIDDeviceImpl<OVR::SensorDevice>
   1.125 +{
   1.126 +public:
   1.127 +     SensorDeviceImpl(SensorDeviceCreateDesc* createDesc);
   1.128 +    ~SensorDeviceImpl();
   1.129 +
   1.130 +
   1.131 +    // DeviceCommaon interface
   1.132 +    virtual bool Initialize(DeviceBase* parent);
   1.133 +    virtual void Shutdown();
   1.134 +    
   1.135 +    virtual void SetMessageHandler(MessageHandler* handler);
   1.136 +
   1.137 +    // HIDDevice::Notifier interface.
   1.138 +    virtual void OnInputReport(UByte* pData, UInt32 length);
   1.139 +    virtual UInt64 OnTicks(UInt64 ticksMks);
   1.140 +
   1.141 +    // HMD-Mounted sensor has a different coordinate frame.
   1.142 +    virtual void SetCoordinateFrame(CoordinateFrame coordframe);    
   1.143 +    virtual CoordinateFrame GetCoordinateFrame() const;    
   1.144 +
   1.145 +    // SensorDevice interface
   1.146 +    virtual bool SetRange(const SensorRange& range, bool waitFlag);
   1.147 +    virtual void GetRange(SensorRange* range) const;
   1.148 +
   1.149 +    // Sets report rate (in Hz) of MessageBodyFrame messages (delivered through MessageHandler::OnMessage call). 
   1.150 +    // Currently supported maximum rate is 1000Hz. If the rate is set to 500 or 333 Hz then OnMessage will be 
   1.151 +    // called twice or thrice at the same 'tick'. 
   1.152 +    // If the rate is  < 333 then the OnMessage / MessageBodyFrame will be called three
   1.153 +    // times for each 'tick': the first call will contain averaged values, the second
   1.154 +    // and third calls will provide with most recent two recorded samples.
   1.155 +    virtual void        SetReportRate(unsigned rateHz);
   1.156 +    // Returns currently set report rate, in Hz. If 0 - error occurred.
   1.157 +    // Note, this value may be different from the one provided for SetReportRate. The return
   1.158 +    // value will contain the actual rate.
   1.159 +    virtual unsigned    GetReportRate() const;
   1.160 +
   1.161 +    // Hack to create HMD device from sensor display info.
   1.162 +    static void EnumerateHMDFromSensorDisplayInfo(const SensorDisplayInfoImpl& displayInfo, 
   1.163 +                                                  DeviceFactory::EnumerateVisitor& visitor);
   1.164 +protected:
   1.165 +
   1.166 +    void openDevice();
   1.167 +    void closeDeviceOnError();
   1.168 +
   1.169 +    Void    setCoordinateFrame(CoordinateFrame coordframe);
   1.170 +    bool    setRange(const SensorRange& range);
   1.171 +
   1.172 +    Void    setReportRate(unsigned rateHz);
   1.173 +
   1.174 +    // Called for decoded messages
   1.175 +    void        onTrackerMessage(TrackerMessage* message);
   1.176 +
   1.177 +    // Helpers to reduce casting.
   1.178 +/*
   1.179 +    SensorDeviceCreateDesc* getCreateDesc() const
   1.180 +    { return (SensorDeviceCreateDesc*)pCreateDesc.GetPtr(); }
   1.181 +
   1.182 +    HIDDeviceDesc* getHIDDesc() const
   1.183 +    { return &getCreateDesc()->HIDDesc; }    
   1.184 +*/
   1.185 +
   1.186 +    // Set if the sensor is located on the HMD.
   1.187 +    // Older prototype firmware doesn't support changing HW coordinates,
   1.188 +    // so we track its state.
   1.189 +    CoordinateFrame Coordinates;
   1.190 +    CoordinateFrame HWCoordinates;
   1.191 +    UInt64      NextKeepAliveTicks;
   1.192 +
   1.193 +    bool        SequenceValid;
   1.194 +    SInt16      LastTimestamp;
   1.195 +    UByte       LastSampleCount;
   1.196 +    float       LastTemperature;
   1.197 +    Vector3f    LastAcceleration;
   1.198 +    Vector3f    LastRotationRate;
   1.199 +    Vector3f    LastMagneticField;
   1.200 +
   1.201 +    // Current sensor range obtained from device. 
   1.202 +    SensorRange MaxValidRange;
   1.203 +    SensorRange CurrentRange;
   1.204 +    
   1.205 +    UInt16      OldCommandId;
   1.206 +};
   1.207 +
   1.208 +
   1.209 +} // namespace OVR
   1.210 +
   1.211 +#endif // OVR_SensorImpl_h