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

📄 cameradevice.cpp

📁 freescale i.mx31 BSP CE5.0全部源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//-----------------------------------------------------------------------------
//
// Copyright (C) 2003-2004, MOTOROLA, INC. All Rights Reserved
// THIS SOURCE CODE IS CONFIDENTIAL AND PROPRIETARY AND MAY NOT
// BE USED OR DISTRIBUTED WITHOUT THE WRITTEN PERMISSION OF
// MOTOROLA, INC.
//
//------------------------------------------------------------------------------
//
//  Copyright (C) 2004, Freescale Semiconductor, Inc. All Rights Reserved
//  THIS SOURCE CODE IS CONFIDENTIAL AND PROPRIETARY AND MAY NOT
//  BE USED OR DISTRIBUTED WITHOUT THE WRITTEN PERMISSION OF
//  Freescale Semiconductor, Inc.
//
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//
//  File:  CameraDevice.cpp
//
//  CameraDevice Class Implementation 
//
//------------------------------------------------------------------------------

#include <windows.h>
#include <ceddk.h>

#include "mxarm11.h"
#include "CsiClass.h"
#include "PrpClass.h"
#include "camera.h"
#include "cameradbg.h"
#include "CameraDriver.h"
#include "PinDriver.h"

#include "CameraProps.h"


//------------------------------------------------------------------------------
// External Functions
extern void BSPGetSensorFormat(DWORD *);

//------------------------------------------------------------------------------
// External Variables


//------------------------------------------------------------------------------
// Defines


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


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


//------------------------------------------------------------------------------
// Local Variables


//------------------------------------------------------------------------------
// Local Functions


//------------------------------------------------------------------------------
//
// Function: CCameraDevice
//
// CCameraDevice constructor.
//
// Parameters:
//      None.
//
// Returns:
//      None.
//
//------------------------------------------------------------------------------
CCameraDevice::CCameraDevice()
{

    m_hStream = NULL;
    m_hCallerProcess = NULL;
    
    InitializeCriticalSection(&m_csDevice);
    
    m_ulCTypes = 0;
    memset(m_StrmInstances, 0x0, sizeof(STREAM_INSTANCES)*MAX_SUPPORTED_PINS);
    memset(m_DevProps, 0x0, sizeof(DEV_PROPERTY)* NUM_PROPERTY_ITEMS);

    m_pCsi = NULL;
    m_pPrp = NULL;
    m_bCameraEncConfig = FALSE;
    m_bCameraVfConfig = FALSE;

    m_bDirectDisplay = FALSE;
    m_bDirectCapture = FALSE;

    m_bFlipVertical = FALSE;
    m_bFlipHorizontal = FALSE;
    m_bRotate = FALSE;

    m_StillPinInherited = STILL;
}


//------------------------------------------------------------------------------
//
// Function: ~CCameraDevice
//
// CCameraDevice destructor.
//
// Parameters:
//      None.
//
// Returns:
//      None.
//
//------------------------------------------------------------------------------
CCameraDevice::~CCameraDevice()
{
    DeleteCriticalSection(&m_csDevice);
    if (NULL != m_hStream)
        DeactivateDevice(m_hStream);

    DeleteCriticalSection(&m_csDevice);

    CameraDeinit();

    if (NULL != m_hStream)
    {
        DeactivateDevice(m_hStream);
    }

    if (m_pPrp != NULL)
    {
        if (!VirtualFree((void*)m_pPrp, 0, MEM_RELEASE))
        {
            DEBUGMSG(ZONE_ERROR, (TEXT("%s : m_pPrp Deallocation failed\r\n"), __WFUNCTION__));
        }
        m_pPrp = NULL;
    }

    if (m_pCsi != NULL)
    {
        if (!VirtualFree((void*)m_pCsi, 0, MEM_RELEASE))
        {
            DEBUGMSG(ZONE_ERROR, (TEXT("$s : m_pCsi Deallocation failed\r\n"), __WFUNCTION__));
        }
        m_pCsi = NULL;
    }
}

//------------------------------------------------------------------------------
//
// Function: Initialize
//
// CCameraDevice initializer.
//
// Parameters:
//      context
//          [in] Device context.
//
// Returns:
//      TRUE if initialization is successful, otherwise return FALSE.
//
//------------------------------------------------------------------------------
bool CCameraDevice::Initialize(PVOID context)
{
    CAM_FUNCTION_ENTRY();
    
    m_hStream = ActivateDeviceEx(PIN_REG_PATH, 0, 0, reinterpret_cast<LPVOID>(this));
    DEBUGMSG(ZONE_INIT, (_T("%s: getlasterror is %d.\r\n"), __WFUNCTION__, GetLastError()));
    
    if (NULL == m_hStream)
    {
        DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T("%s: ActivateDevice on Pin failed\r\n"), __WFUNCTION__));
        return false;
    }

    DEBUGMSG(ZONE_INIT, (_T("%s: ActivateDevice on Pin OK\r\n") __WFUNCTION__));

