Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting skip_unhandled_events on esp timer #526

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 56 additions & 19 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,26 +207,24 @@ where
where
F: FnMut() + Send + 'static,
{
self.internal_timer(callback)
self.internal_timer(callback, false)
}

pub fn timer_async(&self) -> Result<EspAsyncTimer, EspError> {
let notification = Arc::new(Notification::new());

let timer = {
let notification = Arc::downgrade(&notification);
/// Same as `timer` but does not wake the device from light sleep.
pub fn timer_nowake<F>(&self, callback: F) -> Result<EspTimer<'static>, EspError>
where
F: FnMut() + Send + 'static,
{
self.internal_timer(callback, true)
}

self.timer(move || {
if let Some(notification) = notification.upgrade() {
notification.notify(NonZeroU32::new(1).unwrap());
}
})?
};
pub fn timer_async(&self) -> Result<EspAsyncTimer, EspError> {
self.internal_timer_async(false)
}

Ok(EspAsyncTimer {
timer,
notification,
})
/// Same as `timer_async` but does not wake the device from light sleep.
pub fn timer_async_nowake(&self) -> Result<EspAsyncTimer, EspError> {
self.internal_timer_async(true)
}

/// # Safety
Expand Down Expand Up @@ -256,10 +254,27 @@ where
where
F: FnMut() + Send + 'a,
{
self.internal_timer(callback)
self.internal_timer(callback, false)
}

fn internal_timer<'a, F>(&self, callback: F) -> Result<EspTimer<'a>, EspError>
/// # Safety
///
/// Same as `timer_nonstatic` but does not wake the device from light sleep.
pub unsafe fn timer_nonstatic_nowake<'a, F>(
&self,
callback: F,
) -> Result<EspTimer<'a>, EspError>
where
F: FnMut() + Send + 'a,
{
self.internal_timer(callback, true)
}

fn internal_timer<'a, F>(
&self,
callback: F,
skip_unhandled_events: bool,
) -> Result<EspTimer<'a>, EspError>
where
F: FnMut() + Send + 'a,
{
Expand Down Expand Up @@ -287,7 +302,7 @@ where
name: b"rust\0" as *const _ as *const _, // TODO
arg: unsafe_callback.as_ptr(),
dispatch_method,
skip_unhandled_events: false, // TODO
skip_unhandled_events,
},
&mut handle as *mut _,
)
Expand All @@ -298,6 +313,28 @@ where
_callback: callback,
})
}

fn internal_timer_async(&self, skip_unhandled_events: bool) -> Result<EspAsyncTimer, EspError> {
let notification = Arc::new(Notification::new());

let timer = {
let notification = Arc::downgrade(&notification);

self.internal_timer(
move || {
if let Some(notification) = notification.upgrade() {
notification.notify(NonZeroU32::new(1).unwrap());
}
},
skip_unhandled_events,
)?
};

Ok(EspAsyncTimer {
timer,
notification,
})
}
}

pub type EspTaskTimerService = EspTimerService<Task>;
Expand Down
Loading