⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 camtest.cpp

📁 Microsoft WinCE 6.0 BSP FINAL release source code for use with the i.MX27ADS TO2 WCE600_FINAL_MX27_S
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//-----------------------------------------------------------------------------
//
//  File:  CamTest.cpp
//
//  This file contains test code for the CAM driver
//
//------------------------------------------------------------------------------
//
//  Copyright (C) 2006, Freescale Semiconductor, Inc. All Rights Reserved.
//  THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
//  AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------

#include <windows.h>
#include <Devload.h>
#include "camera.h"
#include "camera_ext.h"
#include "main.h"
#include "globals.h"
#include "bsp.h"

#ifndef BSP_MX27
#include "display_vf.h"
#endif

//-----------------------------------------------------------------------------
// External Functions


//-----------------------------------------------------------------------------
// External Variables
extern HANDLE hCamera;
extern HANDLE hPreviewPin;
extern HANDLE hCapturePin;
extern HANDLE hStillPin;
extern HANDLE hFrameRateThread;
extern HANDLE hFrameRateThreadShutdown;
extern HANDLE hCaptureMsgQ;
extern HANDLE hPreviewMsgQ;
extern HANDLE hStillMsgQ;

#ifndef BSP_MX27
extern PCSP_IPU_REGS g_pIPU;
#endif

//-----------------------------------------------------------------------------
// Defines
#define CAM_ZONE_INFO                  0
#define CAM_ZONE_ERROR                 0
#define CAM_ZONE_FUNCTION              0

#define CAM_TEST_FUNCTION_ENTRY() \
    g_pKato->Log(CAM_ZONE_FUNCTION, (TEXT("++%s\r\n"), __WFUNCTION__))
#define CAM_TEST_FUNCTION_EXIT() \
    g_pKato->Log(CAM_ZONE_FUNCTION, (TEXT("--%s\r\n"), __WFUNCTION__))


//-----------------------------------------------------------------------------
// Types

typedef enum CAMERA_STATE
{
    CAMERA_STATE_PAUSE,
    CAMERA_STATE_RUN,
    CAMERA_STATE_STOP
};

typedef enum CAMERA_RESOLUTION
{
    VGA,
    CIF,
    QVGA,
    QCIF,
    QQVGA
};

//-----------------------------------------------------------------------------
// Global Variables

//-----------------------------------------------------------------------------
// Local Variables
static UINT8 int_count = 0;
static CS_STREAM_DESCRIPTOR captureStreamDesc;
static CS_STREAM_DESCRIPTOR previewStreamDesc;
static CS_STREAM_DESCRIPTOR stillStreamDesc;
static MSGQUEUEOPTIONS msgOptions;
static CSPROPERTY_STREAMEX_S CsPropStreamEx;

HDC g_hDisplay;
#ifndef BSP_MX27
DISPLAY_CHARACTERISTIC g_displayCharacteristics;
#endif
static DWORD g_dwPanelWidth, g_dwPanelHeight;
static WORD g_dwPanelBPP;

static CSPROPERTY inputIOCTL;
static CSSTATE state;
static DWORD gBytesTransferred;

static PCS_DATAFORMAT_VIDEOINFOHEADER g_pCapDataFormat;

static UINT8 *pCaptureBuffer = NULL;
static UINT8 *pStillBuffer = NULL;

static UINT32 bufferSize;
static void *FrameBufferVirtualAddr;

static ULONG gCurrentIndex = 0;

static PCSMULTIPLE_ITEM g_pPreviewDataRanges, g_pCaptureDataRanges, g_pStillDataRanges;
static UINT32 g_iNumCaptureDataRanges, g_iNumStillDataRanges, g_iNumPreviewDataRanges;
static BOOL g_bKillFrameRateThread = FALSE;

//-----------------------------------------------------------------------------
// Local Functions
#ifndef BSP_MX27
static void StillCaptureAndDisplay();
#else
static void StillPreviewAndDisplay();
#endif
static BOOL CaptureISR(DWORD);
static BOOL PreviewISR(DWORD);
static void dumpRegisters();
static BOOL CaptureSetState(CAMERA_STATE csNewState);
static BOOL PreviewSetState(CAMERA_STATE csNewState);
static BOOL StillSetState(CAMERA_STATE csNewState);
static void FrameRateThread();
static void GetPanelDimensions();
static void DrawToFrameBuffer(void *srcBuf, UINT32 srcWidth, UINT32 srcHeight, RECT *dest);
static void ReadPinDataRanges(UINT32 pinId);
static BOOL GetDataFormat(UINT32 pinId, CAMERA_RESOLUTION res, PCS_DATARANGE_VIDEO pDataFormatMatched);
static BOOL Zoom(UINT32 zoomVal);
static BOOL ChangeOffset(PCS_DATAFORMAT_VIDEOINFOHEADER pDataFormat);
static BOOL ChangeRotation(DWORD tiltVal);
static BOOL ChangeFlipping(CS_VideoControlFlags flags);


