Struct Segment

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

Represents an Animation unit, called a Segment.

A Segment is composed of multiple Tracks, each containing sets of Keyframe associated with an Output.

The Segment plays the keyframes of its track in a logical and temporal order.

  • A segment searches for keyframe to execute and updates the associated devices based on a given number of times per second, as defined by its fps property.
  • A segment can be set to repeat in a loop within an Animation from a starting point in time called loopback.

§Example

Here is an example of defining a segment to animate a small robot with two actuators (a LED and a servo). The robot will perform a waving motion using its servo and LED.

use hermes_five::animations::{Easing, Keyframe, Segment, Track};
use hermes_five::hardware::Board;
use hermes_five::devices::{Led, Servo};
use hermes_five::io::RemoteIo;

#[hermes_five::runtime]
async fn main() {
    // Define a board on COM4.
    let board = Board::new(RemoteIo::new("COM4")).open();

    // Define a servo attached to the board on PIN 9 (default servo position is 90°).
    let servo = Servo::new(&board, 9, 90).unwrap();
    // Create a track for the servo.
    let servo_track = Track::new(servo)
        // Turns the servo to 180° in 1000ms
        .with_keyframe(Keyframe::new(180, 0, 1000).set_transition(Easing::SineInOut))
        // Turns the servo to 0° in 1000ms
        .with_keyframe(Keyframe::new(0, 2000, 3000).set_transition(Easing::SineInOut));

    // Define a LED attached to the board on PIN 13.
    let led = Led::new(&board, 13, false).unwrap();
    // Create a track for the LED.
    let led_track = Track::new(led)
        // Turns the LED fully (instantly)
        .with_keyframe(Keyframe::new(255, 0, 1))
        // Fade out the LED in 1000ms
        .with_keyframe(Keyframe::new(0, 2000, 3000));

    // Create an animation Segment for this:
    let segment = Segment::default()
        .with_track(servo_track)
        .with_track(led_track)
        .set_repeat(true);
}

Implementations§

Source§

impl Segment

Source

pub async fn play(&mut self) -> Result<(), Error>

Plays the segment. If repeat is true, the segment will loop indefinitely.

Source

pub fn get_duration(&self) -> u64

Gets the total duration of the segment.

The duration is determined by the longest track in the segment.

Source

pub fn get_progress(&self) -> u64

Gets the current play time.

Source

pub fn is_repeat(&self) -> bool

Checks if the segment should repeat.

Source

pub fn get_loopback(&self) -> u64

Returns the loopback time.

Source

pub fn get_speed(&self) -> u8

Returns the playback speed as a percentage (between 0% and 100%).

Source

pub fn get_fps(&self) -> u8

Returns the frames per second (fps) for the segment.

Source

pub fn get_tracks(&self) -> &Vec<Track>

Returns the tracks in the segment.

Source

pub fn set_repeat(self, repeat: bool) -> Self

Sets whether the segment should repeat and returns the updated segment.

Source

pub fn set_loopback(self, loopback: u64) -> Self

Sets the loopback time and returns the updated segment.

Source

pub fn set_speed(self, speed: u8) -> Self

Sets the playback speed as a percentage (between 0% and 100%) and returns the updated segment.

Source

pub fn set_fps(self, fps: u8) -> Self

Sets the frames per second (fps) and returns the updated segment.

Source

pub fn set_tracks(self, tracks: Vec<Track>) -> Self

Sets the tracks for the segment and returns the updated segment.

Source

pub fn with_track(self, track: Track) -> Self

Adds a track to the segment and returns the updated segment.

Trait Implementations§

Source§

impl Clone for Segment

Source§

fn clone(&self) -> Segment

Returns a duplicate 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 Segment

Source§

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

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

impl Default for Segment

Source§

fn default() -> Self

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

impl Display for Segment

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 Segment

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.