1 Star 0 Fork 4.9K

fish / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sensors-usage-example.md 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
NEEN 提交于 2021-03-12 17:59 . !197 Docs Update version 1.0.1

Sensors Usage Example

The following sample code uses the sensor whose sensorTypeId is 0 as an example. The code for other sensor types is similar.

#include "sensor_agent.h"
#include "sensor_agent_type.h"
#include "stdio.h"

void SensorDataCallbackImpl(SensorEvent *event)
{
    if(event == NULL){
        return;
    }
    float *sensorData=(float *)event->data;
    for(int32_t i = 0; i < (int32_t)(event->dataLen / sizeof(uint8_t *)); i++){
        printf("SensorDataCallbackImpl data: %f", *(sensorData + i));
    }
}

/* Test case function */
static int32_t TestSensorService(void)
{
    SensorUser sensorUser;
    sensorUser.callback = SensorDataCallbackImpl;
    SensorInfo *sensorInfo = (SensorInfo *)NULL;
    int32_t count = 0;
    // Obtain the list of sensors on the device.
    int32_t ret = GetAllSensors(&sensorInfo, &count);
    if (ret != 0) {
        printf("GetAllSensors failed! ret: %d", ret);
        return ret;
    }
    // Enable the sensor.
    ret = ActivateSensor(0, &sensorUser);
    if (ret != 0) {
        printf("ActivateSensorfailed! ret: %d", ret);
        return ret;
    }
     // Subscribe to sensor data.
    ret = SubscribeSensor(0, &sensorUser);
    if (ret != 0) {
        printf("SubscribeSensor! ret: %d", ret);
        return ret;
    }
    sleep(10);
    // Unsubscribe from the sensor data.
    ret = UnsubscribeSensor(0, &sensorUser);
    if (ret != 0) {
        printf("UnsubscribeSensor! ret: %d", ret);
        return ret;
    }
    // Disable the sensor.
    ret = DeactivateSensor(0, &sensorUser);
    if (ret != 0) {
        printf("DeactivateSensor! ret: %d", ret);
        return ret;
    }
}
1
https://gitee.com/fish_neil/docs.git
git@gitee.com:fish_neil/docs.git
fish_neil
docs
docs
master

搜索帮助