Accessing Aptina Imaging Devices with Perl
This application note describes how Aptina imaging devices such as demonstration cameras and emulation boards can be accessed using the Perl programming language on a Windows platform. It also introduces the Aptina (Micron) Perl wrapper MIDLib.pm which provides convenient access to mode setting commands which are not normally available.
Prerequisites
ActiveState Perl 5.8 (Win32::OLE module required)
Aptina Imaging software
Aptina Perl MIDLib module (not essential, but provides convenient access to mode set/get options)
An understanding of Object Oriented Perl is useful, but not essential.
Win32 Perl Tips for Unix Programmers
For non-Windows Perl programmers, the first step is getting the correct 'hash bang' line. Like a Unix system this points to your Perl binary, but must also include the Windows drive designator.
It might look something like this -
#! c:/Program\ Files/Perl/bin/perl
Command line access
The native Windows command prompt is a rather impoverished place to work. It is recommended that the Cygwin environment is installed and Perl run from there.
Running Perl Programs
Note that some installations of ActiveState Perl refuse to recognize the path to your Perl binary in your code and may have to be run like this -
%perl my_program.pl
rather than -
%my_program.pl
Finding Out About Available Perl Methods
The OLE-Browser available in the ActiveState Perl menu is an excellent tool for locating methods available for Perl to use. Scroll down the list of libraries to the one labelled MidLIBCom Type Library, clicking on this will show all the method calls available. It is guaranteed to be more up to date than any documentation.
Setting OLE Warnings Level
The OLE class can be set to different levels of warning sensitivity, for debugging code use level 2 or 3. Anything lower risks masking useful messages. The example (Listing 1) shows how this is done.
0 Ignore error, return undef
1 Carp::carp if $^W is set (-w option)
2 always Carp::carp
3 Carp::croak
Perl access to Aptina Imaging Devices
To access data from an Aptina Imaging device several steps are needed.
Create new COM wrapper object which points to the Micron Imaging Library
Get list of all cameras connected to PC (usually just one device)
Open connection to camera
Start transport stream
Listing 1. How these steps are performed in Perl -
#! c:/Program\ Files/Perl/bin/perl
use strict;
use Win32::OLE;
Win32::OLE->Option( Warn => 3 );
- [1] set up COM connection to MIDLib
my $Com = Win32::OLE->new('MIDLibCom.MIDLib');
die "Error - couldn't get COM object\n" unless $Com; - [2] open connections to all available cameras
- If you have a sensor data file (.sdat or .xsdat) pass it's path as an argument.
- If you do not have an sdat file a null string must be passed (not nothing).
my $Cameras = $Com->OpenCameras(''); - [3] $Cameras is an arrayref. Each array item is a Camera object.
my $Camera = $Cameras->[0];
die "Error - couldn't get CAMERA object\n" unless $Camera; - [4] need to start transport stream before we can do anything
$Camera->startTransport;
Once processing has finished the camera stream can be stopped and the COM connection closed.
$Camera->stopTransport;
$Com->closeCameras;
Taking a Photo
Here's an example program that takes a photo and save it to a file in raw format. The file can be viewed using the Aptina 'PlayBack' tool.
#! c:/Program\ Files/Perl/bin/perl
use strict;
use Win32::OLE;
Win32::OLE->Option( Warn => 3 ); - photo is stored here
my $capture = 'photo.raw'; - change sensor data file to suit your imaging device
my $sdat = 'C:\Aptina Imaging\sensor_data\A-3100-ES3-DEV.xsdat';
my $Com = Win32::OLE->new('MIDLibCom.MIDLib');
die "Error - couldn't get COM object ($Com)\n" unless $Com; - open connections to all available cameras
my $Cameras = $Com->OpenCameras($sdat); - $Cameras is an arrayref. Each array item is a Camera object.
my $Camera = $Cameras->[0];
die "Error - couldn't get CAMERA object\n" unless $Cameras; - need to start transport stream before we can do anything
$Camera->startTransport; - $Sensor is an object inside Camera object containing sensor data
my $Sensor = $Camera->sensor; - get buffer size
my $width = $Sensor->width;
my $height = $Sensor->height;
print "Grabbing frame ($width, $height)...\n";
my @frame = $Camera->grabFrame();
print "Stopping transport stream\n";
$Camera->stopTransport();
$Com->closeCameras;
print "Saving photo to $capture\n";
die "Couldn't open $capture for writing\n" unless (open RAW, "> $capture");
binmode RAW;
for (my $y = 0; $y <= $height-1; $y++) {
for (my $x = 0; $x <= $width-1; $x++) {
my $data = $frame[0][$x][$y];
my $fdata = pack("v", $data);
print RAW $fdata;
}
}close RAW; Aptina MIDLib Perl Module
This is a wrapper around the Win32::OLE class that provides convenient access to the Aptina mode setting functions, which are not easy to perform normally. It would be used in a similar way to the Win32::OLE approach as shown below.
The MIDLib module is available here: C:\Aptina Imaging\lib\MIDLib.pm
#! c:/Program\ Files/Perl/bin/perl
use strict;
use MIDLib; - set up MIDLib
my $Com = MIDLib->new('MIDLibCom.MIDLib');
MIDLib->Option( Warn => 2 );
die "Error - couldn't get COM object\n" unless $Com; - open connections to all available cameras
my $Cameras = $Com->OpenCameras(''); - $Camera is an arrayref. Each array item is a Camera object.
my $Camera = $Cameras->[0];
die "Error - couldn't get CAMERA object\n" unless $Camera; - need to start transport stream before we can do anything
$Camera->startTransport;
However, the advantage of the MIDLib module is that it provides convenient access to the setMode command with mode names.
For example -
$Camera->setMode('MI_REG_ADDR_SIZE', 16);# sets mi_reg_addr_size to 16 bits
$Camera->setMode('MI_REG_DATA_SIZE', 8);# sets mi_reg_data_size to 16 bits
And the same with the getMode command -
my $data_bits = $Camera->getMode('MI_REG_DATA_SIZE');