#include "ordevicecamera_device.h"
#include <math.h>
#define ORDEVICECAMERATEMPLATE__CLASS ORDEVICECAMERATEMPLATE__CLASSNAME
#define ORDEVICECAMERATEMPLATE__NAME ORDEVICECAMERATEMPLATE__CLASSSTR
#define ORDEVICECAMERATEMPLATE__LABEL "OR - Camera Tracker Template"
#define ORDEVICECAMERATEMPLATE__DESC "OR - Camera Tracker Template Description"
#define ORDEVICECAMERATEMPLATE__PREFIX "CameraTracker"
#define ORDEVICECAMERATEMPLATE__ICON "devices_camera_tracker.png"
ORDEVICECAMERATEMPLATE__CLASS,
ORDEVICECAMERATEMPLATE__LABEL,
ORDEVICECAMERATEMPLATE__DESC,
ORDEVICECAMERATEMPLATE__ICON );
bool ORDeviceCamera_Template::FBCreate()
{
mHardware.SetDevice( this );
ModelTemplatePrefix = ORDEVICECAMERATEMPLATE__PREFIX;
PacketBufferSize = 100;
SyncDelay = 4;
mFirstPacket = true;
if( FBDeviceCamera::FBCreate() )
{
FBTime lPeriod(0);
SamplingPeriod = lPeriod;
for(
int i=0;
i<PacketBufferSize;
i++ )
{
FBDeviceSync::PacketRegister( new ORDeviceCamera_Template_Packet );
}
return true;
}
return false;
}
void ORDeviceCamera_Template::FBDestroy()
{
while( FBDeviceSync::PacketGetCount() > 0 )
{
ORDeviceCamera_Template_Packet* lPacket = (ORDeviceCamera_Template_Packet*)FBDeviceSync::PacketRemove(0);
delete lPacket;
}
FBDeviceCamera::FBDestroy();
}
bool ORDeviceCamera_Template::DeviceOperation( kDeviceOperations pOperation )
{
switch (pOperation)
{
case kOpInit: return Init();
case kOpStart: return Start();
case kOpStop: return Stop();
case kOpReset: Stop(); return Start();
case kOpDone: return Done();
}
return FBDeviceCamera::DeviceOperation( pOperation );
}
bool ORDeviceCamera_Template::Init()
{
FBProgress lProgress;
lProgress.Caption = "Initializing Device";
lProgress.Text = "Initializing device...";
Status = "Initializing device...";
if(!mHardware.Init())
{
lProgress.Text = "Could not open device!";
Status = "Could not open device!";
return false;
}
lProgress.Text = "Device Initialized.";
Status = "Device Initialized.";
return true;
}
bool ORDeviceCamera_Template::Start()
{
FBProgress lProgress;
lProgress.Caption = "Device Startup";
lProgress.Text = "Opening device communications...";
Status = "Opening device communications...";
if(!mHardware.Open())
{
lProgress.Text = "Opening device communications";
Status = "Opening device communications";
return false;
}
lProgress.Text = "Getting Setup Info...";
Status = "Getting Setup Info...";
if(!mHardware.GetSetupInfo())
{
lProgress.Text = "Could not get setup info!";
Status = "Could not get setup info!";
return false;
}
lProgress.Text = "Starting Streaming Mode...";
Status = "Starting Streaming Mode...";
if(!mHardware.StartDataStream())
{
lProgress.Text = "Could not start streaming!";
Status = "Could not start streaming!";
return false;
}
lProgress.Text = "Device started.";
Status = "Ok.";
return true;
}
bool ORDeviceCamera_Template::Stop()
{
FBProgress lProgress;
lProgress.Caption = "Device Shutdown";
lProgress.Text = "Stopping device streaming";
Status = "Stopping device streaming";
if(!mHardware.StopDataStream())
{
Status = "Could not stop streaming";
return false;
}
lProgress.Text = "Stopping device communications";
Status = "Stopping device communications";
if(!mHardware.Close())
{
Status = "Could not close device";
return false;
}
Status = "?";
return false;
}
bool ORDeviceCamera_Template::Done()
{
FBProgress lProgress;
lProgress.Caption = "Device Removal";
lProgress.Text = "Stopping device...";
Status = "Stopping device...";
if(!mHardware.Close())
{
lProgress.Text = "Could not stop device!";
Status = "Could not stop device!";
return true;
}
Status = "?";
return false;
}
bool ORDeviceCamera_Template::DeviceSyncAnimationNodeNotify(FBAnimationNode* pAnimationNode ,FBEvaluateInfo* pEvaluateInfo, int pCorrectedSync)
{
FBDeviceCamera::DeviceSyncAnimationNodeNotify( pAnimationNode, pEvaluateInfo, pCorrectedSync);
ORDeviceCamera_Template_Packet* lPacket = (ORDeviceCamera_Template_Packet*) FBDeviceSync::PacketFetch( pCorrectedSync );
if( lPacket )
{
mNodeCamera_InData->WriteData( &(lPacket->Data), pEvaluateInfo );
}
FBDeviceSync::PacketRelease( lPacket );
return true;
}
void ORDeviceCamera_Template::DeviceIONotify( kDeviceIOs pAction,FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
FBDeviceCamera::DeviceIONotify( pAction, pDeviceNotifyInfo );
}
void ORDeviceCamera_Template::DeviceCameraEvalData( FBDeviceNotifyInfo &pDeviceNotifyInfo )
{
int lSyncCount = pDeviceNotifyInfo.GetSyncCount();
int lPacketCount = 0;
FBTime lRecordTime;
ORDeviceCamera_Template_Packet* lPacket = (ORDeviceCamera_Template_Packet*)FBDeviceSync::PacketLock();
{
while( lPacket && mHardware.FetchDataPacket() )
{
PrintDebugIOInfo( lPacket, pDeviceNotifyInfo );
lPacket->OriginalSync = lSyncCount;
lPacket->DeviceSync = mHardware.GetSync();
lPacket->Position = mHardware.GetPosition();
lPacket->Rotation = mHardware.GetRotation();
lPacket->Focus = mHardware.GetFocus();
lPacket->Zoom = mHardware.GetZoom();
lPacket->Data = mHardware.GetData();
lPacket->Time = mHardware.GetTime();
ORDeviceCamera_Template_Packet* lRecPacket = (ORDeviceCamera_Template_Packet*) FBDeviceSync::PacketFetch( lSyncCount-SyncDelay );
if( lRecPacket )
{
if( mNodeCamera_InData->GetAnimationToRecord() )
{
if( mFirstPacket )
{
mFirstPacket = false;
mRecordStartTime = pDeviceNotifyInfo.GetLocalTime();
mFirstPacketTime = lRecPacket->Time;
}
}
FBDeviceCameraInstrument* lInstrument = (FBDeviceCameraInstrument*)Instruments[0];
FBDeviceCameraPacket* lIPacket = lInstrument->RecordPacket;
lIPacket->Position =
FBVector3d( lRecPacket->Position );
lIPacket->Rotation =
FBVector3d( lRecPacket->Rotation );
lIPacket->FieldOfView =
FBVector2d( lRecPacket->FieldOfView );
lIPacket->OpticalCenter =
FBVector2d( lRecPacket->OpticalCenter );
lIPacket->Focus = (double)lRecPacket->Focus;
lIPacket->Zoom = (double)lRecPacket->Zoom;
lRecordTime = (lRecPacket->Time-mFirstPacketTime) + mRecordStartTime;
DeviceCameraRecordFrame( lRecordTime , pDeviceNotifyInfo, lRecPacket );
FBDeviceSync::PacketRelease( lRecPacket );
}
AckOneSampleReceived();
lPacketCount++;
FBDeviceSync::PacketUnlock( lPacket, true );
lPacket = (ORDeviceCamera_Template_Packet*)FBDeviceSync::PacketLock();
}
}
if( lPacket )
{
FBDeviceSync::PacketUnlock(lPacket, false);
}
PrintDebugIONotifyInfo( pDeviceNotifyInfo, lPacketCount );
}
void ORDeviceCamera_Template::DeviceCameraRecordFrame( FBTime &pTime, FBDeviceNotifyInfo& pNotifyInfo, ORDeviceCamera_Template_Packet* pPacket )
{
FBDeviceCamera::DeviceCameraRecordFrame( pTime, pNotifyInfo );
{
FBAnimationNode* lData;
lData = mNodeCamera_InData->GetAnimationToRecord();
if( lData )
{
lData->KeyAdd( pTime, &(pPacket->Data) );
}
}
}
void ORDeviceCamera_Template::RecordingDoneAnimation( FBAnimationNode* pAnimationNode )
{
FBDeviceCamera::RecordingDoneAnimation( pAnimationNode );
mFirstPacket = true;
}
bool ORDeviceCamera_Template::ModelTemplateBindNotify( FBModel* pModel, int pIndex, FBModelTemplate* pModelTemplate )
{
static int lOnMyWayOut = 0;
if( pModel )
{
{
FBCamera* lCamera = (FBCamera*) pModel;
FBColor lColor;
lColor[0] = 0.30;
lColor[1] = 0.30;
lColor[2] = 0.30;
lCamera->BackGroundTexture = mSystem.Scene->Textures.GetCount()>0 ? mSystem.Scene->Textures[0]:
NULL;
lCamera->ViewShowGrid = false;
lCamera->UseFrameColor = true;
lCamera->FrameColor = lColor;
lCamera->ViewOpticalCenter = true;
}
}
for ( int Count=0; Count<pModel->Children.GetCount(); Count++)
{
lOnMyWayOut++;
ORDeviceCamera_Template::ModelTemplateBindNotify( pModel->Children[Count], pIndex, pModelTemplate );
}
if( lOnMyWayOut==0 )
{
FBDeviceCamera::ModelTemplateBindNotify( pModel, pIndex, pModelTemplate );
}
else
{
lOnMyWayOut--;
}
return true;
}
#define CAMERATEMPLATE_VERSION_351 0x0351
#define CAMERATEMPLATE_VERSION_NUMBER CAMERATEMPLATE_VERSION_351
#define CAMERATEMPLATE_FBX_VERSION "Version"
#define CAMERATEMPLATE_FBX_SERIAL "Serial"
#define CAMERATEMPLATE_FBX_SIMULATOR "Simulator"
#define CAMERATEMPLATE_FBX_NETWORK "Network"
#define CAMERATEMPLATE_FBX_SHAREDMEM "SharedMemory"
#define CAMERATEMPLATE_FBX_SETTINGS "Settings"
bool ORDeviceCamera_Template::FbxStore(FBFbxObject* pFbxObject,
kFbxObjectStore pStoreWhat)
{
{
pFbxObject->FieldWriteI(CAMERATEMPLATE_FBX_VERSION, CAMERATEMPLATE_VERSION_NUMBER);
pFbxObject->FieldWriteBegin(CAMERATEMPLATE_FBX_SERIAL);
{
pFbxObject->FieldWriteI(this->GetSerialPort());
pFbxObject->FieldWriteI(this->GetSerialBaudRate());
}
pFbxObject->FieldWriteEnd();
pFbxObject->FieldWriteBegin(CAMERATEMPLATE_FBX_SIMULATOR);
{
pFbxObject->FieldWriteD(this->GetSimulatorFrequency());
pFbxObject->FieldWriteD(this->GetSimulatorAmplitude());
pFbxObject->FieldWriteD(this->GetSimulatorZoom());
pFbxObject->FieldWriteD(this->GetSimulatorFocus());
}
pFbxObject->FieldWriteEnd();
pFbxObject->FieldWriteBegin(CAMERATEMPLATE_FBX_NETWORK);
{
pFbxObject->FieldWriteC( this->GetNetworkAddress() );
pFbxObject->FieldWriteI( this->GetNetworkPort() );
}
pFbxObject->FieldWriteEnd();
pFbxObject->FieldWriteBegin(CAMERATEMPLATE_FBX_SHAREDMEM);
{
pFbxObject->FieldWriteC( this->GetSharedMemoryHandle() );
char lBuffer[255];
sprintf( lBuffer, "%#06lx", this->GetSharedMemoryAddress() );
pFbxObject->FieldWriteC( lBuffer );
}
pFbxObject->FieldWriteEnd();
pFbxObject->FieldWriteBegin(CAMERATEMPLATE_FBX_SETTINGS);
{
pFbxObject->FieldWriteD( this->GetHardwareSetting() );
}
pFbxObject->FieldWriteEnd();
}
return FBDeviceCamera::FbxStore( pFbxObject, pStoreWhat );
}
bool ORDeviceCamera_Template::FbxRetrieve(FBFbxObject* pFbxObject,
kFbxObjectStore pStoreWhat)
{
int lVersion;
if (pStoreWhat & kAttributes)
{
lVersion = pFbxObject->FieldReadI(CAMERATEMPLATE_FBX_VERSION);
if( lVersion >= CAMERATEMPLATE_VERSION_351 )
{
if(pFbxObject->FieldReadBegin(CAMERATEMPLATE_FBX_SERIAL))
{
this->SetSerialPort ( pFbxObject->FieldReadI() );
this->SetSerialBaudRate ( pFbxObject->FieldReadI() );
pFbxObject->FieldReadEnd();
}
if(pFbxObject->FieldReadBegin(CAMERATEMPLATE_FBX_SIMULATOR))
{
this->SetSimulatorFrequency ( pFbxObject->FieldReadD() );
this->SetSimulatorAmplitude ( pFbxObject->FieldReadD() );
this->SetSimulatorZoom ( pFbxObject->FieldReadD() );
this->SetSimulatorFocus ( pFbxObject->FieldReadD() );
pFbxObject->FieldReadEnd();
}
if(pFbxObject->FieldReadBegin(CAMERATEMPLATE_FBX_NETWORK) )
{
this->SetNetworkAddress ( pFbxObject->FieldReadC() );
this->SetNetworkPort ( pFbxObject->FieldReadI() );
pFbxObject->FieldReadEnd();
}
if(pFbxObject->FieldReadBegin(CAMERATEMPLATE_FBX_SHAREDMEM) )
{
this->SetSharedMemoryHandle( pFbxObject->FieldReadC() );
char lBuffer[255];
kReference lAddress;
sprintf(lBuffer, "%s", pFbxObject->FieldReadC());
sscanf( lBuffer, "%lx", &lAddress);
this->SetSharedMemoryAddress( lAddress );
pFbxObject->FieldReadEnd();
}
if(pFbxObject->FieldReadBegin(CAMERATEMPLATE_FBX_SETTINGS))
{
this->SetHardwareSetting( pFbxObject->FieldReadD() );
pFbxObject->FieldReadEnd();
}
}
}
return FBDeviceCamera::FbxRetrieve( pFbxObject, pStoreWhat );
}