Skip to main content

TimerFnPtr

Type Alias TimerFnPtr 

Source
pub type TimerFnPtr = dyn Fn(Box<dyn Timer>, Option<TimerParam>) -> Result<TimerParam> + Send + Sync + 'static;
Expand description

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 expired
  • Option<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::traits::{Timer, TimerParam};
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>() {
            println!("Timer expired, count: {}", count);
            return Ok(Arc::new(*count + 1));
        }
    }
    Ok(Arc::new(0u32))
});