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
use std;
use std::time::Duration;
use std::io::Error;
use super::hosts::{HD44780Host, Mode};

pub enum DisplayRow {
    R0,
    R1
}

pub struct HD44780 {
    host: Box<HD44780Host>
}

impl HD44780 {
    pub fn new(h: Box<HD44780Host>) -> HD44780 {
        return HD44780 {host: h};
    }

    pub fn init(&mut self) -> Result<(), Error> {
        try!(self.host.init());

        self.write_cmd(0x33);
        self.write_cmd(0x32);
        self.write_cmd(0x28);
        self.write_cmd(0x0C);
        self.write_cmd(0x06);
        self.write_cmd(0x01);
        Ok(())
    }

    fn delay(&self) {
        std::thread::sleep(Duration::from_millis(3));
    }

    fn reset_data(&mut self) {
        self.host.data4(false);
        self.host.data5(false);
        self.host.data6(false);
        self.host.data7(false);
    }

    fn toggle_enable(&mut self) {
        self.delay();
        self.host.enable(false);
        self.delay();
        self.host.enable(true);
        self.delay();
    }

    fn write_cmd(&mut self, b: u8) {
        self.write_byte_in_mode(b, Mode::Command);
    }

    fn write_data(&mut self, b: u8) {
        self.write_byte_in_mode(b, Mode::Data)
    }

    fn write_byte_in_mode(&mut self, b: u8, mode: Mode) {
        self.host.rs(mode);
        self.reset_data();

        if b&0x10==0x10 {
            self.host.data4(true);
        }
        if b&0x20==0x20 {
            self.host.data5(true);
        }
        if b&0x40==0x40 {
            self.host.data6(true);
        }
        if b&0x80==0x80 {
            self.host.data7(true);
        }

        self.toggle_enable();
        self.reset_data();

        if b&0x01==0x01 {
            self.host.data4(true);
        }
        if b&0x02==0x02 {
            self.host.data5(true);
        }
        if b&0x04==0x04 {
            self.host.data6(true);
        }
        if b&0x08==0x08 {
            self.host.data7(true);
        }

        self.toggle_enable();
    }

    pub fn row_select(&mut self, row: DisplayRow) {
        match row {
            DisplayRow::R0 => self.write_cmd(0x80),
            DisplayRow::R1 => self.write_cmd(0xC0)
        }
    }

    pub fn write_string(&mut self, txt: &str) {
        let bytes = txt.as_bytes();
        let len = if txt.len() > 16 {
            16
        } else {
            txt.len()
        };
        for i in 0..len {
            self.write_data(bytes[i]);
        }
        for _i in (len-1)..17 {
            self.write_data(0x20); //pad rest of line with blanks
        }
    }
}