Skip to content

Commit 5c9ecd7

Browse files
committed
Add support for epd2in13b_v3
1 parent f49b6e9 commit 5c9ecd7

File tree

5 files changed

+635
-0
lines changed

5 files changed

+635
-0
lines changed

examples/epd2in13b_v3.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#![deny(warnings)]
2+
3+
use embedded_graphics::{
4+
mono_font::MonoTextStyleBuilder,
5+
prelude::*,
6+
primitives::{Circle, Line, PrimitiveStyle},
7+
text::{Baseline, Text, TextStyleBuilder},
8+
};
9+
use embedded_hal::prelude::*;
10+
use epd_waveshare::{
11+
color::TriColor,
12+
epd2in13bc_v3::{Display2in13bc, Epd2in13b},
13+
graphics::DisplayRotation,
14+
prelude::*,
15+
};
16+
use linux_embedded_hal::{
17+
spidev::{self, SpidevOptions},
18+
sysfs_gpio::Direction,
19+
Delay, Pin, Spidev,
20+
};
21+
22+
// The pins in this example are for the Universal e-Paper Raw Panel Driver HAT
23+
// activate spi, gpio in raspi-config
24+
// needs to be run with sudo because of some sysfs_gpio permission problems and follow-up timing problems
25+
// see https://github.com/rust-embedded/rust-sysfs-gpio/issues/5 and follow-up issues
26+
27+
fn main() -> Result<(), std::io::Error> {
28+
// Configure SPI
29+
// Settings are taken from
30+
let mut spi = Spidev::open("/dev/spidev0.0").expect("spidev directory");
31+
let options = SpidevOptions::new()
32+
.bits_per_word(8)
33+
.max_speed_hz(4_000_000)
34+
.mode(spidev::SpiModeFlags::SPI_MODE_0)
35+
.build();
36+
spi.configure(&options).expect("spi configuration");
37+
38+
// Configure Digital I/O Pin to be used as Chip Select for SPI
39+
let cs = Pin::new(26); //BCM7 CE0
40+
cs.export().expect("cs export");
41+
while !cs.is_exported() {}
42+
cs.set_direction(Direction::Out).expect("CS Direction");
43+
cs.set_value(1).expect("CS Value set to 1");
44+
45+
let busy = Pin::new(24); // GPIO 24, board J-18
46+
busy.export().expect("busy export");
47+
while !busy.is_exported() {}
48+
busy.set_direction(Direction::In).expect("busy Direction");
49+
//busy.set_value(1).expect("busy Value set to 1");
50+
51+
let dc = Pin::new(25); // GPIO 25, board J-22
52+
dc.export().expect("dc export");
53+
while !dc.is_exported() {}
54+
dc.set_direction(Direction::Out).expect("dc Direction");
55+
dc.set_value(1).expect("dc Value set to 1");
56+
57+
let rst = Pin::new(17); // GPIO 17, board J-11
58+
rst.export().expect("rst export");
59+
while !rst.is_exported() {}
60+
rst.set_direction(Direction::Out).expect("rst Direction");
61+
rst.set_value(1).expect("rst Value set to 1");
62+
63+
let mut delay = Delay {};
64+
65+
let mut epd2in13 =
66+
Epd2in13b::new(&mut spi, cs, busy, dc, rst, &mut delay).expect("eink initalize error");
67+
68+
println!("Test all the rotations");
69+
let mut display = Display2in13bc::default();
70+
71+
display.set_rotation(DisplayRotation::Rotate0);
72+
draw_text(&mut display, "Rotate 0!", 5, 5);
73+
74+
display.set_rotation(DisplayRotation::Rotate90);
75+
draw_text(&mut display, "Rotate 90!", 5, 5);
76+
77+
display.set_rotation(DisplayRotation::Rotate180);
78+
draw_text(&mut display, "Rotate 180!", 5, 5);
79+
80+
display.set_rotation(DisplayRotation::Rotate270);
81+
draw_text(&mut display, "Rotate 270!", 5, 5);
82+
83+
epd2in13.update_frame(&mut spi, display.buffer(), &mut delay)?;
84+
epd2in13
85+
.display_frame(&mut spi, &mut delay)
86+
.expect("display frame new graphics");
87+
delay.delay_ms(5000u16);
88+
89+
display.clear_buffer(TriColor::White);
90+
println!("Now test new graphics with default rotation and some special stuff:");
91+
92+
// draw a analog clock
93+
let _ = Circle::with_center(Point::new(52, 52), 80)
94+
.into_styled(PrimitiveStyle::with_fill(TriColor::Chromatic))
95+
.draw(&mut display);
96+
let _ = Circle::with_center(Point::new(52, 52), 80)
97+
.into_styled(PrimitiveStyle::with_stroke(TriColor::Black, 3))
98+
.draw(&mut display);
99+
let _ = Line::new(Point::new(52, 52), Point::new(18, 28))
100+
.into_styled(PrimitiveStyle::with_stroke(TriColor::White, 5))
101+
.draw(&mut display);
102+
let _ = Line::new(Point::new(52, 52), Point::new(68, 28))
103+
.into_styled(PrimitiveStyle::with_stroke(TriColor::White, 1))
104+
.draw(&mut display);
105+
106+
// draw white on black background
107+
let style = MonoTextStyleBuilder::new()
108+
.font(&embedded_graphics::mono_font::ascii::FONT_6X10)
109+
.text_color(TriColor::White)
110+
.background_color(TriColor::Chromatic)
111+
.build();
112+
let text_style = TextStyleBuilder::new().baseline(Baseline::Top).build();
113+
114+
let _ = Text::with_text_style("Hello World!", Point::new(112, 10), style, text_style)
115+
.draw(&mut display);
116+
117+
// use bigger/different font
118+
let style = MonoTextStyleBuilder::new()
119+
.font(&embedded_graphics::mono_font::ascii::FONT_10X20)
120+
.text_color(TriColor::White)
121+
.background_color(TriColor::Chromatic)
122+
.build();
123+
124+
let _ = Text::with_text_style("Hello\nWorld!", Point::new(112, 40), style, text_style)
125+
.draw(&mut display);
126+
127+
epd2in13.update_color_frame(&mut spi, display.bw_buffer(), display.chromatic_buffer()).unwrap();
128+
epd2in13.display_frame(&mut spi, &mut delay).unwrap();
129+
130+
println!("Finished tests - going to sleep");
131+
epd2in13.clear_frame(&mut spi, &mut delay).unwrap();
132+
epd2in13.sleep(&mut spi, &mut delay)
133+
}
134+
135+
fn draw_text(display: &mut Display2in13bc, text: &str, x: i32, y: i32) {
136+
let style = MonoTextStyleBuilder::new()
137+
.font(&embedded_graphics::mono_font::ascii::FONT_6X10)
138+
.text_color(TriColor::White)
139+
.background_color(TriColor::Black)
140+
.build();
141+
142+
let text_style = TextStyleBuilder::new().baseline(Baseline::Top).build();
143+
144+
let _ = Text::with_text_style(text, Point::new(x, y), style, text_style).draw(display);
145+
}

