wasefire_board_api/timer.rs
1// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Timer interface.
16//!
17//! A timer triggers an event after a given amount of time (possibly periodically).
18
19use derive_where::derive_where;
20
21use crate::{Error, Id, Support};
22
23/// Timer event.
24#[cfg_attr(feature = "defmt", derive(defmt::Format))]
25#[derive_where(Debug, PartialEq, Eq)]
26pub struct Event<B: crate::Api + ?Sized> {
27 /// The timer that triggered the event.
28 pub timer: Id<crate::Timer<B>>,
29}
30
31impl<B: crate::Api> From<Event<B>> for crate::Event<B> {
32 fn from(event: Event<B>) -> Self {
33 crate::Event::Timer(event)
34 }
35}
36
37/// Timer interface.
38pub trait Api: Support<usize> + Send {
39 /// Arms a timer to trigger according to a command.
40 fn arm(timer: Id<Self>, command: &Command) -> Result<(), Error>;
41
42 /// Disarms a timer regardless of whether it already triggered.
43 ///
44 /// The timer won't trigger further events.
45 fn disarm(timer: Id<Self>) -> Result<(), Error>;
46}
47
48/// Timer configuration.
49#[derive(Debug, Clone)]
50pub struct Command {
51 /// Whether the timer should periodically trigger.
52 pub periodic: bool,
53
54 /// Duration in milliseconds after which the timer should trigger.
55 pub duration_ms: usize,
56}