aboutsummaryrefslogtreecommitdiffstats
path: root/sw/deps/hd44780-driver/src/display_mode.rs
blob: 8039f7bbd5dbf72ce9e9a6a58a22555d990a05a3 (plain)
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
use crate::{Cursor, CursorBlink, Display};

pub struct DisplayMode {
    pub cursor_visibility: Cursor,
    pub cursor_blink: CursorBlink,
    pub display: Display,
}

impl Default for DisplayMode {
    fn default() -> DisplayMode {
        DisplayMode {
            cursor_visibility: Cursor::Visible,
            cursor_blink: CursorBlink::On,
            display: Display::On,
        }
    }
}

impl DisplayMode {
    pub fn as_byte(&self) -> u8 {
        let cursor_blink_bits = match self.cursor_blink {
            CursorBlink::On => 0b0000_0001,
            CursorBlink::Off => 0,
        };

        let cursor_visible_bits = match self.cursor_visibility {
            Cursor::Visible => 0b0000_0010,
            Cursor::Invisible => 0,
        };

        let display_bits = match self.display {
            Display::On => 0b0000_0100,
            Display::Off => 0,
        };

        0b0000_1000 | cursor_visible_bits | cursor_blink_bits | display_bits
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn cursor_visible() {
        let dm = DisplayMode {
            cursor_visibility: Cursor::Visible,
            ..Default::default()
        };

        assert!(dm.as_byte() & 0b0000_0010 != 0);

        let dm = DisplayMode {
            cursor_visibility: Cursor::Invisible,
            ..Default::default()
        };

        assert!(dm.as_byte() & 0b0000_0010 == 0);
    }

    #[test]
    fn cursor_blink() {
        let dm = DisplayMode {
            cursor_blink: CursorBlink::On,
            ..Default::default()
        };

        assert!(dm.as_byte() & 0b0000_0001 != 0);

        let dm = DisplayMode {
            cursor_blink: CursorBlink::Off,
            ..Default::default()
        };

        assert!(dm.as_byte() & 0b0000_0001 == 0);
    }

    #[test]
    fn display_visible() {
        let dm = DisplayMode {
            display: Display::On,
            ..Default::default()
        };

        assert!(dm.as_byte() & 0b0000_0100 != 0);

        let dm = DisplayMode {
            display: Display::Off,
            ..Default::default()
        };

        assert!(dm.as_byte() & 0b0000_0100 == 0);
    }
}