src/epd2in13bc_v3/command.rs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
use crate::traits;
3+
4+
/// Epd7in5b_v3 commands
5+
///
6+
/// Should rarely (never?) be needed directly.
7+
///
8+
/// For more infos about the addresses and what they are doing look into the PDFs.
9+
#[allow(dead_code)]
10+
#[derive(Copy, Clone)]
11+
pub(crate) enum Command {
12+
/// Set Resolution, LUT selection, BWR pixels, gate scan direction, source shift
13+
/// direction, booster switch, soft reset.
14+
PanelSetting = 0x00,
15+
16+
/// Selecting internal and external power
17+
PowerSetting = 0x01,
18+
19+
/// After the Power Off command, the driver will power off following the Power Off
20+
/// Sequence; BUSY signal will become "0". This command will turn off charge pump,
21+
/// T-con, source driver, gate driver, VCOM, and temperature sensor, but register
22+
/// data will be kept until VDD becomes OFF. Source Driver output and Vcom will remain
23+
/// as previous condition, which may have 2 conditions: 0V or floating.
24+
PowerOff = 0x02,
25+
26+
/// Setting Power OFF sequence
27+
PowerOffSequenceSetting = 0x03,
28+
29+
/// Turning On the Power
30+
///
31+
/// After the Power ON command, the driver will power on following the Power ON
32+
/// sequence. Once complete, the BUSY signal will become "1".
33+
PowerOn = 0x04,
34+
35+
/// Starting data transmission
36+
BoosterSoftStart = 0x06,
37+
38+
/// This command makes the chip enter the deep-sleep mode to save power.
39+
///
40+
/// The deep sleep mode would return to stand-by by hardware reset.
41+
///
42+
/// The only one parameter is a check code, the command would be excuted if check code = 0xA5.
43+
DeepSleep = 0x07,
44+
45+
/// This command starts transmitting data and write them into SRAM. To complete data
46+
/// transmission, command DSP (Data Stop) must be issued. Then the chip will start to
47+
/// send data/VCOM for panel.
48+
///
49+
/// BLACK/WHITE or OLD_DATA
50+
DataStartTransmissionBlackWhite = 0x10,
51+
52+
/// To stop data transmission, this command must be issued to check the `data_flag`.
53+
///
54+
/// After this command, BUSY signal will become "0" until the display update is
55+
/// finished.
56+
DataStop = 0x11,
57+
58+
/// After this command is issued, driver will refresh display (data/VCOM) according to
59+
/// SRAM data and LUT.
60+
///
61+
/// After Display Refresh command, BUSY signal will become "0" until the display
62+
/// update is finished.
63+
DisplayRefresh = 0x12,
64+
65+
/// RED or NEW_DATA
66+
DataStartTransmissionChromatic = 0x13,
67+
68+
/// Dual SPI - what for?
69+
DualSpi = 0x15,
70+
71+
/// This command builds the VCOM Look-Up Table (LUTC).
72+
LutForVcom = 0x20,
73+
/// This command builds the White to White Look-Up Table (LUTWW).
74+
LutWhiteToWhite = 0x21,
75+
/// This command builds the Black to White Look-Up Table (LUTKW).
76+
LutBlackToWhite = 0x22,
77+
/// This command builds the White to Black Look-Up Table (LUTWK).
78+
LutWhiteToBlack = 0x23,
79+
/// This command builds the Black to Black Look-Up Table (LUTKK).
80+
LutBlackToBlack = 0x24,
81+
/// This command builds the Border? Look-Up Table (LUTR0).
82+
LutBorder = 0x25,
83+
84+
/// This command builds the XON Look-Up Table (LUTXON).
85+
LutXon = 0x2A,
86+
87+
/// The command controls the PLL clock frequency.
88+
PllControl = 0x30,
89+
90+
/// This command reads the temperature sensed by the temperature sensor.
91+
TemperatureSensor = 0x40,
92+
93+
/// This command indicates the interval of Vcom and data output. When setting the
94+
/// vertical back porch, the total blanking will be kept (20 Hsync).
95+
VcomAndDataIntervalSetting = 0x50,
96+
97+
/// This command defines non-overlap period of Gate and Source.
98+
TconSetting = 0x60,
99+
/// This command defines alternative resolution and this setting is of higher priority
100+
/// than the RES\[1:0\] in R00H (PSR).
101+
TconResolution = 0x61,
102+
//TODO /// This command defines MCU host direct access external memory mode.
103+
GateSourceStart = 0x65,
104+
105+
/// The LUT_REV / Chip Revision is read from OTP address = 25001 and 25000.
106+
Revision = 0x70,
107+
108+
/// This command sets `VCOM_DC` value.
109+
VcmDcSetting = 0x82,
110+
111+
// TODO
112+
ProgramMode = 0xA0,
113+
//
114+
// TODO
115+
ActiveProgram = 0xA1,
116+
117+
// TODO
118+
ReadOtpData = 0xA2,
119+
// /// This is in all the Waveshare controllers for Epd7in5, but it's not documented
120+
// /// anywhere in the datasheet `¯\_(ツ)_/¯`
121+
// FlashMode = 0xE5,
122+
}
123+
124+
impl traits::Command for Command {
125+
/// Returns the address of the command
126+
fn address(self) -> u8 {
127+
self as u8
128+
}
129+
}
130+
131+
#[cfg(test)]
132+
mod tests {
133+
use super::*;
134+
use crate::traits::Command as CommandTrait;
135+
136+
#[test]
137+
fn command_addr() {
138+
assert_eq!(Command::PanelSetting.address(), 0x00);
139+
assert_eq!(Command::DisplayRefresh.address(), 0x12);
140+
}
141+
}

