~ruther/simple-clock

ref: b92738752d8b6cc66a2b634b6528a7f5d3f2a315 simple-clock/source/src/clock_display_viewer.rs -rw-r--r-- 4.6 KiB
b9273875 — František Boháček feat(src): add meaningful message to panic in Calendar::days_in_month 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use core::mem;

use crate::{
    clock_display::{ClockDisplay, DisplayPart},
    clock_state::ClockState,
};
use stm32f1xx_hal::timer;

#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(usize)]
pub enum DisplayView {
    ClockView = 0,
    ClockSecondsView = 1,
    ClockDateView = 2,
    DateView = 3,
}

impl TryFrom<usize> for DisplayView {
    fn try_from(value: usize) -> Result<Self, ()> {
        if value <= DisplayView::DateView as usize {
            unsafe { core::mem::transmute(value) }
        } else {
            Err(())
        }
    }

    type Error = ();
}

#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(usize)]
pub enum ClockPart {
    Hours = 0,
    Minutes = 1,
    Seconds = 2,
    Year = 3,
    Month = 4,
    Day = 5,
}

impl TryFrom<usize> for ClockPart {
    fn try_from(value: usize) -> Result<Self, ()> {
        if value <= ClockPart::Day as usize {
            unsafe { core::mem::transmute(value) }
        } else {
            Err(())
        }
    }

    type Error = ();
}

pub struct ClockDisplayViewer {
    clock_display: ClockDisplay,
    parts: [bool; core::mem::variant_count::<ClockPart>()]
}

impl ClockDisplayViewer {
    pub fn new(clock_display: ClockDisplay) -> Self {
        Self {
            clock_display,
            parts: [false; core::mem::variant_count::<ClockPart>()]
        }
    }

    pub fn show(&mut self, part: ClockPart) {
        self.parts[part as usize] = true;
    }

    pub fn hide(&mut self, part: ClockPart) {
        self.parts[part as usize] = false;
    }

    pub fn hide_all(&mut self) {
        for part in self.parts.iter_mut() {
            *part = false;
        }

        self.clock_display.hide(DisplayPart::MainDisplay);
        self.clock_display.hide(DisplayPart::SideDisplay1);
        self.clock_display.hide(DisplayPart::SideDisplay2);
    }

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

    pub fn set_current_view(&mut self, view: DisplayView) {
        self.hide_all();
        match view {
            DisplayView::ClockView => {
                self.show(ClockPart::Hours);
                self.show(ClockPart::Minutes);
            },
            DisplayView::ClockSecondsView => {
                self.show(ClockPart::Hours);
                self.show(ClockPart::Minutes);
                self.show(ClockPart::Seconds);
            },
            DisplayView::ClockDateView => {
                self.show(ClockPart::Hours);
                self.show(ClockPart::Minutes);
                self.show(ClockPart::Day);
                self.show(ClockPart::Month);
            },
            DisplayView::DateView => {
                self.show(ClockPart::Day);
                self.show(ClockPart::Month);
                self.show(ClockPart::Year);
            }
        }
    }

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

        for (i, show) in self.parts.iter().enumerate().filter(|(_, x)| **x) {
            if !show {
                continue;
            }

            let part: ClockPart = ClockPart::try_from(i).unwrap();
            match part {
                ClockPart::Day => {
                    self.clock_display.show_ordinal(DisplayPart::SideDisplay1, state.calendar().day() as u32, true).unwrap();
                },
                ClockPart::Month => {
                    self.clock_display.show_ordinal(DisplayPart::SideDisplay2, state.calendar().month() as u32, true).unwrap();
                },
                ClockPart::Year => {
                    self.clock_display.show_number(DisplayPart::MainDisplay, state.calendar().year() as u32, true).unwrap();
                },
                ClockPart::Hours => {
                    self.clock_display.show_number_at(ClockDisplay::get_part_offset(DisplayPart::MainDisplay), 2, state.calendar().hours() as u32, true).unwrap();
                },
                ClockPart::Minutes => {
                    self.clock_display.show_number_at(ClockDisplay::get_part_offset(DisplayPart::MainDisplay) + 2, 2, state.calendar().minutes() as u32, true).unwrap();
                },
                ClockPart::Seconds => {
                    self.clock_display.show_number(DisplayPart::SideDisplay2, state.calendar().seconds() as u32, true).unwrap();
                },
            }
        }

        if self.parts[ClockPart::Hours as usize] && self.parts[ClockPart::Minutes as usize] {
            self.clock_display.set_colon(state.calendar().seconds() % 2 == 0);
        } else {
            self.clock_display.set_colon(false);
        }

        Ok(())
    }
}
Do not follow this link