Skip to content

Commit

Permalink
Split the code into different files
Browse files Browse the repository at this point in the history
  • Loading branch information
Dschadu committed Jul 19, 2020
1 parent 8a351b3 commit ffc3ec4
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 253 deletions.
239 changes: 46 additions & 193 deletions motionPose/motion_pose.cpp → motionPose/MotionPoseController.cpp
Original file line number Diff line number Diff line change
@@ -1,84 +1,10 @@
#include <openvr_driver.h>
#include "openvr_math.h"
#include "MotionPoseController.h"
#include "driverlog.h"

#include <vector>
#include <thread>
#include <chrono>
#include <math.h>

#include <stdio.h>
#include <stdarg.h>

#if defined( _WINDOWS )
#include <windows.h>
#endif

#include <conio.h>
#include <tchar.h>
#include "WatchdogProvider.h"

using namespace vr;


#if defined(_WIN32)
#define HMD_DLL_EXPORT extern "C" __declspec( dllexport )
#define HMD_DLL_IMPORT extern "C" __declspec( dllimport )
#elif defined(__GNUC__) || defined(COMPILER_GCC) || defined(__APPLE__)
#define HMD_DLL_EXPORT extern "C" __attribute__((visibility("default")))
#define HMD_DLL_IMPORT extern "C"
#else
#error "Unsupported Platform."
#endif

inline HmdQuaternion_t HmdQuaternion_Init(double w, double x, double y, double z)
//class CMotionPoseControllerDriver : public vr::ITrackedDeviceServerDriver
namespace driver
{
HmdQuaternion_t quat;
quat.w = w;
quat.x = x;
quat.y = y;
quat.z = z;
return quat;
}

inline HmdVector3d_t HmdVector3d_t_Init(double x, double y, double z)
{
HmdVector3d_t vec;
vec.v[0] = x;
vec.v[1] = y;
vec.v[2] = z;
return vec;
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CMotionPoseControllerDriver : public vr::ITrackedDeviceServerDriver
{

double rigX;
double rigY;
double rigZ;
double rigPitch;
double rigRoll;
double rigYaw;
double rigHeave;
double rigSurge;
double rigSway;
double yawComp;

char* mmfFile;
DriverPose_t pose;
int hmdDeviceId;
bool hmdValid = false;
bool moverConnected = false;
TrackedDevicePose_t poses[10];
HANDLE hMapFile = NULL;
HmdVector3d_t rigPos;

public:
CMotionPoseControllerDriver()
CMotionPoseControllerDriver::CMotionPoseControllerDriver()
{
m_unObjectId = vr::k_unTrackedDeviceIndexInvalid;
m_ulPropertyContainer = vr::k_ulInvalidPropertyContainer;
Expand Down Expand Up @@ -109,33 +35,33 @@ class CMotionPoseControllerDriver : public vr::ITrackedDeviceServerDriver
rigPos = { 0 };
}

virtual ~CMotionPoseControllerDriver()
CMotionPoseControllerDriver::~CMotionPoseControllerDriver()
{
}

#define BUF_SIZE 256
#define BUF_SIZE 256


virtual EVRInitError Activate(vr::TrackedDeviceIndex_t unObjectId)
vr::EVRInitError CMotionPoseControllerDriver::Activate(vr::TrackedDeviceIndex_t unObjectId)
{
DriverLog("Activate motionPoseController\n");

m_unObjectId = unObjectId;
m_ulPropertyContainer = vr::VRProperties()->TrackedDeviceToPropertyContainer(m_unObjectId);

vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, Prop_ModelNumber_String, m_sModelNumber.c_str());
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, Prop_RenderModelName_String, "locator");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_ModelNumber_String, m_sModelNumber.c_str());
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_RenderModelName_String, "locator");

// return a constant that's not 0 (invalid) or 1 (reserved for Oculus)
vr::VRProperties()->SetUint64Property(m_ulPropertyContainer, Prop_CurrentUniverseId_Uint64, 2);
vr::VRProperties()->SetUint64Property(m_ulPropertyContainer, vr::Prop_CurrentUniverseId_Uint64, 2);

vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, Prop_ControllerRoleHint_Int32, TrackedControllerRole_OptOut);
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, Prop_DeviceClass_Int32, TrackedDeviceClass_GenericTracker);
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, vr::Prop_ControllerRoleHint_Int32, vr::TrackedControllerRole_OptOut);
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, vr::Prop_DeviceClass_Int32, vr::TrackedDeviceClass_GenericTracker);

return VRInitError_None;
return vr::VRInitError_None;
}

void initalizeMoverMmf() {
void CMotionPoseControllerDriver::initalizeMoverMmf()
{
moverConnected = false;
TCHAR szName[] = TEXT("Local\\motionRigPose");

Expand All @@ -148,50 +74,53 @@ class CMotionPoseControllerDriver : public vr::ITrackedDeviceServerDriver
{
// DriverLog("could not open mmf motionRigPose\n");
}
else {
else
{
// DriverLog("successfully opened mmf motionRigPose\n");
mmfFile = (char*)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (strncmp(mmfFile, "posedata", 8) == 0) {
mmfFile = (char*)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (strncmp(mmfFile, "posedata", 8) == 0)
{
DriverLog("successfully receiving posedata\n");
moverConnected = true;
}
else {
// DriverLog("no valid posedata\n");
else
{
// DriverLog("no valid posedata\n");
}
}
}

virtual void Deactivate()
void CMotionPoseControllerDriver::Deactivate()
{
m_unObjectId = vr::k_unTrackedDeviceIndexInvalid;
}

virtual void EnterStandby()
void CMotionPoseControllerDriver::EnterStandby()
{
}

void* GetComponent(const char* pchComponentNameAndVersion)
void* CMotionPoseControllerDriver::GetComponent(const char* pchComponentNameAndVersion)
{
// override this to add a component to a driver
return NULL;
}

virtual void PowerOff()
void CMotionPoseControllerDriver::PowerOff()
{
}

/** debug request from a client */
virtual void DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize)
void CMotionPoseControllerDriver::DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize)
{
if (unResponseBufferSize >= 1)
pchResponseBuffer[0] = 0;
}

virtual DriverPose_t GetPose()
vr::DriverPose_t CMotionPoseControllerDriver::GetPose()
{
// DriverLog("virtualPoseController::GetPose\n");

Expand All @@ -200,7 +129,7 @@ class CMotionPoseControllerDriver : public vr::ITrackedDeviceServerDriver
pose.shouldApplyHeadModel = false;
pose.willDriftInYaw = false;

HmdQuaternion_t yaw = vrmath::quaternionFromRotationY(yawComp);
vr::HmdQuaternion_t yaw = vrmath::quaternionFromRotationY(yawComp);

// Turn Right/Left
if ((GetAsyncKeyState('E') & 0x8000) != 0) yawComp += 0.005;
Expand All @@ -219,13 +148,15 @@ class CMotionPoseControllerDriver : public vr::ITrackedDeviceServerDriver
if ((GetAsyncKeyState('X') & 0x8000) != 0) rigPos = rigPos + vrmath::quaternionRotateVector(yaw, HmdVector3d_t_Init(0, 0, 0.003));

// reset
if ((GetAsyncKeyState('P') & 0x8000) != 0) {
if ((GetAsyncKeyState('P') & 0x8000) != 0)
{
rigPos = { 0 };
yawComp = 0;
}

if (moverConnected && strncmp(mmfFile, "posedata", 8) == 0) {
pose.result = TrackingResult_Running_OK;
if (moverConnected && strncmp(mmfFile, "posedata", 8) == 0)
{
pose.result = vr::TrackingResult_Running_OK;
// <PoseSway><PoseSurge><PoseHeave><PoseYaw><PoseRoll><PosePitch>
double* poseData = (double*)mmfFile;
rigSway = poseData[1];
Expand Down Expand Up @@ -258,8 +189,9 @@ class CMotionPoseControllerDriver : public vr::ITrackedDeviceServerDriver

return pose;
}
else {
pose.result = TrackingResult_Calibrating_InProgress;
else
{
pose.result = vr::TrackingResult_Calibrating_InProgress;
pose.deviceIsConnected = false;
initalizeMoverMmf();

Expand All @@ -271,11 +203,7 @@ class CMotionPoseControllerDriver : public vr::ITrackedDeviceServerDriver

}





void RunFrame()
void CMotionPoseControllerDriver::RunFrame()
{
// Your driver would read whatever hardware state is associated with its input components and pass that
// in to UpdateBooleanComponent. This could happen in RunFrame or on a thread of your own that's reading USB
Expand All @@ -289,86 +217,11 @@ class CMotionPoseControllerDriver : public vr::ITrackedDeviceServerDriver
if (event.trackedDeviceIndex == this->m_unObjectId && event.eventType == EVREventType::VREvent_PropertyChanged) {
if (event.data.property.prop == Prop_RenderModelName_String) {
std::string renderModel = vr::VRProperties()->GetStringProperty(event.data.property.container, Prop_RenderModelName_String);
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, Prop_RenderModelName_String, renderModel.data());
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, Prop_RenderModelName_String, renderModel.data());
}
}
}*/

vr::VRServerDriverHost()->TrackedDevicePoseUpdated(m_unObjectId, GetPose(), sizeof(DriverPose_t));
}

std::string GetSerialNumber() const { return m_sSerialNumber; }

private:
vr::TrackedDeviceIndex_t m_unObjectId;
vr::PropertyContainerHandle_t m_ulPropertyContainer;

std::string m_sSerialNumber;
std::string m_sModelNumber;


};

class CServerDriver_MotionPose : public IServerTrackedDeviceProvider
{
public:
virtual EVRInitError Init(vr::IVRDriverContext* pDriverContext);
virtual void Cleanup();
virtual const char* const* GetInterfaceVersions() { return vr::k_InterfaceVersions; }
virtual void RunFrame();
virtual bool ShouldBlockStandbyMode() { return false; }
virtual void EnterStandby() {}
virtual void LeaveStandby() {}

private:
CMotionPoseControllerDriver* m_pController = nullptr;
};

CServerDriver_MotionPose g_motionPoseDriver;


EVRInitError CServerDriver_MotionPose::Init(vr::IVRDriverContext* pDriverContext)
{
VR_INIT_SERVER_DRIVER_CONTEXT(pDriverContext);
InitDriverLog(vr::VRDriverLog());

m_pController = new CMotionPoseControllerDriver();
vr::VRServerDriverHost()->TrackedDeviceAdded(m_pController->GetSerialNumber().c_str(), vr::TrackedDeviceClass_Controller, m_pController);

return VRInitError_None;
}

void CServerDriver_MotionPose::Cleanup()
{
CleanupDriverLog();
delete m_pController;
m_pController = NULL;
}


void CServerDriver_MotionPose::RunFrame()
{
if (m_pController)
{
m_pController->RunFrame();
}
}

vrmotioncompensation::driver::WatchdogProvider watchdogProvider;

HMD_DLL_EXPORT void* HmdDriverFactory(const char* pInterfaceName, int* pReturnCode)
{
if (0 == strcmp(IServerTrackedDeviceProvider_Version, pInterfaceName))
{
return &g_motionPoseDriver;
}
else if (std::strcmp(IVRWatchdogProvider_Version, pInterfaceName) == 0)
{
return &watchdogProvider;
vr::VRServerDriverHost()->TrackedDevicePoseUpdated(m_unObjectId, GetPose(), sizeof(vr::DriverPose_t));
}

if (pReturnCode)
*pReturnCode = VRInitError_Init_InterfaceNotFound;

return NULL;
}
};
Loading

0 comments on commit ffc3ec4

Please sign in to comment.