Gyroscope
Start from API_LEVEL
3.0
. Please refer to API_LEVEL.
Gyroscope. Measuring the angular velocity of the device rotating along three orthogonal axes (x, y, z), the x and y axes are parallel to the screen, the positive direction refers to the figure, the z axis is perpendicular to the device's screen, the positive direction points upward, and the direction of the rotational angular velocity is determined using the Right-hand rule. The direction of the rotation arrow in the figure is the positive direction.
permission code: device:os.gyroscope
Methods
start
Start listening to gyroscope data
start(): void
stop
Stop listening to gyroscope data
stop(): void
getCurrent
Get current gyroscope data
getCurrent(): Result
Result
Property | Type | Description | API_LEVEL |
---|---|---|---|
x | number | Angular velocity of x-axis in DPS, degrees per second | 3.0 |
y | number | Angular velocity of y-axis in DPS, degrees per second | 3.0 |
z | number | Angular velocity of z-axis in DPS, degrees per second | 3.0 |
onChange
Register the gyroscope data change event listener callback function
onChange(callback: () => void): void
offChange
Cancel the gyroscope data change event listener callback function
offChange(callback: () => void): void
setFreqMode
Start from API_LEVEL
3.0
Set the mode of trigger frequency, mode
value reference frequency mode constant
setFreqMode(mode: number): void
Constants
Frequency Mode
Constant | Description | API_LEVEL |
---|---|---|
FREQ_MODE_LOW | Low power mode with low trigger frequency | 3.0 |
FREQ_MODE_NORMAL | Normal power consumption mode, medium trigger frequency | 3.0 |
FREQ_MODE_HIGH | High power consumption mode with high trigger frequency | 3.0 |
getFreqMode
Start from API_LEVEL
3.0
Get the mode of trigger frequency, result value reference frequency mode constant
getFreqMode(): number
Constants
Frequency Mode
Constant | Description | API_LEVEL |
---|---|---|
FREQ_MODE_LOW | Low power mode with low trigger frequency | 3.0 |
FREQ_MODE_NORMAL | Normal power consumption mode, medium trigger frequency | 3.0 |
FREQ_MODE_HIGH | High power consumption mode with high trigger frequency | 3.0 |
Example
import { Gyroscope, FREQ_MODE_LOW } from '@zos/sensor'
const gyroscope = new Gyroscope()
const callback = () => {
console.log(gyroscope.getCurrent())
}
gyroscope.onChange(callback)
gyroscope.setFreqMode(FREQ_MODE_LOW)
gyroscope.start()
// When not needed for use
gyroscope.offChange()
gyroscope.stop()