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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#![no_std]

//! Generic parallel GPIO interface for display drivers

use embedded_hal::digital::v2::OutputPin;

pub use display_interface::{DisplayError, WriteOnlyDataCommand};

/// Parallel 8 Bit communication interface
///
/// This interface implements an 8-Bit "8080" style write-only display interface using any
/// `embedded_hal` `digital::v2::OutputPin` implementation.
///
/// For the 8-Bit implementation you need to provide 8 types implementing `OutputPin` which
/// ressemble the bits 0 through 7 (which bit 0 being the LSB and 7 the MSB) as well as one
/// `OutputPin` for the data/command selection and one `OutputPin` for the write-enable flag.
///
/// All pins are supposed to be high-active, high for the D/C pin meaning "data" and the
/// write-enable being pulled low before the setting of the bits and supposed to be sampled at a
/// low to high edge.
pub struct PGPIO8BitInterface<P0, P1, P2, P3, P4, P5, P6, P7, DC, WR> {
    p0: P0,
    p1: P1,
    p2: P2,
    p3: P3,
    p4: P4,
    p5: P5,
    p6: P6,
    p7: P7,
    dc: DC,
    wr: WR,
    last: u8,
}

impl<P0, P1, P2, P3, P4, P5, P6, P7, DC, WR>
    PGPIO8BitInterface<P0, P1, P2, P3, P4, P5, P6, P7, DC, WR>
where
    P0: OutputPin,
    P1: OutputPin,
    P2: OutputPin,
    P3: OutputPin,
    P4: OutputPin,
    P5: OutputPin,
    P6: OutputPin,
    P7: OutputPin,
    DC: OutputPin,
    WR: OutputPin,
{
    /// Create new parallel GPIO interface for communication with a display driver
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        p0: P0,
        p1: P1,
        p2: P2,
        p3: P3,
        p4: P4,
        p5: P5,
        p6: P6,
        p7: P7,
        dc: DC,
        wr: WR,
    ) -> Self {
        Self {
            p0,
            p1,
            p2,
            p3,
            p4,
            p5,
            p6,
            p7,
            dc,
            wr,
            last: 0,
        }
    }

    fn set_value(self: &mut Self, value: u8) -> Result<(), DisplayError> {
        let changed = value ^ self.last;

        // It's quite common for multiple consecutive values to be identical, e.g. when filling or
        // clearing the screen, so let's optimize for that case
        if changed == 0 {
            return Ok(());
        }

        self.last = value;

        if changed & 1 != 0 {
            if value & 1 != 0 {
                self.p0.set_high()
            } else {
                self.p0.set_low()
            }
            .map_err(|_| DisplayError::BusWriteError)?
        };

        if changed & 2 != 0 {
            if value & 2 != 0 {
                self.p1.set_high()
            } else {
                self.p1.set_low()
            }
            .map_err(|_| DisplayError::BusWriteError)?
        };

        if changed & 4 != 0 {
            if value & 4 != 0 {
                self.p2.set_high()
            } else {
                self.p2.set_low()
            }
            .map_err(|_| DisplayError::BusWriteError)?
        };

        if changed & 8 != 0 {
            if value & 8 != 0 {
                self.p3.set_high()
            } else {
                self.p3.set_low()
            }
            .map_err(|_| DisplayError::BusWriteError)?
        };

        if changed & 16 != 0 {
            if value & 16 != 0 {
                self.p4.set_high()
            } else {
                self.p4.set_low()
            }
            .map_err(|_| DisplayError::BusWriteError)?
        };

        if changed & 32 != 0 {
            if value & 32 != 0 {
                self.p5.set_high()
            } else {
                self.p5.set_low()
            }
            .map_err(|_| DisplayError::BusWriteError)?
        };

        if changed & 64 != 0 {
            if value & 64 != 0 {
                self.p6.set_high()
            } else {
                self.p6.set_low()
            }
            .map_err(|_| DisplayError::BusWriteError)?
        };

        if changed & 128 != 0 {
            if value & 128 != 0 {
                self.p7.set_high()
            } else {
                self.p7.set_low()
            }
            .map_err(|_| DisplayError::BusWriteError)?
        };

        Ok(())
    }
}

impl<P0, P1, P2, P3, P4, P5, P6, P7, DC, WR> WriteOnlyDataCommand<u8>
    for PGPIO8BitInterface<P0, P1, P2, P3, P4, P5, P6, P7, DC, WR>
where
    P0: OutputPin,
    P1: OutputPin,
    P2: OutputPin,
    P3: OutputPin,
    P4: OutputPin,
    P5: OutputPin,
    P6: OutputPin,
    P7: OutputPin,
    DC: OutputPin,
    WR: OutputPin,
{
    fn send_commands(&mut self, cmds: &[u8]) -> Result<(), DisplayError> {
        self.dc.set_low().map_err(|_| DisplayError::BusWriteError)?;
        cmds.iter().try_for_each(|cmd| {
            self.wr.set_low().map_err(|_| DisplayError::BusWriteError)?;
            self.set_value(*cmd)?;
            self.wr.set_high().map_err(|_| DisplayError::BusWriteError)
        })
    }

    fn send_data(&mut self, buf: &[u8]) -> Result<(), DisplayError> {
        self.dc
            .set_high()
            .map_err(|_| DisplayError::BusWriteError)?;
        buf.iter().try_for_each(|d| {
            self.wr.set_low().map_err(|_| DisplayError::BusWriteError)?;
            self.set_value(*d)?;
            self.wr.set_high().map_err(|_| DisplayError::BusWriteError)
        })
    }
}