epd-waveshare 0.6.0

An embedded-hal based driver for ePaper displays from Waveshare formerly published as eink-waveshare-rs
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! A driver for the Waveshare three-color E-ink Pi Pico hat 'Pico-ePaper-2.66-B' B/W/R.
//!
//!
//! This driver was built and tested for this 296x152, 2.66inch three-color E-Ink display hat for the Pi Pico, it is expected to work for
//! other boards too, but that might depend on how the OTP memory in the display is programmed by the factory.
//!
//! The driver embedded in the display of this board is the SSD1675B, [documented by cursedhardware](https://cursedhardware.github.io/epd-driver-ic/SSD1675B.pdf).
//!
//! The pin assigments are shown on the Waveshare wiki [schematic](https://www.waveshare.com/w/upload/8/8d/Pico-ePaper-2.66.pdf).
//!
//! Information on this display/hat can be found at the [Waveshare Wiki](https://www.waveshare.com/wiki/Pico-ePaper-2.66-B).
//! Do read this documentation, in particular to understand how often this display both should and should not be updated.
//!
//! # Example for the 'Pico-ePaper-2.66-B' B/W/R Pi Pico Hat E-Ink Display
//! This example was created in an environment using the [Knurling](https://github.com/knurling-rs) ```flip-link```, ```defmt``` and ```probe-run``` tools - you will
//! need to adjust for your preferred setup.
//!```ignore
//!#![no_std]
//!#![no_main]
//!use epd_waveshare::{epd2in66b::*, prelude::*};
//!
//!use cortex_m_rt::entry;
//!//use defmt::*;
//!use defmt_rtt as _;
//!use panic_probe as _;
//!
//!// Use embedded-graphics to create a bitmap to show
//!fn drawing() -> Display2in66b {
//!    use embedded_graphics::{
//!        mono_font::{ascii::FONT_10X20, MonoTextStyle},
//!        prelude::*,
//!        primitives::PrimitiveStyle,
//!        text::{Alignment, Text},
//!    };
//!
//!    // Create a Display buffer to draw on, specific for this ePaper
//!    let mut display = Display2in66b::default();
//!
//!    // Landscape mode, USB plug to the right
//!    display.set_rotation(DisplayRotation::Rotate270);
//!
//!    // Change the background from the default black to white
//!    let _ = display
//!        .bounding_box()
//!        .into_styled(PrimitiveStyle::with_fill(TriColor::White))
//!        .draw(&mut display);
//!
//!    // Draw some text on the buffer
//!    let text = "Pico-ePaper-2.66 B/W/R";
//!    Text::with_alignment(
//!        text,
//!        display.bounding_box().center() + Point::new(1, 0),
//!        MonoTextStyle::new(&FONT_10X20, TriColor::Black),
//!        Alignment::Center,
//!    )
//!    .draw(&mut display)
//!    .unwrap();
//!    Text::with_alignment(
//!        text,
//!        display.bounding_box().center() + Point::new(0, 1),
//!        MonoTextStyle::new(&FONT_10X20, TriColor::Chromatic),
//!        Alignment::Center,
//!    )
//!    .draw(&mut display)
//!    .unwrap();
//!
//!    display
//!}
//!
//!#[entry]
//!fn main() -> ! {
//!    use fugit::RateExtU32;
//!    use rp_pico::hal::{
//!        self,
//!        clocks::{init_clocks_and_plls, Clock},
//!        gpio::{FunctionSpi, PinState, Pins},
//!        pac,
//!        sio::Sio,
//!        watchdog::Watchdog,
//!    };
//!
//!    // Boilerplate to access the peripherals
//!    let mut pac = pac::Peripherals::take().unwrap();
//!    let core = pac::CorePeripherals::take().unwrap();
//!    let mut watchdog = Watchdog::new(pac.WATCHDOG);
//!    let external_xtal_freq_hz = 12_000_000u32;
//!    let clocks = init_clocks_and_plls(
//!        external_xtal_freq_hz,
//!        pac.XOSC,
//!        pac.CLOCKS,
//!        pac.PLL_SYS,
//!        pac.PLL_USB,
//!        &mut pac.RESETS,
//!        &mut watchdog,
//!    )
//!    .ok()
//!    .unwrap();
//!    let sio = Sio::new(pac.SIO);
//!    let pins = Pins::new(
//!        pac.IO_BANK0,
//!        pac.PADS_BANK0,
//!        sio.gpio_bank0,
//!        &mut pac.RESETS,
//!    );
//!
//!    // Pin assignments of the Pi Pico-ePaper-2.66 Hat
//!    let _ = pins.gpio10.into_mode::<FunctionSpi>();
//!    let _ = pins.gpio11.into_mode::<FunctionSpi>();
//!    let chip_select_pin = pins.gpio9.into_push_pull_output_in_state(PinState::High);
//!    let is_busy_pin = pins.gpio13.into_floating_input();
//!    let data_or_command_pin = pins.gpio8.into_push_pull_output_in_state(PinState::High);
//!    let reset_pin = pins.gpio12.into_push_pull_output_in_state(PinState::High);
//!
//!    // SPI
//!    let spi = hal::Spi::<_, _, 8>::new(pac.SPI1);
//!    let mut spi = spi.init(
//!        &mut pac.RESETS,
//!        clocks.peripheral_clock.freq(),
//!        20_000_000u32.Hz(), // The SSD1675B docs say 20MHz max
//!        &SPI_MODE,
//!    );
//!
//!    // Delay
//!    let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
//!
//!    // Setup the EPD driver
//!    let mut e_paper = Epd2in66b::new(
//!        &mut spi,
//!        chip_select_pin,
//!        is_busy_pin,
//!        data_or_command_pin,
//!        reset_pin,
//!        &mut delay,
//!        None,
//!    )
//!    .unwrap();
//!
//!    // Create and fill a Display buffer
//!    let display = drawing();
//!
//!    // Send the Display buffer to the ePaper RAM
//!    e_paper
//!        .update_color_frame(
//!            &mut spi,
//!            &mut delay,
//!            &display.bw_buffer(),
//!            &display.chromatic_buffer(),
//!        )
//!        .unwrap();
//!
//!    // Render the ePaper RAM - takes time.
//!    e_paper.display_frame(&mut spi, &mut delay).unwrap();
//!
//!    // Always turn off your EPD as much as possible - ePaper wears out while powered on.
//!    e_paper.sleep(&mut spi, &mut delay).unwrap();
//!
//!    delay.delay_ms(60 * 1000);
//!
//!    // Set the display all-white before storing your ePaper long-term.
//!    e_paper.wake_up(&mut spi, &mut delay).unwrap();
//!    e_paper.clear_frame(&mut spi, &mut delay).unwrap();
//!    e_paper.display_frame(&mut spi, &mut delay).unwrap();
//!    e_paper.sleep(&mut spi, &mut delay).unwrap();
//!
//!    loop {}
//!}
//!```

