Skip to main content

butt_head/
service_timing.rs

1use crate::TimeDuration;
2
3/// Indicates when `update()` should next be called.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6pub enum ServiceTiming<D: TimeDuration> {
7    /// Call back as soon as possible.
8    Immediate,
9
10    /// Nothing will happen until at least this delay elapses.
11    Delay(D),
12
13    /// Button is idle. Only call again when the input changes.
14    Idle,
15}
16
17impl<D: TimeDuration> ServiceTiming<D> {
18    /// Returns the sooner of two service timings.
19    pub fn min(self, other: Self) -> Self {
20        match (self, other) {
21            (ServiceTiming::Immediate, _) | (_, ServiceTiming::Immediate) => {
22                ServiceTiming::Immediate
23            }
24            (ServiceTiming::Delay(a), ServiceTiming::Delay(b)) => {
25                if a.as_millis() <= b.as_millis() {
26                    ServiceTiming::Delay(a)
27                } else {
28                    ServiceTiming::Delay(b)
29                }
30            }
31            (ServiceTiming::Delay(d), ServiceTiming::Idle)
32            | (ServiceTiming::Idle, ServiceTiming::Delay(d)) => ServiceTiming::Delay(d),
33            (ServiceTiming::Idle, ServiceTiming::Idle) => ServiceTiming::Idle,
34        }
35    }
36}