Tel: 137-2421-1742 E-mail: paul@rdbuy.cn
ICP備案證書號:粵ICP備12082730號-1
共執行 65 個查詢,用時 0.019058 秒,在線 203 人,Gzip 已啟用,占用內存 3.169 MB
Powered by ECShop v4.0.1
Android4.1系統內置對傳感器的支持達13種,他們分別是:加速度傳感器(accelerometer)、磁力傳感器(magnetic field)、方向傳感器(orientation)、陀螺儀(gyroscope)、環境光照傳感器(light)、壓力傳感器(pressure)、溫度傳感器(temperature)和距離傳感器(proximity)等。Android實現傳感器系統包括以下幾個部分:
n
n
n
n
各部分之間架構圖如下:
Google為Sensor提供了統一的HAL接口,不同的硬件廠商需要根據該接口來實現并完成具體的硬件抽象層,Android中Sensor的HAL接口定義在:
hardware/libhardware/include/hardware/sensors.h
n
#define SENSOR_TYPE_ACCELEROMETER #define SENSOR_TYPE_MAGNETIC_FIELD #define SENSOR_TYPE_ORIENTATION #define SENSOR_TYPE_GYROSCOPE #define SENSOR_TYPE_LIGHT #define SENSOR_TYPE_PRESSURE #define SENSOR_TYPE_TEMPERATURE #define SENSOR_TYPE_PROXIMITY #define SENSOR_TYPE_GRAVITY #define SENSOR_TYPE_LINEAR_ACCELERATION 10 #define SENSOR_TYPE_ROTATION_VECTOR #define SENSOR_TYPE_RELATIVE_HUMIDITY #define SENSOR_TYPE_AMBIENT_TEMPERATURE 13 |
n
struct sensors_module_t { }; |
該接口的定義實際上是對標準的硬件模塊hw_module_t的一個擴展,增加了一個get_sensors_list函數,用于獲取傳感器的列表。
n
struct sensor_t { }; |
n
typedef struct sensors_event_t { } sensors_event_t; |
其中,sensor為傳感器的標志符,而不同的傳感器則采用union方式來表示,sensors_vec_t結構體用來表示不同傳感器的數據,
n
typedef struct { } sensors_vec_t; |
n
struct sensors_poll_device_t { struct hw_device_t common; //Activate/deactivate one sensor }; |
n
static inline int sensors_open(const struct hw_module_t* module, } static inline int sensors_close(struct sensors_poll_device_t* device) { } |
SensorDevice屬于JNI層,與HAL進行通信的接口,在JNI層調用了HAL層的open_sensors()方法打開設備模塊,再調用poll__activate()對設備使能,然后調用poll__poll讀取數據。
在bma250傳感器中,只有加速度傳感器,所以在sensor.cpp中,首先需要定義傳感器數組sSensorList,其實就是初始化struct sensor_t結構體,只有加速傳感器,初始化如下:
static const struct sensor_t sSensorList[] = { }; |
n
static struct hw_module_methods_t sensors_module_methods = { }; static int open_sensors(const struct hw_module_t* module, const char* name, { } |
在這個方法中,首先需要為hw_device_t分配內存空間,并對其初始化,設置重要方法的實現,然后調用open_input_device打開設備節點,返回文件描述符。
n
static int poll__activate(struct sensors_poll_device_t *device, } static int set_sysfs_input_attr(char *class_path, { } |
代碼很簡單,通過系統調用open方法打開設備,然后調用write()方法寫指令使能。
n
static int poll__poll(struct sensors_poll_device_t *device, |