Skip to content

Commit e46224f

Browse files
committed
Adds Timer::is_ready().
Signed-off-by: Agustin Alba Chicar <[email protected]>
1 parent 9af9dd9 commit e46224f

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

rclrs/src/timer.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,24 @@ impl Timer {
115115
to_rclrs_result(unsafe {rcl_timer_call(&mut *rcl_timer)})
116116
}
117117

118+
pub fn is_ready(&self) -> Result<bool, RclrsError>
119+
{
120+
let (is_ready, is_ready_result) = unsafe {
121+
let mut is_ready: bool = false;
122+
let rcl_timer = self.rcl_timer.lock().unwrap();
123+
let is_ready_result = rcl_timer_is_ready(
124+
&* rcl_timer,
125+
&mut is_ready
126+
);
127+
(is_ready, is_ready_result)
128+
};
129+
to_rclrs_result(is_ready_result).map(|_| {
130+
is_ready
131+
})
132+
}
118133
// handle() -> RCLC Timer Type
119134

120135
// clock() -> Clock ?
121-
122-
// is_ready() -> bool
123-
124-
// reset() -> None
125-
126136
}
127137

128138
impl Drop for rcl_timer_t {
@@ -288,4 +298,22 @@ mod tests {
288298
let elapsed = dut.time_until_next_call().unwrap();
289299
assert!(elapsed < 500000i64, "time_until_next_call after call: {}", elapsed);
290300
}
301+
302+
#[test]
303+
fn test_is_ready() {
304+
let clock = Clock::steady();
305+
let context = Context::new(vec![]).unwrap();
306+
let period_ns: i64 = 1e6 as i64; // 1 millisecond.
307+
let dut = Timer::new(&clock, &context, period_ns).unwrap();
308+
309+
let is_ready = dut.is_ready();
310+
assert!(is_ready.is_ok());
311+
assert!(!is_ready.unwrap());
312+
313+
thread::sleep(time::Duration::from_micros(1100));
314+
315+
let is_ready = dut.is_ready();
316+
assert!(is_ready.is_ok());
317+
assert!(is_ready.unwrap());
318+
}
291319
}

0 commit comments

Comments
 (0)