Summary of sensor porting on Android

Source: Internet
Author: User

As an input device of the Android system, sensor is essential for Android devices. Sensor mainly reports G-sensor, lightssensor, proximitysensor, and temperaturesensor. The porting of each sensor is similar. This article describes G-sensor and lightsensor.

 

The transplantation of sensor mainly includes three parts:

Development of drivers for related devices in Linux kernel, development of relevant libraries in Hal in Android, and development of test programs in the application layer in Android.

 

1. Development of drivers for related devices in Linux kernel.

The development of the sensor driver consists of two parts: sensor data collection, reporting, and sensor control.

1. collect and report sensor data.

A. Sensor Data Collection refers to reading relevant data from hardware devices.

There are two ways to collect data:

When an interruption occurs during registration, the driver collects data;

Actively collect data through polling. The timer is used to periodically collect data.

B. Report sensor data

Because the sensor is an input device, the data reported by the sensor must be reported to the Linux Kernel Input subsystem. The upper layer only needs to be in the corresponding subsystem.

Read the data.

The hardware of the Linux Kernel Input subsystem that needs to report data includes touchscreen, keyboard, mouse, and sensor.

The reported data code is roughly

Static void report_abs (void)
{
Short X, Y, Z, tilt;
If (read_data (& X, & Y, & Z, & tilt )! = 0 ){
/* Report the absulate sensor data to input device */
Input_report_abs (IDEV, abs_x, y );
Input_report_abs (IDEV, abs_y, X );
Input_report_abs (IDEV, abs_z, Z );
Input_sync (IDEV );
}

 

2. sensor control.

The sensor control includes enable/disable, enable, and so on.

The sensor control is mainly implemented through IOCTL.

In fact, the development of drivers is similar, as long as the corresponding callback function is implemented, and pay attention to the above two points.

2. Development of Hal-related libraries in Android.

Android framework supports sensor, which has been completed in Android. However, since the Hal layer is closely connected with hardware and everything in Hal corresponding to each hardware is different, the Hal layer in anroid does not support sensor. Therefore, development is required. You only need to name the changed library "libsensor. So" and put it in a fixed directory.

The development of libsensor. So in this article is based on android2.2. The overall structure of the Code on android2.3 is different, but it is also similar. You only need to know the core content.

For libsensor. So library development, you only need to write a C program and compile it into a library. For android2.2, we don't need to develop this library by ourselves, because android2.2 provides HTC-related source files. We just need to take the key points and change them.

The following describes the key points. See device/HTC/passion-Common/libsensors/sensors. c In the android directory.

1. open_sensors (...)

This function is important. Most of the functions implemented in the sensors. c file are system callback functions. In this function, the main function is to write your own function. Note:

The System-used callback function.

2. Define supported Sensors


/*************************************** **************************************/

# Define max_num_sensors 6// Define the number of hosts

# Define supported_sensors (1 <max_num_sensors)-1)

# Define array_size (A) (sizeof (a)/sizeof (A [0])

// Define supported sensors types

# Define id_a (0)
# Define id_m (1)
# Define id_o (2)
# Define id_t (3)
# Define id_p (4)
# Define id_l (5)

Static int id_to_sensor [max_num_sensors] = {
[Id_a] = sensor_type_accelerometer,
[Id_m] = sensor_type_magnetic_field,
[Id_o] = sensor_type_orientation,
[Id_t] = sensor_type_temperature,
[Id_p] = sensor_type_proximity,
[Id_l] = sensor_type_light,
};

// Define each sensors. Because multiple sensors can be integrated on one hardware, ***** _ group is introduced to identify multiple sensors on one hardware.

ItemsSensors

# Define sensors_akm_acceleration (1 <id_a)
# Define sensors_akm_magnetic_field (1 <id_m)
# Define sensors_akm_orientation (1 <id_o)
# Define sensors_akm_temperature (1 <id_t)
# Define sensors_akm_group (1 <id_a) | (1 <id_m) | (1 <id_o) | (1 <id_t ))

# Define sensors_cm_proximity (1 <id_p)
# Define sensors_cm_group (1 <id_p)

# Define sensors_light (1 <id_l)
# Define sensors_light_group (1 <id_l)

/*************************************** **************************************/

These definitions need to be redefined according to the actual situation. The general idea remains unchanged.

 

3. Define the data structure used by sensors

// Because HTC sensor integrates three hardware, the following three file descriptors are defined: akmd_fd, pai_fd, and lsd_fd..

Struct sensors_control_context_t {
Struct sensors_control_device_t device; // must be first
Int akmd_fd;
Int cmd_fd;
Int lsd_fd;
Uint32_t active_sensors;
};

// Because HTC sensor integrates three hardware, events_fd [3] is defined below to obtain events.

Struct sensors_data_context_t {
Struct sensors_data_device_t device; // must be first
Int events_fd [3];
Sensors_data_t sensors [max_num_sensors];
Uint32_t pendingsensors;
};

These definitions need to be redefined based on the actual number of hardware, and the general idea remains unchanged.

 

4. Define the sensors list

Static const struct sensor_t ssensorlist [] = {
{"Bma150 3-axis accelerometer ",
"Bosh ",
1, sensors_handle_base + id_a,
Sensor_type_accelerometer, 4.0f * 9.81f, (4.0f * 9.81f)/256.0f, 0.2f ,{}},
{"Ak8973 3-axis magnetic field sensor ",
"Asahi kasei ",
1, sensors_handle_base + id_m,
Sensor_type_magnetic_field, 20000000f, 1.0f/16366f, 6.8f ,{}},
{"Ak8973 orientation sensor ",
"Asahi kasei ",
1, sensors_handle_base + id_o,
Sensor_type_orientation, 360.0f, 1.0f, 7.0f ,{}},
{"Cm3602 proximity sensor ",
"Capella Microsystems ",
1, sensors_handle_base + id_p,
Sensor_type_proximity,
Proximity_threshold_cm, proximity_threshold_cm,
0.5f ,{}},
{"Cm3602 light sensor ",
"Capella Microsystems ",
1, sensors_handle_base + id_l,
Sensor_type_light, 10240.0f, 1.0f, 0.5f ,{}},
};
The system's sensors list is defined above, so that detailed information about sensors can be obtained during system loading.

5. Define the file name of the hardware in the/dev directory.

# Define akm_device_name "/dev/akm8973_aot"
# Define cm_device_name "/dev/cm3602"
# Define ls_device_name "/dev/lightsensor"
The role of defining these file nodes is: Because using sensors to obtain data is the main function of sensor, you also need to control the sensor, such as open,

Close, setdelay, etc. These related controls are essential. The sensor is controlled by the corresponding file nodes in the/dev/directory.

Therefore, the above file nodes are available. The names of these file nodes must match those defined in the driver.

 

6. open_inputs (...) Function

The open_inputs function is used to find the corresponding file nodes of each sensors in the input subsystem of the kernel and return them to the sensor system.

If (FD> = 0 ){
Char name [80];
If (IOCTL (FD, eviocgname (sizeof (name)-1), & name) <1 ){
Name [0] = '/0 ';
}

// The compass, promixity, and lightsensor-level defined below are the names of the three hardware devices in the input subsystem of the kernel.

And DriverTo match
If (! Strcmp (name, "Compass ")){
Logv ("using % s (name = % s)", devname, name );
* Akm_fd = FD;
}
Else if (! Strcmp (name, "proximity ")){
Logv ("using % s (name = % s)", devname, name );
* P_fd = FD;
}
Else if (! Strcmp (name, "lightsensor-level ")){
Logv ("using % s (name = % s)", devname, name );
* L_fd = FD;
}
Else
Close (FD );
}

 

If (DIR = NULL)
Return-1;

Memset (devname, 0, sizeof (devname); // This line is appended; otherwise, libsensor. So will crash
Strcpy (devname, dirname );

7. Modify Android. mk.

The compiled libsensor. So library must be compiled to the system at the specified location/system/lib/HW to be loaded. However, unfortunately, the device/HTC/passion-

Compile common/libsensors/sensors. C. The compiled library cannot be compiled to a specified location. Modify Android. mk and force

Copy libsensor. So to the specified directory.

Product_copy_files: =/

Out/target/product/armv7/obj/shared_libraries/sensors. default_intermediates/linked/sensors. Default. So:System/lib/HW/sensors. Default. So

 

 

In this way, you can forcibly copy the files under the specified directory to the specified directory.

Modify the preceding key points to implement libsensor. So.

 

3. Development of related test programs in the application layer of Android.

There are many ways to test each sensors in Android. You can download various applications to test sensors. I prefer to do it myself, so I did it myself.

One application tests the sensor.

Enable the sensor function on the Android Application layer.

In Android, several sensor-related classes are:

Sensormanager: This allows the system to call the sensor.

Sensorevent: encapsulate each sensor data. For details, refer to the android development documentation.

Sensoreventlistener: monitors the sensor data. Once there is data, the corresponding function is called.

SENSOR: encapsulation of sensor.

 

To implement functions related to sensor.

1. Define the sensor to be monitored.

Sensor = sensor. type_accelerometer;

2. Implement sensorevenetlistener.

Private sensoreventlistener mlistener = new sensoreventlistener (){

Public void onsensorchanged (sensorevent event ){
......

Handle sensor data code

......
}
}

Public void onaccuracychanged (sensor, int accuracy ){
// Todo auto-generated method stub

}
};

3. Register sensoreventlistener with the system.

Msensormanager = (sensormanager) getsystemservice (context. sensor_service );

Msensor = (msensormanager. getsensorlist (sensor). Get (0 );
Msensormanager. registerlistener (mlistener, msensor, sensormanager. sensor_delay_normal );

Remember to log out after use

Msensormanager. unregisterlistener (mlistener );

In this way, the sensor can be used. When the sensor data changes, it will be passed in.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.