• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2023 Unionman Technology Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16 
17 #include <cstdio>
18 #include <unistd.h>
19 #include "hdf_base.h"
20 #include "hdf_log.h"
21 #include "hdf_sbuf.h"
22 #include "hdf_io_service_if.h"
23 #include "sensor_if.h"
24 #include "sensor_type.h"
25 
26 /* 创建回调函数 */
SensorDataCallback(const struct SensorEvents * event)27 static int32_t SensorDataCallback(const struct SensorEvents *event)
28 {
29     if (event == nullptr) {
30         return HDF_FAILURE;
31     }
32 
33     float sensorData = *((float *)event->data);
34     printf("sensor id [%d], data : %.2f\n", event->sensorId, sensorData);
35 
36     return HDF_SUCCESS;
37 }
38 
SensorEntry(void)39 static void SensorEntry(void)
40 {
41     int ret;
42     struct SensorInformation *sensorInfo = nullptr;
43     int32_t count = 0;
44     int32_t sensorInterval = 200000000; /* 数据采样率设置200毫秒,单位纳秒 */
45     int32_t reportInterval = 400000000; /* 数据上报间隔设置400毫秒,单位纳秒 */
46     int32_t sleepTime = 1000000; /* 1秒,单位毫秒 */
47 
48     /* 1.创建传感器接口实例 */
49     const struct SensorInterface *sensorDev = NewSensorInterfaceInstance();
50     if (sensorDev == nullptr) {
51         printf("NewSensorInterfaceInstance fail!\n");
52         return;
53     }
54 
55     printf("NewSensorInterfaceInstance success\n");
56 
57     /* 2.订阅者注册传感器数据回调处理函数 */
58     ret = sensorDev->Register(TRADITIONAL_SENSOR_TYPE, SensorDataCallback);
59     if (ret != HDF_SUCCESS) {
60         return;
61     }
62 
63     printf("Register success\n");
64 
65     /* 3.获取设备支持的Sensor列表 */
66     ret = sensorDev->GetAllSensors(&sensorInfo, &count);
67     if (ret != HDF_SUCCESS) {
68         return;
69     }
70 
71     printf("GetAllSensors count: %d\n", count);
72 
73     for (int i = 0; i < count; i++) {
74         printf("sensor [%d] : sensorName:%s, vendorName:%s, sensorTypeId:%d, sensorId:%d\n", i,
75                sensorInfo[i].sensorName, sensorInfo[i].vendorName, sensorInfo[i].sensorTypeId, sensorInfo[i].sensorId);
76     }
77 
78     for (int i = 0; i < count; i++) {
79         /* 4.设置传感器采样率 */
80         ret = sensorDev->SetBatch(sensorInfo[i].sensorId, sensorInterval, reportInterval);
81         if (ret != HDF_SUCCESS) {
82             printf("SetBatch failed,ret: %d\n", ret);
83             continue;
84         }
85 
86         printf("SetBatch success\n");
87 
88         /* 5.使能传感器 */
89         ret = sensorDev->Enable(sensorInfo[i].sensorId);
90         if (ret != HDF_SUCCESS) {
91             continue;
92         }
93 
94         printf("Enable success\n");
95 
96         /* 等待1秒钟数据回调 */
97         usleep(sleepTime);
98 
99         /* 6.去使能传感器 */
100         ret = sensorDev->Disable(sensorInfo[i].sensorId);
101         if (ret != HDF_SUCCESS) {
102             continue;
103         }
104 
105         printf("Disable success\n");
106     }
107 
108     /* 7.取消传感器数据订阅函数 */
109     ret = sensorDev->Unregister(TRADITIONAL_SENSOR_TYPE, SensorDataCallback);
110     if (ret != HDF_SUCCESS) {
111         return;
112     }
113 
114     printf("Unregister success\n");
115 
116     /* 8.释放传感器接口实例 */
117     ret = FreeSensorInterfaceInstance();
118     if (ret != HDF_SUCCESS) {
119         return;
120     }
121 
122     printf("FreeSensorInterfaceInstance success\n");
123 }
124 
main(int argc,char * argv[])125 int main(int argc, char *argv[])
126 {
127     SensorEntry();
128     return HDF_SUCCESS;
129 }
130