Skip to content

Adding a timer with vibration #12

Description

@realroot2185

I added a timer:

//state.rs
impl TimerState {
    pub async fn draw(&mut self, _device: &mut Device<'_>) {}
    pub async fn next(&mut self, device: &mut Device<'_>) -> WatchState {
        let screen = &mut device.screen;
        let button = &mut device.button;
      	let mut secs = 60;
      	
        let timer = async {
            loop {
                TimerView::new(time::Duration::seconds(secs))
                    .draw(screen.display())
                    .unwrap();
                screen.on();
                Timer::after_secs(1).await;
                secs -= 1;

                if secs <= 0 {
                    break;
                }
            }
        };
        let next = match select(button.wait(), timer).await {
            Either::First(_) => WatchState::Menu(MenuState::new(MenuView::main())),
            //What am I supposed to do here with `Either::Second(_)`?
            Either::Second(_) => WatchState::Menu(MenuState::new(MenuView::main())),
        };
        next
    }
}

//lib.rs
impl TimerView {
    pub fn new(remaining: time::Duration) -> Self {
        Self { remaining }
    }
    pub fn draw<D: DrawTarget<Color = Rgb>>(&self, display: &mut D) -> Result<(), D::Error> {
        display.clear(Rgb::BLACK)?;
        
        let mut buf: heapless::String<16> = heapless::String::new();
        write!(
            buf,
            "{:02}:{:02}",
            self.remaining.whole_minutes(),
            self.remaining.whole_seconds() % 60,
        )
        .unwrap();

		let cd = Text::with_text_style(
            &buf,
            display.bounding_box().center(),
            watch_text_style(Rgb::CSS_LIME),
            TextStyleBuilder::new()
                .alignment(embedded_graphics::text::Alignment::Center)
                .baseline(embedded_graphics::text::Baseline::Alphabetic)
                .build(),
        );

        let display_area = display.bounding_box();
        LinearLayout::vertical(Chain::new(cd))
            .with_spacing(spacing::FixedMargin(10))
            .with_alignment(horizontal::Center)
            .arrange()
            .align_to(&display_area, horizontal::Center, vertical::Center)
            .draw(display)?;

        Ok(())
    }
}

I think that it would be nice to have the pinetime vibrate when it ends.

Right now the timer is too slow 1 minute takes like 120 seconds.
Can I fix it?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions