Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vhdirk committed Jun 21, 2024
1 parent 1244f03 commit 4946380
Show file tree
Hide file tree
Showing 32 changed files with 1,547 additions and 597 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ embedded-graphics = "0.8"
embedded-hal-mock = { version = "=0.10.0", default-features = false, features = [
"eh1",
] }
anyhow = { version = "1.0", default-features = false}

[target.'cfg(unix)'.dev-dependencies]
linux-embedded-hal = "0.4.0"
Expand Down
33 changes: 17 additions & 16 deletions examples/epd1in54_no_graphics.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#![deny(warnings)]

use anyhow;
use embedded_hal::delay::DelayNs;
use epd_waveshare::{epd1in54::Epd1in54, prelude::*};
use linux_embedded_hal::{
spidev::{self, SpidevOptions},
sysfs_gpio::Direction,
Delay, SPIError, SpidevDevice, SysfsPin,
Delay, SpidevDevice, SysfsPin,
};

// activate spi, gpio in raspi-config
// needs to be run with sudo because of some sysfs_gpio permission problems and follow-up timing problems
// see https://github.com/rust-embedded/rust-sysfs-gpio/issues/5 and follow-up issues

fn main() -> Result<(), SPIError> {
fn main() -> Result<(), anyhow::Error> {
// Configure SPI
// SPI settings are from eink-waveshare-rs documenation
let mut spi = SpidevDevice::open("/dev/spidev0.0")?;
let mut spi = SpidevDevice::open("/dev/spidev0.0").map_err(anyhow::Error::msg)?;
let options = SpidevOptions::new()
.bits_per_word(8)
.max_speed_hz(4_000_000)
Expand Down Expand Up @@ -58,14 +59,14 @@ fn main() -> Result<(), SPIError> {

// Setup of the needed pins is finished here
// Now the "real" usage of the eink-waveshare-rs crate begins
let mut epd = Epd1in54::new(&mut spi, busy, dc, rst, &mut delay, Some(5))?;
let mut epd = Epd1in54::new(&mut spi, busy, dc, rst, &mut delay, Some(5)).map_err(anyhow::Error::msg)?;

// Clear the full screen
epd.clear_frame(&mut spi, &mut delay)?;
epd.display_frame(&mut spi, &mut delay)?;
epd.clear_frame(&mut spi, &mut delay).map_err(anyhow::Error::msg)?;
epd.display_frame(&mut spi, &mut delay).map_err(anyhow::Error::msg)?;

// Speeddemo
epd.set_lut(&mut spi, &mut delay, Some(RefreshLut::Quick))?;
epd.set_lut(&mut spi, &mut delay, Some(RefreshLut::Quick)).map_err(anyhow::Error::msg)?;
let small_buffer = [Color::Black.get_byte_value(); 32]; //16x16
let number_of_runs = 1;
for i in 0..number_of_runs {
Expand All @@ -78,30 +79,30 @@ fn main() -> Result<(), SPIError> {
25 + offset,
16,
16,
)?;
epd.display_frame(&mut spi, &mut delay)?;
).map_err(anyhow::Error::msg)?;
epd.display_frame(&mut spi, &mut delay).map_err(anyhow::Error::msg)?;
}

// Clear the full screen
epd.clear_frame(&mut spi, &mut delay)?;
epd.display_frame(&mut spi, &mut delay)?;
epd.clear_frame(&mut spi, &mut delay).map_err(anyhow::Error::msg)?;
epd.display_frame(&mut spi, &mut delay).map_err(anyhow::Error::msg)?;

// Draw some squares
let small_buffer = [Color::Black.get_byte_value(); 3200]; //160x160
epd.update_partial_frame(&mut spi, &mut delay, &small_buffer, 20, 20, 160, 160)?;
epd.update_partial_frame(&mut spi, &mut delay, &small_buffer, 20, 20, 160, 160).map_err(anyhow::Error::msg)?;

let small_buffer = [Color::White.get_byte_value(); 800]; //80x80
epd.update_partial_frame(&mut spi, &mut delay, &small_buffer, 60, 60, 80, 80)?;
epd.update_partial_frame(&mut spi, &mut delay, &small_buffer, 60, 60, 80, 80).map_err(anyhow::Error::msg)?;

let small_buffer = [Color::Black.get_byte_value(); 8]; //8x8
epd.update_partial_frame(&mut spi, &mut delay, &small_buffer, 96, 96, 8, 8)?;
epd.update_partial_frame(&mut spi, &mut delay, &small_buffer, 96, 96, 8, 8).map_err(anyhow::Error::msg)?;

// Display updated frame
epd.display_frame(&mut spi, &mut delay)?;
epd.display_frame(&mut spi, &mut delay).map_err(anyhow::Error::msg)?;
delay.delay_ms(5000);

// Set the EPD to sleep
epd.sleep(&mut spi, &mut delay)?;
epd.sleep(&mut spi, &mut delay).map_err(anyhow::Error::msg)?;

Ok(())
}
9 changes: 5 additions & 4 deletions examples/epd2in13_v2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(warnings)]

