Struct KeypadListener

Source
pub struct KeypadListener<'a> { /* private fields */ }
Expand description

Polls the keypad.

use ndless_async::task::{block_on, AsyncListeners};
use ndless_async::keypad::{KeypadListener, KeyState};
use ndless_async::StreamExt;

let listeners = AsyncListeners::new();
block_on(&listeners, async {
    let keypad = KeypadListener::new(&listeners.timer());
    let mut stream = keypad.stream();
    while let Some(event) = stream.next().await {
        println!(
            "Key {:?} was {}",
            event.key,
            if event.state == KeyState::Released {
                "released"
            } else {
                "pressed"
            }
        );
    }
});

Implementations§

Source§

impl<'a> KeypadListener<'a>

Source

pub fn new(timer_listener: &'a TimerListener) -> Self

Creates a new keypad listener that polls the keypad 30 times per second. Use the new_with_* series of functions to change the polling rate. You may also poll the keypad manually by using new_manually_polled.

Source

pub fn new_with_hz(timer_listener: &'a TimerListener, hz: u32) -> Self

Creates a new keypad listener that polls the keypad with the specified number of events per second.

Source

pub fn new_with_ms(timer_listener: &'a TimerListener, dur: u32) -> Self

Creates a new keypad listener that polls the keypad every dur milliseconds.

Source

pub fn new_with_rate(timer_listener: &'a TimerListener, dur: Duration) -> Self

Creates a new keypad listener that polls the keypad with the specified interval.

Source

pub fn new_with_ticks(timer_listener: &'a TimerListener, ticks: u32) -> Self

Creates a new keypad listener that polls the keypad every specified ticks.

Source

pub fn new_manually_polled() -> Self

Creates a new keypad listener that isn’t automatically polled. You’ll need to use poll periodically to poll the keypad.

Source

pub fn poll(&self)

Polls the keypad. You shouldn’t have to use this normally.

Source

pub fn stream(&self) -> KeyStream

Each call to stream returns a unique stream, meaning that calling it from different tasks will allow each task to receive every event. A buffer of 100 keypress events is allocated. Use stream_with_buffer to specify a custom size.

§Warning

Don’t use this function in a loop. You should call stream before the loop, or use a stream combinator such as for_each. Failure to do so will result in lost events and less efficient code.

Source

pub fn stream_with_buffer(&self, size: usize) -> KeyStream

This is the same as stream, except that it allows specifying a buffer size other than the default of 100.

§Warning

Don’t use this function in a loop. You should call stream_with_buffer before the loop, or use a stream combinator such as for_each. Failure to do so will result in lost events and less efficient code.

Source

pub fn list_keys(&self) -> Ref<'_, Vec<Key>>

Returns a Ref to the keys that are currently pressed. You must drop this reference either explicitly or by ending the current scope before .awaiting something. The program will crash otherwise.

Auto Trait Implementations§

§

impl<'a> !Freeze for KeypadListener<'a>

§

impl<'a> !RefUnwindSafe for KeypadListener<'a>

§

impl<'a> !Send for KeypadListener<'a>

§

impl<'a> !Sync for KeypadListener<'a>

§

impl<'a> Unpin for KeypadListener<'a>

§

impl<'a> !UnwindSafe for KeypadListener<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.