use embedded_hal::{
    delay::DelayNs,
    digital::{InputPin, OutputPin},
    spi::SpiDevice,
};

use crate::color::TriColor;
use crate::interface::DisplayInterface;
use crate::traits::{
    InternalWiAdditions, RefreshLut, WaveshareDisplay, WaveshareThreeColorDisplay,
};

pub(crate) mod command;
use self::command::*;
use crate::buffer_len;

/// Display height in pixels.
pub const WIDTH: u32 = 152;
/// Display width in pixels
pub const HEIGHT: u32 = 296;

const SINGLE_BYTE_WRITE: bool = true;

/// White, display this during long-term storage
pub const DEFAULT_BACKGROUND_COLOR: TriColor = TriColor::White;

/// A Display buffer configured with our extent and color depth.
#[cfg(feature = "graphics")]
pub type Display2in66b = crate::graphics::Display<
    WIDTH,
    HEIGHT,
    false,
    { buffer_len(WIDTH as usize, HEIGHT as usize) * 2 },
    TriColor,
>;

/// The EPD 2in66-B driver.
pub struct Epd2in66b<SPI, BUSY, DC, RST, DELAY> {
    interface: DisplayInterface<SPI, BUSY, DC, RST, DELAY, SINGLE_BYTE_WRITE>,
    background: TriColor,
}

impl<SPI, BUSY, DC, RST, DELAY> InternalWiAdditions<SPI, BUSY, DC, RST, DELAY>
    for Epd2in66b<SPI, BUSY, DC, RST, DELAY>
where
    SPI: SpiDevice,
    BUSY: InputPin,
    DC: OutputPin,
    RST: OutputPin,
    DELAY: DelayNs,
{
    fn init(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
        // We follow the sequence of the Pi-Pico hat example code.
        self.hw_reset(delay)?;
        self.sw_reset(spi, delay)?;
        self.data_entry_mode(spi, DataEntryRow::XMinor, DataEntrySign::IncYIncX)?;
        self.set_display_window(spi, 0, 0, WIDTH - 1, HEIGHT - 1)?;
        self.update_control1(
            spi,
            WriteMode::Normal,
            WriteMode::Normal,
            OutputSource::S8ToS167,
        )?;
        self.set_cursor(spi, 0, 0)?;

        Ok(())
    }
}

