devices/devicecamera/ordevicecamera_hardware.cxx

devices/devicecamera/ordevicecamera_hardware.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 "ordevicecamera_hardware.h"
#include <math.h>
#if defined( KARCH_ENV_WIN )
#include <winsock2.h>
#endif
#if defined( KARCH_ENV_UNIX )
#include <netinet/in.h>
#endif
/************************************************
* Constructor.
************************************************/
ORDeviceCamera_Template_Hardware::ORDeviceCamera_Template_Hardware()
{
// Communications settings
mSerialPort = 1;
mSerialBaudRate = 38400;
mNetworkAddress = "127.0.0.1";
mNetworkPort = 3001;
mNetworkProtocol = kFBCommTypeNetworkTCP;
mAmplitude = 1.0;
mFrequency = 1.0;
mFocus = 0.0;
mZoom = 0.0;
#ifdef KARCH_ENV_UNIX
mHandleName = "/usr/tmp/camera.arena";
mHandleAddress = 0x910000;
#else
mHandleName = "HMC_SHM_V1";
mHandleAddress = 0x000000;
mShmHandle = NULL;
#endif
mCounter = 0;
mDevice = NULL;
mHardwareSetting = 0.0;
}
/************************************************
* Destructor.
************************************************/
ORDeviceCamera_Template_Hardware::~ORDeviceCamera_Template_Hardware()
{
}
/************************************************
* Open device communications.
************************************************/
bool ORDeviceCamera_Template_Hardware::Init()
{
// TODO: Initialize device
return true;
}
/************************************************
* Open device communications.
************************************************/
bool ORDeviceCamera_Template_Hardware::Open()
{
bool lSuccess = false;
if( GetCommunicationType() == kFBCommTypeSerial )
{
// Configure
mCommPort.PortNumber = mSerialPort;
mCommPort.BaudRate = mSerialBaudRate;
mCommPort.ByteSize = 8;
mCommPort.Parity = kFBParityOdd;
mCommPort.StopBits = 1;
mCommPort.Active = true;
if( mCommPort.Active )
{
lSuccess = true;
}
}
else if( GetCommunicationType() == kFBCommTypeSharedMemory )
{
#ifdef KARCH_ENV_WIN
mShmHandle = OpenFileMapping( FILE_MAP_READ, FALSE, mHandleName );
if( mShmHandle )
{
// TODO: Map memory to local object.
//mShmMap = (MyCameraClass*) MapViewOfFile( mShmHandle, FILE_MAP_READ, 0, 0, 0 );
/*
mShmMap = NULL;
if( mShmMap )
{
lSuccess = true;
}
*/
}
#elif defined(KARCH_ENV_UNIX)
// TODO: Open IRIX shared memory!
#endif
}
else if( GetCommunicationType() == kFBCommTypeSimulator )
{
mCounter = 0;
lSuccess = true;
}
else if( GetCommunicationType() == kFBCommTypeNetworkUDP )
{
mTCPIP.CreateSocket ( mNetworkSocket, kFBTCPIP_DGRAM );
mTCPIP.Bind ( mNetworkSocket, INADDR_ANY, mNetworkPort );
/*
// open network connection
// connect
unsigned char lMessage[12];
int lWritten = 0;
memset( lMessage, 0, 12 );
lMessage[0] = 0xff;
lMessage[1] = 0x11; // reset
lMessage[11]= 0xff;
mTCPIP.WriteDatagram( mNetworkSocket, lMessage, 12, &lWritten, inet_addr(mNetworkAddress), mNetworkPort );
*/
if( mTCPIP.Select( mNetworkSocket, true, false, false, 1000 ) )
{
lSuccess = true;
}
else
{
mTCPIP.CloseSocket( mNetworkSocket );
}
}
else if( GetCommunicationType() == kFBCommTypeNetworkTCP )
{
mTCPIP.CreateSocket( mNetworkSocket, kFBTCPIP_Stream );
mTCPIP.Connect( mNetworkSocket, mNetworkAddress, mNetworkPort );
if( mTCPIP.Select( mNetworkSocket, true, false, false, 1000 ) )
{
lSuccess = true;
}
else
{
mTCPIP.CloseSocket( mNetworkSocket );
}
}
else
{
}
return lSuccess;
}
/************************************************
* Get device setup information.
************************************************/
bool ORDeviceCamera_Template_Hardware::GetSetupInfo()
{
// TODO: Get setup information from client
return true;
}
/************************************************
* Start device data stream.
************************************************/
bool ORDeviceCamera_Template_Hardware::StartDataStream()
{
// TODO: ask server to start sending data
mCounter = 0;
return true;
}
/************************************************
* Fetch a data packet from the device.
************************************************/
bool ORDeviceCamera_Template_Hardware::FetchDataPacket()
{
bool lSuccess = false;
if( GetCommunicationType() == kFBCommTypeSimulator )
{
lSuccess = RealTimeUpdate();
}
else if( GetCommunicationType() == kFBCommTypeSerial )
{
// TODO:
// Some sort of real-time packet reading
// Must take into account partial packets!!!
// char lBuffer;
//mCommPort.Read(lBuffer,1,&mBytesRead);
}
else if( GetCommunicationType() == kFBCommTypeSharedMemory )
{
// TODO:
// Read data out of shared memory object.
}
else if( GetCommunicationType() == kFBCommTypeNetworkUDP )
{
// TODO:
// Read data from network
// With UDP, you probably will not need as much partial packet handling
// As only complete packets make it through.
}
else if( GetCommunicationType() == kFBCommTypeNetworkTCP )
{
// TODO:
// Read data from network
// Must take into account partial packets (like serial)
// due to TCP protocol.
}
return lSuccess;
}
/************************************************
* Stop device data stream.
************************************************/
bool ORDeviceCamera_Template_Hardware::StopDataStream()
{
// TODO: ask server to stop sending data
return true;
}
/************************************************
* Close device communications.
************************************************/
bool ORDeviceCamera_Template_Hardware::Close()
{
if( GetCommunicationType() == kFBCommTypeSimulator )
{
}
else if( GetCommunicationType() == kFBCommTypeSerial )
{
mCommPort.Active = false;
}
else if( GetCommunicationType() == kFBCommTypeSharedMemory )
{
#ifdef KARCH_ENV_WIN
/*
if( mShmMap )
{
UnmapViewOfFile( mShmMap );
mShmMap = NULL;
}
*/
if( mShmHandle )
{
CloseHandle( mShmHandle );
mShmHandle = NULL;
}
#elif defined(KARCH_ENV_UNIX)
// TODO: Close IRIXS shared memory.
#endif
}
else if( (GetCommunicationType() == kFBCommTypeNetworkUDP) || (GetCommunicationType() == kFBCommTypeNetworkTCP) )
{
mTCPIP.CloseSocket( mNetworkSocket );
}
return true;
}
/************************************************
* Shut down hardware.
************************************************/
bool ORDeviceCamera_Template_Hardware::Done()
{
// TODO: Close network connection to server
return true;
}
/************************************************
* Simulator update.
************************************************/
bool ORDeviceCamera_Template_Hardware::RealTimeUpdate()
{
//Returns true if a new data packet is ready
// TODO: insert time stamp from data packet
FBTime lTime = mSystem.SystemTime;
double lVal = lTime.GetSecondDouble();
double p = 3.14;
double a = mAmplitude;
double w = mFrequency;
double lSin = a*sin( w*lVal + p );
double lCos = a*cos( w*lVal + p );
double lFocus;
double lZoom;
double lData;
lFocus = mFocus;
lZoom = mZoom;
lData = lCos;
mDefaultPacket.Position = FBVector3d ( 40*lSin+20*lCos, 40*lSin+20*lCos, 40*lSin+20*lCos );
mDefaultPacket.Rotation = FBVector3d ( 4*lSin+4*lCos, 4*lSin+4*lCos, 4*lSin+4*lCos );
mDefaultPacket.Focus = lFocus;
mDefaultPacket.Zoom = lZoom;
mDefaultPacket.Time = lTime;
mDefaultPacket.Data = lData;
mCounter++;
if( mCounter % 2 == 0 )
{
return false;
}
return true;
}