testing haptic feedback - for designers

cepetr/haptic-test
cepetr 3 weeks ago
parent e9f6eb0b2b
commit fcf4ef8995

@ -407,7 +407,8 @@ fn generate_trezorhal_bindings() {
.allowlist_function("button_read")
// haptic
.allowlist_type("haptic_effect_t")
.allowlist_function("haptic_play");
.allowlist_function("haptic_play")
.allowlist_function("haptic_play_raw");
// Write the bindings to a file in the OUR_DIR.
bindings

@ -8,6 +8,12 @@ pub enum HapticEffect {
pub fn play(effect: HapticEffect) {
unsafe {
ffi::haptic_play(effect as _);
//ffi::haptic_play(effect as _);
}
}
pub fn play_raw(effect: u8) {
unsafe {
ffi::haptic_play_raw(effect);
}
}

@ -1,5 +1,6 @@
mod render;
use heapless::String;
use crate::{
strutil::TString,
time::{Duration, Instant},
@ -32,6 +33,9 @@ use render::{
homescreen, homescreen_blurred, HomescreenNotification, HomescreenText,
HOMESCREEN_IMAGE_HEIGHT, HOMESCREEN_IMAGE_WIDTH,
};
use crate::trezorhal::haptic;
use crate::ui::component::ComponentExt;
use crate::ui::model_tt::component::{PinKeyboard, PinKeyboardMsg};
use super::{theme, Loader, LoaderMsg};
@ -53,7 +57,10 @@ pub struct Homescreen {
loader: Loader,
pad: Pad,
paint_notification_only: bool,
pin: bool,
delay: Option<TimerToken>,
kbd: PinKeyboard<'static>,
effect: u8,
}
pub enum HomescreenMsg {
@ -74,6 +81,9 @@ impl Homescreen {
pad: Pad::with_background(theme::BG),
paint_notification_only: false,
delay: None,
pin: false,
kbd: PinKeyboard::new("Enter effect num".into(), "".into(), None, true),
effect: 1,
}
}
@ -86,24 +96,16 @@ impl Homescreen {
}
}
fn get_notification(&self) -> Option<HomescreenNotification> {
if !usb_configured() {
let (color, icon) = Self::level_to_style(0);
Some(HomescreenNotification {
text: TR::homescreen__title_no_usb_connection.into(),
icon,
color,
})
} else if let Some((notification, level)) = self.notification {
let (color, icon) = Self::level_to_style(level);
Some(HomescreenNotification {
text: notification,
icon,
color,
})
} else {
None
}
fn get_notification<'a>(&'a self, s: &'a mut String<20>) -> Option<HomescreenNotification> {
let (color, icon) = Self::level_to_style(0);
unwrap!(s.push_str("Effect: "));
unwrap!(s.push_str(inttostr!(self.effect)));
Some(HomescreenNotification {
text: TString::from_str(s.as_str()),
icon,
color,
})
}
fn paint_loader(&mut self) {
@ -180,22 +182,70 @@ impl Component for Homescreen {
fn place(&mut self, bounds: Rect) -> Rect {
self.pad.place(AREA);
self.kbd.place(AREA);
self.loader.place(AREA.translate(LOADER_OFFSET));
bounds
}
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
Self::event_usb(self, ctx, event);
if self.hold_to_lock {
Self::event_hold(self, ctx, event).then_some(HomescreenMsg::Dismissed)
} else {
if let Event::Touch(TouchEvent::TouchStart(_)) = event {
haptic::play_raw(self.effect);
if !self.pin{
self.pin= true;
self.kbd.clear(ctx);
self.pad.clear();
ctx.request_paint();
self.request_complete_repaint(ctx);
return None;
}
}
if self.pin {
if let Some(e)=self.kbd.event(ctx, event) {
match e {
PinKeyboardMsg::Confirmed => {
let pin = self.kbd.pin();
pin.parse().ok().map(|pin :u32| {
if pin <= 255 {
if pin == 0 {
self.effect = 1;
} else {
self.effect = pin as u8;
}
}
self.pin = false;
});
self.pad.clear();
self.pin = false;
ctx.request_paint();
self.request_complete_repaint(ctx);
}
PinKeyboardMsg::Cancelled => {
self.pin = false;
self.pad.clear();
ctx.request_paint();
self.request_complete_repaint(ctx);
}
}
}
None
}else {
Self::event_usb(self, ctx, event);
if self.hold_to_lock {
Self::event_hold(self, ctx, event).then_some(HomescreenMsg::Dismissed)
} else {
None
}
}
}
fn paint(&mut self) {
self.pad.paint();
if self.loader.is_animating() || self.loader.is_completely_grown(Instant::now()) {
if self.pin {
self.kbd.paint();
} else if self.loader.is_animating() || self.loader.is_completely_grown(Instant::now()) {
self.paint_loader();
} else {
let mut label_style = theme::TEXT_DEMIBOLD;
@ -208,7 +258,9 @@ impl Component for Homescreen {
icon: None,
};
let notification = self.get_notification();
let mut s: String<20> = String::new();
let notification = self.get_notification(&mut s);
let res = get_user_custom_image();
let mut show_default = true;

@ -32,8 +32,8 @@ pub struct HomescreenText<'a> {
}
#[derive(Clone, Copy)]
pub struct HomescreenNotification {
pub text: TString<'static>,
pub struct HomescreenNotification<'a> {
pub text: TString<'a>,
pub icon: Icon,
pub color: Color,
}

@ -103,7 +103,7 @@ impl<'a> PinKeyboard<'a> {
fn generate_digit_buttons() -> [Child<Button>; DIGIT_COUNT] {
// Generate a random sequence of digits from 0 to 9.
let mut digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
random::shuffle(&mut digits);
//random::shuffle(&mut digits);
digits
.map(|c| Button::with_text(c.into()))
.map(|b| b.styled(theme::button_pin()))
@ -142,6 +142,11 @@ impl<'a> PinKeyboard<'a> {
pub fn pin(&self) -> &str {
self.textbox.inner().pin()
}
pub fn clear(&mut self, ctx: &mut EventCtx) {
self.textbox.mutate(ctx, |ctx, t| t.clear(ctx));
self.pin_modified(ctx);
}
}
impl Component for PinKeyboard<'_> {
@ -297,7 +302,7 @@ impl PinDots {
pad: Pad::with_background(style.background_color),
style,
digits: String::new(),
display_digits: false,
display_digits: true,
}
}
@ -429,7 +434,7 @@ impl Component for PinDots {
None
}
Event::Touch(TouchEvent::TouchEnd(_)) => {
if mem::replace(&mut self.display_digits, false) {
if mem::replace(&mut self.display_digits, true) {
self.pad.clear();
ctx.request_paint();
};

@ -23,4 +23,6 @@ bool haptic_test(uint16_t duration_ms);
// Play haptic effect
void haptic_play(haptic_effect_t effect);
void haptic_play_raw(uint8_t effect);
#endif

@ -188,6 +188,18 @@ void haptic_play(haptic_effect_t effect) {
}
}
void haptic_play_raw(uint8_t effect) {
if (effect < 124) {
haptic_play_lib((haptic_effect_t) effect);
} else {
switch (effect) {
case 200:
haptic_play(HAPTIC_BUTTON_PRESS);
break;
}
}
}
bool haptic_test(uint16_t duration_ms) {
return haptic_play_RTP(PRODTEST_EFFECT_AMPLITUDE, duration_ms);
}

@ -76,7 +76,7 @@ void i2c_init_instance(uint16_t idx, i2c_instance_t *instance) {
// configure CTP I2C SCL and SDA GPIO lines
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed =
GPIO_SPEED_FREQ_LOW; // I2C is a KHz bus and low speed is still good into
// the low MHz

Loading…
Cancel
Save