Example Code

The following example demonstrates how to initialize the midlib interface. After initialization, it shows how to read registers from the attached camera. A second example shows how to enable USB device removal notification.
Please note that there are additional source code examples in the "C:\Aptina Imaging\samples" directory.

include "c:\aptina imaging\include\midlib2.h"
mi_camera_t *cameras[MI_MAX_CAMERAS];//cameras found
mi_s32 num_cameras;//number of cameras found
mi_camera_t *camera= NULL;//current camera
int chipId;//register number for chipID
mi_reg_data_t *chip_id_reg;//pointer to the chipid reg
mi_reg_data_t *reg;//pointer to a register
mi_u8 *pCameraBuff = NULL;//camera image buffer
mi_u32 val;//register value returned
int new_val;//register value to write
int error;//error return value
mi_s32 retVal;//Return value
//mi_OpenCameras() is the first midlib routine which must be called
//The third parameter is the directory containing the sensor data files
//The environment variable MI_HOME is set at installation, the default
//location is C:\Aptina Imaging
retVal = mi_OpenCameras(cameras,&num_cameras, mi_SensorData());
//we need at least one camera to use the sensor
if (num_cameras == 0) {
if (retVal == MI_SENSOR_ERROR)
Error("Could not find sensor data file.");
//Could prompt user for a particular filename and try again
else
Error("Could not connect to camera.");
mi_CloseCameras();
exit(0);
}
//Let the user choose the camera to use, we will use the
//first camera found in this example
camera = cameras[0];
//Allocate a buffer to store the images, use the size that is in
//sensor->bufferSize
pCameraBuff = (mi_u8*)malloc(camera->sensor->bufferSize);
//We must start the camera before we can grab an image
mi_startTransport(camera);
//An example of reading a register by name
mi_ReadSensorRegStr(camera, "CHIP_VERSION_REG", NULL, &val);
//Examples of reading and writing registers by address. In this case we are
// reading from register address 0 and writing to register address 0x2B
mi_ReadSensorRegAddr(camera, MI_REG_ADDR, 0, 0, 1, &val);
mi_WriteSensorRegAddr(camera, MI_REG_ADDR, 0, 0x2B, 1, 0x10);
//Continue to grabFrames until user prompts to stop
while (!done) {
//Only draw the "good" frames
if (mi_grabFrame(camera, pCameraBuff, camera->sensor->bufferSize) ==
MI_CAMERA_SUCCESS)
draw_image(pCameraBuffer);
}
//stop the camera and clean up
mi_stopTransport(camera);
mi_CloseCameras();
free(pCameraBuff);

// The following code demonstrates USB device removal notification. First,
// define the function that will be called when the USB device is removed.
mi_u32 miDevCallBack (HWND hwnd, _mi_camera_t *pCamera, mi_u32 Flags) {
if (Flags & MI_DEVEVENT_REMOVAL) {
MessageBox(hwnd, TEXT("Device Removed"), TEXT("Device Status"), MB_OK);
}
return MI_CAMERA_SUCCESS;
}

// Next, enable USB device removal notification. The call back function
// is called on removal. NOTE: MIUSB2.SYS driver version 5.1.0.3505
// or higher must be used.
retVal = mi_EnableDeviceNotification(myhWnd, pCamera, &miDevCallBack);
if (retVal == MI_CAMERA_SUCCESS) {
// Notification initialization was successfully enabled!
}
. . .
// When no longer needed or when shutting down the camera, disable
// device notification.
retVal = mi_DisableDeviceNotification(myhWnd, pCamera);