Struct Animation

Source
pub struct Animation { /* private fields */ }
Expand description

Represents an animation: a collection of ordered Segment to be run in sequence.

  • An animation can be played, paused, resumed, and stopped.
  • Each Segment in the animation is played in order (eventually looped, see Segment) until all are done.

§Example

Here is an example of an animation made of a single segment that sweeps a servo indefinitely, from position 0° to 180° and back: (sidenote: prefer use Servo::sweep() helper for this purpose).

use hermes_five::pause;
use hermes_five::animations::{Animation, Easing, Keyframe, Segment, Track};
use hermes_five::hardware::{Board, BoardEvent};
use hermes_five::devices::Servo;

#[hermes_five::runtime]
async fn main() {
    let board = Board::run();
    board.on(BoardEvent::OnReady, |board: Board| async move {
        let servo = Servo::new(&board, 9, 0).unwrap();

        let mut animation = Animation::from(
            Segment::from(
                Track::new(servo)
                    .with_keyframe(Keyframe::new(180, 0, 500).set_transition(Easing::SineInOut))
                    .with_keyframe(Keyframe::new(90, 1000, 2000).set_transition(Easing::SineInOut)),
            )
            .set_fps(100)
            .set_repeat(true)
        );

        animation.play();
        pause!(3000);

        animation.stop();

        Ok(())
    });
}

Implementations§

Source§

impl Animation

Source

pub fn play(&mut self) -> &mut Self

Starts or resumes the animation.

The animation will start from the current segment or from the beginning if it was stopped.

Source

pub fn pause(&mut self) -> &mut Self

Pauses the animation.

When resumed, the animation will continue from the point it was paused.

Source

pub fn next(&mut self) -> &mut Self

Skips the current sequence and jump to next.

Caveats:

  • Skipping the current segment does not pause / resume the animation: if it was running, it continues to do so (from the beginning of next segment).
  • If already on the last segment, the animation loop to the first one, hence, restart.
Source

pub fn stop(&mut self) -> &mut Self

Stops the animation.

The animation will be reset to the beginning. The current segment is reset, and the index is set to 0.

Source

pub fn is_playing(&self) -> bool

Indicates if the animation is currently playing.

Source

pub fn get_duration(&self) -> u64

Gets the total duration of the animation.

The duration is determined by the sum of segment durations or u64::MAX if a segment is on repeat mode.

Source

pub fn get_progress(&self) -> u64

Gets the current play time. @todo fix: because we clone self on .play(), the progress is no longer available on segment.

Source

pub fn get_segments(&self) -> &Vec<Segment>

Returns the list of segments in the animation.

Source

pub fn set_segments(self, segments: Vec<Segment>) -> Self

Sets the list of segments for the animation.

Source

pub fn with_segment(self, segment: Segment) -> Self

Adds a new segment to the animation.

This segment will be enqueued at the end of current segment list.

Source

pub fn get_current(&self) -> usize

Returns the index of the currently running segment.

Source

pub fn set_current(&self, index: usize)

Sets the index of the currently running segment.

Source

pub fn on<S, F, T, Fut>(&self, event: S, callback: F) -> EventHandler
where S: Into<String>, T: 'static + Send + Sync + Clone, F: FnMut(T) -> Fut + Send + 'static, Fut: Future<Output = Result<(), Error>> + Send + 'static,

Registers a callback to be executed on a given event.

Available events for an animation are defined by the enum: AnimationEvent:

  • OnSegmentDone | segment_done: Triggered when a segment is done.
    The callback must receive the following parameter: |_: Segment| { ... }
  • OnStart | start: Triggered when the animation starts.
    The callback must receive the following parameter: |_: Animation| { ... }
  • OnComplete | complete: Triggered when the animation ends.
    The callback must receive the following parameter: |_: Animation| { ... }
§Example
use hermes_five::hardware::Board;
use hermes_five::hardware::BoardEvent;
use hermes_five::devices::{Output, Led};
use hermes_five::animations::{Animation, AnimationEvent, Easing};

#[hermes_five::runtime]
async fn main() {
    let board = Board::run();
    board.on(BoardEvent::OnReady, |board: Board| async move {
        let mut led = Led::new(&board, 11, false)?;
        // This is a dummy animation (does nothing).
        let animation = Animation::default();

        animation.on(AnimationEvent::OnStart, |_: Animation| async move {
            println!("Animation has started");
            Ok(())
        });
        animation.on(AnimationEvent::OnComplete, |_: Animation| async move {
            println!("Animation done");
            Ok(())
        });
        Ok(())
    });
}

Trait Implementations§

Source§

impl Clone for Animation

Source§

fn clone(&self) -> Animation

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Animation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Animation

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Animation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Segment> for Animation

Source§

fn from(segment: Segment) -> Self

Converts to this type from the input type.
Source§

impl From<Track> for Animation

Source§

fn from(track: Track) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.