Skip to content
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
31 changes: 29 additions & 2 deletions rclrs/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ impl Timer {
})
}

pub fn timer_period_ns(&self) -> Result<i64, RclrsError> {
let mut timer_period_ns = 0;
let get_period_result = unsafe {
let rcl_timer = self.rcl_timer.lock().unwrap();
rcl_timer_get_period(
&* rcl_timer,
&mut timer_period_ns
)
};
to_rclrs_result(get_period_result).map(|_| {
timer_period_ns
})
}

pub fn cancel(&self) -> Result<(), RclrsError> {
let mut rcl_timer = self.rcl_timer.lock().unwrap();
let cancel_result = unsafe { rcl_timer_cancel(&mut *rcl_timer) };
Expand Down Expand Up @@ -105,8 +119,6 @@ impl Timer {

// clock() -> Clock ?

// timer_period_ns -> i64 ?

// is_ready() -> bool

// reset() -> None
Expand Down Expand Up @@ -178,6 +190,21 @@ mod tests {
assert!(dut.is_ok());
}

#[test]
fn test_get_period() {
let clock = Clock::steady();
let context = Context::new(vec![]).unwrap();
let period: i64 = 1e6 as i64; // 1 milliseconds.

let dut = Timer::new(&clock, &context, period);
assert!(dut.is_ok());
let dut = dut.unwrap();
let period_result = dut.timer_period_ns();
assert!(period_result.is_ok());
let period_result = period_result.unwrap();
assert_eq!(period_result, 1e6 as i64);
}

#[test]
fn test_cancel() {
let clock = Clock::steady();
Expand Down