287 lines
8.1 KiB
C++
287 lines
8.1 KiB
C++
/**
|
|
* @file
|
|
*
|
|
* @author Mamadou Babaei <mamadou@senseglove.com>
|
|
*
|
|
* @section LICENSE
|
|
*
|
|
* (The MIT License)
|
|
*
|
|
* Copyright (c) 2020 - 2022 SenseGlove
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
* @section DESCRIPTION
|
|
*
|
|
*
|
|
*/
|
|
|
|
|
|
#include "SGCoreImpl/SGDeviceImpl.h"
|
|
#include <string>
|
|
|
|
#include "Containers/StringConv.h"
|
|
#include "Misc/AssertionMacros.h"
|
|
#include "Runtime/Launch/Resources/Version.h"
|
|
|
|
#include "SGBuildHacks/SGInclude_Core_SGDevice.h"
|
|
#include "SGCoreImpl/SGBetaDeviceImpl.h"
|
|
#include "SGCoreImpl/SGCoreEnumCast.h"
|
|
#include "SGCoreImpl/SGHapticGloveImpl.h"
|
|
#include "SGCoreImpl/SGNovaGloveImpl.h"
|
|
#include "SGCoreImpl/SGSenseGloveImpl.h"
|
|
#include "Templates/Casts.h"
|
|
|
|
struct FSGDeviceImpl::FImpl
|
|
{
|
|
/************************
|
|
* Member variables
|
|
************************/
|
|
|
|
FSGDevicePtrType SGDevice;
|
|
|
|
/************************
|
|
* Owner object
|
|
************************/
|
|
|
|
FSGDeviceImpl* Owner;
|
|
|
|
/************************
|
|
* Constructor / Destructor
|
|
************************/
|
|
|
|
explicit FImpl(FSGDeviceImpl* InOwner);
|
|
~FImpl();
|
|
|
|
/************************
|
|
* Default copy constructor & copy assignment operator
|
|
************************/
|
|
|
|
FImpl(const FImpl& Rhs) = default;
|
|
FImpl& operator=(const FImpl& Rhs) = default;
|
|
};
|
|
|
|
void FSGDeviceImpl::ParseFirmware(const FString& RawFirmware, int32& OutMainVersion, int32& OutSubVersion)
|
|
{
|
|
FSGDeviceType::ParseFirmware(TCHAR_TO_UTF8(*RawFirmware), OutMainVersion, OutSubVersion);
|
|
}
|
|
|
|
FString FSGDeviceImpl::ToString(ESGDeviceType DeviceType)
|
|
{
|
|
const FString String{UTF8_TO_TCHAR(FSGDeviceType::ToString(FSGCoreEnumCast::ToEDeviceType(DeviceType)).c_str())};
|
|
return String;
|
|
}
|
|
|
|
FSGDeviceImpl::FSGDeviceImpl()
|
|
:
|
|
#if ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 )
|
|
Pimpl(TUniquePtr<FImpl, FImplDeleter>(new FImpl{this}, PimplDeleter))
|
|
#else
|
|
Pimpl(TUniquePtr<FImpl, FImplDeleter>(new FImpl{this}))
|
|
#endif /* ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) */
|
|
{
|
|
}
|
|
|
|
FSGDeviceImpl::FSGDeviceImpl(const FSGDevicePtrType SGDevice)
|
|
:
|
|
#if ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 )
|
|
Pimpl(TUniquePtr<FImpl, FImplDeleter>(new FImpl{this}, PimplDeleter))
|
|
#else
|
|
Pimpl(TUniquePtr<FImpl, FImplDeleter>(new FImpl{this}))
|
|
#endif /* ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) */
|
|
{
|
|
ensureAlwaysMsgf(SGDevice.get(), TEXT("Invalid SGDevice!"));
|
|
Pimpl->SGDevice = SGDevice;
|
|
}
|
|
|
|
FSGDeviceImpl::FSGDeviceImpl(const FSGDeviceImpl& Rhs)
|
|
:
|
|
#if ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 )
|
|
Pimpl(TUniquePtr<FImpl, FImplDeleter>(new FImpl{*Rhs.Pimpl}, PimplDeleter))
|
|
#else
|
|
Pimpl(TUniquePtr<FImpl, FImplDeleter>(new FImpl{*Rhs.Pimpl}))
|
|
#endif /* ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) */
|
|
{
|
|
Pimpl->Owner = this;
|
|
}
|
|
|
|
FSGDeviceImpl::FSGDeviceImpl(FSGDeviceImpl&& Rhs) noexcept = default;
|
|
|
|
FSGDeviceImpl::~FSGDeviceImpl() = default;
|
|
|
|
FSGDeviceImpl& FSGDeviceImpl::operator=(const FSGDeviceImpl& Rhs)
|
|
{
|
|
*Pimpl = *Rhs.Pimpl;
|
|
Pimpl->Owner = this;
|
|
return *this;
|
|
}
|
|
|
|
FSGDeviceImpl& FSGDeviceImpl::operator=(FSGDeviceImpl&& Rhs) noexcept = default;
|
|
|
|
FSGDeviceImpl::FSGDevicePtrType FSGDeviceImpl::ToSGDevicePtr() const
|
|
{
|
|
return Pimpl->SGDevice;
|
|
}
|
|
|
|
FSGDeviceImpl::FSGDevicePtrType FSGDeviceImpl::ToSGDevicePtrChecked() const
|
|
{
|
|
ensureAlwaysMsgf(Pimpl->SGDevice.get(), TEXT("Invalid Pimpl->SGDevice!"));
|
|
return Pimpl->SGDevice;
|
|
}
|
|
|
|
TSharedRef<FSGDeviceImpl> FSGDeviceImpl::ToSGDeviceImplRef()
|
|
{
|
|
return SharedThis(this);
|
|
}
|
|
|
|
TSharedRef<FSGBetaDeviceImpl> FSGDeviceImpl::ToBetaDeviceImplRef()
|
|
{
|
|
FSGBetaDeviceImpl* BetaDevice(dynamic_cast<FSGBetaDeviceImpl*>(this));
|
|
ensureAlwaysMsgf(BetaDevice, TEXT("Invalid BetaDevice!"));
|
|
return SharedThis(BetaDevice);
|
|
}
|
|
|
|
TSharedRef<FSGHapticGloveImpl> FSGDeviceImpl::ToHapticGloveImplRef()
|
|
{
|
|
FSGHapticGloveImpl* HapticGlove(dynamic_cast<FSGHapticGloveImpl*>(this));
|
|
ensureAlwaysMsgf(HapticGlove, TEXT("Invalid HapticGlove!"));
|
|
return SharedThis(HapticGlove);
|
|
}
|
|
|
|
TSharedRef<FSGNovaGloveImpl> FSGDeviceImpl::ToNovaGloveImplRef()
|
|
{
|
|
FSGNovaGloveImpl* NovaGlove(dynamic_cast<FSGNovaGloveImpl*>(this));
|
|
ensureAlwaysMsgf(NovaGlove, TEXT("Invalid NovaGlove!"));
|
|
return SharedThis(NovaGlove);
|
|
}
|
|
|
|
TSharedRef<FSGSenseGloveImpl> FSGDeviceImpl::ToSenseGloveImplRef()
|
|
{
|
|
FSGSenseGloveImpl* SenseGlove(dynamic_cast<FSGSenseGloveImpl*>(this));
|
|
ensureAlwaysMsgf(SenseGlove, TEXT("Invalid SenseGlove!"));
|
|
return SharedThis(SenseGlove);
|
|
}
|
|
|
|
ESGDeviceType FSGDeviceImpl::GetDeviceType() const
|
|
{
|
|
return FSGCoreEnumCast::ToESGDeviceType(ToSGDevicePtrChecked()->GetDeviceType());
|
|
}
|
|
|
|
FString FSGDeviceImpl::GetDeviceId() const
|
|
{
|
|
const FString Id{UTF8_TO_TCHAR(ToSGDevicePtrChecked()->GetDeviceId().c_str())};
|
|
return Id;
|
|
}
|
|
|
|
FString FSGDeviceImpl::GetHardwareVersion() const
|
|
{
|
|
const FString Version{UTF8_TO_TCHAR(ToSGDevicePtrChecked()->GetHardwareVersion().c_str())};
|
|
return Version;
|
|
}
|
|
|
|
int32 FSGDeviceImpl::GetFirmwareVersion() const
|
|
{
|
|
return ToSGDevicePtrChecked()->GetFirmwareVersion();
|
|
}
|
|
|
|
int32 FSGDeviceImpl::GetSubFirmwareVersion() const
|
|
{
|
|
return ToSGDevicePtrChecked()->GetSubFirmwareVersion();
|
|
}
|
|
|
|
FString FSGDeviceImpl::GetFirmwareString() const
|
|
{
|
|
const FString String{UTF8_TO_TCHAR(ToSGDevicePtrChecked()->GetFirmwareString().c_str())};
|
|
return String;
|
|
}
|
|
|
|
FString FSGDeviceImpl::GetAddress() const
|
|
{
|
|
const FString String(UTF8_TO_TCHAR(ToSGDevicePtrChecked()->GetAddress().c_str()));
|
|
return String;
|
|
}
|
|
|
|
bool FSGDeviceImpl::IsConnected() const
|
|
{
|
|
return ToSGDevicePtrChecked()->IsConnected();
|
|
}
|
|
|
|
ESGConnectionType FSGDeviceImpl::GetConnectionType() const
|
|
{
|
|
return FSGCoreEnumCast::ToESGConnectionType(ToSGDevicePtrChecked()->GetConnectionType());
|
|
}
|
|
|
|
int32 FSGDeviceImpl::PacketsPerSecondReceived() const
|
|
{
|
|
return ToSGDevicePtrChecked()->PacketsPerSecondReceived();
|
|
}
|
|
|
|
int32 FSGDeviceImpl::PacketsPerSecondSent() const
|
|
{
|
|
return ToSGDevicePtrChecked()->PacketsPerSecondSent();
|
|
}
|
|
|
|
int32 FSGDeviceImpl::GetDeviceIndex() const
|
|
{
|
|
return ToSGDevicePtrChecked()->GetDeviceIndex();
|
|
}
|
|
|
|
void FSGDeviceImpl::SetDeviceIndex(const int32 NewIndex)
|
|
{
|
|
ToSGDevicePtrChecked()->SetDeviceIndex(NewIndex);
|
|
}
|
|
|
|
bool FSGDeviceImpl::IsWireless() const
|
|
{
|
|
return ToSGDevicePtrChecked()->IsWireless();
|
|
}
|
|
|
|
bool FSGDeviceImpl::HasBattery() const
|
|
{
|
|
return ToSGDevicePtrChecked()->HasBattery();
|
|
}
|
|
|
|
bool FSGDeviceImpl::IsCharging()
|
|
{
|
|
return ToSGDevicePtrChecked()->IsCharging();
|
|
}
|
|
|
|
bool FSGDeviceImpl::GetBatteryLevel(float& OutLevel)
|
|
{
|
|
return ToSGDevicePtrChecked()->GetBatteryLevel(OutLevel);
|
|
}
|
|
|
|
FString FSGDeviceImpl::ToString() const
|
|
{
|
|
const FString String(UTF8_TO_TCHAR(ToSGDevicePtrChecked()->ToString().c_str()));
|
|
return String;
|
|
}
|
|
|
|
FSGDeviceImpl::FImpl::FImpl(FSGDeviceImpl* InOwner)
|
|
: SGDevice(std::make_shared<FSGDeviceType>()),
|
|
Owner(InOwner)
|
|
{
|
|
}
|
|
|
|
FSGDeviceImpl::FImpl::~FImpl() = default;
|
|
|
|
void FSGDeviceImpl::FImplDeleter::operator()(const FSGDeviceImpl::FImpl* P) const
|
|
{
|
|
delete P;
|
|
} |