天天看點

Android HAL之sensorSensor相關檔案路徑HAL層sensor代碼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):當傳感器的準确值更新時調用此函數。

================

如有錯誤,歡迎糾正!