|
| 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 | +} |
0 commit comments