osal_rs/traits/
timer.rs

1/***************************************************************************
2 *
3 * osal-rs
4 * Copyright (C) 2023/2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***************************************************************************/
19
20use core::any::Any;
21
22use alloc::{boxed::Box, sync::Arc};
23
24use crate::os::types::TickType;
25use crate::utils::{OsalRsBool, Result};
26
27
28pub type TimerParam = Arc<dyn Any + Send + Sync>;
29pub type TimerFnPtr = dyn Fn(Box<dyn Timer>, Option<TimerParam>) -> Result<TimerParam> + Send + Sync + 'static;
30
31pub trait Timer {
32    fn new<F>(name: &str, timer_period_in_ticks: TickType, auto_reload: bool, param: Option<TimerParam>, callback: F) -> Result<Self>
33    where
34        Self: Sized,
35        F: Fn(Box<dyn Timer>, Option<TimerParam>) -> Result<TimerParam> + Send + Sync + Clone + 'static;
36
37    fn start(&self, ticks_to_wait: TickType) -> OsalRsBool;
38    fn stop(&self, ticks_to_wait: TickType)  -> OsalRsBool;
39    fn reset(&self, ticks_to_wait: TickType) -> OsalRsBool;
40    fn change_period(&self, new_period_in_ticks: TickType, new_period_ticks: TickType) -> OsalRsBool;
41    fn delete(&mut self, ticks_to_wait: TickType) -> OsalRsBool;
42}