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