1# Motion
2
3## 简介
4
5基于HDF(Hardware Driver Foundation)驱动框架开发的Motion驱动,能够屏蔽硬件器件差异,为上层服务提供稳定的手势识别基础能力接口,包括Motion使能/去使能、Motion订阅/取消订阅等稳定的接口。
6
7Motion驱动模块如图1所示,上层为Framework层,提供MSDP服务,通过UHDF(User Hardware Driver Foundation)层的Motion Proxy与Motion Stub进行交互;而Motion Stub可调用Motion HDI实现类接口,从而实现上层服务的手势识别使能/去使能、手势识别订阅/取消订阅等能力。
8
9**图 1** Motion驱动模块架构图
10
11![](figures/motion-driver-module-architecture_zh.png)
12
13## 目录
14
15Motion驱动源代码目录结构如下所示:
16
17```
18/drivers/peripheral/motion
19├── hdi_service                          # motion模块对上层服务提供的驱动能力
20├── test                                 # motion模块测试代码
21│   └── unittest\hdi                     # motion模块hdi单元测试代码
22```
23
24## 说明
25
26### 接口说明
27
28Motion驱动模块为上层服务提供可直接调用的能力接口,涉及的主要功能有:使能/去使能手势识别、订阅/取消订阅手势识别数据等操作。Motion驱动模型对HDI开放的API接口功能如表1:
29
30**表 1**  Motion HDI 接口列表
31
32| 接口名                                                       | 功能描述                                                     |
33| ------------------------------------------------------------ | ------------------------------------------------------------ |
34| int32_t EnableMotion(int32_t motionType)                     | 使能一种手势识别类型,只有数据订阅者使能手势识别后,才能获取订阅的手势识别数据。 |
35| int32_t DisableMotion(int32_t motionType)                    | 去使能一种手势识别类型。                                     |
36| int32_t Register(const sptr\<IMotionCallback\> &callbackObj)   | 订阅者成功注册手势识别数据回调函数,系统会将获取到的手势识别数据上报给订阅者。 |
37| int32_t Unregister(const sptr\<IMotionCallback\> &callbackObj) | 订阅者取消注册手势识别数据回调函数。                           |
38
39### 使用说明
40
41本节以订阅拿起手势识别数据为例进行介绍。
42
43代码示例
44
45```
46#include "v1_0/imotion_interface.h"
47
48/* 手势识别回调服务类 */
49class MotionCallbackService : public IMotionCallback {
50public:
51    MotionCallbackService() = default;
52    virtual ~MotionCallbackService() = default;
53    int32_t OnDataEvent(const HdfMotionEvent &event) override;
54};
55
56/* 回调函数 */
57int32_t MotionCallbackService::OnDataEvent(const HdfMotionEvent &event)
58{
59    printf("moton :[%d], result[%d]:, status[%d]\n\r", event.motion, event.result, event.status);
60    return HDF_SUCCESS;
61}
62
63void MotionSample(void)
64{
65    int32_t ret;
66    sptr<IMotionInterface> g_motionInterface = nullptr;
67    sptr<IMotionCallback> g_motionCallback = new MotionCallbackService();
68    sptr<IMotionCallback> g_motionCallbackUnregistered = new MotionCallbackService();
69
70    /* 1.获取motion服务 */
71    g_motionInterface = IMotionInterface::Get();
72    if (g_motionInterface == nullptr) {
73        return;
74    }
75    /* 2.订阅者注册手势识别数据回调处理函数 */
76    ret = g_motionInterface->Register(g_motionCallback);
77    if (ret != 0) {
78        return;
79    }
80    /* 3.使能手势识别 */
81    ret = g_motionInterface->EnableMotion(HDF_MOTION_TYPE_PICKUP);
82    if (ret != 0) {
83        return;
84    }
85    /* 4.去使能手势识别 */
86    ret = g_motionInterface->DisableMotion(HDF_MOTION_TYPE_PICKUP);
87    if (ret != 0) {
88        return;
89    }
90    /* 5.取消手势识别数据订阅函数 */
91    ret = g_motionInterface->Unregister(g_motionCallback);
92    if (ret != 0) {
93        return;
94    }
95}
96```
97
98## 相关仓
99
100[驱动子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E9%A9%B1%E5%8A%A8%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
101
102[drivers_hdf_core](https://gitee.com/openharmony/drivers_hdf_core)
103
104[drivers_interface](https://gitee.com/openharmony/drivers_interface)
105
106[**drivers\_peripheral**](https://gitee.com/openharmony/drivers_peripheral)