liquid_crystal/lcd_trait/interfaces/
mod.rs

1#![allow(unused)]
2
3pub mod dummy;
4use embedded_hal::i2c::I2c;
5use embedded_hal::digital::OutputPin;
6
7pub const EN: u8 = 0b00000100;
8pub const _RW: u8 = 0b00000010; // NO READ FUNCTION
9pub const RS: u8 = 0b00000001;
10
11#[deprecated(since="0.2.0", note="This address is only valid in the LCM1602 IIC module, just type 0x27 instead")]
12pub const I2C_ADDRESS: u8 = 0x27;
13
14pub trait Interface {
15    fn send(&mut self, config: u8, data: u8);
16}
17
18pub struct Parallel<D1, D2, D3, D4, RS, EN, EN2>
19where
20    D1: OutputPin,
21    D2: OutputPin,
22    D3: OutputPin,
23    D4: OutputPin,
24    RS: OutputPin,
25    EN: OutputPin,
26    EN2: OutputPin,
27{
28    d1: D1,
29    d2: D2,
30    d3: D3,
31    d4: D4,
32    rs: RS,
33    en: EN,
34    en2: EN2,
35}
36
37impl<D1, D2, D3, D4, RS, EN, EN2> Parallel<D1, D2, D3, D4, RS, EN, EN2>
38where
39    D1: OutputPin,
40    D2: OutputPin,
41    D3: OutputPin,
42    D4: OutputPin,
43    RS: OutputPin,
44    EN: OutputPin,
45    EN2: OutputPin,
46{
47    pub fn new(
48        d1: D1,
49        d2: D2,
50        d3: D3,
51        d4: D4,
52        rs: RS,
53        en: EN,
54        en2: EN2,
55    ) -> Parallel<D1, D2, D3, D4, RS, EN, EN2> {
56        Parallel {
57            d1,
58            d2,
59            d3,
60            d4,
61            rs,
62            en,
63            en2,
64        }
65    }
66}
67
68impl<D1, D2, D3, D4, RS, EN, EN2> Interface for Parallel<D1, D2, D3, D4, RS, EN, EN2>
69where
70    D1: OutputPin,
71    D2: OutputPin,
72    D3: OutputPin,
73    D4: OutputPin,
74    RS: OutputPin,
75    EN: OutputPin,
76    EN2: OutputPin,
77{
78    fn send(&mut self, config: u8, data: u8) {
79        if (data & 0b0001_0000) != 0 {
80            self.d1.set_high();
81        } else {
82            self.d1.set_low();
83        }
84
85        if (data & 0b0010_0000) != 0 {
86            self.d2.set_high();
87        } else {
88            self.d2.set_low();
89        }
90
91        if (data & 0b0100_0000) != 0 {
92            self.d3.set_high();
93        } else {
94            self.d3.set_low();
95        }
96
97        if (data & 0b1000_0000) != 0 {
98            self.d4.set_high();
99        } else {
100            self.d4.set_low();
101        }
102
103        if (config & 0b0000_0001) != 0 {
104            self.rs.set_high();
105        } else {
106            self.rs.set_low();
107        }
108
109        if (config & 0b0000_0100) != 0 {
110            self.en.set_high();
111        } else {
112            self.en.set_low();
113        }
114
115        if (config & 0b0000_1000) != 0 {
116            self.en2.set_high();
117        } else {
118            self.en2.set_low();
119        }
120    }
121}
122
123pub struct I2C<T: I2c> {
124    i2c_bus: T,
125    addr: u8,
126}
127
128impl<T: I2c> I2C<T> {
129    pub fn new(i2c_bus: T, addr: u8) -> I2C<T> {
130        I2C { i2c_bus, addr }
131    }
132}
133
134impl<T: I2c> Interface for I2C<T> {
135    fn send(&mut self, config: u8, data: u8) {
136        let byte = (config & 0b00000111) | (data & 0xF0) | 0x08; //ignores possible additional Enables, i2C Module does not support multiple displays
137        self.i2c_bus.write(self.addr, &[byte]); //0x08 (0b0000_1000) corresponds to the display backlight in the I2C module
138    }
139}