Android Sensor architecture-depth analysis of [turn]

This article is reproduced from:

1, Android sensor framework

Android4.0 system built up on the sensor support 13 species, which are: an acceleration sensor (Accelerometer), a magnetic sensor (magnetic field), a direction sensor (Orientation), gyroscopes (Gyroscope), ambient light sensor (Light), a pressure sensors (pressure), temperature sensor (temperature) and a distance sensor (Proximity) and the like.

The sensor system implemented Android includes the following components:

Between the portions of the organization chart below:

2, Sensor HAL layer interface

Google Sensor provides a unified interface to HAL, different hardware manufacturers need to achieve and accomplish specific hardware abstraction layer based on this interface, Android in Sensor Interface Definition of HAL in: hardware / libhardware / include / hardware / sensors.h

Definition of the type of sensor:

Definition of the structure of the sensor module are as follows:

The definition of the interface is actually an extension of the standard hardware modules hw_module_t, get_sensors_list adds a function for list acquisition sensor.

Sensor_t has a structure of any one of the sensor device are defined as follows:

Data from each sensor is represented by sensors_event_t structures, defined as follows:

Wherein, sensor is a sensor identifier, different sensors are employed to represent the union embodiment, sensors_vec_t data structure used to represent different sensors, sensors_vec_t defined as follows:

Sensor device structure sensors_poll_device_t, hardware hw_device_t extended standard device structure, the main completion of reading the underlying data, and the data stored in struct sensors_poll_device_t structural body, poll function to retrieve the underlying data, the call will be blocked is defined as follows:

The control device opening / closing structure is defined as follows:

3, Sensor HAL implemented (LM75 temperature sensor in the example)

(1) Open the device flowchart

(2) implementation code analysis

在代码中含有两个传感器ADC电位器和LM75温度传感器,所以在sensor.c中,首先需要定义传感器数组device_sensor_list[],其实就是初始化struct sensor_t结构体,初始化如下:

定义open_sensors函数,来打开Sensor模块,代码如下:

在这个方法中,首先需要为hw_device_t分配内存空间,并对其初始化,设置重要方法的实现。

control_open_data_source()打开传感器并使能设备:

调用sensor__data_poll方法读取数据:

/*轮询读取数据*/
        static int sensors__data_poll(struct sensors_data_context_t *dev, sensors_data_t * values)
        {
            int n;
            int mag;
            float temp;
            char buf[10];
            while (1) {
            if(count % 3 == 2) // 读取ADC值
            {
                if( read(dev->event_fd[0], &mag, sizeof(mag)) < 0)
                {
                   LOGE("read adc error");
                }else{ 
                dev->sensors[ID_MAGNETIC_FIELD].magnetic.v[0] =(float)mag; 
                LOGE("read adc %f\n",(float)mag);
                *values = dev->sensors[ID_MAGNETIC_FIELD];
                values->sensor = ID_MAGNETIC_FIELD;
                count++;
                }
                usleep(500000);
                return ID_MAGNETIC_FIELD;
                }
                else if(count%3 == 1) //读取温度传感器值
                 {
                memset(buf, 0 ,sizeof(buf));
                if((n = read(dev->event_fd[1], buf, 10)) < 0)
                {
                    LOGE("read temp error");
                    }else{
                    buf[n - 1] = '\0';
                    temp =(float) (atoi(buf) / 1000);
                    dev->sensors[ID_TEMPERATURE].temperature = temp;
                    LOGE("read temp %f\n",temp);
                    *values = dev->sensors[ID_TEMPERATURE];
                    values->sensor = ID_TEMPERATURE;
                    count++;
                }
                    close(dev->event_fd[1]);
                    dev->event_fd[1]= open("/sys/bus/i2c/devices/0-0048/temp1_input", O_RDONLY);
                    usleep(500000);
                    return ID_TEMPERATURE;
               }
               else if(count%3 == 0) //读取方向传感器模拟值
                 {
                    LOGI("read orientation\n");
                    /* fill up data of orientation */
                    dev->sensors[ID_ORIENTATION].orientation.azimuth = x + 5;
                    dev->sensors[ID_ORIENTATION].orientation.pitch = y + 5;
                    dev->sensors[ID_ORIENTATION].orientation.roll = z + 5;
                    *values = dev->sensors[ID_ORIENTATION];
                    values->sensor = ID_ORIENTATION;
                    count++;
                    x += 0.0001; y += 0.0001; z += 0.0001;
                    usleep (500000);
                    return ID_ORIENTATION;
              }
            }
        }

Guess you like

Origin www.cnblogs.com/zzb-Dream-90Time/p/10963407.html