#ifdef SUPPORT_3_PINS
    m_ulCTypes = 3;
#else
    m_ulCTypes = 2;
#endif

    //VideoProcAmp

    //CameraControl
    m_DevProps[ENUM_ZOOM].ulCurrentValue           = ZoomDefault;
    m_DevProps[ENUM_ZOOM].ulDefaultValue           = ZoomDefault;
    m_DevProps[ENUM_ZOOM].pRangeNStep              = &ZoomRangeAndStep[0];
    m_DevProps[ENUM_ZOOM].ulFlags                  = CSPROPERTY_CAMERACONTROL_FLAGS_AUTO;
    m_DevProps[ENUM_ZOOM].ulCapabilities           = CSPROPERTY_CAMERACONTROL_FLAGS_MANUAL|CSPROPERTY_CAMERACONTROL_FLAGS_AUTO;
    m_DevProps[ENUM_ZOOM].fSetSupported            = CameraControlProperties[ENUM_ZOOM-NUM_VIDEOPROCAMP_ITEMS].SetSupported;
    m_DevProps[ENUM_ZOOM].fGetSupported            = CameraControlProperties[ENUM_ZOOM-NUM_VIDEOPROCAMP_ITEMS].GetSupported;
    m_DevProps[ENUM_ZOOM].pCsPropValues            = &ZoomValues;

    m_DevProps[ENUM_TILT].ulCurrentValue           = TiltDefault;
    m_DevProps[ENUM_TILT].ulDefaultValue           = TiltDefault;
    m_DevProps[ENUM_TILT].pRangeNStep              = &TiltRangeAndStep[0];
    m_DevProps[ENUM_TILT].ulFlags                  = CSPROPERTY_CAMERACONTROL_FLAGS_AUTO;
    m_DevProps[ENUM_TILT].ulCapabilities           = CSPROPERTY_CAMERACONTROL_FLAGS_MANUAL|CSPROPERTY_CAMERACONTROL_FLAGS_AUTO;
    m_DevProps[ENUM_TILT].fSetSupported            = CameraControlProperties[ENUM_TILT-NUM_VIDEOPROCAMP_ITEMS].SetSupported;
    m_DevProps[ENUM_TILT].fGetSupported            = CameraControlProperties[ENUM_TILT-NUM_VIDEOPROCAMP_ITEMS].GetSupported;
    m_DevProps[ENUM_TILT].pCsPropValues            = &TiltValues;

    // Video Format initialization
#ifdef SUPPORT_3_PINS
    m_PinVideoFormat[PREVIEW].categoryGUID         = PINNAME_VIDEO_PREVIEW;
    m_PinVideoFormat[PREVIEW].ulAvailFormats       = 3;
    m_PinVideoFormat[PREVIEW].pCsDataRangeVideo[0] = &DCAM_StreamMode_5; // RGB888
//    m_PinVideoFormat[PREVIEW].pCsDataRangeVideo[0] = &DCAM_StreamMode_3; // RGB565
    m_PinVideoFormat[PREVIEW].pCsDataRangeVideo[1] = &DCAM_StreamMode_0; // YUYV
    m_PinVideoFormat[PREVIEW].pCsDataRangeVideo[2] = &DCAM_StreamMode_2; // UYVY
#endif

    m_PinVideoFormat[CAPTURE].categoryGUID         = PINNAME_VIDEO_CAPTURE;
    m_PinVideoFormat[CAPTURE].ulAvailFormats       = 2;
    // TODO: Switch back
    m_PinVideoFormat[CAPTURE].pCsDataRangeVideo[0] = &DCAM_StreamMode_5; // RGB888
//    m_PinVideoFormat[CAPTURE].pCsDataRangeVideo[0] = &DCAM_StreamMode_0; // YUYV
    m_PinVideoFormat[CAPTURE].pCsDataRangeVideo[1] = &DCAM_StreamMode_1; // YUV444

    m_PinVideoFormat[STILL].categoryGUID           = PINNAME_VIDEO_STILL;
    m_PinVideoFormat[STILL].ulAvailFormats         = 2;
    m_PinVideoFormat[STILL].pCsDataRangeVideo[0]   = &DCAM_StreamMode_5; // RGB888
