pub struct Button { /* private fields */ }
ev3
only.Expand description
Ev3 brick button handler. Opens the corresponding /dev/input
file handlers.
This implementation depends on the availability of the EVIOCGKEY ioctl to be able to read the button state buffer. See Linux kernel source in /include/uapi/linux/input.h for details.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let mut button = Button::new()?;
button.set_down_handler(|is_pressed| {
println!("Is 'down' pressed: {is_pressed}");
});
loop {
button.process();
println!("Is 'up' pressed: {}", button.is_up());
println!("Pressed buttons: {:?}", button.get_pressed_buttons());
thread::sleep(Duration::from_millis(100));
}
Implementations§
Source§impl Button
impl Button
Sourcepub fn new() -> Ev3Result<Self>
pub fn new() -> Ev3Result<Self>
Ev3 brick button handler. Opens the corresponding /dev/input
file handlers.
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let button = Button::new()?;
7
8 loop {
9 button.process();
10
11 println!(
12 "{}, {}, {}, {}, {}, {}",
13 button.is_up(),
14 button.is_down(),
15 button.is_left(),
16 button.is_right(),
17 button.is_enter(),
18 button.is_backspace(),
19 );
20 println!("{:?}", button.get_pressed_buttons());
21
22 std::thread::sleep(std::time::Duration::from_secs(1));
23 }
24}
More examples
5fn main() -> Ev3Result<()> {
6 let mut button = Button::new()?;
7
8 button.set_change_handler(|pressed_buttons| println!("Pressed buttons: {:?}", pressed_buttons));
9
10 button.set_up_handler(|is_pressed| println!("Button 'up' is pressed: {}", is_pressed));
11 button.set_down_handler(|is_pressed| println!("Button 'down' is pressed: {}", is_pressed));
12 button.set_left_handler(|is_pressed| println!("Button 'left' is pressed: {}", is_pressed));
13 button.set_right_handler(|is_pressed| println!("Button 'right' is pressed: {}", is_pressed));
14 button.set_enter_handler(|is_pressed| println!("Button 'enter' is pressed: {}", is_pressed));
15 button.set_backspace_handler(|is_pressed| {
16 println!("Button 'backspace' is pressed: {}", is_pressed)
17 });
18 button.set_backspace_handler(|is_pressed| {
19 println!("Button 'backspace' is pressed: {}", is_pressed)
20 });
21
22 loop {
23 button.process();
24 std::thread::sleep(std::time::Duration::from_millis(100));
25 }
26}
Sourcepub fn process(&self)
pub fn process(&self)
Check for currently pressed buttons. If the new state differs from the old state, call the appropriate button event handlers.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let mut button = Button::new()?;
button.set_down_handler(|is_pressed| {
println!("Is 'down' pressed: {is_pressed}");
});
loop {
button.process();
println!("Is 'up' pressed: {}", button.is_up());
println!("Pressed buttons: {:?}", button.get_pressed_buttons());
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let button = Button::new()?;
7
8 loop {
9 button.process();
10
11 println!(
12 "{}, {}, {}, {}, {}, {}",
13 button.is_up(),
14 button.is_down(),
15 button.is_left(),
16 button.is_right(),
17 button.is_enter(),
18 button.is_backspace(),
19 );
20 println!("{:?}", button.get_pressed_buttons());
21
22 std::thread::sleep(std::time::Duration::from_secs(1));
23 }
24}
More examples
5fn main() -> Ev3Result<()> {
6 let mut button = Button::new()?;
7
8 button.set_change_handler(|pressed_buttons| println!("Pressed buttons: {:?}", pressed_buttons));
9
10 button.set_up_handler(|is_pressed| println!("Button 'up' is pressed: {}", is_pressed));
11 button.set_down_handler(|is_pressed| println!("Button 'down' is pressed: {}", is_pressed));
12 button.set_left_handler(|is_pressed| println!("Button 'left' is pressed: {}", is_pressed));
13 button.set_right_handler(|is_pressed| println!("Button 'right' is pressed: {}", is_pressed));
14 button.set_enter_handler(|is_pressed| println!("Button 'enter' is pressed: {}", is_pressed));
15 button.set_backspace_handler(|is_pressed| {
16 println!("Button 'backspace' is pressed: {}", is_pressed)
17 });
18 button.set_backspace_handler(|is_pressed| {
19 println!("Button 'backspace' is pressed: {}", is_pressed)
20 });
21
22 loop {
23 button.process();
24 std::thread::sleep(std::time::Duration::from_millis(100));
25 }
26}
Get all pressed buttons by name.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let button = Button::new()?;
loop {
button.process();
println!("Pressed buttons: {:?}", button.get_pressed_buttons());
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let button = Button::new()?;
7
8 loop {
9 button.process();
10
11 println!(
12 "{}, {}, {}, {}, {}, {}",
13 button.is_up(),
14 button.is_down(),
15 button.is_left(),
16 button.is_right(),
17 button.is_enter(),
18 button.is_backspace(),
19 );
20 println!("{:?}", button.get_pressed_buttons());
21
22 std::thread::sleep(std::time::Duration::from_secs(1));
23 }
24}
Sourcepub fn set_change_handler(
&mut self,
handler: impl Fn(HashSet<String>) + 'static,
)
pub fn set_change_handler( &mut self, handler: impl Fn(HashSet<String>) + 'static, )
Set an event handler, that is called by process()
if any button state changes.
Has a set of all pressed buttons as parameter.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let mut button = Button::new()?;
button.set_change_handler(|pressed_buttons| {
println!("pressed buttons: {:?}", pressed_buttons);
});
loop {
button.process();
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let mut button = Button::new()?;
7
8 button.set_change_handler(|pressed_buttons| println!("Pressed buttons: {:?}", pressed_buttons));
9
10 button.set_up_handler(|is_pressed| println!("Button 'up' is pressed: {}", is_pressed));
11 button.set_down_handler(|is_pressed| println!("Button 'down' is pressed: {}", is_pressed));
12 button.set_left_handler(|is_pressed| println!("Button 'left' is pressed: {}", is_pressed));
13 button.set_right_handler(|is_pressed| println!("Button 'right' is pressed: {}", is_pressed));
14 button.set_enter_handler(|is_pressed| println!("Button 'enter' is pressed: {}", is_pressed));
15 button.set_backspace_handler(|is_pressed| {
16 println!("Button 'backspace' is pressed: {}", is_pressed)
17 });
18 button.set_backspace_handler(|is_pressed| {
19 println!("Button 'backspace' is pressed: {}", is_pressed)
20 });
21
22 loop {
23 button.process();
24 std::thread::sleep(std::time::Duration::from_millis(100));
25 }
26}
Sourcepub fn remove_change_handler(&mut self)
pub fn remove_change_handler(&mut self)
Removes the change event handler.
Sourcepub fn is_up(&self) -> bool
pub fn is_up(&self) -> bool
Check if the up
button is pressed.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let button = Button::new()?;
loop {
button.process();
println!("Is 'up' pressed: {}", button.is_up());
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let button = Button::new()?;
7
8 loop {
9 button.process();
10
11 println!(
12 "{}, {}, {}, {}, {}, {}",
13 button.is_up(),
14 button.is_down(),
15 button.is_left(),
16 button.is_right(),
17 button.is_enter(),
18 button.is_backspace(),
19 );
20 println!("{:?}", button.get_pressed_buttons());
21
22 std::thread::sleep(std::time::Duration::from_secs(1));
23 }
24}
Sourcepub fn set_up_handler(&mut self, handler: impl Fn(bool) + 'static)
pub fn set_up_handler(&mut self, handler: impl Fn(bool) + 'static)
Set an event handler, that is called by process()
if the pressed state of the up
button changes.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let mut button = Button::new()?;
button.set_up_handler(|is_pressed| {
println!("Is 'up' pressed: {}", is_pressed);
});
loop {
button.process();
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let mut button = Button::new()?;
7
8 button.set_change_handler(|pressed_buttons| println!("Pressed buttons: {:?}", pressed_buttons));
9
10 button.set_up_handler(|is_pressed| println!("Button 'up' is pressed: {}", is_pressed));
11 button.set_down_handler(|is_pressed| println!("Button 'down' is pressed: {}", is_pressed));
12 button.set_left_handler(|is_pressed| println!("Button 'left' is pressed: {}", is_pressed));
13 button.set_right_handler(|is_pressed| println!("Button 'right' is pressed: {}", is_pressed));
14 button.set_enter_handler(|is_pressed| println!("Button 'enter' is pressed: {}", is_pressed));
15 button.set_backspace_handler(|is_pressed| {
16 println!("Button 'backspace' is pressed: {}", is_pressed)
17 });
18 button.set_backspace_handler(|is_pressed| {
19 println!("Button 'backspace' is pressed: {}", is_pressed)
20 });
21
22 loop {
23 button.process();
24 std::thread::sleep(std::time::Duration::from_millis(100));
25 }
26}
Sourcepub fn remove_up_handler(&mut self)
pub fn remove_up_handler(&mut self)
Removes the event handler of the up
button.
Sourcepub fn is_down(&self) -> bool
pub fn is_down(&self) -> bool
Check if the down
button is pressed.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let button = Button::new()?;
loop {
button.process();
println!("Is 'down' pressed: {}", button.is_down());
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let button = Button::new()?;
7
8 loop {
9 button.process();
10
11 println!(
12 "{}, {}, {}, {}, {}, {}",
13 button.is_up(),
14 button.is_down(),
15 button.is_left(),
16 button.is_right(),
17 button.is_enter(),
18 button.is_backspace(),
19 );
20 println!("{:?}", button.get_pressed_buttons());
21
22 std::thread::sleep(std::time::Duration::from_secs(1));
23 }
24}
Sourcepub fn set_down_handler(&mut self, handler: impl Fn(bool) + 'static)
pub fn set_down_handler(&mut self, handler: impl Fn(bool) + 'static)
Set an event handler, that is called by process()
if the pressed state of the down
button changes.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let mut button = Button::new()?;
button.set_down_handler(|is_pressed| {
println!("Is 'down' pressed: {}", is_pressed);
});
loop {
button.process();
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let mut button = Button::new()?;
7
8 button.set_change_handler(|pressed_buttons| println!("Pressed buttons: {:?}", pressed_buttons));
9
10 button.set_up_handler(|is_pressed| println!("Button 'up' is pressed: {}", is_pressed));
11 button.set_down_handler(|is_pressed| println!("Button 'down' is pressed: {}", is_pressed));
12 button.set_left_handler(|is_pressed| println!("Button 'left' is pressed: {}", is_pressed));
13 button.set_right_handler(|is_pressed| println!("Button 'right' is pressed: {}", is_pressed));
14 button.set_enter_handler(|is_pressed| println!("Button 'enter' is pressed: {}", is_pressed));
15 button.set_backspace_handler(|is_pressed| {
16 println!("Button 'backspace' is pressed: {}", is_pressed)
17 });
18 button.set_backspace_handler(|is_pressed| {
19 println!("Button 'backspace' is pressed: {}", is_pressed)
20 });
21
22 loop {
23 button.process();
24 std::thread::sleep(std::time::Duration::from_millis(100));
25 }
26}
Sourcepub fn remove_down_handler(&mut self)
pub fn remove_down_handler(&mut self)
Removes the event handler of the down
button.
Sourcepub fn is_left(&self) -> bool
pub fn is_left(&self) -> bool
Check if the left
button is pressed.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let button = Button::new()?;
loop {
button.process();
println!("Is 'left' pressed: {}", button.is_left());
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let button = Button::new()?;
7
8 loop {
9 button.process();
10
11 println!(
12 "{}, {}, {}, {}, {}, {}",
13 button.is_up(),
14 button.is_down(),
15 button.is_left(),
16 button.is_right(),
17 button.is_enter(),
18 button.is_backspace(),
19 );
20 println!("{:?}", button.get_pressed_buttons());
21
22 std::thread::sleep(std::time::Duration::from_secs(1));
23 }
24}
Sourcepub fn set_left_handler(&mut self, handler: impl Fn(bool) + 'static)
pub fn set_left_handler(&mut self, handler: impl Fn(bool) + 'static)
Set an event handler, that is called by process()
if the pressed state of the left
button changes.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let mut button = Button::new()?;
button.set_left_handler(|is_pressed| {
println!("Is 'left' pressed: {}", is_pressed);
});
loop {
button.process();
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let mut button = Button::new()?;
7
8 button.set_change_handler(|pressed_buttons| println!("Pressed buttons: {:?}", pressed_buttons));
9
10 button.set_up_handler(|is_pressed| println!("Button 'up' is pressed: {}", is_pressed));
11 button.set_down_handler(|is_pressed| println!("Button 'down' is pressed: {}", is_pressed));
12 button.set_left_handler(|is_pressed| println!("Button 'left' is pressed: {}", is_pressed));
13 button.set_right_handler(|is_pressed| println!("Button 'right' is pressed: {}", is_pressed));
14 button.set_enter_handler(|is_pressed| println!("Button 'enter' is pressed: {}", is_pressed));
15 button.set_backspace_handler(|is_pressed| {
16 println!("Button 'backspace' is pressed: {}", is_pressed)
17 });
18 button.set_backspace_handler(|is_pressed| {
19 println!("Button 'backspace' is pressed: {}", is_pressed)
20 });
21
22 loop {
23 button.process();
24 std::thread::sleep(std::time::Duration::from_millis(100));
25 }
26}
Sourcepub fn remove_left_handler(&mut self)
pub fn remove_left_handler(&mut self)
Removes the event handler of the left
button.
Sourcepub fn is_right(&self) -> bool
pub fn is_right(&self) -> bool
Check if the right
button is pressed.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let button = Button::new()?;
loop {
button.process();
println!("Is 'right' pressed: {}", button.is_right());
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let button = Button::new()?;
7
8 loop {
9 button.process();
10
11 println!(
12 "{}, {}, {}, {}, {}, {}",
13 button.is_up(),
14 button.is_down(),
15 button.is_left(),
16 button.is_right(),
17 button.is_enter(),
18 button.is_backspace(),
19 );
20 println!("{:?}", button.get_pressed_buttons());
21
22 std::thread::sleep(std::time::Duration::from_secs(1));
23 }
24}
Sourcepub fn set_right_handler(&mut self, handler: impl Fn(bool) + 'static)
pub fn set_right_handler(&mut self, handler: impl Fn(bool) + 'static)
Set an event handler, that is called by process()
if the pressed state of the right
button changes.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let mut button = Button::new()?;
button.set_right_handler(|is_pressed| {
println!("Is 'right' pressed: {}", is_pressed);
});
loop {
button.process();
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let mut button = Button::new()?;
7
8 button.set_change_handler(|pressed_buttons| println!("Pressed buttons: {:?}", pressed_buttons));
9
10 button.set_up_handler(|is_pressed| println!("Button 'up' is pressed: {}", is_pressed));
11 button.set_down_handler(|is_pressed| println!("Button 'down' is pressed: {}", is_pressed));
12 button.set_left_handler(|is_pressed| println!("Button 'left' is pressed: {}", is_pressed));
13 button.set_right_handler(|is_pressed| println!("Button 'right' is pressed: {}", is_pressed));
14 button.set_enter_handler(|is_pressed| println!("Button 'enter' is pressed: {}", is_pressed));
15 button.set_backspace_handler(|is_pressed| {
16 println!("Button 'backspace' is pressed: {}", is_pressed)
17 });
18 button.set_backspace_handler(|is_pressed| {
19 println!("Button 'backspace' is pressed: {}", is_pressed)
20 });
21
22 loop {
23 button.process();
24 std::thread::sleep(std::time::Duration::from_millis(100));
25 }
26}
Sourcepub fn remove_right_handler(&mut self)
pub fn remove_right_handler(&mut self)
Removes the event handler of the right
button.
Sourcepub fn is_enter(&self) -> bool
pub fn is_enter(&self) -> bool
Check if the enter
button is pressed.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let button = Button::new()?;
loop {
button.process();
println!("Is 'enter' pressed: {}", button.is_enter());
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let button = Button::new()?;
7
8 loop {
9 button.process();
10
11 println!(
12 "{}, {}, {}, {}, {}, {}",
13 button.is_up(),
14 button.is_down(),
15 button.is_left(),
16 button.is_right(),
17 button.is_enter(),
18 button.is_backspace(),
19 );
20 println!("{:?}", button.get_pressed_buttons());
21
22 std::thread::sleep(std::time::Duration::from_secs(1));
23 }
24}
Sourcepub fn set_enter_handler(&mut self, handler: impl Fn(bool) + 'static)
pub fn set_enter_handler(&mut self, handler: impl Fn(bool) + 'static)
Set an event handler, that is called by process()
if the pressed state of the enter
button changes.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let mut button = Button::new()?;
button.set_enter_handler(|is_pressed| {
println!("Is 'enter' pressed: {}", is_pressed);
});
loop {
button.process();
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let mut button = Button::new()?;
7
8 button.set_change_handler(|pressed_buttons| println!("Pressed buttons: {:?}", pressed_buttons));
9
10 button.set_up_handler(|is_pressed| println!("Button 'up' is pressed: {}", is_pressed));
11 button.set_down_handler(|is_pressed| println!("Button 'down' is pressed: {}", is_pressed));
12 button.set_left_handler(|is_pressed| println!("Button 'left' is pressed: {}", is_pressed));
13 button.set_right_handler(|is_pressed| println!("Button 'right' is pressed: {}", is_pressed));
14 button.set_enter_handler(|is_pressed| println!("Button 'enter' is pressed: {}", is_pressed));
15 button.set_backspace_handler(|is_pressed| {
16 println!("Button 'backspace' is pressed: {}", is_pressed)
17 });
18 button.set_backspace_handler(|is_pressed| {
19 println!("Button 'backspace' is pressed: {}", is_pressed)
20 });
21
22 loop {
23 button.process();
24 std::thread::sleep(std::time::Duration::from_millis(100));
25 }
26}
Sourcepub fn remove_enter_handler(&mut self)
pub fn remove_enter_handler(&mut self)
Removes the event handler of the enter
button.
Sourcepub fn is_backspace(&self) -> bool
pub fn is_backspace(&self) -> bool
Check if the backspace
button is pressed.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let button = Button::new()?;
loop {
button.process();
println!("Is 'backspace' pressed: {}", button.is_backspace());
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let button = Button::new()?;
7
8 loop {
9 button.process();
10
11 println!(
12 "{}, {}, {}, {}, {}, {}",
13 button.is_up(),
14 button.is_down(),
15 button.is_left(),
16 button.is_right(),
17 button.is_enter(),
18 button.is_backspace(),
19 );
20 println!("{:?}", button.get_pressed_buttons());
21
22 std::thread::sleep(std::time::Duration::from_secs(1));
23 }
24}
Sourcepub fn set_backspace_handler(&mut self, handler: impl Fn(bool) + 'static)
pub fn set_backspace_handler(&mut self, handler: impl Fn(bool) + 'static)
Set an event handler, that is called by process()
if the pressed state of the backspace
button changes.
use ev3dev_lang_rust::Button;
use std::thread;
use std::time::Duration;
let mut button = Button::new()?;
button.set_backspace_handler(|is_pressed| {
println!("Is 'backspace' pressed: {}", is_pressed);
});
loop {
button.process();
thread::sleep(Duration::from_millis(100));
}
Examples found in repository?
5fn main() -> Ev3Result<()> {
6 let mut button = Button::new()?;
7
8 button.set_change_handler(|pressed_buttons| println!("Pressed buttons: {:?}", pressed_buttons));
9
10 button.set_up_handler(|is_pressed| println!("Button 'up' is pressed: {}", is_pressed));
11 button.set_down_handler(|is_pressed| println!("Button 'down' is pressed: {}", is_pressed));
12 button.set_left_handler(|is_pressed| println!("Button 'left' is pressed: {}", is_pressed));
13 button.set_right_handler(|is_pressed| println!("Button 'right' is pressed: {}", is_pressed));
14 button.set_enter_handler(|is_pressed| println!("Button 'enter' is pressed: {}", is_pressed));
15 button.set_backspace_handler(|is_pressed| {
16 println!("Button 'backspace' is pressed: {}", is_pressed)
17 });
18 button.set_backspace_handler(|is_pressed| {
19 println!("Button 'backspace' is pressed: {}", is_pressed)
20 });
21
22 loop {
23 button.process();
24 std::thread::sleep(std::time::Duration::from_millis(100));
25 }
26}
Sourcepub fn remove_backspace_handler(&mut self)
pub fn remove_backspace_handler(&mut self)
Removes the event handler of the backspace
button.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Button
impl !RefUnwindSafe for Button
impl !Send for Button
impl !Sync for Button
impl Unpin for Button
impl !UnwindSafe for Button
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more