1use crate::base::*;
2#[cfg(feature = "delay-until")]
3use crate::shim::*;
4use crate::task::*;
5use crate::units::*;
6
7#[cfg(feature = "delay-until")]
8pub struct TaskDelay {
11 last_wake_time: FreeRtosTickType,
12}
13
14#[cfg(feature = "delay-until")]
15impl TaskDelay {
16 pub fn new() -> TaskDelay {
19 TaskDelay {
20 last_wake_time: FreeRtosUtils::get_tick_count(),
21 }
22 }
23
24 pub fn delay_until<D: DurationTicks>(&mut self, delay: D) {
27 unsafe {
28 freertos_rs_vTaskDelayUntil(
29 &mut self.last_wake_time as *mut FreeRtosTickType,
30 delay.to_ticks(),
31 );
32 }
33 }
34}
35
36pub struct TaskDelayPeriodic {
42 last_wake_time: FreeRtosTickType,
43 period_ticks: FreeRtosTickType,
44}
45
46impl TaskDelayPeriodic {
47 pub fn new<D: DurationTicks>(period: D) -> TaskDelayPeriodic {
49 let l = FreeRtosUtils::get_tick_count();
50
51 TaskDelayPeriodic {
52 last_wake_time: l,
53 period_ticks: period.to_ticks(),
54 }
55 }
56
57 pub fn should_run(&mut self) -> bool {
59 let c = FreeRtosUtils::get_tick_count();
60 if (c - self.last_wake_time) < (self.period_ticks) {
61 false
62 } else {
63 self.last_wake_time = c;
64 true
65 }
66 }
67
68 pub fn set_period<D: DurationTicks>(&mut self, period: D) {
70 self.period_ticks = period.to_ticks();
71 }
72
73 pub fn reset(&mut self) {
75 self.last_wake_time = FreeRtosUtils::get_tick_count();
76 }
77}