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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#![no_std]

extern crate embedded_hal as hal;

#[cfg(feature = "graphics")]
extern crate embedded_graphics;

use hal::blocking::spi;
use hal::blocking::delay::DelayMs;
use hal::spi::{Mode, Phase, Polarity};
use hal::digital::OutputPin;

use core::iter::IntoIterator;
use core::fmt::Debug;

/// SPI mode
pub const MODE: Mode = Mode {
    polarity: Polarity::IdleLow,
    phase: Phase::CaptureOnFirstTransition,
};

const WIDTH: usize = 240;
const HEIGHT: usize = 320;

#[derive(Debug)]
pub enum Error<E> {
    Spi(E),
}

/// The default orientation is Portrait
pub enum Orientation {
    Portrait,
    PortraitFlipped,
    Landscape,
    LandscapeFlipped,
}

/// There are two method for drawing to the screen:
/// [draw_raw](struct.Ili9341.html#method.draw_raw) and
/// [draw_iter](struct.Ili9341.html#method.draw_iter).
///
/// In both cases the expected pixel format is rgb565.
///
/// The hardware makes it efficient to draw rectangles on the screen.
///
/// What happens is the following:
///
/// - A drawing window is prepared (with the 2 opposite corner coordinates)
/// - The starting point for drawint is the top left corner of this window
/// - Every pair of bytes received is intepreted as a pixel value in rgb565
/// - As soon as a pixel is received, an internal counter is incremented,
///   and the next word will fill the next pixel (the adjacent on the right, or
///   the first of the next row if the row ended)
pub struct Ili9341<SPI, CS, DC, RESET> {
    spi: SPI,
    cs: CS,
    dc: DC,
    reset: RESET,
    width: usize,
    height: usize,
}

