#ifdef _MSC_VER
# if _MSC_VER < 1300
# pragma warning( disable : 4786 )
# endif
#endif
#include <conio.h>
#include <windows.h>
#include <process.h>
#include <iostream>
#include <apps/Common/exampleHelper.h>
#include <mvIMPACT_CPP/mvIMPACT_acquire.h>
#include <mvDisplay/Include/mvIMPACT_acquire_display.h>
using namespace std;
class CriticalSection
{
CRITICAL_SECTION m_criticalSection;
public:
CriticalSection()
{
InitializeCriticalSection( &m_criticalSection );
}
~CriticalSection()
{
DeleteCriticalSection( &m_criticalSection );
}
void lock( void )
{
EnterCriticalSection( &m_criticalSection );
}
void unlock( void )
{
LeaveCriticalSection( &m_criticalSection );
}
} g_critSect;
class LockedScope
{
CriticalSection c_;
public:
explicit LockedScope( CriticalSection& c ) : c_( c )
{
c_.lock();
}
~LockedScope()
{
c_.unlock();
}
};
class ThreadData
{
volatile bool boTerminateThread_;
public:
explicit ThreadData() : boTerminateThread_( false ) {}
virtual ~ThreadData() {}
bool terminated( void ) const
{
return boTerminateThread_;
}
void terminateThread( void )
{
boTerminateThread_ = true;
}
};
class DeviceData : public ThreadData
{
int lastRequestNr_;
public:
explicit DeviceData(
Device* p ) : ThreadData(), pDev_( p ), pFI_( 0 ), pIOSS_( 0 ), pSS_( 0 ), pDisplayWindow_( 0 ), lastRequestNr_(
INVALID_ID ) {}
~DeviceData()
{
{
}
delete pFI_;
delete pSS_;
delete pIOSS_;
delete pDisplayWindow_;
}
void init( const std::string& windowName )
{
{
LockedScope lockedScope( g_critSect );
cout << "Please note that there will be just one refresh for the display window, so if it is" << endl
<< "hidden under another window the result will not be visible." << endl;
}
}
{
return pDev_;
}
{
return pFI_;
}
{
return pIOSS_;
}
{
return pSS_;
}
{
return pDisplayWindow_;
}
};
class TriggerSignal : public ThreadData
{
unsigned int frequency_Hz_;
public:
explicit TriggerSignal(
DigitalOutput* pTriggerOutput,
unsigned int frequency_Hz ) : ThreadData(), pTriggerOutput_( pTriggerOutput ), frequency_Hz_( frequency_Hz ) {}
{
return pTriggerOutput_;
}
unsigned int frequency_Hz( void ) const
{
return frequency_Hz_;
}
};
unsigned int __stdcall liveThread( void* pData )
{
DeviceData* pThreadParameter = reinterpret_cast<DeviceData*>( pData );
unsigned int cnt = 0;
ImageDisplay& display = pThreadParameter->pDisp()->GetImageDisplay();
if( result != DEV_NO_FREE_REQUEST_AVAILABLE )
{
LockedScope lockedScope( g_critSect );
cout << "'FunctionInterface.imageRequestSingle' returned with an unexpected result: " << result
<< "(" << ImpactAcquireException::getErrorCodeAsString( result ) << ")" << endl;
}
manuallyStartAcquisitionIfNeeded( pThreadParameter->device(), *pFI );
const unsigned int timeout_ms = 200;
while( !pThreadParameter->terminated() )
{
{
{
++cnt;
if( cnt % 100 == 0 )
{
LockedScope lockedScope( g_critSect );
cout << "Info from " << pThreadParameter->device()->serial.read()
}
}
else
{
LockedScope lockedScope( g_critSect );
}
{
}
lastRequestNr = requestNr;
}
else
{
}
}
{
LockedScope lockedScope( g_critSect );
cout << "Overall good frames captured from device " << pThreadParameter->device()->serial.read() << ": " << cnt << endl;
}
manuallyStopAcquisitionIfNeeded( pThreadParameter->device(), *pFI );
{
}
return 0;
}
unsigned int __stdcall triggerThread( void* pData )
{
TriggerSignal* pSignal = reinterpret_cast<TriggerSignal*>( pData );
unsigned int cnt = 0;
const unsigned int sleepPeriod_ms = 1000 / ( pSignal->frequency_Hz() * 2 );
while( !pSignal->terminated() )
{
Sleep( sleepPeriod_ms );
pSignal->triggerOutput()->flip();
Sleep( sleepPeriod_ms );
pSignal->triggerOutput()->flip();
++cnt;
if( cnt % 100 == 0 )
{
LockedScope lockedScope( g_critSect );
cout << "Trigger signals generated: " << cnt << endl;
}
}
LockedScope lockedScope( g_critSect );
cout << "Overall trigger signals generated: " << cnt << endl;
return 0;
}
unsigned int getNumberFromUser( void )
{
unsigned int nr = 0;
std::cin >> nr;
std::cin.get();
return nr;
}
bool isDeviceSupportedBySample(
const Device*
const pDev )
{
return match( pDev->
product.
read(),
string(
"mvBlueCOUGAR-X*" ),
'*' ) == 0;
}
void setupTriggerInput( DeviceData* pDevData )
{
cout << "Select the digital INPUT of device(" << pDevData->device()->serial.read() << ")(as a string) that shall serve as a trigger input:" << endl;
DisplayPropertyDictionary<mvIMPACT::acquire::PropertyI>( cs.triggerSource );
modifyPropertyValue( cs.triggerSource );
cs.triggerMode.write( ctmOnRisingEdge );
cs.imageRequestTimeout_ms.write( 0 );
}
int main( void )
{
cout << "This sample is meant for mvBlueCOUGAR-X devices only. Other devices might be installed" << endl
<< "but won't be recognized by the application." << endl
<< endl;
std::vector<mvIMPACT::acquire::Device*> validDevices;
if( getValidDevices( devMgr, validDevices, isDeviceSupportedBySample ) < 2 )
{
cout << "This sample needs at least 2 valid devices(one master and one slave). " << validDevices.size() << " device(s) has/have been detected." << endl
<< "Unable to continue! Press [ENTER] to end the application" << endl;
cin.get();
return 1;
}
cout << "Please select the MASTER device(the one that will create the trigger for all devices).\n\n";
Device* pMaster = getDeviceFromUserInput( devMgr, isDeviceSupportedBySample,
true );
if( !pMaster )
{
cout << "Master device has not been properly selected. Unable to continue!\n"
<< "Press [ENTER] to end the application\n";
cin.get();
return 1;
}
set<Device*> setOfSlaves;
do
{
cout << "\nPlease select a SLAVE device(this one will be triggered by the master).\n\n";
Device* p = getDeviceFromUserInput( devMgr, isDeviceSupportedBySample,
true );
if( p == pMaster )
{
cout << "Master and slave must be different. Skipped!" << endl;
}
else if( p )
{
if( setOfSlaves.find( p ) == setOfSlaves.end() )
{
setOfSlaves.insert( p );
}
else
{
cout << "ALL slaves must be different. This one has already been selected. Skipped!" << endl;
}
}
cout << "\nAdd another slave device('y')? ";
}
while( _getch() == 'y' );
cout << endl;
vector<DeviceData*> devices;
devices.push_back( new DeviceData( pMaster ) );
set<Device*>::const_iterator it = setOfSlaves.begin();
set<Device*>::const_iterator itEND = setOfSlaves.end();
while( it != itEND )
{
devices.push_back( new DeviceData( *it ) );
++it;
}
const vector<DeviceData*>::size_type DEV_COUNT = devices.size();
try
{
for( vector<DeviceData*>::size_type i = 0; i < DEV_COUNT; i++ )
{
cout << "Initialising device " << devices[i]->device()->serial.read() << "..." << endl;
devices[i]->init( ( ( i == 0 ) ? string( "Master " ) : string( "Slave " ) ) + devices[i]->device()->serial.read() );
cout << endl
<< "Setup the " << ( ( i == 0 ) ? "MASTER" : "SLAVE" ) << " device:" << endl
<< "===========================" << endl
<< endl;
if( i == 0 )
{
const unsigned int digoutCount = devices[i]->IOSS()->getOutputCount();
for( unsigned int digOut = 0; digOut < digoutCount; digOut++ )
{
cout <<
" [" << digOut <<
"]: " << devices[i]->IOSS()->output( digOut )->
getDescription() << endl;
}
cout << endl
<< "Select the digital OUTPUT of the MASTER device(" << devices[i]->device()->serial.read() << ") where the trigger pulse shall be generated on: ";
pTriggerOutput = devices[i]->IOSS()->output( getNumberFromUser() );
cout << endl;
}
setupTriggerInput( devices[i] );
}
}
{
cout <<
"An error occurred while opening the devices(error code: " << e.
getErrorCodeAsString() <<
")." << endl
<< "Press [ENTER] to end the application" << endl;
cin.get();
return 1;
}
try
{
unsigned int triggerFrequency_Hz = 0;
bool boRun = true;
while( boRun )
{
cout << "Please enter the approx. desired trigger frequency in Hz: ";
triggerFrequency_Hz = getNumberFromUser();
if( ( triggerFrequency_Hz >= 1 ) && ( triggerFrequency_Hz <= 100 ) )
{
boRun = false;
continue;
}
cout << "Invalid Selection. This sample will accept values from 1 - 100." << endl;
}
TriggerSignal triggerSignal( pTriggerOutput, triggerFrequency_Hz );
HANDLE* pHandles = new HANDLE[DEV_COUNT];
for( unsigned int j = 0; j < DEV_COUNT; j++ )
{
unsigned int dwThreadID;
pHandles[j] = ( HANDLE )_beginthreadex( 0, 0, liveThread, ( LPVOID )devices[j], 0, &dwThreadID );
}
Sleep( 1000 );
HANDLE hTriggerThread;
unsigned int dwTriggerThreadID;
hTriggerThread = ( HANDLE )_beginthreadex( 0, 0, triggerThread, ( LPVOID )&triggerSignal, 0, &dwTriggerThreadID );
{
LockedScope lockedScope( g_critSect );
cout << "Press [ENTER] to end the acquisition" << endl;
}
if( _getch() == EOF )
{
printf( "Calling '_getch()' did return EOF...\n" );
}
{
LockedScope lockedScope( g_critSect );
cout << "Terminating threads..." << endl;
}
for( unsigned int k = 0; k < DEV_COUNT; k++ )
{
devices[k]->terminateThread();
}
triggerSignal.terminateThread();
WaitForMultipleObjects( static_cast<DWORD>( DEV_COUNT ), pHandles, true, INFINITE );
for( unsigned int l = 0; l < DEV_COUNT; l++ )
{
CloseHandle( pHandles[l] );
}
WaitForSingleObject( hTriggerThread, INFINITE );
CloseHandle( hTriggerThread );
hTriggerThread = 0;
delete [] pHandles;
}
{
cout << "Press [ENTER] to end the application" << endl;
cin.get();
return 1;
}
cout << "Press [ENTER] to end the application" << endl;
cin.get();
return 0;
}
mvBlueCOUGAR related camera settings (Device specific interface layout only).
Definition mvIMPACT_acquire.h:20241
std::string name(void) const
Returns the name of the component referenced by this object.
Definition mvIMPACT_acquire.h:1206
Grants access to devices that can be operated by this software interface.
Definition mvIMPACT_acquire.h:7171
This class and its functions represent an actual device detected by this interface in the current sys...
Definition mvIMPACT_acquire.h:6118
PropertyS product
A string property (read-only) containing the product name of this device.
Definition mvIMPACT_acquire.h:6537
void open(void)
Opens a device.
Definition mvIMPACT_acquire.h:6420
A class to represent a digital output pin(Device specific interface layout only).
Definition mvIMPACT_acquire.h:15546
std::string getDescription(void) const
Returns a description for this digital output.
Definition mvIMPACT_acquire.h:15609
The function interface to devices supported by this interface.
Definition mvIMPACT_acquire.h:10758
int imageRequestWaitFor(int timeout_ms, int queueNr=0) const
Waits for a request object to become ready.
Definition mvIMPACT_acquire.h:11616
int imageRequestUnlock(int nr) const
Unlocks the request for the driver again.
Definition mvIMPACT_acquire.h:11584
int imageRequestSingle(ImageRequestControl *pImageRequestControl=0, int *pRequestUsed=0) const
Sends an image request to the mvIMPACT::acquire::Device driver.
Definition mvIMPACT_acquire.h:11503
bool isRequestNrValid(int nr) const
Check if nr specifies a valid mvIMPACT::acquire::Request.
Definition mvIMPACT_acquire.h:11763
int imageRequestReset(int requestCtrlNr, int mode) const
Deletes all requests currently queued for the specified mvIMPACT::acquire::ImageRequestControl.
Definition mvIMPACT_acquire.h:11450
Request * getRequest(int nr) const
Returns a pointer to the desired mvIMPACT::acquire::Request.
Definition mvIMPACT_acquire.h:11189
A base class to handle digital inputs and outputs(Device specific interface layout only).
Definition mvIMPACT_acquire.h:16385
A base class for exceptions generated by Impact Acquire.
Definition mvIMPACT_acquire.h:256
std::string getErrorCodeAsString(void) const
Returns a string representation of the error associated with the exception.
Definition mvIMPACT_acquire.h:288
std::string read(int index=0) const
Reads a value from a property.
Definition mvIMPACT_acquire.h:5323
std::string readS(int index=0, const std::string &format="") const
Reads data from this property as a string.
Definition mvIMPACT_acquire.h:3340
Contains information about a captured buffer.
Definition mvIMPACT_acquire.h:8640
PropertyI64 infoFrameID
A 64 bit integer property (read-only) containing a frame identifier.
Definition mvIMPACT_acquire.h:9849
bool isOK(void) const
Convenience function to check if a request has been processed successfully.
Definition mvIMPACT_acquire.h:9474
PropertyIRequestResult requestResult
An enumerated integer property (read-only) defining the result of this request.
Definition mvIMPACT_acquire.h:9780
PropertyI64 infoFrameNr
A 64 bit integer property (read-only, zero-based) containing the number of image requests processed s...
Definition mvIMPACT_acquire.h:9856
Contains basic statistical information.
Definition mvIMPACT_acquire.h:14509
PropertyF framesPerSecond
A float property (read-only) containing the current number of frames captured per second.
Definition mvIMPACT_acquire.h:14586
PropertyI errorCount
An integer property (read-only) containing the overall count of image requests which returned with an...
Definition mvIMPACT_acquire.h:14568
PropertyI frameCount
An integer property (read-only) containing the overall count of images captured since the mvIMPACT::a...
Definition mvIMPACT_acquire.h:14592
A class that can be used to display images in a window.
Definition mvIMPACT_acquire_display.h:606
A class that can be used for displaying images within existing windows or GUI elements that can provi...
Definition mvIMPACT_acquire_display.h:176
void RemoveImage(void)
Removes the current image from the display.
Definition mvIMPACT_acquire_display.h:395
void SetImage(const void *pData, int width, int height, int bitsPerPixel, int pitch)
Sets the next image to display.
Definition mvIMPACT_acquire_display.h:316
void Update(void) const
Immediately redraws the current image.
Definition mvIMPACT_acquire_display.h:405
TDMR_ERROR
Errors reported by the device manager.
Definition mvDriverBaseEnums.h:2601
const int INVALID_ID
A constant to check for an invalid ID returned from the property handling module.
Definition mvPropHandlingDatatypes.h:62
@ DMR_NO_ERROR
The function call was executed successfully.
Definition mvDriverBaseEnums.h:2603
This namespace contains classes and functions that can be used to display images.
This namespace contains classes and functions belonging to the image acquisition module of this SDK.
Definition mvCommonDataTypes.h:34