1# Motion
2
3## Introduction
4
5The 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.
6
7The 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.
8
9**Figure 1** Architecture of the motion module
10
11![](figures/motion-driver-module-architecture.png)
12
13## Directory Structure
14
15The directory structure of the motion module is as follows:
16
17```
18/drivers/peripheral/motion
19├── hdi_service                          # Driver capability provided by the motion module for upper-layer services
20├── test                                 # Test codes for the motion module
21│   └── unittest\hdi                     # HDI unit test code of the motion driver module
22```
23
24## Description
25
26### Available APIs
27
28The 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.
29
30**Table 1** Motion HDI APIs
31
32| API                                                      | Description                                                    |
33| ------------------------------------------------------------ | ------------------------------------------------------------ |
34| int32_t EnableMotion(int32_t motionType)                     | Enables a motion type. The motion data can be obtained only when motion is enabled.|
35| int32_t DisableMotion(int32_t motionType)                    | Disables a motion type.                                    |
36| 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.|
37| int32_t Unregister(const sptr<IMotionCallback> &callbackObj) | Unregisters from the callback for motion data.                          |
38
39### How to Use
40
41This section describes how to subscribe to pickup data.
42
43Sample Code
44
45```
46#include "v1_0/imotion_interface.h"
47
48/* MotionCallbackService class */
49class MotionCallbackService : public IMotionCallback {
50public:
51    MotionCallbackService() = default;
52    virtual ~MotionCallbackService() = default;
53    int32_t OnDataEvent(const HdfMotionEvent &event) override;
54};
55
56/* Callback */
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. Obtain the motion service. */
71    g_motionInterface = IMotionInterface::Get();
72    if (g_motionInterface == nullptr) {
73        return;
74    }
75    /* 2. Register the callback for motion data. */
76    ret = g_motionInterface->Register(g_motionCallback);
77    if (ret != 0) {
78        return;
79    }
80    /* 3. Enable motion. */
81    ret = g_motionInterface->EnableMotion(HDF_MOTION_TYPE_PICKUP);
82    if (ret != 0) {
83        return;
84    }
85    /* 4. Disable motion. */
86    ret = g_motionInterface->DisableMotion(HDF_MOTION_TYPE_PICKUP);
87    if (ret != 0) {
88        return;
89    }
90    /* 5. Unregister from the callback for motion data. */
91    ret = g_motionInterface->Unregister(g_motionCallback);
92    if (ret != 0) {
93        return;
94    }
95}
96```
97
98## Repositories Involved
99
100[Driver](https://gitee.com/openharmony/docs/blob/master/en/readme/driver.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)
107