1094332d3Sopenharmony_ci# Vibrator 2094332d3Sopenharmony_ci 3094332d3Sopenharmony_ci## Introduction 4094332d3Sopenharmony_ci 5094332d3Sopenharmony_ciThe vibrator driver model provides and implements vibrator-related Hardware Device Interfaces (HDIs). It supports vibration of the following types: 6094332d3Sopenharmony_ci 7094332d3Sopenharmony_ci- One-shot vibration for a specified duration (**StartOnce**). 8094332d3Sopenharmony_ci- Vibration with the specified effect (**StartEffect**). The effect is configured in the HDF Configuration Source (HCS). 9094332d3Sopenharmony_ci- Vibration with the specified duration, intensity, and frequency (**EnableVibratorModulation**). 10094332d3Sopenharmony_ci 11094332d3Sopenharmony_ci**Figure 1** Vibrator driver model 12094332d3Sopenharmony_ci 13094332d3Sopenharmony_ci 14094332d3Sopenharmony_ci 15094332d3Sopenharmony_ci## Directory Structure 16094332d3Sopenharmony_ci 17094332d3Sopenharmony_ciThe directory structure of the vibrator module is as follows: 18094332d3Sopenharmony_ci 19094332d3Sopenharmony_ci``` 20094332d3Sopenharmony_ci/drivers/peripheral/vibrator 21094332d3Sopenharmony_ci├── chipset # Driver code of the vibrator module 22094332d3Sopenharmony_ci├── hal # HAL code 23094332d3Sopenharmony_ci│ ├── include # HAL header files 24094332d3Sopenharmony_ci│ └── src # HAL code implementation 25094332d3Sopenharmony_ci├── interfaces # Driver APIs provided for upper-layer services 26094332d3Sopenharmony_ci│ └── include # APIs exposed externally 27094332d3Sopenharmony_ci└── test # Test code 28094332d3Sopenharmony_ci └── unittest # Unit test code 29094332d3Sopenharmony_ci``` 30094332d3Sopenharmony_ci 31094332d3Sopenharmony_ci## Usage 32094332d3Sopenharmony_ci 33094332d3Sopenharmony_ci### Available APIs 34094332d3Sopenharmony_ci 35094332d3Sopenharmony_ciThe APIs provided for the vibrator are used to start and stop vibration. The following table describes these APIs. 36094332d3Sopenharmony_ci 37094332d3Sopenharmony_ci**Table 1** Main APIs of the vibrator module 38094332d3Sopenharmony_ci 39094332d3Sopenharmony_ci| API | Description | 40094332d3Sopenharmony_ci| ------------------------------------------------------------ | ------------------------------------------------------------ | 41094332d3Sopenharmony_ci| int32_t StartOnce(uint32_t duration) | Starts vibration for a given **duration**. | 42094332d3Sopenharmony_ci| int32_t Start(const char *effectType) | Starts vibration with a given effect, which is specified by **effectType**. | 43094332d3Sopenharmony_ci| int32_t Stop(enum VibratorMode mode) | Stops vibration based on the specified vibration mode. | 44094332d3Sopenharmony_ci| int32_t EnableVibratorModulation(uint32_t duration, int32_t intensity, int32_t frequency) | Starts vibration with a given **duration**, **intensity**, and **frequency**.| 45094332d3Sopenharmony_ci| int32_t GetVibratorInfo(struct VibratorInfo **vibratorInfo); | Obtains vibrator information, including whether the intensity and frequency can be set and the intensity and frequency range.| 46094332d3Sopenharmony_ci 47094332d3Sopenharmony_ci### How to Use 48094332d3Sopenharmony_ci 49094332d3Sopenharmony_ciThe sample code is as follows: 50094332d3Sopenharmony_ci 51094332d3Sopenharmony_ci```c++ 52094332d3Sopenharmony_ci#include "vibrator_if.h" 53094332d3Sopenharmony_ci 54094332d3Sopenharmony_cienum VibratorMode { 55094332d3Sopenharmony_ci VIBRATOR_MODE_ONCE = 0, // Start one-shot vibration for a specified period. 56094332d3Sopenharmony_ci VIBRATOR_MODE_PRESET = 1, // Start periodic vibration with the preset effect. 57094332d3Sopenharmony_ci}; 58094332d3Sopenharmony_ci 59094332d3Sopenharmony_civoid VibratorSample(void) 60094332d3Sopenharmony_ci{ 61094332d3Sopenharmony_ci int32_t startRet; 62094332d3Sopenharmony_ci int32_t endRet; 63094332d3Sopenharmony_ci uint32_t g_duration = 1000; 64094332d3Sopenharmony_ci uint32_t g_sleepTime1 = 2000; 65094332d3Sopenharmony_ci uint32_t g_sleepTime2 = 5000; 66094332d3Sopenharmony_ci int32_t g_intensity1 = 30; 67094332d3Sopenharmony_ci int32_t g_frequency1 = 200; 68094332d3Sopenharmony_ci const char *g_timeSequence = "haptic.clock.timer"; 69094332d3Sopenharmony_ci struct VibratorInfo *g_vibratorInfo = nullptr; 70094332d3Sopenharmony_ci /* Create a VibratorInterface instance. */ 71094332d3Sopenharmony_ci struct VibratorInterface *g_vibratorDev = NewVibratorInterfaceInstance(); 72094332d3Sopenharmony_ci if (g_vibratorDev == NULL) { 73094332d3Sopenharmony_ci return; 74094332d3Sopenharmony_ci } 75094332d3Sopenharmony_ci /* Obtain vibrator information, including whether the intensity and frequency can be set and the intensity and frequency range. */ 76094332d3Sopenharmony_ci startRet = g_vibratorDev->GetVibratorInfo(&g_vibratorInfo); 77094332d3Sopenharmony_ci if (startRet != 0) { 78094332d3Sopenharmony_ci return; 79094332d3Sopenharmony_ci } 80094332d3Sopenharmony_ci /* Start vibration with the specified duration. */ 81094332d3Sopenharmony_ci startRet = g_vibratorDev->StartOnce(g_duration); 82094332d3Sopenharmony_ci if (startRet != 0) { 83094332d3Sopenharmony_ci return; 84094332d3Sopenharmony_ci } 85094332d3Sopenharmony_ci OsalMSleep(g_sleepTime1); 86094332d3Sopenharmony_ci /* Stop vibration based on the specified vibration mode. */ 87094332d3Sopenharmony_ci endRet = g_vibratorDev->Stop(VIBRATOR_MODE_ONCE); 88094332d3Sopenharmony_ci if (endRet != 0) { 89094332d3Sopenharmony_ci return; 90094332d3Sopenharmony_ci } 91094332d3Sopenharmony_ci /* Start vibration with the preset effect. */ 92094332d3Sopenharmony_ci startRet = g_vibratorDev->Start(g_timeSequence); 93094332d3Sopenharmony_ci if (endRet != 0) { 94094332d3Sopenharmony_ci return; 95094332d3Sopenharmony_ci } 96094332d3Sopenharmony_ci OsalMSleep(g_sleepTime2); 97094332d3Sopenharmony_ci /* Stop vibration based on the specified vibration mode. */ 98094332d3Sopenharmony_ci endRet = g_vibratorDev->Stop(VIBRATOR_MODE_PRESET); 99094332d3Sopenharmony_ci if (endRet != 0) { 100094332d3Sopenharmony_ci return; 101094332d3Sopenharmony_ci } 102094332d3Sopenharmony_ci /* Start vibration based on the specified duration, intensity, and frequency. */ 103094332d3Sopenharmony_ci startRet = g_vibratorDev->EnableVibratorModulation(g_duration, g_intensity1, g_frequency1); 104094332d3Sopenharmony_ci if (endRet != 0) { 105094332d3Sopenharmony_ci return; 106094332d3Sopenharmony_ci } 107094332d3Sopenharmony_ci OsalMSleep(g_sleepTime1); 108094332d3Sopenharmony_ci /* Stop vibration based on the specified vibration mode. */ 109094332d3Sopenharmony_ci startRet = g_vibratorDev->Stop(VIBRATOR_MODE_ONCE); 110094332d3Sopenharmony_ci if (endRet != 0) { 111094332d3Sopenharmony_ci return; 112094332d3Sopenharmony_ci } 113094332d3Sopenharmony_ci /* Release the VibratorInterface instance. */ 114094332d3Sopenharmony_ci ret = FreeVibratorInterfaceInstance(); 115094332d3Sopenharmony_ci if (ret != 0) { 116094332d3Sopenharmony_ci return; 117094332d3Sopenharmony_ci } 118094332d3Sopenharmony_ci} 119094332d3Sopenharmony_ci``` 120094332d3Sopenharmony_ci 121094332d3Sopenharmony_ci## Repositories Involved 122094332d3Sopenharmony_ci 123094332d3Sopenharmony_ci[Drive Subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/driver.md) 124094332d3Sopenharmony_ci 125094332d3Sopenharmony_ci[drivers_hdf_core](https://gitee.com/openharmony/drivers_hdf_core/blob/master/README_zh.md) 126094332d3Sopenharmony_ci 127094332d3Sopenharmony_ci[drivers_peripheral](https://gitee.com/openharmony/drivers_peripheral) 128