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
154
155
156
157
158
159
160
161
162
163
164
165
/// Provides a direct access to the HD44780.
///
/// # Caveats
///
/// 1. The `clear` and `home` methods are slow (HD44780 requires an additional delay to process
///    them). If performance is a concern, please consider using the buffered frontend.

use super::super::*;
use super::super::buses::Bus;
use super::super::buses::command::*;

pub struct Direct {
    bus: Box<Bus>,
    properties: Properties,
    state: State,
}

struct State {
    cursor_blinking: bool,
    cursor_visible: bool,
    text_visible: bool,
}

impl Direct {
    /// Creates a new direct HD44780 on given bus.
    pub fn new(bus: Box<Bus>, width: usize, height: usize) -> Result<Direct> {
        Direct::new_ex(
            bus,

            Properties {
                height,
                width,

                font: if height == 1 { Font::Font5x10 } else { Font::Font5x8 },
            },
        )
    }

    /// Creates a new direct HD44780 on given bus.
    pub fn new_ex(bus: Box<Bus>, properties: Properties) -> Result<Direct> {
        let mut lcd = Direct {
            bus,
            properties,

            state: State {
                cursor_blinking: false,
                cursor_visible: false,
                text_visible: true,
            },
        };

        lcd.initialize()?;

        Ok(lcd)
    }

    /// Initializes the screen.
    fn initialize(&mut self) -> UnitResult {
        // initialize the bus
        self.bus.initialize()?;

        // initialize the screen
        let height = self.height();
        let bus_width = self.bus.width();

        self.bus.execute(Command::SetFunctions {
            font_5x10: self.properties.font == Font::Font5x10,
            height,
            eight_bit_bus: bus_width == 8,
        })?;

        self.bus.execute(Command::SetEntryMode {
            enable_shift: false,
            increment_counter: true,
        })?;

        self.refresh_display_flags()
    }

    /// Issues the "set display flags" command with current LCD's state.
    fn refresh_display_flags(&mut self) -> UnitResult {
        self.bus.execute(Command::SetDisplayFlags {
            cursor_blinking: self.state.cursor_blinking,
            cursor_visible: self.state.cursor_visible,
            text_visible: self.state.text_visible,
        })
    }
}

impl Hd44780 for Direct {
    /// Clears the screen.
    /// It's a slow command, re-writing screen with new data should be a preferred way if one is
    /// concerned about the performance (that's precisely what the "buffered" frontend does).
    fn clear(&mut self) -> UnitResult {
        self.bus.execute(Command::Clear {})
    }

    /// Moves cursor at (0, 0).
    /// It's actually slower than "move_at(0, 0)", because HD44780 takes some time to process this
    /// one.
    fn home(&mut self) -> UnitResult {
        self.bus.execute(Command::Home {})
    }

    fn move_at(&mut self, y: usize, x: usize) -> UnitResult {
        if y >= self.height() || x >= self.width() {
            return Err(
                format!("Tried to move the cursor outside the screen (at y={}, x={}).", y, x).into()
            );
        }

        let addresses = vec![0x00, 0x40, 0x14, 0x54];

        self.bus.execute(Command::SetDDRamAddress {
            address: (addresses[y] + x) as u8,
        })
    }

    fn print_char(&mut self, ch: u8) -> UnitResult {
        self.bus.write_data(ch)
    }

    fn set_backlight(&mut self, enabled: bool) -> UnitResult {
        self.bus.set_backlight(enabled)
    }

    fn set_cursor_blinking(&mut self, enabled: bool) -> UnitResult {
        self.state.cursor_blinking = enabled;
        self.refresh_display_flags()
    }

    fn set_cursor_visible(&mut self, enabled: bool) -> UnitResult {
        self.state.cursor_visible = enabled;
        self.refresh_display_flags()
    }

    fn set_text_visible(&mut self, enabled: bool) -> UnitResult {
        self.state.text_visible = enabled;
        self.refresh_display_flags()
    }

    fn create_char(&mut self, idx: u8, lines: [u8; 8]) -> UnitResult {
        if idx > 7 {
            return Err("Index out of range - character index must be in range <0, 7>".into());
        }

        self.bus.execute(Command::SetCGRamAddress {
            address: idx << 3,
        })?;

        for line in lines.iter() {
            self.bus.write_data(*line)?;
        }

        Ok(())
    }

    fn height(&self) -> usize {
        self.properties.height
    }

    fn width(&self) -> usize {
        self.properties.width
    }
}