//    m_PinVideoFormat[STILL].pCsDataRangeVideo[0]   = &DCAM_StreamMode_3; // RGB565
    m_PinVideoFormat[STILL].pCsDataRangeVideo[1]   = &DCAM_StreamMode_2; // UYVY

    // Video Control Caps
    // TODO : Set the following caps to 0 for this null DD.
#ifdef SUPPORT_3_PINS
    m_PinVideoCaps[PREVIEW].ulVideoControlCaps     = DefaultVideoControlCaps[PREVIEW];
#endif
    m_PinVideoCaps[CAPTURE].ulVideoControlCaps     = DefaultVideoControlCaps[CAPTURE];
    m_PinVideoCaps[STILL].ulVideoControlCaps       = DefaultVideoControlCaps[STILL];

    if (!CameraInit())
    {
        return false;
    }

    CAM_FUNCTION_EXIT();

    return true;
}

//-----------------------------------------------------------------------------
//
// Function: CameraInit
//
// This function initializes the Camera Sensor Interface and
// Preprocessor (Image Converter) modules.
//
// Parameters:
//      None.
//
// Returns:
//      TRUE if success
//      FALSE if failure
//
//-----------------------------------------------------------------------------
BOOL CCameraDevice::CameraInit()
{
    CAM_FUNCTION_ENTRY();

    // Create (and initialize) preprocessor class object
    if(m_pPrp == NULL)
    {
        m_pPrp = new PrpClass;
    }
    else
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("%s: Failed creating PrpClass object!\r\n")));
    }

    // Create (and initialize) CSI class object
    if(m_pCsi == NULL)
    {
        m_pCsi = new CsiClass;
    }
    else
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("%s: Failed creating CsiClass object!\r\n")));
    }

    CAM_FUNCTION_EXIT();

    return TRUE;
}

//-----------------------------------------------------------------------------
//
// Function: CameraDeinit
//
// This function deinitializes the Camera Sensor Interface and
// Image Converter modules.
//
// Parameters:
//      None.
//
// Returns:
//      None.
//
//-----------------------------------------------------------------------------
void CCameraDevice::CameraDeinit()
{
    CAM_FUNCTION_ENTRY();

    // TODO: Remember that the Preprocessor may still be in use, 
    // even if Camera is closed.  When we create a preprocessor driver, 
    // there must be a mechanism for determining between these two 
    // drivers whether the IC is still in use.
    delete(m_pPrp);

    delete(m_pCsi);

    CAM_FUNCTION_EXIT();
}

//-----------------------------------------------------------------------------
//
// Function: CameraConfig
//
// This function configures the camera sensor and preprocessing module.
//
// Parameters:
//      None.
//
// Returns:
//      TRUE if success
//      FALSE if failure
//
//-----------------------------------------------------------------------------
BOOL CCameraDevice::CameraConfig()
{
    ULONG PinID;
    prpEncConfigData prpEncConfig;
    prpVfConfigData prpVfConfig;
    prpInputFormat inFormat;
    prpFrameSize inFrameSize;
    DWORD Format;
    DWORD sensorFormat;
    csiSensorOutputFormat sensorOutputFormat;
    csiSensorOutputResolution sensorOutputResolution;
    WORD bitDepth;
    static BOOL displayInitialized = FALSE;

    CAM_FUNCTION_ENTRY();

    // If no changes to the camera settings have been made
    // since the last configuration, we do not need to
    // go through configuration steps, so we bail out.
    if ((m_bCameraEncConfig || (m_StrmInstances[CAPTURE].pPinDev == NULL))
        && (m_bCameraVfConfig || (m_StrmInstances[PREVIEW].pPinDev == NULL)))
    {
        return TRUE;
    }

    //Stop all running pins before config
    for (PinID = 0; PinID <= (MAX_SUPPORTED_PINS - 1); PinID++)
    {
        if(m_StrmInstances[PinID].pPinDev != NULL && m_StrmInstances[PinID].pPinDev->m_CsState != CSSTATE_STOP)
        {
            DEBUGMSG(ZONE_ERROR, (TEXT("%s: Pins are running. Stop all Pin before config camera!\r\n"), __WFUNCTION__));
            return FALSE;
        }
    }

    if ((m_StrmInstances[PREVIEW].pPinDev == NULL) && (m_StrmInstances[CAPTURE].pPinDev == NULL))
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("%s: No Pins are active!  Aborting camera configuration.\r\n"), __WFUNCTION__));
        return FALSE;
    }

    // Get display characteristics for PrpClass.
    // Only perform once.
    if (!displayInitialized)
    {
        m_pPrp->PrpInitDisplayCharacteristics();
        displayInitialized = TRUE;
    }

    //----------------------------------------------------
    // Configure camera sensor and CSI
    //----------------------------------------------------

⌨️ 快捷键说明

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