27
Introduction to G-Sesnsor and E-Compass Jiahe Jou 2, Dec., 2011

Introduction to Android G-sensor

Embed Size (px)

Citation preview

Page 1: Introduction to Android G-sensor

Introduction to G-Sesnsor and

E-CompassJiahe Jou

2, Dec., 2011

Page 2: Introduction to Android G-sensor

Outlines

● Introduction○ Sensor System

● G-Sensor System○ Java Application Layer○ Java Framework Layer○ JNI○ Hardware Abstraction Layer○ Linux Kernel○ Setup G-Sensor Driver

● Conclusion

Page 3: Introduction to Android G-sensor

IntroductionSensor System

Page 4: Introduction to Android G-sensor

● Detect the environment to provide better user experience○ Accelerometer○ Magnetometer○ Light Sensor○ Temperature Meter

● Application○ Game feature○ Rotate screen○ E-compass

Sensor System

Page 5: Introduction to Android G-sensor

Sensor System

● General architecture of sensor system

Page 6: Introduction to Android G-sensor

Sensor System

● API○ Provide a interface to get system sensor manager

● Framework○ Sensor manager service○ Definitions of sensor, sensor event, event listener

● JNI○ Link the framework layer and HAL

● HAL○ The hardware foundation of Android

Page 7: Introduction to Android G-sensor

Sensor System

● In more detail

Page 8: Introduction to Android G-sensor

G-Sensor System

Java Application LayerJava Framework Layer

JNIHAL

Linux KernelSetup the G-Sensor driver

Page 9: Introduction to Android G-sensor

Java Application Layer

● Implement a sensor application○ Get sensor manager○ Get a specific sensor ○ Register sensor event listener

...

mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

mAccerlerometer = mSensorManager.getDefautSensor(Sensor.TYPE_ACCELEROMETER);

mSensorManager.registerListener(this, mAccelerometer,SensorManager.SENSOR_DELAY_GAME);

...

Page 10: Introduction to Android G-sensor

Java Application Layer

● Implement event listener...// Called when the sensor accuracy changedpublic void onAccuracyChanged(int sensor, int accuracy){

// You can leave this function empty}

...// Called when the sensor value changedpublic void onSensorChanged(SensorEvent event{

...Log.d(TAG, "onSensorChanged==> sensor: " + sensor +

", x: " + event.values[0] +", y: " + event.values[1] +", z: " + event.values[2]);

…}...

Page 11: Introduction to Android G-sensor

G-Sensor System

Java Application LayerJava Framework Layer

JNIHAL

Linux KernelSetup the G-Sensor driver

Page 12: Introduction to Android G-sensor

Java Framework Layer

● Sensor Manager○ getDefautSensor(int type)

○ registerListener(SensorEventListener listener, Sensor sensor, int rate)

...public Sensor getDefaultSensor( int type){

// just return the 1st sensorList<Sensor> l = getSensorList(type);return l.isEmpty() ? null : l.get(0);

}…

...public boolean registerListener( (SensorEventListener listener, Sensor sensor, int rate){

// Another function was returnreturn registerListener(listener, sensor, rate, null);

}…

Page 13: Introduction to Android G-sensor

Java Framework Layer

○ registerListener( (SensorEventListener listener, Sensor sensor, int rate, Handler handler)■ Delegate a listener on a sensor

■ Lock the listening thread before sensor enabled

■ Enable the sensor

■ Unlock the listening thread

■ Start polling

PS. Please refer to the Figure 7 in document

Page 14: Introduction to Android G-sensor

G-Sensor System

Java Application LayerJava Framework Layer

JNIHAL

Linux KernelSetup the G-Sensor driver

Page 15: Introduction to Android G-sensor

JNI

● Sensor Manager○ Mapped to the sensor manager in framework layer

■ nativeClassInit()○ Provide the native function interface, e.g.:

■ sensors_enable_sensor()■ sensors_data_poll()

PS. Please refer to the Figure 8 and Figure 9 in document

Page 16: Introduction to Android G-sensor

G-Sensor System

Java Application LayerJava Framework Layer

JNIHAL

Linux KernelSetup the G-Sensor driver

Page 17: Introduction to Android G-sensor

Hardware Abstraction Layer

● Located in /hardware/STSensors/*● Built on SensorBase.cpp

○ openInput(const char* inpuName)○ Open input device for a given name when

construction

P.S. Please refer to the Figure 10 in document

Page 18: Introduction to Android G-sensor

Hardware Abstraction Layer

● AccSensor○ #define INPUT_SYSFS_PATH_ACC "/sys/class/i2c-

adapter/i2c-4/4-0019/"○ readEvents()

■ Get data from sensor event■ Calibrate for real world

P.S. Please refer to the Figure 12 in document

Page 19: Introduction to Android G-sensor

G-Sensor System

Java Application LayerJava Framework Layer

JNIHAL

Linux KernelSetup the G-Sensor driver

Page 20: Introduction to Android G-sensor

Linux Kernel

● User space communicate with kernel space by system call

● Hardware drivers● i2c protocol used

Page 21: Introduction to Android G-sensor

Linux Kernel

● General architecture from user space to hardware

Page 22: Introduction to Android G-sensor

Linux Kernel

● i2c driver need implement four methods:○ probe

■ Check the i2c functionality■ Initialize the input status■ Register the poll function■ Create the sysfs interface

○ remove■ Unregister the poll device■ Shutdown the power■ Remove sysfs interface■ Free the memory

○ resume○ suspend

P.S. Please refer the Figure 16 and Figure 17 in document

Page 23: Introduction to Android G-sensor

Linux Kernel

● lsm303dlh_acc_report_values()

P.S. Please refer to the Figure 18 and Figure 19 in document

static void lsm303dlh_acc_report_values(struct lsm303dlh_acc_data *acc, int *xyz){

struct input_dev *input = acc->input_poll_dev->input;

input_report_abs(input, ABS_X, xyz[0]);input_report_abs(input, ABS_Y, xyz[1]);input_report_abs(input, ABS_Z, xyz[2]);input_sync(input);

}

Page 24: Introduction to Android G-sensor

G-Sensor System

Java Application LayerJava Framework Layer

JNIHAL

Linux KernelSetup the G-Sensor driver

Page 25: Introduction to Android G-sensor

Setup G-Sensor Driver

● Let Linux kernel load the driver○ innocomm_oracle_deconfig○ KConfig○ Makefile

P.S. Please refer to Figure 21 in document

● Setup regulator comsumer○ board-oracle.c

P.S. Please refer to Figure 22 in document

● Setup the i2c between oracle board and chip○ board-oracle-i2c.c

P.S. Please refer to Figure 23 in document

Page 26: Introduction to Android G-sensor

ConclusionConclusion

Page 27: Introduction to Android G-sensor

Conclusion

● A top-down view:○ Java application layer○ Java framework layer○ HAL○ Linux kernel

● E-Compass system is as same as the G-Sensor system