pub struct Segment { /* private fields */ }
Expand description
Represents an Animation
unit, called a Segment
.
A Segment
is composed of multiple Track
s, 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 calledloopback
.
§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
impl Segment
Sourcepub async fn play(&mut self) -> Result<(), Error>
pub async fn play(&mut self) -> Result<(), Error>
Plays the segment. If repeat
is true, the segment will loop indefinitely.
Sourcepub fn get_duration(&self) -> u64
pub fn get_duration(&self) -> u64
Gets the total duration of the segment.
The duration is determined by the longest track in the segment.
Sourcepub fn get_progress(&self) -> u64
pub fn get_progress(&self) -> u64
Gets the current play time.
Sourcepub fn get_loopback(&self) -> u64
pub fn get_loopback(&self) -> u64
Returns the loopback time.
Sourcepub fn get_speed(&self) -> u8
pub fn get_speed(&self) -> u8
Returns the playback speed as a percentage (between 0% and 100%).
Sourcepub fn get_tracks(&self) -> &Vec<Track>
pub fn get_tracks(&self) -> &Vec<Track>
Returns the tracks in the segment.
Sourcepub fn set_repeat(self, repeat: bool) -> Self
pub fn set_repeat(self, repeat: bool) -> Self
Sets whether the segment should repeat and returns the updated segment.
Sourcepub fn set_loopback(self, loopback: u64) -> Self
pub fn set_loopback(self, loopback: u64) -> Self
Sets the loopback time and returns the updated segment.
Sourcepub fn set_speed(self, speed: u8) -> Self
pub fn set_speed(self, speed: u8) -> Self
Sets the playback speed as a percentage (between 0% and 100%) and returns the updated segment.
Sourcepub fn set_fps(self, fps: u8) -> Self
pub fn set_fps(self, fps: u8) -> Self
Sets the frames per second (fps) and returns the updated segment.
Sourcepub fn set_tracks(self, tracks: Vec<Track>) -> Self
pub fn set_tracks(self, tracks: Vec<Track>) -> Self
Sets the tracks for the segment and returns the updated segment.
Sourcepub fn with_track(self, track: Track) -> Self
pub fn with_track(self, track: Track) -> Self
Adds a track to the segment and returns the updated segment.