1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 12:28:09 +00:00

fix(core): fix tt ui loader haptic feedback

[no changelog]
This commit is contained in:
tychovrahe 2024-05-02 09:29:05 +02:00 committed by TychoVrahe
parent c680187b0e
commit d04ecba815
6 changed files with 34 additions and 12 deletions

View File

@ -359,7 +359,7 @@ fn generate_trezorhal_bindings() {
// haptic
.allowlist_type("haptic_effect_t")
.allowlist_function("haptic_play")
.allowlist_function("haptic_play_rtp");
.allowlist_function("haptic_play_custom");
// Write the bindings to a file in the OUR_DIR.
bindings

View File

@ -12,8 +12,8 @@ pub fn play(effect: HapticEffect) {
}
}
pub fn play_rtp(amplitude: i8, duration: u16) {
pub fn play_custom(amplitude_pct: i8, duration_ms: u16) {
unsafe {
ffi::haptic_play_rtp(amplitude, duration);
ffi::haptic_play_custom(amplitude_pct, duration_ms);
}
}

View File

@ -1,5 +1,5 @@
#[cfg(feature = "haptic")]
use crate::trezorhal::haptic::{self, play, HapticEffect};
use crate::trezorhal::haptic::{self, HapticEffect};
use crate::{
time::{Duration, Instant},
ui::{
@ -18,6 +18,9 @@ use super::theme;
const GROWING_DURATION_MS: u32 = 1000;
const SHRINKING_DURATION_MS: u32 = 500;
const HAPTIC_AMPLITUDE_MAX_PCT: i16 = 16;
const HAPTIC_AMPLITUDE_DURATION_MS: u16 = 100;
pub enum LoaderMsg {
GrownCompletely,
ShrunkCompletely,
@ -169,14 +172,20 @@ impl Component for Loader {
if self.is_completely_grown(now) {
#[cfg(feature = "haptic")]
play(HapticEffect::HoldToConfirm);
haptic::play(HapticEffect::HoldToConfirm);
return Some(LoaderMsg::GrownCompletely);
} else if self.is_completely_shrunk(now) {
return Some(LoaderMsg::ShrunkCompletely);
} else {
let progress = self.progress(now).unwrap() as f32 / 1000.0;
let ampl = i16::lerp(0, 76, progress);
haptic::play_rtp(ampl as i8, 100);
#[cfg(feature = "haptic")]
{
if matches!(self.state, State::Growing(_)) {
let progress =
self.progress(now).unwrap() as f32 / display::LOADER_MAX as f32;
let ampl = i16::lerp(0, HAPTIC_AMPLITUDE_MAX_PCT, progress);
haptic::play_custom(ampl as i8, HAPTIC_AMPLITUDE_DURATION_MS);
}
}
// There is further progress in the animation, request an animation frame event.
ctx.request_anim_frame();

View File

@ -49,8 +49,9 @@ where
T: Component,
{
pub fn with_hold(mut self) -> Result<Self, Error> {
self.button_confirm =
Button::with_text(TR::buttons__hold_to_confirm.into()).styled(theme::button_confirm());
self.button_confirm = Button::with_text(TR::buttons__hold_to_confirm.into())
.styled(theme::button_confirm())
.without_haptics();
self.loader = Some(Loader::new());
Ok(self)
}

View File

@ -28,6 +28,6 @@ void haptic_play(haptic_effect_t effect);
// The function can be invoked repeatedly during the specified duration
// (`duration_ms`) to modify the amplitude dynamically, allowing
// the creation of customized haptic effects.
bool haptic_play_rtp(int8_t amplitude, uint16_t duration_ms);
bool haptic_play_custom(int8_t amplitude_pct, uint16_t duration_ms);
#endif

View File

@ -78,6 +78,7 @@
#define PRESS_EFFECT_AMPLITUDE 25
#define PRESS_EFFECT_DURATION 10
#define MAX_AMPLITUDE 127
#define PRODTEST_EFFECT_AMPLITUDE 127
static bool set_reg(uint8_t addr, uint8_t value) {
@ -144,7 +145,7 @@ void haptic_init(void) {
TIM16->BDTR |= TIM_BDTR_MOE;
}
bool haptic_play_rtp(int8_t amplitude, uint16_t duration_ms) {
static bool haptic_play_rtp(int8_t amplitude, uint16_t duration_ms) {
if (!playing_rtp) {
if (!set_reg(DRV2625_REG_MODE,
DRV2625_REG_MODE_RTP | DRV2625_REG_MODE_TRGFUNC_ENABLE)) {
@ -198,6 +199,17 @@ void haptic_play(haptic_effect_t effect) {
}
}
bool haptic_play_custom(int8_t amplitude_pct, uint16_t duration_ms) {
if (amplitude_pct < 0) {
amplitude_pct = 0;
} else if (amplitude_pct > 100) {
amplitude_pct = 100;
}
return haptic_play_rtp((int8_t)((amplitude_pct * MAX_AMPLITUDE) / 100),
duration_ms);
}
bool haptic_test(uint16_t duration_ms) {
return haptic_play_rtp(PRODTEST_EFFECT_AMPLITUDE, duration_ms);
}