embedded_multi_page_hmi/
lifetime.rs

1use crate::PageNavigation;
2
3/// PageLifetime enables a page to automatically switch to another page after a certain time.
4///
5/// The page lifetime is only applied while a page is presented.
6/// Each page type is responsible to care if page lifetime is to be considered.
7/// Page lifetime is measured in update events. I.e. an update event shall cause a call to
8/// increase_age.
9#[derive(Clone, Copy)]
10pub struct PageLifetime {
11    target: PageNavigation,
12    lifetime_in_updates: u16,
13    update_counter: u16,
14}
15
16impl PageLifetime {
17    pub fn new(target: PageNavigation, lifetime_in_updates: u16) -> Self {
18        PageLifetime {
19            target,
20            lifetime_in_updates,
21            update_counter: 0,
22        }
23    }
24    /// Check if lifetime is over
25    pub fn is_over(&self) -> bool {
26        self.update_counter >= self.lifetime_in_updates
27    }
28
29    /// Where to navigate to if lifetime is over
30    pub fn get_target(&self) -> PageNavigation {
31        self.target
32    }
33
34    /// Increase page age - to be called by page if it receives page update event.
35    pub fn increase_age(&mut self) {
36        self.update_counter += 1;
37    }
38
39    /// Rebirth of a page - to be called by page has just turned active.
40    pub fn reset_age(&mut self) {
41        self.update_counter = 0;
42    }
43}
44
45#[cfg(test)]
46mod tests;