impl<E, SPI, CS, DC, RESET> Ili9341<SPI, CS, DC, RESET>
where
    SPI: spi::Transfer<u8, Error = E> + spi::Write<u8, Error = E>,
    CS: OutputPin,
    DC: OutputPin,
    RESET: OutputPin,
{
    pub fn new<DELAY: DelayMs<u16>>(
        spi: SPI,
        cs: CS,
        dc: DC,
        reset: RESET,
        delay: &mut DELAY,
    ) -> Result<Self, Error<E>> {
        let mut ili9341 = Ili9341 {
            spi,
            cs,
            dc,
            reset,
            width: WIDTH,
            height: HEIGHT,
        };

        ili9341.hard_reset(delay);
        ili9341.command(Command::SoftwareReset, &[])?;
        delay.delay_ms(200);

        ili9341.command(Command::PowerControlA, &[0x39, 0x2c, 0x00, 0x34, 0x02])?;
        ili9341.command(Command::PowerControlB, &[0x00, 0xc1, 0x30])?;
        ili9341.command(Command::DriverTimingControlA, &[0x85, 0x00, 0x78])?;
        ili9341.command(Command::DriverTimingControlB, &[0x00, 0x00])?;
        ili9341.command(Command::PowerOnSequenceControl, &[0x64, 0x03, 0x12, 0x81])?;
        ili9341.command(Command::PumpRatioControl, &[0x20])?;
        ili9341.command(Command::PowerControl1, &[0x23])?;
        ili9341.command(Command::PowerControl2, &[0x10])?;
        ili9341.command(Command::VCOMControl1, &[0x3e, 0x28])?;
        ili9341.command(Command::VCOMControl2, &[0x86])?;
        ili9341.command(Command::MemoryAccessControl, &[0x48])?;
        ili9341.command(Command::PixelFormatSet, &[0x55])?;
        ili9341.command(Command::FrameControlNormal, &[0x00, 0x18])?;
        ili9341.command(Command::DisplayFunctionControl, &[0x08, 0x82, 0x27])?;
        ili9341.command(Command::Enable3G, &[0x00])?;
        ili9341.command(Command::GammaSet, &[0x01])?;
        ili9341.command(
            Command::PositiveGammaCorrection,
            &[
                0x0f, 0x31, 0x2b, 0x0c, 0x0e, 0x08, 0x4e, 0xf1, 0x37, 0x07, 0x10, 0x03, 0x0e, 0x09,
                0x00,
            ],
        )?;
        ili9341.command(
            Command::NegativeGammaCorrection,
            &[
                0x00, 0x0e, 0x14, 0x03, 0x11, 0x07, 0x31, 0xc1, 0x48, 0x08, 0x0f, 0x0c, 0x31, 0x36,
                0x0f,
            ],
        )?;
        ili9341.command(Command::SleepOut, &[])?;
        delay.delay_ms(120);
        ili9341.command(Command::DisplayOn, &[])?;

        Ok(ili9341)
    }

    fn hard_reset<DELAY: DelayMs<u16>>(&mut self, delay: &mut DELAY) {
        // set high if previously low
        self.reset.set_high();
        delay.delay_ms(200);
        // set low for reset
        self.reset.set_low();
        delay.delay_ms(200);
        // set high for normal operation
        self.reset.set_high();
        delay.delay_ms(200);
    }
    fn command(&mut self, cmd: Command, args: &[u8]) -> Result<(), Error<E>> {
        self.cs.set_low();

        self.dc.set_low();
        self.spi.write(&[cmd as u8]).map_err(Error::Spi)?;

        self.dc.set_high();
        self.spi.write(args).map_err(Error::Spi)?;

        self.cs.set_high();
        Ok(())
    }
    fn write_iter<I: IntoIterator<Item = u16>>(&mut self, data: I) -> Result<(), Error<E>> {
        self.cs.set_low();

        self.dc.set_low();
        self.spi
            .write(&[Command::MemoryWrite as u8])
            .map_err(Error::Spi)?;

        self.dc.set_high();
        for d in data.into_iter() {
            self.spi
                .write(&[(d >> 8) as u8, (d & 0xff) as u8])
                .map_err(Error::Spi)?;
        }

        self.cs.set_high();
        Ok(())
    }
    fn write_raw(&mut self, data: &[u8]) -> Result<(), Error<E>> {
        self.cs.set_low();

        self.dc.set_low();
        self.spi
            .write(&[Command::MemoryWrite as u8])
            .map_err(Error::Spi)?;

        self.dc.set_high();
        self.spi.write(data).map_err(Error::Spi)?;

        self.cs.set_high();
        Ok(())
    }
    fn set_window(&mut self, x0: u16, y0: u16, x1: u16, y1: u16) -> Result<(), Error<E>> {
        self.command(
            Command::ColumnAddressSet,
            &[
                (x0 >> 8) as u8,
                (x0 & 0xff) as u8,
                (x1 >> 8) as u8,
                (x1 & 0xff) as u8,
            ],
        )?;
        self.command(
            Command::PageAddressSet,
            &[
                (y0 >> 8) as u8,
                (y0 & 0xff) as u8,
                (y1 >> 8) as u8,
                (y1 & 0xff) as u8,
            ],
        )?;
        Ok(())
    }
    /// Draw a rectangle on the screen, represented by top-left corner (x0, y0)
    /// and bottom-right corner (x1, y1).
    ///
    /// The border is included.
    ///
    /// This method accepts an iterator of rgb565 pixel values.
    ///
    /// The iterator is useful to avoid wasting memory by holding a buffer for
    /// the whole screen when it is not necessary.
    pub fn draw_iter<I: IntoIterator<Item = u16>>(
        &mut self,
        x0: u16,
        y0: u16,
        x1: u16,
        y1: u16,
        data: I,
    ) -> Result<(), Error<E>> {
        self.set_window(x0, y0, x1, y1)?;
        self.write_iter(data)
    }
    /// Draw a rectangle on the screen, represented by top-left corner (x0, y0)
    /// and bottom-right corner (x1, y1).
    ///
    /// The border is included.
    ///
    /// This method accepts a raw buffer of bytes that will be copied to the screen
    /// video memory.
    ///
    /// The expected format is rgb565, and the two bytes for a pixel
    /// are in big endian order.
    pub fn draw_raw(
        &mut self,
        x0: u16,
        y0: u16,
        x1: u16,
        y1: u16,
        data: &[u8],
    ) -> Result<(), Error<E>> {
        self.set_window(x0, y0, x1, y1)?;
        self.write_raw(data)
    }
    /// Change the orientation of the screen
    pub fn set_orientation(&mut self, mode: Orientation) -> Result<(), Error<E>> {
        match mode {
            Orientation::Portrait => {
                self.width = WIDTH;
                self.height = HEIGHT;
                self.command(Command::MemoryAccessControl, &[0x40 | 0x08])
            }
            Orientation::Landscape => {
                self.width = HEIGHT;
                self.height = WIDTH;
                self.command(Command::MemoryAccessControl, &[0x20 | 0x08])
            }
            Orientation::PortraitFlipped => {
                self.width = WIDTH;
                self.height = HEIGHT;
                self.command(Command::MemoryAccessControl, &[0x80 | 0x08])
            }
            Orientation::LandscapeFlipped => {
                self.width = HEIGHT;
                self.height = WIDTH;
                self.command(Command::MemoryAccessControl, &[0x40 | 0x80 | 0x20 | 0x08])
            }
        }
    }
    /// Get the current screen width. It can change based on the current orientation
    pub fn width(&self) -> usize {
        self.width
    }
    /// Get the current screen heighth. It can change based on the current orientation
    pub fn height(&self) -> usize {
        self.height
    }
}

