1use rust_rocket::simple::{Event, Rocket};
2use std::{
3 thread,
4 time::{Duration, Instant},
5};
6
7struct TimeSource {
12 start: Instant,
13 offset: Duration,
14 paused: bool,
15}
16
17impl TimeSource {
18 pub fn new() -> Self {
19 Self {
20 start: Instant::now(),
21 offset: Duration::from_secs(0),
22 paused: false,
23 }
24 }
25
26 pub fn get_time(&self) -> Duration {
27 if self.paused {
28 self.offset
29 } else {
30 self.start.elapsed() + self.offset
31 }
32 }
33
34 pub fn pause(&mut self, state: bool) {
35 self.offset = self.get_time();
36 self.start = Instant::now();
37 self.paused = state;
38 }
39
40 pub fn seek(&mut self, to: Duration) {
41 self.offset = to;
42 self.start = Instant::now();
43 }
44}
45
46fn main() {
47 let mut rocket = Rocket::new("tracks.bin", 60.).unwrap();
48 let mut time_source = TimeSource::new();
49 let mut previous_print_time = Duration::ZERO;
50
51 'main: loop {
52 while let Some(event) = rocket.poll_events().ok().flatten() {
56 match event {
57 Event::Seek(to) => time_source.seek(to),
58 Event::Pause(state) => time_source.pause(state),
59 Event::NotConnected =>
60 {
62 std::thread::sleep(Duration::from_millis(10));
63 continue 'main;
64 }
65 }
66 }
67
68 let time = time_source.get_time();
70 rocket.set_time(&time);
71
72 if time != previous_print_time {
76 println!("{:?}: test = {}", time, rocket.get_value("test"));
77 }
78 previous_print_time = time;
79 thread::sleep(Duration::from_millis(10));
80 }
81}