69 Star 68 Fork 971

OpenHarmony / drivers_peripheral

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 11.42 KB
一键复制 编辑 原始数据 按行查看 历史
王勃懿 提交于 2023-04-24 09:15 . fix:modify dead url

sensor

Introduction

The sensor driver module provides and implements sensor-related Hardware Driver Interfaces (HDIs), including obtaining sensor information, enabling or disabling a sensor, subscribing to or unsubscribing from sensor data, and setting sensor options. These APIs make service development easier.

Figure 1 Sensor driver module architecture

Directory Structure

The directory structure of the sensor driver module is as follows:

/drivers/peripheral/sensor
├── hal                # HAL code
│   └── include       # HAL header files
│   └── src           # HAL code implementation
├── interfaces         # Driver capability APIs provided for upper-layer services
│   └── include       # APIs exposed externally
├── test               # Test code
│   └── unittest      # Unit test code

Usage

This section uses the acceleration sensor as an example to describe how to use sensor APIs.

Available APIs

The HAL module of the sensor driver provides APIs that can be directly called by sensor services to obtain, set, and subscribe to or unsubscribe from sensor data. The following table lists the APIs provided by the sensor driver module.

Table 1 Major HDIs of the sensor module

Category

API

Description

Query

int32_t GetAllSensors(struct SensorInformation **sensorInfo, int32_t *count)

Obtains information about all sensors in the system. The information about a sensor generally includes the sensor name, sensor vendor, firmware version, hardware version, sensor type ID, sensor ID, maximum measurement range, accuracy, and power.

Setting

int32_t Enable(int32_t sensorId)

Enables the sensor that has been subscribed to. The subscriber can obtain the sensor data only after the sensor is enabled.

int32_t Disable(int32_t sensorId)

Disables a sensor.

int32_t SetBatch(iint32_t sensorId, int64_t samplingInterval, int64_t reportInterval)

Sets the data sampling interval and data reporting interval for the specified sensor.

int32_t SetMode(int32_t sensorTypeId, SensorUser *user, int32_t mode)

Sets the data reporting mode for the specified sensor.

int32_t SetOption(int32_t sensorId, uint32_t option)

Sets options for the specified sensor, including its measurement range and accuracy.

Data subscription and unsubscription

int32_t Register(RecordDataCallback cb)

Registers the callback for reporting sensor data to the subscriber.

int32_t Unregister(void)

Unregisters the callback for reporting sensor data.

Instance creation

const struct SensorInterface *NewSensorInterfaceInstance(void)

Creates a SensorInterface instance.

int32_t FreeSensorInterfaceInstance(void)

Releases the SensorInterface instance.

Usage Guidelines

Sample code

#include "sensor_if.h"
#include "sensor_type.h"

/* Create a callback. */
void SensorDataCallback(struct SensorEvents *event)
{
    if(event == NULL){
        return;
    }
    float *sensorData=(float *)event->data;
    printf("sensor data[%f]", *sensorData);
}

void SensorSample(void)
{
    int ret;
    struct SensorInformation *sensorInfo = NULL;
    int32_t count = 0;
    int32_t sensorInterval = 200000000; /* Set the data sampling rate to 200000000, in the unit of nanoseconds (200 ms). */

    /* 1. Create a SensorInterface instance. */
    const struct SensorInterface *sensorDev = NewSensorInterfaceInstance();
    if (sensorDev == NULL) {
        return;
    }
     /* 2. Register a sensor data callback. */
    ret = sensorDev->Register(0, SensorDataCallback);
    if (ret != 0) {
        return;
    }
    /* 3. Obtain the list of sensors supported by the device. */
    ret = sensorDev->GetAllSensors(&sensorInfo, &count);
    if (ret != 0) {
        return;
    }
    /* 4. Set the sensor sampling rate. */
    ret = sensorDev->SetBatch(SENSOR_TYPE_ACCELEROMETER, sensorInterval, 0);
    if (ret != 0) {
        return;
    }
    /* 5. Enable the sensor. */
    ret = sensorDev->Enable(SENSOR_TYPE_ACCELEROMETER);
    if (ret != 0) {
        return;
    }
      /* 6. Disable the sensor. */
    ret = sensorDev->Disable(SENSOR_TYPE_ACCELEROMETER);
    if (ret != 0) {
        return;
    }
    /* 7. Unregister the sensor data callback. */
    ret = sensorDev->Unregister(0);
    if (ret != 0) {
        return;
    }
    /* 8. Release the SensorInterface instance.
    ret = FreeSensorInterfaceInstance();
    if (ret != 0) {
        return;
    }
}

Repositories Involved

Driver subsystem

drivers_framework

drivers_adapter

drivers_adapter_khdf_linux

drivers_peripheral

1
https://gitee.com/openharmony/drivers_peripheral.git
git@gitee.com:openharmony/drivers_peripheral.git
openharmony
drivers_peripheral
drivers_peripheral
master

搜索帮助