Android HAL之sensor

HAL之sensor

Sensor相关文件路径

(1)传感器系统的java部分,实现文件为sensor*.java
frameworks\base\core\java\android\hardware
(2)传感器系统等JNI部分,演示Android.hardware.Sensor.Manager类的本质支持
frameworks\base\core\jni\android_hardware_SensorManager.cpp
(3)传感器的HAL层,演示传感器系统的硬件抽象层需要具体的实现
hardware\libhardware\include\hardware\sensors.h
(4)驱动层,根据不同平台有所差异
kernel\drivers\hwmon$(PROJECT)\sensor

HAL层sensor代码

(1)文件的android.mk

LOCAL_MODULE := sensors.default  #重要,定义好的so名字,在JNI中会被加载调用
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS := -DLOG_TAG=\"Sensors\" \
				-Wall \
				-DSENSORHAL_ACC_ADXL346
#				-DSENSORHAL_ACC_KXTF9
LOCAL_SRC_FILES := \
			SensorBase.cpp \
			InputEventReader.cpp \
			AkmSensor.cpp \
			sensors.cpp \
			AdxlSensor.cpp
LOCAL_SHARED_LIBRARIES := liblog libcutils libdl
LOCAL_PRELINK_MODULE := false
include $(BUILD_SHARED_LIBRARY)

(2)填充相关结构体–sensors.h中定义的

sensors_module_t 定义sensor模块

struct sensors_module_t {
    struct hw_module_t common;
    /**
     * Enumerate all available sensors. The list is returned in "list".
     * @return number of sensors in the list
     */
    int (*get_sensors_list)(struct sensors_module_t* module,
            struct sensor_t const** list);
};

其中get_sensors_list是用来获取sensor列表

sensor_t用来描述传感器信息

struct sensor_t {
    const char*     name; //名称
    const char*     vendor; //vendor?
    int             version;//版本
    int             handle;//句柄
    int             type; //类型
    float           maxRange; //最大范围
    float           resolution;//解析度
    float           power; //功耗
    int32_t         minDelay;
    uint32_t        fifoReservedEventCount;
    uint32_t        fifoMaxEventCount;
    void*           reserved[6];
};

结构体和联合,sensor数据

/**
 * Union of the various types of sensor data
 * that can be returned.
 */
typedef struct sensors_event_t {
    /* must be sizeof(struct sensors_event_t) */
    int32_t version;
    /* sensor identifier */
    int32_t sensor;
    /* sensor type */
    int32_t type;
    /* reserved */
    int32_t reserved0;
    /* time is in nanosecond */
    int64_t timestamp;
    union {
        union {
            float           data[16];
            /* acceleration values are in meter per second per second (m/s^2) */
            sensors_vec_t   acceleration;
            /* magnetic vector values are in micro-Tesla (uT) */
            sensors_vec_t   magnetic;
            /* orientation values are in degrees */
            sensors_vec_t   orientation;
            /* gyroscope values are in rad/s */
            sensors_vec_t   gyro;
            /* temperature is in degrees centigrade (Celsius) */
            float           temperature;
            /* distance in centimeters */
            float           distance;
            /* light in SI lux units */
            float           light;
            /* pressure in hectopascal (hPa) */
            float           pressure;
            /* relative humidity in percent */
            float           relative_humidity;
           /* uncalibrated gyroscope values are in rad/s */
            uncalibrated_event_t uncalibrated_gyro;
            /* uncalibrated magnetometer values are in micro-Teslas */
            uncalibrated_event_t uncalibrated_magnetic;
            /* this is a special event. see SENSOR_TYPE_META_DATA above.
             * sensors_meta_data_event_t events are all reported with a type of
             * SENSOR_TYPE_META_DATA. The handle is ignored and must be zero.
             */
            meta_data_event_t meta_data;
        };
        union {
            uint64_t        data[8];
            /* step-counter */
            uint64_t        step_counter;
        } u64;
    };
    uint32_t reserved1[4];
} sensors_event_t;

(3)适配层函数接口

/** convenience API for opening and closing a device */

static inline int sensors_open(const struct hw_module_t* module,
        struct sensors_poll_device_t** device) {
    return module->methods->open(module,
            SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
}

static inline int sensors_close(struct sensors_poll_device_t* device) {
    return device->common.close(&device->common);
}

static inline int sensors_open_1(const struct hw_module_t* module,
        sensors_poll_device_1_t** device) {
    return module->methods->open(module,
            SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
}

static inline int sensors_close_1(sensors_poll_device_1_t* device) {
    return device->common.close(&device->common);

在驱动提供的代码要实现file_operations。

sensor编程的流程

(1)获取系统服务,返回一个SensorManager对象
sensormanager = (SensorManager)getSystemServer(SENSOR_SERVICE);
(2)通过SensorManager对象获取相应的Sensor类型对象
sensorObject = sensormanager.getDefaultSesor(Sensor Type);
(3)声明一个SensorEventListener对象检测Sensor事件,重载onSensorChanged方法
SensorEventListener = new SensorEventListener(){ };
(4)注册相应的SensorService
sensormanager.registerListener(sensorListener, sensorObject, Sensor type);
(5)销毁SensorService
sensormanager.registerListener(sensorListener, sensorObject);

SensorListener接口是整个传感器应用程序的核心,包含如下两个必须的方法:
onSensorChange(int sensor, float values[]):此方法在传感器值更改时调用,该方法只对受此应用程序监视的传感器调用()。该方法包含两个参数:
1、一个整数:指出更改的传感器是哪个
2、一个浮点值数组:表示传感器数据
onAccuracyChanged(int sensor, int accuracy):当传感器的准确值更新时调用此函数。

================
如有错误,欢迎纠正!

你可能感兴趣的:(bsp开发)