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>
impl<'a> KeypadListener<'a>
Sourcepub fn new(timer_listener: &'a TimerListener) -> Self
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
.
Sourcepub fn new_with_hz(timer_listener: &'a TimerListener, hz: u32) -> Self
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.
Sourcepub fn new_with_ms(timer_listener: &'a TimerListener, dur: u32) -> Self
pub fn new_with_ms(timer_listener: &'a TimerListener, dur: u32) -> Self
Creates a new keypad listener that polls the keypad every dur
milliseconds.
Sourcepub fn new_with_rate(timer_listener: &'a TimerListener, dur: Duration) -> Self
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.
Sourcepub fn new_with_ticks(timer_listener: &'a TimerListener, ticks: u32) -> Self
pub fn new_with_ticks(timer_listener: &'a TimerListener, ticks: u32) -> Self
Creates a new keypad listener that polls the keypad every specified ticks.
Sourcepub fn new_manually_polled() -> Self
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.
Sourcepub fn stream(&self) -> KeyStream
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.
Sourcepub fn stream_with_buffer(&self, size: usize) -> KeyStream
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.