From 7727f01fd200259e65184eb371ef032c2e0bf3a3 Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Mon, 18 Apr 2022 13:57:52 +0200 Subject: [PATCH] Fix timing in draw routine --- README.org | 2 +- src/main.rs | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.org b/README.org index 9c6a084..e042cbc 100644 --- a/README.org +++ b/README.org @@ -25,5 +25,5 @@ The =video-to-ascii.py= conversion script uses [[https://github.com/ivanl-exe/im It is assumed to be located on the same height as this project and that it is build in release-mode (due to performance). * TODO -- [ ] Fix timing in draw +- [X] Fix timing in draw - [ ] Rewrite ascii conversion in Rust or Haskell (with an own implementation of =ascii-to-text=) diff --git a/src/main.rs b/src/main.rs index 4d35a64..f754d87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,6 +77,11 @@ fn main() -> ! { loop { let monotimer_instant = monotimer.now(); + // reset + if button1.is_high().unwrap() { + index = 0 + } + display.clear(); let text = Text::with_baseline( @@ -93,19 +98,14 @@ fn main() -> ! { index = (index + IMAGE_LEN) % IMAGE_END; // adjust for desired framerate - // TODO: chip is too fast for some reason - let freq = monotimer.frequency().0; - // maybe use float calc here? - let ms_per_draw = 1000 / (freq / monotimer_instant.elapsed()); - rprintln!("Draw took {}ms", ms_per_draw); + // WARNING: don't do anything after elapsed() has been calculated, + // otherwise it will delay the frame being drawn, which will + // knock it off sync + let freq: f64 = monotimer.frequency().0.into(); + let ms_per_draw: f64 = f64::from(1000 ) / (freq / f64::from(monotimer_instant.elapsed())); match DRAW_TIME.checked_sub(ms_per_draw as usize) { Some(result) => delay_provider.delay_ms(result as u32), None => (), }; - - // reset - if button1.is_high().unwrap() { - index = 0 - } } }