1use crate::base::*;
2use crate::task::*;
3use crate::shim::*;
4use crate::units::*;
5
6pub struct TaskDelay {
9 last_wake_time: FreeRtosTickType,
10}
11
12impl TaskDelay {
13 pub fn new() -> TaskDelay {
16 TaskDelay { last_wake_time: FreeRtosUtils::get_tick_count() }
17 }
18
19 pub fn delay_until<D: DurationTicks>(&mut self, delay: D) {
22 unsafe {
23 freertos_rs_vTaskDelayUntil(&mut self.last_wake_time as *mut FreeRtosTickType,
24 delay.to_ticks());
25 }
26 }
27}
28
29pub struct TaskDelayPeriodic {
35 last_wake_time: FreeRtosTickType,
36 period_ticks: FreeRtosTickType,
37}
38
39impl TaskDelayPeriodic {
40 pub fn new<D: DurationTicks>(period: D) -> TaskDelayPeriodic {
42 let l = FreeRtosUtils::get_tick_count();
43
44 TaskDelayPeriodic {
45 last_wake_time: l,
46 period_ticks: period.to_ticks(),
47 }
48 }
49
50 pub fn should_run(&mut self) -> bool {
52 let c = FreeRtosUtils::get_tick_count();
53 if (c - self.last_wake_time) < (self.period_ticks) {
54 false
55 } else {
56 self.last_wake_time = c;
57 true
58 }
59 }
60
61 pub fn set_period<D: DurationTicks>(&mut self, period: D) {
63 self.period_ticks = period.to_ticks();
64 }
65
66 pub fn reset(&mut self) {
68 self.last_wake_time = FreeRtosUtils::get_tick_count();
69 }
70}