//------------------------------------------------------------------------------
//
// Function: CameraInit
//
// This function initializes the camera for testing.  The following tasks
// are performed:
//      - Retrieving panel dimensions, so that we can draw directly
//        to the framebuffer during testing.
//      - Creating message queues for each pin instance
//      - Creating a pin instance for PREVIEW, CAPTURE, and STILL.
//      - Reading the Datarange information for each pin into
//        a global variable.
//
// Parameters:
//      uiMsg
//           [in] Ignored.
//
//      tpParam
//           [in] Ignored.
//
//      lpFTE
//           [in] Ignored.
//
// Returns:
//      Specifies if the test passed (TPR_PASS), failed (TPR_FAIL), or was
//      skipped (TPR_SKIP).
//
//------------------------------------------------------------------------------
TESTPROCAPI CameraInit(UINT uMsg, TPPARAM tpParam, LPFUNCTION_TABLE_ENTRY lpFTE)
{
    CSPROPERTY csPropIn;
    CSALLOCATOR_FRAMING csAllocatorFramingOut;

    CAM_TEST_FUNCTION_ENTRY();

    // Validate that the shell wants the test to run
    if (uMsg != TPM_EXECUTE)
    {
        return TPR_NOT_HANDLED;
    }

    // Retrieve display panel size from registry
    GetPanelDimensions();            

    bufferSize = g_dwPanelWidth * g_dwPanelHeight * (g_dwPanelBPP/8);

#ifndef BSP_MX27
    // Retrieve 
    if (ExtEscape(g_hDisplay, GETVFRAMEPHYSICAL, 0,
            NULL, sizeof(UCHAR *), (LPSTR) &FrameBufferVirtualAddr) <= 0)
    {
        g_pKato->Log(CAM_ZONE_ERROR, (TEXT("ConfigDisplay: Error getting framebuf.\r\n")));
        return FALSE;
    }
#else
    PHYSICAL_ADDRESS phyAddr;

    phyAddr.QuadPart = IMAGE_SHARE_FRAMEBUFFER_RAM_PA_START;

    FrameBufferVirtualAddr = (UINT8 *)MmMapIoSpace(phyAddr, bufferSize, FALSE);

    if (FrameBufferVirtualAddr == NULL)
    {
        g_pKato->Log(CAM_ZONE_ERROR, (TEXT("ConfigDisplay: Frame buffer VirtualAlloc failed!\r\n")));
        return FALSE;
    }
  
#endif
    g_pKato->Log(CAM_ZONE_ERROR,
        (TEXT("FrameBufferVirtualAddr: %x\n bufferSize: %x\n width: %x\n height: %x\n bpp: %x\r\n")),
        FrameBufferVirtualAddr, bufferSize, g_dwPanelWidth, g_dwPanelHeight, g_dwPanelBPP);

    // Create message queue options for capture pin
    msgOptions.dwSize = sizeof(MSGQUEUEOPTIONS);
    msgOptions.dwFlags = MSGQUEUE_ALLOW_BROKEN;
    msgOptions.dwMaxMessages = 0;
    msgOptions.cbMaxMessage  = sizeof(CS_MSGQUEUE_BUFFER);
    msgOptions.bReadAccess = TRUE;


    // Open msg queue for Capture Pin
    hCaptureMsgQ = CreateMsgQueue(NULL, &msgOptions);
    if(!hCaptureMsgQ)
    {
        RETAILMSG(CAM_ZONE_ERROR, (TEXT("CameraTest1: CreateMsgQueue for capture pin failed!\r\n")));
        return 0;
    }

    RETAILMSG(CAM_ZONE_ERROR, (TEXT("CameraTest1: CreateMsgQueue for capture pin done!\r\n")));

    // Open msg queue for Preview Pin
    hPreviewMsgQ = CreateMsgQueue(NULL, &msgOptions);
    if(!hPreviewMsgQ)
    {
        RETAILMSG(CAM_ZONE_ERROR, (TEXT("CameraTest1: CreateMsgQueue for preview pin failed!\r\n")));
        return 0;
    }

    // Open msg queue for Still Pin
    hStillMsgQ = CreateMsgQueue(NULL, &msgOptions);
    if(!hStillMsgQ)
    {
        RETAILMSG(CAM_ZONE_ERROR, (TEXT("CameraTest1: CreateMsgQueue for still pin failed!\r\n")));
        return 0;
    }

    // create capture pin instance
    CsPropStreamEx.hMsgQueue = hCaptureMsgQ;
    CsPropStreamEx.CsPin.PinId = 0;
    CsPropStreamEx.CsPin.Property.Set = CSPROPSETID_StreamEx;
    CsPropStreamEx.CsPin.Property.Id = CSPROPERTY_STREAMEX_INIT;
    CsPropStreamEx.CsPin.Property.Flags = CSPROPERTY_TYPE_SET;

    DeviceIoControl(hCapturePin,                   // file handle to the driver
                    IOCTL_STREAM_INSTANTIATE,      // I/O control code
                    &CsPropStreamEx,               // in buffer
                    sizeof(CSPROPERTY_STREAMEX_S), // in buffer size
                    NULL,                          // out buffer
                    0,                             // out buffer size
                    &gBytesTransferred,            // number of bytes returned
                    NULL);                         // ignored (=NULL)

    // create preview pin instance
    CsPropStreamEx.hMsgQueue = hPreviewMsgQ;
    CsPropStreamEx.CsPin.PinId = 2;
    CsPropStreamEx.CsPin.Property.Set = CSPROPSETID_StreamEx;
    CsPropStreamEx.CsPin.Property.Id = CSPROPERTY_STREAMEX_INIT;
    CsPropStreamEx.CsPin.Property.Flags = CSPROPERTY_TYPE_SET;

    DeviceIoControl(hPreviewPin,                   // file handle to the driver
                    IOCTL_STREAM_INSTANTIATE,      // I/O control code
                    &CsPropStreamEx,               // in buffer
                    sizeof(CSPROPERTY_STREAMEX_S), // in buffer size
                    NULL,                          // out buffer
                    0,                             // out buffer size
                    &gBytesTransferred,            // number of bytes returned
                    NULL);                         // ignored (=NULL)

    // create still pin instance
    CsPropStreamEx.hMsgQueue = hStillMsgQ;
    CsPropStreamEx.CsPin.PinId = 1;
    CsPropStreamEx.CsPin.Property.Set = CSPROPSETID_StreamEx;
    CsPropStreamEx.CsPin.Property.Id = CSPROPERTY_STREAMEX_INIT;
    CsPropStreamEx.CsPin.Property.Flags = CSPROPERTY_TYPE_SET;

    DeviceIoControl(hStillPin,                     // file handle to the driver
                    IOCTL_STREAM_INSTANTIATE,      // I/O control code
                    &CsPropStreamEx,               // in buffer
                    sizeof(CSPROPERTY_STREAMEX_S), // in buffer size
                    NULL,                          // out buffer
                    0,                             // out buffer size
                    &gBytesTransferred,            // number of bytes returned
                    NULL);                         // ignored (=NULL)

    // Get Basic support
    csPropIn.Set = CSPROPSETID_Connection;
    csPropIn.Id = CSPROPERTY_CONNECTION_ALLOCATORFRAMING;
    csPropIn.Flags = CSPROPERTY_TYPE_BASICSUPPORT;

    DeviceIoControl(hCapturePin,                   // file handle to the driver
                    IOCTL_CS_PROPERTY,             // I/O control code
                    &csPropIn,                     // in buffer
                    sizeof(CSPROPERTY),            // in buffer size
                    &csAllocatorFramingOut,        // out buffer
                    sizeof(CSALLOCATOR_FRAMING),   // out buffer size
                    &gBytesTransferred,            // number of bytes returned
                    NULL);                         // ignored (=NULL)


    // Check the basic info
    g_pKato->Log(CAM_ZONE_ERROR, (TEXT("CameraInit: Basic Info - Buffer mode = %x, Frames = %x, Max FrameSize = %x\r\n")),
        csAllocatorFramingOut.RequirementsFlags, csAllocatorFramingOut.Frames, csAllocatorFramingOut.FrameSize);

    // Use test pattern instead of capturing from sensor
    // UseCsiTestPattern();

    // Read Capture pin DataRanges
    ReadPinDataRanges(0);

    // Read Still pin DataRanges
    ReadPinDataRanges(1);

    // Read Preview pin DataRanges
    ReadPinDataRanges(2);

    gCurrentIndex = 2;

    // Kick off thread to retrieve frame rate
    hFrameRateThread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)FrameRateThread, NULL, 0, NULL);

    if (hFrameRateThread == NULL)
    {
        g_pKato->Log(CAM_ZONE_ERROR, (TEXT("CameraTestPreview: CreateThread failed!\r\n")));
        return TPR_FAIL;
    }
    else
    {
        g_pKato->Log(CAM_ZONE_ERROR, (TEXT("CameraTestPreview: create encoding buffer worker thread success\r\n")));
    }

    return TPR_PASS;
}

//------------------------------------------------------------------------------
//
// Function: CameraTestCapture
//
// This function test the functionality of camera capture.  It changes 
// the data format for the CAPTURE pin to a size that can be drawn to 
// the display.  It then allocates buffers for the pin.  Finally, it
// kicks off an ISR to capture images and display them.
//
// Parameters:
//      uiMsg
//           [in] Ignored.
//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -