1094332d3Sopenharmony_ci# Motion
2094332d3Sopenharmony_ci
3094332d3Sopenharmony_ci## Introduction
4094332d3Sopenharmony_ci
5094332d3Sopenharmony_ciThe motion driver is developed based on the Hardware Driver Foundation (HDF). It shields hardware differences and provides stable motion capabilities for upper-layer services. The motion capabilities include enabling or disabling motion and subscribing to or unsubscribing from motion data.
6094332d3Sopenharmony_ci
7094332d3Sopenharmony_ciThe figure below shows the motion driver architecture. The framework layer provides MSDP services, and interacts with the Motion Hardware Device Interface (HDI) Server through the Motion HDI Client. The Motion HDI Server calls the Motion HDI Impl APIs to provide motion recognition capabilities for upper-layer services.
8094332d3Sopenharmony_ci
9094332d3Sopenharmony_ci**Figure 1** Architecture of the motion module
10094332d3Sopenharmony_ci
11094332d3Sopenharmony_ci![](figures/motion-driver-module-architecture.png)
12094332d3Sopenharmony_ci
13094332d3Sopenharmony_ci## Directory Structure
14094332d3Sopenharmony_ci
15094332d3Sopenharmony_ciThe directory structure of the motion module is as follows:
16094332d3Sopenharmony_ci
17094332d3Sopenharmony_ci```
18094332d3Sopenharmony_ci/drivers/peripheral/motion
19094332d3Sopenharmony_ci├── hdi_service                          # Driver capability provided by the motion module for upper-layer services
20094332d3Sopenharmony_ci├── test                                 # Test codes for the motion module
21094332d3Sopenharmony_ci│   └── unittest\hdi                     # HDI unit test code of the motion driver module
22094332d3Sopenharmony_ci```
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_ci## Description
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_ci### Available APIs
27094332d3Sopenharmony_ci
28094332d3Sopenharmony_ciThe motion driver module provides upper-layer services with APIs that can be directly called for various purposes, such as enabling or disabling motion and subscribing to or unsubscribing from motion data. Table 1 lists the APIs provided by the motion driver module.
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_ci**Table 1** Motion HDI APIs
31094332d3Sopenharmony_ci
32094332d3Sopenharmony_ci| API                                                      | Description                                                    |
33094332d3Sopenharmony_ci| ------------------------------------------------------------ | ------------------------------------------------------------ |
34094332d3Sopenharmony_ci| int32_t EnableMotion(int32_t motionType)                     | Enables a motion type. The motion data can be obtained only when motion is enabled.|
35094332d3Sopenharmony_ci| int32_t DisableMotion(int32_t motionType)                    | Disables a motion type.                                    |
36094332d3Sopenharmony_ci| int32_t Register(const sptr<IMotionCallback> &callbackObj)   | Registers the callback for motion data. When the registration is successful, the system will report the motion data to the subscriber.|
37094332d3Sopenharmony_ci| int32_t Unregister(const sptr<IMotionCallback> &callbackObj) | Unregisters from the callback for motion data.                          |
38094332d3Sopenharmony_ci
39094332d3Sopenharmony_ci### How to Use
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_ciThis section describes how to subscribe to pickup data.
42094332d3Sopenharmony_ci
43094332d3Sopenharmony_ciSample Code
44094332d3Sopenharmony_ci
45094332d3Sopenharmony_ci```
46094332d3Sopenharmony_ci#include "v1_0/imotion_interface.h"
47094332d3Sopenharmony_ci
48094332d3Sopenharmony_ci/* MotionCallbackService class */
49094332d3Sopenharmony_ciclass MotionCallbackService : public IMotionCallback {
50094332d3Sopenharmony_cipublic:
51094332d3Sopenharmony_ci    MotionCallbackService() = default;
52094332d3Sopenharmony_ci    virtual ~MotionCallbackService() = default;
53094332d3Sopenharmony_ci    int32_t OnDataEvent(const HdfMotionEvent &event) override;
54094332d3Sopenharmony_ci};
55094332d3Sopenharmony_ci
56094332d3Sopenharmony_ci/* Callback */
57094332d3Sopenharmony_ciint32_t MotionCallbackService::OnDataEvent(const HdfMotionEvent &event)
58094332d3Sopenharmony_ci{
59094332d3Sopenharmony_ci    printf("moton :[%d], result[%d]:, status[%d]\n\r", event.motion, event.result, event.status);
60094332d3Sopenharmony_ci    return HDF_SUCCESS;
61094332d3Sopenharmony_ci}
62094332d3Sopenharmony_ci
63094332d3Sopenharmony_civoid MotionSample(void)
64094332d3Sopenharmony_ci{
65094332d3Sopenharmony_ci    int32_t ret;
66094332d3Sopenharmony_ci    sptr<IMotionInterface> g_motionInterface = nullptr;
67094332d3Sopenharmony_ci    sptr<IMotionCallback> g_motionCallback = new MotionCallbackService();
68094332d3Sopenharmony_ci    sptr<IMotionCallback> g_motionCallbackUnregistered = new MotionCallbackService();
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_ci    /* 1. Obtain the motion service. */
71094332d3Sopenharmony_ci    g_motionInterface = IMotionInterface::Get();
72094332d3Sopenharmony_ci    if (g_motionInterface == nullptr) {
73094332d3Sopenharmony_ci        return;
74094332d3Sopenharmony_ci    }
75094332d3Sopenharmony_ci    /* 2. Register the callback for motion data. */
76094332d3Sopenharmony_ci    ret = g_motionInterface->Register(g_motionCallback);
77094332d3Sopenharmony_ci    if (ret != 0) {
78094332d3Sopenharmony_ci        return;
79094332d3Sopenharmony_ci    }
80094332d3Sopenharmony_ci    /* 3. Enable motion. */
81094332d3Sopenharmony_ci    ret = g_motionInterface->EnableMotion(HDF_MOTION_TYPE_PICKUP);
82094332d3Sopenharmony_ci    if (ret != 0) {
83094332d3Sopenharmony_ci        return;
84094332d3Sopenharmony_ci    }
85094332d3Sopenharmony_ci    /* 4. Disable motion. */
86094332d3Sopenharmony_ci    ret = g_motionInterface->DisableMotion(HDF_MOTION_TYPE_PICKUP);
87094332d3Sopenharmony_ci    if (ret != 0) {
88094332d3Sopenharmony_ci        return;
89094332d3Sopenharmony_ci    }
90094332d3Sopenharmony_ci    /* 5. Unregister from the callback for motion data. */
91094332d3Sopenharmony_ci    ret = g_motionInterface->Unregister(g_motionCallback);
92094332d3Sopenharmony_ci    if (ret != 0) {
93094332d3Sopenharmony_ci        return;
94094332d3Sopenharmony_ci    }
95094332d3Sopenharmony_ci}
96094332d3Sopenharmony_ci```
97094332d3Sopenharmony_ci
98094332d3Sopenharmony_ci## Repositories Involved
99094332d3Sopenharmony_ci
100094332d3Sopenharmony_ci[Driver](https://gitee.com/openharmony/docs/blob/master/en/readme/driver.md)
101094332d3Sopenharmony_ci
102094332d3Sopenharmony_ci[drivers_hdf_core](https://gitee.com/openharmony/drivers_hdf_core)
103094332d3Sopenharmony_ci
104094332d3Sopenharmony_ci[drivers_interface](https://gitee.com/openharmony/drivers_interface)
105094332d3Sopenharmony_ci
106094332d3Sopenharmony_ci[**drivers\_peripheral**](https://gitee.com/openharmony/drivers_peripheral)
107