wasefire_board_api/
button.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//! Button interface.
16//!
17//! A button is an input interface with 2 states: pressed and released. Buttons must support
18//! triggering events when changing state. Events may be enabled or disabled per button.
19
20use derive_where::derive_where;
21
22use crate::{Error, Id, Support};
23
24/// Button event.
25#[cfg_attr(feature = "defmt", derive(defmt::Format))]
26#[derive_where(Debug, PartialEq, Eq)]
27pub struct Event<B: crate::Api + ?Sized> {
28    /// The button that triggered the event.
29    pub button: Id<crate::Button<B>>,
30
31    /// Whether the event was a button press or release.
32    pub pressed: bool,
33}
34
35impl<B: crate::Api> From<Event<B>> for crate::Event<B> {
36    fn from(event: Event<B>) -> Self {
37        crate::Event::Button(event)
38    }
39}
40
41/// Button interface.
42pub trait Api: Support<usize> + Send {
43    /// Enables events for a given button.
44    fn enable(button: Id<Self>) -> Result<(), Error>;
45
46    /// Disables events for a given button.
47    fn disable(button: Id<Self>) -> Result<(), Error>;
48}