use ohos_vibrator_sys::{
OH_Vibrator_Cancel, OH_Vibrator_PlayVibration, OH_Vibrator_PlayVibrationCustom,
Vibrator_Attribute, Vibrator_FileDescription,
};
mod error;
mod usage;
pub use error::*;
pub use usage::*;
pub fn cancel() -> Result<(), VibratorError> {
let ret = unsafe { OH_Vibrator_Cancel() };
if ret != 0 {
return Err(VibratorError::InternalError(ret));
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Attribute {
pub vibrator_id: i32,
pub usage: VibratorUsage,
}
impl Default for Attribute {
fn default() -> Self {
Self {
vibrator_id: 0,
usage: VibratorUsage::Unknown,
}
}
}
impl From<Attribute> for Vibrator_Attribute {
fn from(attribute: Attribute) -> Self {
Vibrator_Attribute {
vibratorId: attribute.vibrator_id,
usage: attribute.usage.into(),
}
}
}
pub fn start(duration: i32, attribute: Attribute) -> Result<(), VibratorError> {
let ret = unsafe { OH_Vibrator_PlayVibration(duration, attribute.into()) };
if ret != 0 {
return Err(VibratorError::InternalError(ret));
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FileDescription {
pub fd: i32,
pub offset: u64,
pub length: u64,
}
impl From<FileDescription> for Vibrator_FileDescription {
fn from(file_description: FileDescription) -> Self {
Vibrator_FileDescription {
fd: file_description.fd,
offset: file_description.offset as _,
length: file_description.length as _,
}
}
}
pub fn custom_start(
file_description: FileDescription,
attribute: Attribute,
) -> Result<(), VibratorError> {
let ret = unsafe { OH_Vibrator_PlayVibrationCustom(file_description.into(), attribute.into()) };
if ret != 0 {
return Err(VibratorError::InternalError(ret));
}
Ok(())
}