use anyhow;
use embedded_graphics::{
mono_font::MonoTextStyleBuilder,
prelude::*,
Expand All @@ -16,15 +17,15 @@ use epd_waveshare::{
use linux_embedded_hal::{
spidev::{self, SpidevOptions},
sysfs_gpio::Direction,
Delay, SPIError, SpidevDevice, SysfsPin,
Delay, SpidevDevice, SysfsPin,
};

// The pins in this example are for the Universal e-Paper Raw Panel Driver HAT
// activate spi, gpio in raspi-config
// needs to be run with sudo because of some sysfs_gpio permission problems and follow-up timing problems
// see https://github.com/rust-embedded/rust-sysfs-gpio/issues/5 and follow-up issues

fn main() -> Result<(), SPIError> {
fn main() -> Result<(), anyhow::Error> {
// Configure SPI
// Settings are taken from
let mut spi = SpidevDevice::open("/dev/spidev0.0").expect("spidev directory");
Expand Down Expand Up @@ -80,7 +81,7 @@ fn main() -> Result<(), SPIError> {
display.set_rotation(DisplayRotation::Rotate270);
draw_text(&mut display, "Rotate 270!", 5, 50);

epd2in13.update_frame(&mut spi, display.buffer(), &mut delay)?;
epd2in13.update_frame(&mut spi, display.buffer(), &mut delay).map_err(anyhow::Error::msg)?;
epd2in13
.display_frame(&mut spi, &mut delay)
.expect("display frame new graphics");
Expand Down Expand Up @@ -156,7 +157,7 @@ fn main() -> Result<(), SPIError> {
}

println!("Finished tests - going to sleep");
epd2in13.sleep(&mut spi, &mut delay)
epd2in13.sleep(&mut spi, &mut delay).map_err(anyhow::Error::msg)
}

fn draw_text(display: &mut Display2in13, text: &str, x: i32, y: i32) {
Expand Down
13 changes: 7 additions & 6 deletions examples/epd2in13bc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(warnings)]

use anyhow;
use embedded_graphics::{
mono_font::MonoTextStyleBuilder,
prelude::*,
Expand All @@ -16,7 +17,7 @@ use epd_waveshare::{
use linux_embedded_hal::{
spidev::{self, SpidevOptions},
sysfs_gpio::Direction,
Delay, SPIError, SpidevDevice, SysfsPin,
Delay, SpidevDevice, SysfsPin,
};

// activate spi, gpio in raspi-config
Expand All @@ -34,7 +35,7 @@ use linux_embedded_hal::{
//
// after finishing, put the display to sleep

fn main() -> Result<(), SPIError> {
fn main() -> Result<(), anyhow::Error> {
let busy = SysfsPin::new(24); // GPIO 24, board J-18
busy.export().expect("busy export");
while !busy.is_exported() {}
Expand Down Expand Up @@ -142,7 +143,7 @@ fn main() -> Result<(), SPIError> {
&mut delay,
display.bw_buffer(),
display.chromatic_buffer(),
)?;
).map_err(anyhow::Error::msg)?;
epd2in13
.display_frame(&mut spi, &mut delay)
.expect("display frame new graphics");
Expand All @@ -157,11 +158,11 @@ fn main() -> Result<(), SPIError> {
&mut delay,
display.bw_buffer(),
display.chromatic_buffer(),
)?;
epd2in13.display_frame(&mut spi, &mut delay)?;
).map_err(anyhow::Error::msg)?;
epd2in13.display_frame(&mut spi, &mut delay).map_err(anyhow::Error::msg)?;

println!("Finished tests - going to sleep");
epd2in13.sleep(&mut spi, &mut delay)
epd2in13.sleep(&mut spi, &mut delay).map_err(anyhow::Error::msg)
}

fn draw_text(display: &mut Display2in13bc, text: &str, x: i32, y: i32) {
Expand Down
9 changes: 5 additions & 4 deletions examples/epd4in2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(warnings)]

use anyhow;
use embedded_graphics::{
mono_font::MonoTextStyleBuilder,
prelude::*,
Expand All @@ -16,14 +17,14 @@ use epd_waveshare::{
use linux_embedded_hal::{
spidev::{self, SpidevOptions},
sysfs_gpio::Direction,
Delay, SPIError, SpidevDevice, SysfsPin,
Delay, SpidevDevice, SysfsPin,
};

// activate spi, gpio in raspi-config
// needs to be run with sudo because of some sysfs_gpio permission problems and follow-up timing problems
// see https://github.com/rust-embedded/rust-sysfs-gpio/issues/5 and follow-up issues

fn main() -> Result<(), SPIError> {
fn main() -> Result<(), anyhow::Error> {
// Configure SPI
// Settings are taken from
let mut spi = SpidevDevice::open("/dev/spidev0.0").expect("spidev directory");
Expand Down Expand Up @@ -79,7 +80,7 @@ fn main() -> Result<(), SPIError> {
display.set_rotation(DisplayRotation::Rotate270);
draw_text(&mut display, "Rotate 270!", 5, 50);

epd4in2.update_frame(&mut spi, display.buffer(), &mut delay)?;
epd4in2.update_frame(&mut spi, display.buffer(), &mut delay).map_err(anyhow::Error::msg)?;
epd4in2
.display_frame(&mut spi, &mut delay)
.expect("display frame new graphics");
Expand Down Expand Up @@ -147,7 +148,7 @@ fn main() -> Result<(), SPIError> {
}

println!("Finished tests - going to sleep");
epd4in2.sleep(&mut spi, &mut delay)
epd4in2.sleep(&mut spi, &mut delay).map_err(anyhow::Error::msg)
}

fn draw_text(display: &mut Display4in2, text: &str, x: i32, y: i32) {
Expand Down
7 changes: 4 additions & 3 deletions examples/epd4in2_variable_size.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(warnings)]
#![deny(warnings)]

use anyhow;
use embedded_graphics::{
mono_font::MonoTextStyleBuilder,
prelude::*,
Expand All @@ -17,14 +18,14 @@ use epd_waveshare::{
use linux_embedded_hal::{
spidev::{self, SpidevOptions},
sysfs_gpio::Direction,
Delay, SPIError, SpidevDevice, SysfsPin,
Delay, SpidevDevice, SysfsPin,
};

// activate spi, gpio in raspi-config
// needs to be run with sudo because of some sysfs_gpio permission problems and follow-up timing problems
// see https://github.com/rust-embedded/rust-sysfs-gpio/issues/5 and follow-up issues

fn main() -> Result<(), SPIError> {
fn main() -> Result<(), anyhow::Error> {
// Configure SPI
// Settings are taken from
let mut spi = SpidevDevice::open("/dev/spidev0.0").expect("spidev directory");
Expand Down Expand Up @@ -150,7 +151,7 @@ fn main() -> Result<(), SPIError> {
}

println!("Finished tests - going to sleep");
epd4in2.sleep(&mut spi, &mut delay)
epd4in2.sleep(&mut spi, &mut delay).map_err(anyhow::Error::msg)
}

fn draw_text(display: &mut impl DrawTarget<Color = Color>, text: &str, x: i32, y: i32) {
Expand Down
Loading

0 comments on commit 4946380

Please sign in to comment.