如何理解Sensor架构 - 华清远见 - 图文 下载本文

struct hw_device_t** device);

static int sensors__get_sensors_list(struct sensors_module_t* module, struct sensor_t const** list) {

*list = sSensorList;

return ARRAY_SIZE(sSensorList); }

static struct hw_module_methods_t sensors_module_methods = { .open = open_sensors };

const struct sensors_module_t HAL_MODULE_INFO_SYM = { .common = {

.tag = HARDWARE_MODULE_TAG, .version_major = 1, .version_minor = 0,

.id = SENSORS_HARDWARE_MODULE_ID,

.name = \.author = \.methods = &sensors_module_methods, },

.get_sensors_list = sensors__get_sensors_list };

static int open_sensors(const struct hw_module_t* module, const char* name, struct hw_device_t** device)

{

return init_nusensors(module, device); //待后面讲解 }

2.6.6 struct sensors_poll_device_t 实现

实现代码位于:/hardware/mychip/sensor/st/nusensors.cpp

从上面的代码中可以看出,当调用init_nusensors时,它将返回sensors_poll_device_t,然后就可以调用sensors_poll_device_t 的以下方法进行相关操作: 1) activate 2) setDelay 3) poll

6.1) struct sensors_poll_context_t 定义 [cpp]

view plaincopystruct sensors_poll_context_t {

struct sensors_poll_device_t device; // must be first sensors_poll_context_t(); ~sensors_poll_context_t();

int activate(int handle, int enabled); int setDelay(int handle, int64_t ns);

int pollEvents(sensors_event_t* data, int count); private: enum { light = 0, proximity = 1, mma = 2,

akm = 3, gyro = 4, numSensorDrivers, numFds, };

static const size_t wake = numFds - 1; static const char WAKE_MESSAGE = 'W'; struct pollfd mPollFds[numFds]; int mWritePipeFd;

SensorBase* mSensors[numSensorDrivers]; int handleToDriver(int handle) const { switch (handle) { case ID_A: return mma; case ID_M: case ID_O: return akm; case ID_P: return proximity; case ID_L: return light; case ID_GY: return gyro; }

return -EINVAL; } }

6.2) init_nusensors 实现 [cpp]

view plaincopyint init_nusensors(hw_module_t const* module, hw_device_t** device) {

int status = -EINVAL;

sensors_poll_context_t *dev = new sensors_poll_context_t(); memset(&dev->device, 0, sizeof(sensors_poll_device_t)); dev->device.common.tag = HARDWARE_DEVICE_TAG; dev->device.common.version = 0;

dev->device.common.module = const_cast(module); dev->device.common.close = poll__close; dev->device.activate = poll__activate; dev->device.setDelay = poll__setDelay; dev->device.poll = poll__poll; *device = &dev->device.common; status = 0; return status; }

由以上代码可见,sensors_poll_device_t的activate、setDelay和poll的实现函数分别为: (1) poll__activate (2) poll__setDelay

(3) poll__poll

下面讲解以上三个关键函数的实现

6.3) struct sensors_poll_context_t 的实现 [cpp]

view plaincopysensors_poll_context_t::sensors_poll_context_t() {

mSensors[light] = new LightSensor();

mPollFds[light].fd = mSensors[light]->getFd(); mPollFds[light].events = POLLIN; mPollFds[light].revents = 0;

mSensors[proximity] = new ProximitySensor();

mPollFds[proximity].fd = mSensors[proximity]->getFd(); mPollFds[proximity].events = POLLIN; mPollFds[proximity].revents = 0;

mSensors[mma] = new MmaSensor(); //下面MmmaSensor为例进行分析 mPollFds[mma].fd = mSensors[mma]->getFd(); mPollFds[mma].events = POLLIN; mPollFds[mma].revents = 0; mSensors[akm] = new AkmSensor();

mPollFds[akm].fd = mSensors[akm]->getFd(); mPollFds[akm].events = POLLIN; mPollFds[akm].revents = 0;

mSensors[gyro] = new GyroSensor();

mPollFds[gyro].fd = mSensors[gyro]->getFd();