src/epd2in13bc_v3/graphics.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use crate::epd2in13bc_v3::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH, NUM_DISPLAY_BITS};
2+
use crate::graphics::{DisplayColorRendering, DisplayRotation, TriDisplay};
3+
use crate::color::TriColor;
4+
use embedded_graphics_core::prelude::*;
5+
6+
/// Full size buffer for use with the 7in5 EPD
7+
///
8+
/// Can also be manually constructed:
9+
/// `buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); WIDTH / 8 * HEIGHT]`
10+
pub struct Display2in13bc {
11+
buffer: [u8; 2 * NUM_DISPLAY_BITS as usize],
12+
rotation: DisplayRotation,
13+
}
14+
15+
impl Default for Display2in13bc {
16+
fn default() -> Self {
17+
Display2in13bc {
18+
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
19+
2 * NUM_DISPLAY_BITS as usize],
20+
rotation: DisplayRotation::default(),
21+
}
22+
}
23+
}
24+
25+
impl DrawTarget for Display2in13bc {
26+
type Color = TriColor;
27+
type Error = core::convert::Infallible;
28+
29+
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
30+
where
31+
I: IntoIterator<Item = Pixel<Self::Color>>,
32+
{
33+
for pixel in pixels {
34+
self.draw_helper_tri(WIDTH, HEIGHT, pixel, DisplayColorRendering::Positive)?;
35+
}
36+
Ok(())
37+
}
38+
}
39+
40+
impl OriginDimensions for Display2in13bc {
41+
fn size(&self) -> Size {
42+
Size::new(WIDTH, HEIGHT)
43+
}
44+
}
45+
46+
impl TriDisplay for Display2in13bc {
47+
fn buffer(&self) -> &[u8] {
48+
&self.buffer
49+
}
50+
51+
fn get_mut_buffer(&mut self) -> &mut [u8] {
52+
&mut self.buffer
53+
}
54+
55+
fn set_rotation(&mut self, rotation: DisplayRotation) {
56+
self.rotation = rotation;
57+
}
58+
59+
fn rotation(&self) -> DisplayRotation {
60+
self.rotation
61+
}
62+
63+
fn chromatic_offset(&self) -> usize {
64+
NUM_DISPLAY_BITS as usize
65+
}
66+
67+
fn bw_buffer(&self) -> &[u8] {
68+
&self.buffer[0..self.chromatic_offset()]
69+
}
70+
71+
fn chromatic_buffer(&self) -> &[u8] {
72+
&self.buffer[self.chromatic_offset()..]
73+
}
74+
}

0 commit comments

Comments
 (0)