159 lines
5.0 KiB
C++

////======================================================================================================
//// Copyright 2023, NaturalPoint Inc.
////======================================================================================================
/**
* ExampleGloveAdapterSingleton class is an adapter class provided to demonstrate how communication between plugin device and
* the glove SDK can be managed. A singleton instance of this class manages interaction between plugin device and the glove
* device SDK. The adapter instance also stores a keeps the latest data and device info map that stores the currently detected device serials
* and the latest frame data so that corresponding glove device can poll from it. Please note that this is provided only as an example, and
* there could be other ways to set this up.
*/
#pragma once
#include <list>
#include <mutex>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <functional>
// OptiTrack Peripheral Device API
#include "AnalogChannelDescriptor.h"
#include "GloveDataFormat.h"
#include "HardwareSimulator.h"
namespace OptiTrackPluginDevices
{
namespace ExampleDevice
{
class ExampleGloveAdapterSingleton;
static ExampleGloveAdapterSingleton* s_Instance = nullptr;
static std::unique_ptr<ExampleGloveAdapterSingleton> gGloveAdapter;
/**
* This is an example glove adapter (singleton) class provided to demonstrate how glove data can be populated.
* This adapter class is reponsible for all interaction with the device SDK, and populating the glove data.
* Glove data for multiple glove devices should get aggregated in here.
*/
class ExampleGloveAdapterSingleton
{
friend class ExampleGloveDevice;
public:
ExampleGloveAdapterSingleton(AnalogSystem::IDeviceManager* pDeviceManager);
~ExampleGloveAdapterSingleton();
// Singleton design pattern
ExampleGloveAdapterSingleton(const ExampleGloveAdapterSingleton&) = delete; // Should not be cloneable
ExampleGloveAdapterSingleton& operator=(const ExampleGloveAdapterSingleton& other) = delete; // Shounot be assignable
/**
* Should call the shutdown from the plugin SDK. In this case, shutsdown the simulator
*/
bool ClientShutdown();
/**
* Detection thread for connecting to the glove server.
*/
void DoDetectionThread();
std::thread mDetectionThread;
unsigned int mConnectionAttemptCount = 0;
const unsigned int kMaxConnectionAttempt = 10;
/**
* Connect to the glove host.
*/
bool ConnectToHost();
private:
/**
* Returns whether detector is connected to Glove Host.
*/
bool IsConnected();
/**
* Saves the latest data to the map
*/
void SetLatestData(const sGloveDeviceData& gloveFingerData);
/**
* Pointer to device manager for additional operations
*/
void SetDeviceManager(AnalogSystem::IDeviceManager* pDeviceManager);
/**
* Saves the latest glove data to the map
*/
void SetLatestDeviceInfo(const sGloveDeviceBaseInfo& deviceInfo);
/**
* Gets the latest data for device with given unique serial
*/
bool GetLatestData(const std::uint64_t mDeviceSerial, sGloveDeviceData& gloveFingerData);
/**
* Creates new device by instantiating the factory and transferring it to Motive
*/
void CreateNewGloveDevice(sGloveDeviceBaseInfo& deviceInfo);
/**
* Prints error into Motive.
*/
void NotifyConnectionFail();
bool bIsConnected = false;
bool bIsDetecting = true;
int mCurrentDeviceIndex = 0;
std::string mServerAddress = "";
/**
* [Glove SDK Placeholder]
* Example data map for storing aggregated device data.
*/
uint16_t mDeviceCount = 0;
std::vector<uint64_t> mDetectedDevices;
std::unordered_map<uint64_t, sGloveDeviceData> mLatestGloveData;
std::unordered_map<uint64_t, sGloveDeviceBaseInfo> mLatestDeviceInfo;
/**
* [Glove SDK Placeholder]
* The following methods are sample callback functions as a demonstration of how this adapter could
* Communicate with plugin SDK. It receives aggregated frame data through the data callback.
*/
bool bIsSimulating;
void StartSimulatedHardware(int deviceCount);
static void RegisterSDKCallbacks();
static void OnDataCallback(std::vector<SimulatedPluginDevices::SimulatedGloveFrameData>& gloveFingerData);
static void OnDeviceInfoCallback(std::vector<SimulatedPluginDevices::SimulatedDeviceInfo>& newGloveInfo);
static sGloveDeviceBaseInfo ConvertDeviceInfoFormat(SimulatedPluginDevices::SimulatedDeviceInfo& glove);
static sGloveDeviceData ConvertDataFormat(const SimulatedPluginDevices::SimulatedGloveFrameData& glove);
protected:
/**
* [Glove SDK Simulator]
* Instance of simulator
*/
SimulatedPluginDevices::HardwareSimulator* mGloveSimulator;
/**
* [Glove SDK Placeholder]
* Example glove data mutex
*/
std::recursive_mutex* mGloveDataMutex;
/**
* Pointer to device manager in Motive for reporting error messages.
*/
AnalogSystem::IDeviceManager* mDeviceManager;
};
}
}