autd3_driver/utils/
timer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use std::num::NonZeroU32;

#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
pub struct TimerResolutionGurad {
    timer_resolution: Option<NonZeroU32>,
}

impl TimerResolutionGurad {
    pub fn new(timer_resolution: Option<NonZeroU32>) -> Self {
        #[cfg(target_os = "windows")]
        timer_resolution.map(|timer_resolution| unsafe {
            windows::Win32::Media::timeBeginPeriod(timer_resolution.get())
        });
        Self { timer_resolution }
    }
}

impl Drop for TimerResolutionGurad {
    fn drop(&mut self) {
        #[cfg(target_os = "windows")]
        self.timer_resolution.map(|timer_resolution| unsafe {
            windows::Win32::Media::timeEndPeriod(timer_resolution.get())
        });
    }
}