~ruther/simple-clock

ref: 7532a6fe169d2b63baf7451bcb75ab14ad22dab1 simple-clock/src/clock_display_viewer.rs -rw-r--r-- 1.6 KiB
7532a6fe — František Boháček feat(src): add clock seven segment display support 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::{
    clock_display::ClockDisplay,
    clock_state::ClockState,
    display_view::{
        clock_display_view::ClockDisplayView, date_display_view::DateDisplayView, DisplayView,
        DisplayViews,
    },
};
use alloc::{boxed::Box, vec, vec::Vec};
use stm32f1xx_hal::timer;

pub struct ClockDisplayViewer {
    clock_display: ClockDisplay,
    views: Vec<Box<dyn DisplayView + Send>>,
    current_view: Option<DisplayViews>,
}

impl ClockDisplayViewer {
    pub fn new(clock_display: ClockDisplay) -> Self {
        Self {
            clock_display,
            views: vec![
                Box::new(ClockDisplayView::new()),
                Box::new(ClockDisplayView::with_seconds()),
                Box::new(ClockDisplayView::with_date()),
                Box::new(DateDisplayView::new()),
            ],
            current_view: None,
        }
    }

    pub fn clock_display<'a>(&'a mut self) -> &'a mut ClockDisplay {
        &mut self.clock_display
    }

    pub fn current_view(&self) -> Option<DisplayViews> {
        self.current_view
    }

    pub fn set_current_view(&mut self, view: DisplayViews) {
        self.current_view = Some(view);
    }

    pub fn clear_current_view(&mut self) {
        self.current_view = None;
    }

    pub fn update(&mut self, state: &ClockState) -> nb::Result<(), timer::Error> {
        self.clock_display.update()?;

        if let Some(view) = self.current_view {
            let view = &mut self.views[view as usize];
            view.update_display(state, &mut self.clock_display).unwrap(); // TODO: get rid of the unwrap
        }
        Ok(())
    }
}
Do not follow this link