devices/devicesync/ordevicesync_device.cxx

devices/devicesync/ordevicesync_device.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2009 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
//--- Class declaration
#include "ordevicesync_device.h"
#include <math.h>
//--- Registration defines
#define ORDEVICESYNCTEMPLATE__CLASS ORDEVICESYNCTEMPLATE__CLASSNAME
#define ORDEVICESYNCTEMPLATE__NAME ORDEVICESYNCTEMPLATE__CLASSSTR
#define ORDEVICESYNCTEMPLATE__LABEL "OR - Sync Device Template"
#define ORDEVICESYNCTEMPLATE__DESC "OR - Synchronized Device Template Description"
#define ORDEVICESYNCTEMPLATE__PREFIX "SyncDevice"
//--- FiLMBOX implementation and registration
FBDeviceImplementation ( ORDEVICESYNCTEMPLATE__CLASS );
FBRegisterDevice ( ORDEVICESYNCTEMPLATE__NAME,
ORDEVICESYNCTEMPLATE__CLASS,
ORDEVICESYNCTEMPLATE__LABEL,
ORDEVICESYNCTEMPLATE__DESC,
FB_DEFAULT_SDK_ICON ); // Icon filename (default=Open Reality icon)
/************************************************
* FiLMBOX Constructor.
************************************************/
bool ORDeviceSync_Template::FBCreate()
{
PacketBufferSize = 100;
if( FBDeviceSync::FBCreate() )
{
// Variables
mFirstPacket = true;
// Create my animation nodes
mNodeCamera_InData = AnimationNodeOutCreate( 0, "Data", ANIMATIONNODE_TYPE_NUMBER );
// Set sampling rate to be on external (or video) sync
FBTime lPeriod(0);
SamplingPeriod = lPeriod;
for( int i=0; i<PacketBufferSize; i++ )
{
FBDeviceSync::PacketRegister( new ORDeviceSync_Template_Packet );
}
return true;
}
return false;
}
/************************************************
* FiLMBOX Destructor.
************************************************/
void ORDeviceSync_Template::FBDestroy()
{
while( FBDeviceSync::PacketGetCount() > 0 )
{
ORDeviceSync_Template_Packet* lPacket = (ORDeviceSync_Template_Packet*)FBDeviceSync::PacketRemove(0);
delete lPacket;
}
FBDevice::FBDestroy();
}
/************************************************
* Device operation.
************************************************/
bool ORDeviceSync_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 FBDevice::DeviceOperation( pOperation );
}
/************************************************
* Initialization of device.
************************************************/
bool ORDeviceSync_Template::Init()
{
FBProgress lProgress;
lProgress.Caption = "Initializing Device";
// Step 1: Init device communications
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;
}
/************************************************
* Device is put online.
************************************************/
bool ORDeviceSync_Template::Start()
{
FBProgress lProgress;
lProgress.Caption = "Starting Up Device";
// Step 1: Open device communications
lProgress.Text = "Opening device communications...";
Status = "Opening device communications...";
if(!mHardware.Open())
{
lProgress.Text = "Opening device communications";
Status = "Opening device communications";
return false;
}
// Step 2: Get device information
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;
}
// Step 3: Start data stream.
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";
mFirstPacket = true;
mRecordStartTime.Set( 0 );
mFirstPacketTime.Set( 0 );
return true;
}
/************************************************
* Device is stopped (offline).
************************************************/
bool ORDeviceSync_Template::Stop()
{
FBProgress lProgress;
lProgress.Caption = "Shutting down device";
// Step 1: Stop streaming data
lProgress.Text = "Sending STOP STREAM command";
Status = "Stopping device streaming";
if(!mHardware.StopDataStream())
{
Status = "Could not stop streaming";
return false;
}
// Step 2: Close communications
lProgress.Text = "Stopping device communications";
Status = "Stopping device communications";
if(!mHardware.Close())
{
Status = "Could not close device";
return false;
}
Status = "?";
return false;
}
/************************************************
* Removal of device.
************************************************/
bool ORDeviceSync_Template::Done()
{
FBProgress lProgress;
lProgress.Caption = "Shutting Down Device";
// Step 1: Stop hardware
lProgress.Text = "Stopping device...";
Status = "Stopping device...";
if(!mHardware.Close())
{
lProgress.Text = "Could not stop device!";
Status = "Could not stop device!";
return true;
}
return false;
}
/************************************************
* Real-Time Engine Evaluation.
************************************************/
bool ORDeviceSync_Template::DeviceSyncAnimationNodeNotify(FBAnimationNode* pAnimationNode, FBEvaluateInfo* pEvaluateInfo, int pCorrectedSync)
{
ORDeviceSync_Template_Packet* lPacket;
lPacket = (ORDeviceSync_Template_Packet*)FBDeviceSync::PacketFetch( pCorrectedSync );
if( lPacket )
{
mNodeCamera_InData->WriteData( &(lPacket->Data), pEvaluateInfo );
}
FBDeviceSync::PacketRelease( lPacket );
return true;
}
/************************************************
* Real-Time Synchronous Device IO.
************************************************/
void ORDeviceSync_Template::DeviceIONotify( kDeviceIOs pAction,FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
int lSyncCount = pDeviceNotifyInfo.GetSyncCount();
int lPacketCount = 0;
FBTime lRecordTime;
switch (pAction)
{
case kIOPlayModeWrite:
case kIOStopModeWrite:
break;
case kIOStopModeRead:
case kIOPlayModeRead:
{
// Lock a packet
ORDeviceSync_Template_Packet* lPacket = (ORDeviceSync_Template_Packet*)FBDeviceSync::PacketLock();
{
while( lPacket && mHardware.FetchDataPacket() )
{
// Store packet
lPacket->OriginalSync = lSyncCount;
lPacket->DeviceSync = mHardware.GetSync();
lPacket->Data = mHardware.GetData();
lPacket->Time = mHardware.GetTime();
// Establish the FiLMBOX recording time
if( mNodeCamera_InData->GetAnimationToRecord() )
{
if( mFirstPacket )
{
mFirstPacket = false;
mRecordStartTime = pDeviceNotifyInfo.GetLocalTime();
mFirstPacketTime = lPacket->Time;
}
}
lRecordTime = (lPacket->Time-mFirstPacketTime) + mRecordStartTime;
// Record frame & stats
DeviceRecordFrame( lRecordTime, lPacket );
AckOneSampleReceived();
// Cleanup & prep for next packet
lPacketCount++;
FBDeviceSync::PacketUnlock( lPacket, true );
lPacket = (ORDeviceSync_Template_Packet*)FBDeviceSync::PacketLock();
}
}
// If a packet is still locked, free packet
if( lPacket )
{
FBDeviceSync::PacketUnlock(lPacket, false);
}
}
break;
}
}
void ORDeviceSync_Template::RecordingDoneAnimation( FBAnimationNode* pAnimationNode )
{
// Parent call
FBDeviceSync::RecordingDoneAnimation( pAnimationNode );
mFirstPacket = true;
}
/************************************************
* Device record frame
************************************************/
void ORDeviceSync_Template::DeviceRecordFrame(FBTime &pRecordTime, ORDeviceSync_Template_Packet* pPacket)
{
if( mPlayerControl.GetTransportMode() == kFBTransportPlay )
{
FBAnimationNode* lData;
lData = mNodeCamera_InData->GetAnimationToRecord();
if( lData )
{
lData->KeyAdd( pRecordTime, &(pPacket->Data) );
}
}
}
/************************************************
* Store data in FBX.
************************************************/
bool ORDeviceSync_Template::FbxStore(FBFbxObject* pFbxObject,kFbxObjectStore pStoreWhat)
{
if (pStoreWhat & kAttributes)
{
}
return true;
}
/************************************************
* Retrieve data from FBX.
************************************************/
bool ORDeviceSync_Template::FbxRetrieve(FBFbxObject* pFbxObject,kFbxObjectStore pStoreWhat)
{
if (pStoreWhat & kAttributes)
{
}
return true;
}