impl<SPI, BUSY, DC, RST, DELAY> WaveshareThreeColorDisplay<SPI, BUSY, DC, RST, DELAY>
    for Epd2in66b<SPI, BUSY, DC, RST, DELAY>
where
    SPI: SpiDevice,
    BUSY: InputPin,
    DC: OutputPin,
    RST: OutputPin,
    DELAY: DelayNs,
{
    fn update_color_frame(
        &mut self,
        spi: &mut SPI,
        delay: &mut DELAY,
        black: &[u8],
        chromatic: &[u8],
    ) -> Result<(), SPI::Error> {
        self.update_achromatic_frame(spi, delay, black)?;
        self.update_chromatic_frame(spi, delay, chromatic)
    }

    fn update_achromatic_frame(
        &mut self,
        spi: &mut SPI,
        _delay: &mut DELAY,
        black: &[u8],
    ) -> Result<(), SPI::Error> {
        self.set_cursor(spi, 0, 0)?;
        self.interface.cmd(spi, Command::WriteBlackWhiteRAM)?;
        self.interface.data(spi, black)
    }

    fn update_chromatic_frame(
        &mut self,
        spi: &mut SPI,
        _delay: &mut DELAY,
        chromatic: &[u8],
    ) -> Result<(), SPI::Error> {
        self.set_cursor(spi, 0, 0)?;
        self.interface.cmd(spi, Command::WriteRedRAM)?;
        self.interface.data(spi, chromatic)
    }
}

impl<SPI, BUSY, DC, RST, DELAY> WaveshareDisplay<SPI, BUSY, DC, RST, DELAY>
    for Epd2in66b<SPI, BUSY, DC, RST, DELAY>
where
    SPI: SpiDevice,
    BUSY: InputPin,
    DC: OutputPin,
    RST: OutputPin,
    DELAY: DelayNs,
{
    type DisplayColor = TriColor;

    fn new(
        spi: &mut SPI,
        busy: BUSY,
        dc: DC,
        rst: RST,
        delay: &mut DELAY,
        delay_us: Option<u32>,
    ) -> Result<Self, SPI::Error>
    where
        Self: Sized,
    {
        let mut epd = Self {
            interface: DisplayInterface::new(busy, dc, rst, delay_us),
            background: DEFAULT_BACKGROUND_COLOR,
        };
        epd.init(spi, delay)?;
        Ok(epd)
    }

    fn sleep(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> {
        self.interface.cmd_with_data(
            spi,
            Command::DeepSleepMode,
            &[DeepSleep::SleepLosingRAM as u8],
        )
    }

    fn wake_up(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
        self.init(spi, delay)
    }

    fn set_background_color(&mut self, color: Self::DisplayColor) {
        self.background = color;
    }

    fn background_color(&self) -> &Self::DisplayColor {
        &self.background
    }

    fn width(&self) -> u32 {
        WIDTH
    }

    fn height(&self) -> u32 {
        HEIGHT
    }

    fn update_frame(
        &mut self,
        spi: &mut SPI,
        buffer: &[u8],
        delay: &mut DELAY,
    ) -> Result<(), SPI::Error> {
        self.set_cursor(spi, 0, 0)?;
        self.update_achromatic_frame(spi, delay, buffer)?;
        self.red_pattern(spi, delay, PatW::W160, PatH::H296, StartWith::Zero) // do NOT consider background here since red overrides other colors
    }

    fn update_partial_frame(
        &mut self,
        spi: &mut SPI,
        delay: &mut DELAY,
        buffer: &[u8],
        x: u32,
        y: u32,
        width: u32,
        height: u32,
    ) -> Result<(), SPI::Error> {
        self.set_display_window(spi, x, y, x + width, y + height)?;
        self.set_cursor(spi, x, y)?;
        self.update_achromatic_frame(spi, delay, buffer)?;
        self.set_display_window(spi, 0, 0, WIDTH, HEIGHT)
    }

    fn display_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
        self.interface.cmd(spi, Command::MasterActivation)?;
        self.wait_until_idle(delay)
    }

    fn update_and_display_frame(
        &mut self,
        spi: &mut SPI,
        buffer: &[u8],
        delay: &mut DELAY,
    ) -> Result<(), SPI::Error> {
        self.update_frame(spi, buffer, delay)?;
        self.display_frame(spi, delay)
    }

    fn clear_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
        let (white, red) = match self.background {
            TriColor::Black => (StartWith::Zero, StartWith::Zero),
            TriColor::White => (StartWith::One, StartWith::Zero),
            TriColor::Chromatic => (StartWith::Zero, StartWith::One),
        };
        self.black_white_pattern(spi, delay, PatW::W160, PatH::H296, white)?;
        self.red_pattern(spi, delay, PatW::W160, PatH::H296, red)
    }

    fn set_lut(
        &mut self,
        _spi: &mut SPI,
        _delay: &mut DELAY,
        _refresh_rate: Option<RefreshLut>,
    ) -> Result<(), SPI::Error> {
        Ok(())
    }

    fn wait_until_idle(&mut self, _spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
        self.wait_until_idle(delay)
    }
}

