前言
在集成eis算法时,我们需要获取sensor的数据:如gyro,ois,accelerate等传感器的数据。
本文以gyro数据为例子。
1.初始化
1.1 获取服务
void GyroReader::init()
{
LOGE("zcf_g:E");
//步骤1:获取sensor服务
this->smgr = ISensorManager::getService();
if(smgr == nullptr) {
LOGE("zcf_g:Fail to get sensor manager!");
this->deinit();
}
//打印所有支持的sensor信息
this->printSensors();
Result latestResult;
//步骤2:获取相应的sensor:这里以gyro为例子
this->smgr->getDefaultSensor(SensorType::GYROSCOPE,
[&](const SensorInfo& sensorinfo,const Result & result){
this->info = sensorinfo;
latestResult = result;
});
//如果想获取accelerate的:
/*
this->smgr->getDefaultSensor(:SensorType::ACCELEROMETER,
[&](const SensorInfo& sensorinfo,const Result & result){
this->info = sensorinfo;
latestResult = result;
});
*/
if(latestResult != Result::OK) {
LOGE("zcf_g:getDefaultSensor gyro failed !");
return;
}
LOGE("[%s]: name: %s, version: 0x%x",
this->iinfo.typeAsString.c_str(), this->iinfo.name.c_str(), this->iinfo.version);
LOGE("[%s]: minDelay: %d, maxDelay: %d",
this->iinfo.typeAsString.c_str(), this->iinfo.minDelay, this->iinfo.maxDelay);
LOGE("[%s]: handle = 0x%x, flags: 0x%x",
this->iinfo.typeAsString.c_str(), this->iinfo.sensorHandle, this->iinfo.flags);
LOGE("sample_rate = ",USEC_PER_SEC/info.minDelay):
/*callback object(sensorCb) is freed by IEventqueue implemter, don't delete in deinit*/
this->sensorCb = new SensorEventCallback();
this->sensorCb->gyroReader_ptr = (void*)this;
//步骤3:创建EventQueue
this->smgr->createEventQueue(this->sensorCb,
[&](const auto& queue,const auto& result)->void{
this->eventQueue = queue;
latestResult = result;
});
if(latestResult != Result::OK){
LOGE("zcf_g:createEventQueue failed!!!");
}
//步骤4:enable gyro sensor
this->eventQueue->enableSensor(info.sensorHandle,info.minDelay,0);
LOGE("zcf_g:X");
}
1.2 创建sensorCallback
class SensorEventCallback : public IEventQueueCallback
{
public:
SensorEventCallback()
{
LOGE("zcf_g:SensorEventCallback 构造函数");
}
~SensorEventCallback()
{
LOGE("zcf_g:SensorEventCallback 析构函数");
}
Return<void> onEvent(const Event &e)
{
GyroReader * gr_obj = (GyroReader *)this->gyroReader_ptr;
if(gr_obj != nullptr) {
gr_obj->processEvent(e);//这里调用processEvent去处理事件
}else {
LOGE("zcf_g:gr_obj is null!")
}
return Void();
}
public:
void* gyroReader_ptr;//GyroReader类的指针
};
1.3 processEvent
void GyroReader::processEvent(const Event& e)
{
if(!e.sensorHandle){
LOGE("zcf_g:invalid sensor handle, ignore");
return;
}
switch(e.sensorType)
{
//这里只打印数据,有必要的话,可以创建数组存储起来,后续使用
case SensorType::GYROSCOPE:
LOGE("zcf_gd:Event type - gyro!");
LOGE("zcf_gd:Gyro: evt_ts:%lld us x:%f y:%f z:%f",
e.timestamp,
e.u.uncal.x,
e.u.uncal.y,
e.u.uncal.z);
break;
case SensorType::ACCELEROMETER:
LOGE("zcf_gd:Event type - Accel!");
LOGE("zcf_gd:Accel: evt_timestamp:%lld us x:%f y:%f z:%f",
e.timestamp,
e.u.uncal.x,
e.u.uncal.y,
e.u.uncal.z);
break;
default:
LOGE("zcf_gd:Event for sensor(type: %d) not interested", e.sensorType);
break;
}
}
1.4 编译
hardware/qcom/camera/QCamera2/Android.mk
--- a/hardware/qcom/camera/QCamera2/Android.mk
+++ b/hardware/qcom/camera/QCamera2/Android.mk
@@ -40,7 +40,7 @@ LOCAL_SRC_FILES += \
HAL3/QCamera3CropRegionMapper.cpp \
HAL3/QCamera3StreamMem.cpp
-LOCAL_CFLAGS := -Wall -Wextra -Werror
+LOCAL_CFLAGS := -Wall -Wextra
LOCAL_CFLAGS += -DFDLEAK_FLAG
LOCAL_CFLAGS += -DMEMLEAK_FLAG
#HAL 1.0 source
@@ -68,7 +68,8 @@ LOCAL_SRC_FILES += \
util/QCameraExtZoomTranslator.cpp \
util/QCameraPprocManager.cpp \
util/QCameraBokeh.cpp \
- util/QCameraClearSight.cpp
+ util/QCameraClearSight.cpp \
+ HAL/GyroReader.cpp
endif
# System header file path prefix
LOCAL_SHARED_LIBRARIES += libmmcamera_interface libmmjpeg_interface libui libcamera_metadata
LOCAL_SHARED_LIBRARIES += libqdMetaData libqservice libbinder
LOCAL_SHARED_LIBRARIES += libcutils libdl libhal_dbg
+LOCAL_SHARED_LIBRARIES += libhidlbase libhidltransport android.frameworks.sensorservice@1.0
ifeq ($(IS_QC_BOKEH_SUPPORTED),true)
LOCAL_SHARED_LIBRARIES += libdualcameraddm
LOCAL_CFLAGS += -DENABLE_QC_BOKEH
1.5 调用
hardware/qcom/camera/QCamera2/HAL/QCamera2HWI.h
--- a/hardware/qcom/camera/QCamera2/HAL/QCamera2HWI.h
+++ b/hardware/qcom/camera/QCamera2/HAL/QCamera2HWI.h
@@ -62,6 +62,8 @@ extern "C" {
#include "QCameraTrace.h"
+#include "GyroReader.h"
+
namespace qcamera {
#ifndef TRUE
@@ -863,6 +865,7 @@ private:
bool bDepthAFCallbacks;
bool m_bOptimizeCacheOps;
bool m_bNeedHalPP;
+ ISensorAPI::GyroReader gy;
};
}; // namespace qcamera
2 完整源码
hardware/qcom/camera/QCamera2/HAL/GyroReader.h
#ifndef GYRO_READ_H_
#define GYRO_READ_H_
extern "C" {
#include "mm_camera_dbg.h"
}
#include <android/frameworks/sensorservice/1.0/ISensorManager.h>
#include <android/frameworks/sensorservice/1.0/types.h>
//#include <hardware/sensors.h> // for sensor type strings
using ::android::frameworks::sensorservice::V1_0::ISensorManager;
using ::android::frameworks::sensorservice::V1_0::Result;
using ::android::frameworks::sensorservice::V1_0::IEventQueue;
using ::android::frameworks::sensorservice::V1_0::IEventQueueCallback;
using ::android::hardware::sensors::V1_0::Event;
using ::android::hardware::sensors::V1_0::SensorType;
using ::android::hardware::sensors::V1_0::SensorInfo;
using ::android::sp;
using ::android::hardware::Void;
using ::android::hardware::Return;
class SensorEventCallback;
namespace ISensorAPI {
class GyroReader
{
public:
GyroReader();
~GyroReader();
void init();
void deinit();
void processEvent(const Event& e);
void printSensors();
static constexpr int32_t USEC_PER_SEC = 1e6;
static constexpr int32_t NSEC_PER_SEC = 1e9;
private:
SensorInfo info;
sp<ISensorManager> smgr;
/*callback object(sensorCb) is freed by IEventqueue implemter, don't delete in deinit*/
//因此可以不用定义成智能指针:sp<SensorEventCallback > sensorCb;
SensorEventCallback * sensorCb;
sp<IEventQueue> eventQueue;
};
}
#endif /* GYRO_READ_H_ */
hardware/qcom/camera/QCamera2/HAL/GyroReader.cpp
#include "GyroReader.h"
using namespace ISensorAPI;
class SensorEventCallback : public IEventQueueCallback
{
public:
SensorEventCallback()
{
LOGE("zcf_g:SensorEventCallback 构造函数");
}
~SensorEventCallback()
{
LOGE("zcf_g:SensorEventCallback 析构函数");
}
Return<void> onEvent(const Event &e)
{
GyroReader * gr_obj = (GyroReader *)this->gyroReader_ptr;
if(gr_obj != nullptr) {
gr_obj->processEvent(e);
}else {
LOGE("zcf_g:gr_obj is null!")
}
return Void();
}
public:
void* gyroReader_ptr;
};
GyroReader::GyroReader()
{
this->init();
}
GyroReader::~GyroReader()
{
this->deinit();
}
void GyroReader::init()
{
LOGE("zcf_g:E");
//步骤1:获取sensor服务
this->smgr = ISensorManager::getService();
if(smgr == nullptr) {
LOGE("zcf_g:Fail to get sensor manager!");
this->deinit();
}
//打印所有支持的sensor信息
this->printSensors();
Result latestResult;
//步骤2:获取相应的sensor:这里以gyro为例子
this->smgr->getDefaultSensor(SensorType::GYROSCOPE,
[&](const SensorInfo& sensorinfo,const Result & result){
this->info = sensorinfo;
latestResult = result;
});
//如果想获取accelerate的:
/*
this->smgr->getDefaultSensor(:SensorType::ACCELEROMETER,
[&](const SensorInfo& sensorinfo,const Result & result){
this->info = sensorinfo;
latestResult = result;
});
*/
if(latestResult != Result::OK) {
LOGE("zcf_g:getDefaultSensor gyro failed !");
return;
}
LOGE("[%s]: name: %s, version: 0x%x",
this->iinfo.typeAsString.c_str(), this->iinfo.name.c_str(), this->iinfo.version);
LOGE("[%s]: minDelay: %d, maxDelay: %d",
this->iinfo.typeAsString.c_str(), this->iinfo.minDelay, this->iinfo.maxDelay);
LOGE("[%s]: handle = 0x%x, flags: 0x%x",
this->iinfo.typeAsString.c_str(), this->iinfo.sensorHandle, this->iinfo.flags);
LOGE("sample_rate = ",USEC_PER_SEC/info.minDelay):
/*callback object(sensorCb) is freed by IEventqueue implemter, don't delete in deinit*/
this->sensorCb = new SensorEventCallback();
this->sensorCb->gyroReader_ptr = (void*)this;
//步骤3:创建EventQueue
this->smgr->createEventQueue(this->sensorCb,
[&](const auto& queue,const auto& result)->void{
this->eventQueue = queue;
latestResult = result;
});
if(latestResult != Result::OK){
LOGE("zcf_g:createEventQueue failed!!!");
}
//步骤4:enable gyro sensor
this->eventQueue->enableSensor(info.sensorHandle,info.minDelay,0);
LOGE("zcf_g:X");
}
void GyroReader::deinit()
{
this->eventQueue->disableSensor(info.sensorHandle);
}
void GyroReader::processEvent(const Event& e)
{
if(!e.sensorHandle){
LOGE("zcf_g:invalid sensor handle, ignore");
return;
}
switch(e.sensorType)
{
case SensorType::GYROSCOPE:
LOGE("zcf_gd:Event type - gyro!");
LOGE("zcf_gd:Gyro: evt_ts:%lld us x:%f y:%f z:%f",
e.timestamp,
e.u.uncal.x,
e.u.uncal.y,
e.u.uncal.z);
break;
case SensorType::ACCELEROMETER:
LOGE("zcf_gd:Event type - Accel!");
LOGE("zcf_gd:Accel: evt_timestamp:%lld us x:%f y:%f z:%f",
e.timestamp,
e.u.uncal.x,
e.u.uncal.y,
e.u.uncal.z);
break;
default:
LOGE("zcf_gd:Event for sensor(type: %d) not interested", e.sensorType);
break;
}
}
void GyroReader::printSensors()
{
if(this->smgr.get() != nullptr){
this->smgr.get()->getSensorList([&](const auto &list,auto result){
if(result == Result::OK){
for(unsigned int i =0;i<list.size();i++){
LOGE("zcf_g:Sensor:%s",list[i].name.c_str());
}
}else{
LOGE("zcf_g:failed to get sensor list!");
}
});
}
}
网友评论