對於加速度傳感器,我們只需要知道他的三軸的數據,而android上層也是只需要一個數據結構中的數據而已。
1、關於linux驅動層
驅動主要是註冊了input設備,通過i2c接口讀取傳感器寄存器中的三軸數據,並上報這三個數據。
2、關於android硬件抽象層
在hardware\libhardware\include\hardware下有一個sensors.h的頭文件,主要填寫這裏的一個數據結構就可以了。
結構如下:
- typedef struct {
- union {
- float v[3];
- struct {
- float x;
- float y;
- float z;
- };
- struct {
- float azimuth;
- float pitch;、
- float roll;
- };
- };
- int8_t status;
- uint8_t reserved[3];
- } sensors_vec_t;
- /**
- * Union ofthe various types of sensor data
- * that can be returned.
- */
- typedef structsensors_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 {
- float data[16];
- /* acceleration values are in meter persecond per second (m/s^2) */
- sensors_vec_t acceleration;
- /* magnetic vector values are inmicro-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;
- };
- uint32_t reserved1[4];
- }sensors_event_t;
而android中源碼也有爲了這個抽象層專門搞了一個sensor的類,用來處理上報上來的數據,並提供給jni,給android上層。
具體代碼在device\samsung\tuna\libsensors這個文件夾下。
具體移植如下:
- static structsensor_t sSensorList[LOCAL_SENSORS + MPLSensor::numSensors] = {
- { "GP2A Light sensor",
- "Sharp",
- 1, SENSORS_LIGHT_HANDLE,
- SENSOR_TYPE_LIGHT, 3000.0f, 1.0f,0.75f, 0, { } },
- { "GP2A Proximity sensor",
- "Sharp",
- 1, SENSORS_PROXIMITY_HANDLE,
- SENSOR_TYPE_PROXIMITY, 5.0f, 5.0f,0.75f, 0, { } },
- { "BMP180 Pressure sensor",
- "Bosch",
- 1, SENSORS_PRESSURE_HANDLE,
- SENSOR_TYPE_PRESSURE, 1100.0f, 0.01f,0.67f, 20000, { } },
- };
這裏要修改爲自己的設備。
- private:
- enum {
- mpl = 0, //all mpl entries must be consecutive and inthis order
- mpl_accel,
- mpl_timer,
- light,
- proximity,
- pressure,
- numSensorDrivers, // wake pipe goes here
- mpl_power, //special handle for MPL pminteraction
- numFds,
- };
這裏選擇自己的設備對應的編號
- inthandleToDriver(int handle) const {
- switch (handle) {
- case ID_RV:
- case ID_LA:
- case ID_GR:
- case ID_GY:
- case ID_A:
- case ID_M:
- case ID_O:
- return mpl;
- case ID_L:
- return light;
- case ID_P:
- return proximity;
- case ID_PR:
- return pressure;
- }
- return -EINVAL;
- }
這裏根據不同的id對應上面填寫的編號就行。比如我這裏是三軸加速度,所以、只要
Case ID_A:
Return accel;
- sensors_poll_context_t::sensors_poll_context_t()
- {
- FUNC_LOG;
- MPLSensor* p_mplsen = new MPLSensor();
- setCallbackObject(p_mplsen); //setup thecallback object for handing mpl callbacks
- numSensors =
- LOCAL_SENSORS +
- p_mplsen->populateSensorList(sSensorList + LOCAL_SENSORS,
- sizeof(sSensorList[0]) * (ARRAY_SIZE(sSensorList) - LOCAL_SENSORS));
- mSensors[mpl] = p_mplsen;
- mPollFds[mpl].fd =mSensors[mpl]->getFd();
- mPollFds[mpl].events = POLLIN;
- mPollFds[mpl].revents = 0;
- ……
- }
這裏的構造函數裏,填寫三軸加速度的就行。
然後根據自己的傳感器來新建一個類。創建兩個文件,AccelSensor.cpp, AccelSensor.h
具體函數根據libsensor中的就行。
移植好後,mm編譯後會得到一個sensor.*.so,其中*可以根據你的Android.mk來得到。有了這個然後再跑android系統。就可以使用了。
3、關於測試
可以下載一個加速度傳感器測試儀來測試,也可以根據重力加速度可以使屏幕旋轉來測試。