// Helper functions that enforce some type and value constraints. Meant to help with code readability. They caught some of my silly errors -> yay rust!.
impl<SPI, BUSY, DC, RST, DELAY> Epd2in66b<SPI, BUSY, DC, RST, DELAY>
where
    SPI: SpiDevice,
    BUSY: InputPin,
    DC: OutputPin,
    RST: OutputPin,
    DELAY: DelayNs,
{
    fn wait_until_idle(&mut self, delay: &mut DELAY) -> Result<(), SPI::Error> {
        self.interface.wait_until_idle(delay, false);
        Ok(())
    }
    fn hw_reset(&mut self, delay: &mut DELAY) -> Result<(), SPI::Error> {
        // The initial delay is taken from other code here, the 2 ms comes from the SSD1675B datasheet.
        self.interface.reset(delay, 20_000, 2_000);
        self.wait_until_idle(delay)
    }
    fn sw_reset(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
        self.interface.cmd(spi, Command::Reset)?;
        self.wait_until_idle(delay)
    }
    fn data_entry_mode(
        &mut self,
        spi: &mut SPI,
        row: DataEntryRow,
        sign: DataEntrySign,
    ) -> Result<(), SPI::Error> {
        self.interface
            .cmd_with_data(spi, Command::DataEntryMode, &[row as u8 | sign as u8])
    }
    fn set_display_window(
        &mut self,
        spi: &mut SPI,
        xstart: u32,
        ystart: u32,
        xend: u32,
        yend: u32,
    ) -> Result<(), SPI::Error> {
        self.interface.cmd_with_data(
            spi,
            Command::SetXAddressRange,
            &[(((xstart >> 3) & 0x1f) as u8), (((xend >> 3) & 0x1f) as u8)],
        )?;
        self.interface.cmd_with_data(
            spi,
            Command::SetYAddressRange,
            &[
                ((ystart & 0xff) as u8),
                (((ystart >> 8) & 0x01) as u8),
                ((yend & 0xff) as u8),
                (((yend >> 8) & 0x01) as u8),
            ],
        )
    }
    fn update_control1(
        &mut self,
        spi: &mut SPI,
        red_mode: WriteMode,
        bw_mode: WriteMode,
        source: OutputSource,
    ) -> Result<(), SPI::Error> {
        self.interface.cmd_with_data(
            spi,
            Command::DisplayUpdateControl1,
            &[((red_mode as u8) << 4 | bw_mode as u8), (source as u8)],
        )
    }

    fn set_cursor(&mut self, spi: &mut SPI, x: u32, y: u32) -> Result<(), SPI::Error> {
        self.interface.cmd_with_data(
            spi,
            Command::SetXAddressCounter,
            &[((x >> 3) & 0x1f) as u8],
        )?;
        self.interface.cmd_with_data(
            spi,
            Command::SetYAddressCounter,
            &[((y & 0xff) as u8), (((y >> 8) & 0x01) as u8)],
        )
    }

    fn black_white_pattern(
        &mut self,
        spi: &mut SPI,
        delay: &mut DELAY,
        w: PatW,
        h: PatH,
        phase: StartWith,
    ) -> Result<(), SPI::Error> {
        self.interface.cmd_with_data(
            spi,
            Command::BlackWhiteRAMTestPattern,
            &[phase as u8 | h as u8 | w as u8],
        )?;
        self.wait_until_idle(delay)
    }
    fn red_pattern(
        &mut self,
        spi: &mut SPI,
        delay: &mut DELAY,
        w: PatW,
        h: PatH,
        phase: StartWith,
    ) -> Result<(), SPI::Error> {
        self.interface.cmd_with_data(
            spi,
            Command::RedRAMTestPattern,
            &[phase as u8 | h as u8 | w as u8],
        )?;
        self.wait_until_idle(delay)
    }
}