hermes_five/devices/input/mod.rs
1use crate::devices::Device;
2use crate::utils::State;
3
4pub mod analog;
5pub mod button;
6pub mod digital;
7
8/// A trait for devices that can sense or measure data: they "input" some data into the board.
9///
10/// This trait extends [`Device`] and is intended for sensors that require the same capabilities
11/// as devices, including debugging, cloning, and concurrency support.
12#[cfg_attr(feature = "serde", typetag::serde(tag = "type"))]
13pub trait Input: Device {
14 /// Returns the sensor current state.
15 fn get_state(&self) -> State;
16}
17dyn_clone::clone_trait_object!(Input);
18
19/// Lists all events a Input type device can emit/listen.
20pub enum InputEvent {
21 /// Triggered when the Input value changes.
22 OnChange,
23 /// Triggered when the button is pressed.
24 OnPress,
25 /// Triggered when the button is released.
26 OnRelease,
27 /// Triggered when a value changes to HIGH.
28 OnHigh,
29 /// Triggered when a value changes to LOW.
30 OnLow,
31}
32
33/// Convert events to string to facilitate usage with [`EventManager`](crate::utils::EventManager).
34impl From<InputEvent> for String {
35 fn from(value: InputEvent) -> Self {
36 let event = match value {
37 InputEvent::OnChange => "change",
38 InputEvent::OnPress => "press",
39 InputEvent::OnRelease => "release",
40 InputEvent::OnHigh => "high",
41 InputEvent::OnLow => "low",
42 };
43 event.into()
44 }
45}