#[cfg(feature = "graphics")]
use embedded_graphics::drawable;
#[cfg(feature = "graphics")]
use embedded_graphics::{drawable::Pixel, pixelcolor::PixelColorU16, Drawing};

#[cfg(feature = "graphics")]
impl<E, SPI, CS, DC, RESET> Drawing<PixelColorU16> for Ili9341<SPI, CS, DC, RESET>
where
    SPI: spi::Transfer<u8, Error = E> + spi::Write<u8, Error = E>,
    CS: OutputPin,
    DC: OutputPin,
    RESET: OutputPin,
    E: Debug,
{
    fn draw<T>(&mut self, item_pixels: T)
    where
        T: Iterator<Item = drawable::Pixel<PixelColorU16>>,
    {
        for Pixel(pos, color) in item_pixels {
            self.draw_raw(
                pos.0 as u16,
                pos.1 as u16,
                pos.0 as u16,
                pos.1 as u16,
                if color == PixelColorU16(0) {
                    &[0xff, 0xff]
                } else {
                    &[0, 0]
                },
            )
            .expect("Failed to communicate with device");
        }
    }
}

#[derive(Clone, Copy)]
enum Command {
    SoftwareReset = 0x01,
    PowerControlA = 0xcb,
    PowerControlB = 0xcf,
    DriverTimingControlA = 0xe8,
    DriverTimingControlB = 0xea,
    PowerOnSequenceControl = 0xed,
    PumpRatioControl = 0xf7,
    PowerControl1 = 0xc0,
    PowerControl2 = 0xc1,
    VCOMControl1 = 0xc5,
    VCOMControl2 = 0xc7,
    MemoryAccessControl = 0x36,
    PixelFormatSet = 0x3a,
    FrameControlNormal = 0xb1,
    DisplayFunctionControl = 0xb6,
    Enable3G = 0xf2,
    GammaSet = 0x26,
    PositiveGammaCorrection = 0xe0,
    NegativeGammaCorrection = 0xe1,
    SleepOut = 0x11,
    DisplayOn = 0x29,
    ColumnAddressSet = 0x2a,
    PageAddressSet = 0x2b,
    MemoryWrite = 0x2c,
}