pub type TimerFnPtr = dyn Fn(Box<dyn Timer>, Option<TimerParam>) -> Result<TimerParam> + Send + Sync + 'static;Expand description
All OSAL trait definitions for advanced usage. Timer callback function pointer type.
Callbacks receive the timer handle and optional parameter, and can return an updated parameter value.
§Parameters
Box<dyn Timer>- Handle to the timer that expiredOption<TimerParam>- Optional parameter passed at creation
§Returns
Result<TimerParam> - Updated parameter or error
§Execution Context
Callbacks execute in the timer service task, not ISR context. They should be short and avoid blocking operations.
§Trait Bounds
The function must be Send + Sync + 'static to safely execute
in the timer service task.
§Examples
use osal_rs::os::*;
use std::sync::Arc;
let callback: Box<TimerFnPtr> = Box::new(|_timer, param| {
if let Some(p) = param {
if let Some(count) = p.downcast_ref::<u32>() {
// Timer expired: hand the next invocation an updated count.
return Ok(Arc::new(*count + 1));
}
}
Ok(Arc::new(0u32))
});
// This is what the timer service does on every expiration: it passes the
// expired timer and the current parameter, and keeps whatever comes back.
let timer = Timer::new("counter", 50, true, None, |_t, _p| Ok(Arc::new(()))).unwrap();
let updated = callback(Box::new(timer), Some(Arc::new(41u32))).unwrap();
assert_eq!(updated.downcast_ref::<u32>(), Some(&42));