Skip to content

Commit 9af9dd9

Browse files
Added timer_period_ns (#2)
1 parent 59ed7e2 commit 9af9dd9

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

rclrs/src/timer.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ impl Timer {
4141
})
4242
}
4343

44+
pub fn timer_period_ns(&self) -> Result<i64, RclrsError> {
45+
let mut timer_period_ns = 0;
46+
let get_period_result = unsafe {
47+
let rcl_timer = self.rcl_timer.lock().unwrap();
48+
rcl_timer_get_period(
49+
&* rcl_timer,
50+
&mut timer_period_ns
51+
)
52+
};
53+
to_rclrs_result(get_period_result).map(|_| {
54+
timer_period_ns
55+
})
56+
}
57+
4458
pub fn cancel(&self) -> Result<(), RclrsError> {
4559
let mut rcl_timer = self.rcl_timer.lock().unwrap();
4660
let cancel_result = unsafe { rcl_timer_cancel(&mut *rcl_timer) };
@@ -105,8 +119,6 @@ impl Timer {
105119

106120
// clock() -> Clock ?
107121

108-
// timer_period_ns -> i64 ?
109-
110122
// is_ready() -> bool
111123

112124
// reset() -> None
@@ -178,6 +190,21 @@ mod tests {
178190
assert!(dut.is_ok());
179191
}
180192

193+
#[test]
194+
fn test_get_period() {
195+
let clock = Clock::steady();
196+
let context = Context::new(vec![]).unwrap();
197+
let period: i64 = 1e6 as i64; // 1 milliseconds.
198+
199+
let dut = Timer::new(&clock, &context, period);
200+
assert!(dut.is_ok());
201+
let dut = dut.unwrap();
202+
let period_result = dut.timer_period_ns();
203+
assert!(period_result.is_ok());
204+
let period_result = period_result.unwrap();
205+
assert_eq!(period_result, 1e6 as i64);
206+
}
207+
181208
#[test]
182209
fn test_cancel() {
183210
let clock = Clock::steady();

0 commit comments

Comments
 (0)