Skip to content

Add non-blocking update function#72

Merged
rfuest merged 5 commits intoembedded-graphics:masterfrom
SakiiCode:master
Apr 18, 2026
Merged

Add non-blocking update function#72
rfuest merged 5 commits intoembedded-graphics:masterfrom
SakiiCode:master

Conversation

@SakiiCode
Copy link
Copy Markdown
Contributor

Thank you for helping out with embedded-graphics-simulator development! Please:

  • Check that you've added passing tests and documentation
  • Add an example where applicable
  • Add a CHANGELOG.md entry in the Unreleased section under the appropriate heading (Added, Fixed, etc) if your changes affect the public API
  • Run rustfmt on the project
  • Run just build (Linux/macOS only) and make sure it passes. If you use Windows, check that CI passes once you've opened the PR.

PR description

Resolves #70

Copy link
Copy Markdown
Member

@rfuest rfuest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

I would prefer to implement this in another way, without requiring two variants of the update method. There is already a way to change the target FPS via Window::set_max_fps and MultiWindow::set_max_fps and the option to disable the FPS limit should be accessible in a similar way. I would suggest to add disable_fps_limit methods to Window and MultiWindow, which set the fps_limiter field to None.

@SakiiCode
Copy link
Copy Markdown
Contributor Author

I wanted to avoid having an is_none() check in every update cycle, but did not find a better way to do it.

What if the check is in fps_limiter.sleep() like this:

    fn sleep(&mut self) {
        if self.max_fps == 0 {
            return;
        }
        let sleep_duration = (self.frame_start + self.desired_loop_duration())
            .saturating_duration_since(Instant::now());
        thread::sleep(sleep_duration);

        self.frame_start = Instant::now();
    }

This way the Window functions do not need any modification and the fps limit can be adjusted flexibly

@rfuest
Copy link
Copy Markdown
Member

rfuest commented Apr 18, 2026

I wanted to avoid having an is_none() check in every update cycle, but did not find a better way to do it.

Why do you want to avoid this? This doesn't seem bad to me:

    pub fn update<C>(&mut self, display: &SimulatorDisplay<C>)
    where
        C: PixelColor + Into<Rgb888> + From<Rgb888>,
    {
        ...

        if let Some(fps_limiter) = self.fps_limiter.as_mut() {
            fps_limiter.sleep();
        }
    }

Using max_fps == 0 as a special value would work and is something I would use in a C program, but in Rust it's better to use an enum for the special value.

@SakiiCode
Copy link
Copy Markdown
Contributor Author

if let Some(fps_limiter) = self.fps_limiter.as_mut() {
       fps_limiter.sleep();
}

It is not possible to later restore the FpsLimiter if needed

Using max_fps == 0 as a special value would work and is something I would use in a C program, but in Rust it's better to use an enum for the special value.

Then it should be Option<NonNull<u32>>

@rfuest
Copy link
Copy Markdown
Member

rfuest commented Apr 18, 2026

It is not possible to later restore the FpsLimiter if needed

Why not? Calling set_max_fps can restore the fps limiter.

Then it should be Option<NonNull>

Inside the FpsLimiter or as the parameter to set_max_fps? The second would be a breaking change of the API.

@SakiiCode
Copy link
Copy Markdown
Contributor Author

Okay updated the PR, please check if it is good

Copy link
Copy Markdown
Member

@rfuest rfuest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really what I was thinking, because it still uses 0 as a special value in set_max_fps. But I'm OK with merging this, when you change the documentation of the set_max_fps methods to mention that 0 has a special meaning.

Comment thread src/window/mod.rs Outdated
Comment thread src/window/mod.rs Outdated
SakiiCode and others added 4 commits April 18, 2026 18:27
Co-authored-by: Ralf Fuest <mail@rfuest.de>
Co-authored-by: Ralf Fuest <mail@rfuest.de>
@SakiiCode
Copy link
Copy Markdown
Contributor Author

Ok done

Copy link
Copy Markdown
Member

@rfuest rfuest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rfuest rfuest merged commit cb6825e into embedded-graphics:master Apr 18, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update without sleep delay

2 participants