forked from larry-robotics/elkodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[larry-robotics#3] Remove the SignalHandler from all public examples …
…and introduce `Elk::wait()` has replacement
- Loading branch information
Showing
8 changed files
with
152 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//! # Example | ||
//! | ||
//! ## Simple Event Loop | ||
//! | ||
//! ```no_run | ||
//! use elkodon::prelude::*; | ||
//! | ||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
//! const CYCLE_TIME: Duration = Duration::from_secs(1); | ||
//! | ||
//! while let ElkEvent::Tick = Elk::wait(CYCLE_TIME) { | ||
//! // your algorithm in here | ||
//! } | ||
//! # Ok(()) | ||
//! # } | ||
//! ``` | ||
//! | ||
//! ## Advanced Event Loop | ||
//! | ||
//! ```no_run | ||
//! use elkodon::prelude::*; | ||
//! | ||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
//! const CYCLE_TIME: Duration = Duration::from_secs(1); | ||
//! | ||
//! loop { | ||
//! match Elk::wait(CYCLE_TIME) { | ||
//! ElkEvent::Tick => { | ||
//! println!("entered next cycle"); | ||
//! } | ||
//! ElkEvent::TerminationRequest => { | ||
//! println!("User pressed CTRL+c, terminating"); | ||
//! break; | ||
//! } | ||
//! ElkEvent::InterruptSignal => { | ||
//! println!("Someone send an interrupt signal ..."); | ||
//! } | ||
//! } | ||
//! } | ||
//! # Ok(()) | ||
//! # } | ||
//! ``` | ||
use core::time::Duration; | ||
use elkodon_bb_log::fatal_panic; | ||
use elkodon_bb_posix::clock::{nanosleep, NanosleepError}; | ||
use elkodon_bb_posix::signal::SignalHandler; | ||
|
||
/// A complete list of all events that can occur in the main event loop, [`Elk::wait()`]. | ||
pub enum ElkEvent { | ||
Tick, | ||
TerminationRequest, | ||
InterruptSignal, | ||
} | ||
|
||
/// The main event loop handling mechanism. | ||
#[derive(Debug)] | ||
pub struct Elk { | ||
_priv: (), | ||
} | ||
|
||
impl Elk { | ||
fn get_instance() -> &'static Self { | ||
static INSTANCE: Elk = Elk { _priv: () }; | ||
&INSTANCE | ||
} | ||
|
||
fn wait_impl(&self, cycle_time: Duration) -> ElkEvent { | ||
if SignalHandler::termination_requested() { | ||
return ElkEvent::TerminationRequest; | ||
} | ||
|
||
match nanosleep(cycle_time) { | ||
Ok(()) => { | ||
if SignalHandler::termination_requested() { | ||
ElkEvent::TerminationRequest | ||
} else { | ||
ElkEvent::Tick | ||
} | ||
} | ||
Err(NanosleepError::InterruptedBySignal(_)) => ElkEvent::InterruptSignal, | ||
Err(v) => { | ||
fatal_panic!(from self, | ||
"Failed to wait with cycle time {:?} in main event look, caused by ({:?}).", | ||
cycle_time, v); | ||
} | ||
} | ||
} | ||
|
||
/// Waits until an event has received. It returns | ||
/// [`ElkEvent::Tick`] when the `cycle_time` has passed, otherwise the other event that | ||
/// can occur. | ||
pub fn wait(cycle_time: Duration) -> ElkEvent { | ||
Self::get_instance().wait_impl(cycle_time) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.