Skip to main content

rmicrobit/buttons/
builtin.rs

1//! Access to the micro:bit's built-in buttons.
2//!
3//! This module defines:
4//!  - [`Button`] implementations for the micro:bit's buttons A and B
5//!  - Monitor implementations specialised to those buttons.
6//!
7//! The normal way to access these is via the re-exports in the [`buttons`]
8//! module.
9//!
10//! [`buttons`]: crate::buttons
11//! [`Button`]: crate::buttons::core::Button
12
13use nrf51_hal::gpio::{Floating, Input};
14use nrf51_hal::gpio::gpio::{PIN17, PIN26};
15use crate::gpio::ButtonPins;
16use crate::buttons::core::Button;
17use crate::buttons::debouncing::TrivialDebouncer;
18use crate::buttons::monitors::holding::DefaultHoldDescriptor;
19use crate::buttons::monitors::single;
20use crate::buttons::monitors::dual;
21use crate::buttons::monitors::single_with_hold;
22use crate::buttons::monitors::dual_with_hold;
23
24/// The micro:bit's 'A' (left) button, with no debouncing.
25pub type ButtonA = Button<PIN17<Input<Floating>>, TrivialDebouncer>;
26/// The micro:bit's 'B' (right) button, with no debouncing.
27pub type ButtonB = Button<PIN26<Input<Floating>>, TrivialDebouncer>;
28
29/// Make [`ButtonA`] and [`ButtonB`] from the GPIO pins.
30pub fn from_pins(pins: ButtonPins) -> (ButtonA, ButtonB) {
31    // See https://github.com/nrf-rs/nrf51-hal/issues/20
32    (ButtonA::new(pins.pin17.into_floating_input()),
33     ButtonB::new(pins.pin26.into_floating_input()))
34}
35
36/// Wrapper for the micro:bit's 'A' (left) button generating click events on
37/// release.
38pub type LazyButtonAMonitor = single::LazyMonitor<ButtonA>;
39/// Wrapper for the micro:bit's 'B' (right) button generating click events on
40/// release.
41pub type LazyButtonBMonitor = single::LazyMonitor<ButtonB>;
42/// Wrapper for the micro:bit's 'A' (left) button generating click events on
43/// press.
44pub type EagerButtonAMonitor = single::EagerMonitor<ButtonA>;
45/// Wrapper for the micro:bit's 'B' (right) button generating click events on
46/// press.
47pub type EagerButtonBMonitor = single::EagerMonitor<ButtonB>;
48
49
50/// Wrapper for the micro:bit's 'A' (left) button generating click and hold
51/// events.
52pub type ButtonAMonitorWithHold =
53    single_with_hold::Monitor<ButtonA, DefaultHoldDescriptor>;
54/// Wrapper for the micro:bit's 'B' (right) button generating click and hold
55/// events.
56pub type ButtonBMonitorWithHold =
57    single_with_hold::Monitor<ButtonB, DefaultHoldDescriptor>;
58
59/// Wrapper for the micro:bit's two buttons generating click events on
60/// release.
61pub type ABMonitor = dual::Monitor<ButtonA, ButtonB>;
62
63/// Wrapper for the micro:bit's two buttons generating click and hold events.
64pub type ABMonitorWithHold =
65    dual_with_hold::Monitor<ButtonA, ButtonB, DefaultHoldDescriptor>;
66