Skip to content

Commit

Permalink
move epd code to separate crate
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanBall committed Nov 2, 2023
1 parent a2f86b2 commit b682b00
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 119 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[workspace]
members = [
"esp32c3_nostd",
"airquamon_domain",
"display_themes",
"airquamon_domain"
"esp32c3_nostd",
"epd_display"
]

resolver = "2"
Expand Down
14 changes: 14 additions & 0 deletions epd_display/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "epd_display"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
airquamon_domain = { path = "../airquamon_domain" }
display_themes = { path = "../display_themes" }
embedded-graphics = "0.8.1"
embedded-hal = "1.0.0-rc.1"
epd-waveshare = { path = "/home/brendan/dev/projects/epd-waveshare" }
log = { version = "0.4.18" }
114 changes: 114 additions & 0 deletions epd_display/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#![no_std]

use epd_waveshare::{prelude::*, graphics};
use embedded_hal::spi::SpiDevice;
use embedded_hal::delay::DelayUs;
use embedded_graphics::prelude::*;
use core::fmt;
use airquamon_domain::Data;
use display_themes::Theme;
use log::info;

pub trait ChromaticBuffer {
fn bw_buffer(&self) -> &[u8];
fn chromatic_buffer(&self) -> &[u8];
}

impl<
const WIDTH: u32,
const HEIGHT: u32,
const BWRBIT: bool,
const BYTECOUNT: usize,
> ChromaticBuffer for graphics::Display<WIDTH, HEIGHT, BWRBIT, BYTECOUNT, TriColor> {

fn bw_buffer(&self) -> &[u8] {
self.bw_buffer()
}

fn chromatic_buffer(&self) -> &[u8] {
self.chromatic_buffer()
}
}

pub struct Display<SPI, EPD, DRAWTARGET, DELAY, THEME>
where
SPI: SpiDevice,
EPD: WaveshareThreeColorDisplayV2<SPI, DELAY>,
DRAWTARGET: DrawTarget<Color = TriColor> + ChromaticBuffer,
DRAWTARGET::Error: fmt::Debug,
DELAY: DelayUs,
THEME: Theme<TriColor>

{
spi: SPI,
epd: EPD,
draw_target: DRAWTARGET,
delay: DELAY,
theme: THEME,
}

impl<SPI, EPD, DRAWTARGET, DELAY, THEME> Display<SPI, EPD, DRAWTARGET, DELAY, THEME>
where
SPI: SpiDevice,
EPD: WaveshareThreeColorDisplayV2<SPI, DELAY>,
DRAWTARGET: DrawTarget<Color = TriColor> + ChromaticBuffer,
DRAWTARGET::Error: fmt::Debug,
DELAY: DelayUs,
THEME: Theme<TriColor> {
pub fn new(spi: SPI, epd: EPD, draw_target: DRAWTARGET, delay: DELAY, theme: THEME) -> Self {
Self {
spi,
draw_target,
epd,
delay,
theme,
}
}
}

pub trait DisplayTheme {

type Error;

fn draw(&mut self, data: &Data) -> Result<(), Self::Error>;
}

impl<SPI, EPD, DRAWTARGET, DELAY, THEME> DisplayTheme for Display<SPI, EPD, DRAWTARGET, DELAY, THEME>
where
SPI: SpiDevice,
EPD: WaveshareThreeColorDisplayV2<SPI, DELAY>,
SPI: SpiDevice,
DRAWTARGET: DrawTarget<Color = TriColor> + ChromaticBuffer + OriginDimensions,
DRAWTARGET::Error: fmt::Debug,
DELAY: DelayUs,
THEME: Theme<TriColor>
{
type Error = SPI::Error;

fn draw(&mut self, data: &Data) -> Result<(), Self::Error> {
let _ = self.theme.draw(data, &mut self.draw_target);
draw_to_epd(&mut self.spi, &mut self.epd, &mut self.draw_target, &mut self.delay)?;
Ok(())
}
}

fn draw_to_epd<'a, SPI, EPD, BUFFER, DELAY>(spi: &mut SPI, epd: &mut EPD, buffer: &mut BUFFER, delay: &mut DELAY) -> Result<(), SPI::Error>
where
SPI: SpiDevice,
EPD: WaveshareThreeColorDisplayV2<SPI, DELAY>,
BUFFER: ChromaticBuffer,
DELAY: DelayUs {
info!("waking up display");
epd.wake_up(spi, delay)?;

epd.wait_until_idle(spi, delay)?;


info!("updating display frame");
epd.update_color_frame(spi, delay, buffer.bw_buffer(), buffer.chromatic_buffer())?;
epd.display_frame(spi, delay)?;

// Set the EPD to sleep
epd.sleep(spi, delay)?;
Ok(())
}
1 change: 1 addition & 0 deletions esp32c3_nostd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
airquamon_domain = { path = "../airquamon_domain" }
display_themes = { path = "../display_themes" }
epd_display = { path = "../epd_display" }
esp32c3-hal = { git = "https://github.com/esp-rs/esp-hal.git", rev = "33bfe80d958911f4d0b43adb89cca34b5dce1676", features = ["eh1"] }
esp-hal-common = { git = "https://github.com/esp-rs/esp-hal.git", rev = "33bfe80d958911f4d0b43adb89cca34b5dce1676", features = ["esp32c3", "eh1"] }
esp-backtrace = { version = "0.8.0", features = ["esp32c3", "panic-handler", "exception-handler", "print-uart"] }
Expand Down
129 changes: 12 additions & 117 deletions esp32c3_nostd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ use esp32c3_hal::{
prelude::*,
Delay,
};
use embedded_hal::spi::SpiDevice;
use embedded_hal::delay::DelayUs;
use scd4x::scd4x::Scd4x;
use embedded_graphics::prelude::*;
use epd_waveshare::{epd2in9b_v3::*, prelude::*, color::{TriColor, ColorType}, graphics};
use epd_waveshare::{epd2in9b_v3::{Display2in9b, Epd2in9b}, graphics::DisplayRotation};
use log::info;
use airquamon_domain::Data;
use display_themes::{Theme, Theme2};
use core::fmt;
use display_themes::Theme2;
use epd_display::{Display, DisplayTheme};

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -81,16 +79,16 @@ fn main() -> ! {
None
).expect("failing setting up epd");

let mut display = Display2in9b::default();
display.set_rotation(DisplayRotation::Rotate270);
let mut draw_target = Display2in9b::default();
draw_target.set_rotation(DisplayRotation::Rotate270);

let mut display = Display {
spi: spi,
draw_target: display,
epd: epd,
delay: delay,
theme: Theme2::new(),
};
let mut display = Display::new(
spi,
epd,
draw_target,
delay,
Theme2::new()
);

sensor.wake_up();
// sensor.set_automatic_self_calibration(true).expect("failed enabling sensor automatic self calibration");
Expand Down Expand Up @@ -146,106 +144,3 @@ fn main() -> ! {
DelayUs::delay_ms(&mut delay, 60000);
}
}

trait Buffer {
fn buffer(&self) -> &[u8];
}

trait ChromaticBuffer {
fn bw_buffer(&self) -> &[u8];
fn chromatic_buffer(&self) -> &[u8];
}

impl<
const WIDTH: u32,
const HEIGHT: u32,
const BWRBIT: bool,
const BYTECOUNT: usize,
COLOR: ColorType,
> Buffer for graphics::Display<WIDTH, HEIGHT, BWRBIT, BYTECOUNT, COLOR> {

fn buffer(&self) -> &[u8] {
self.buffer()
}

}

impl<
const WIDTH: u32,
const HEIGHT: u32,
const BWRBIT: bool,
const BYTECOUNT: usize,
> ChromaticBuffer for graphics::Display<WIDTH, HEIGHT, BWRBIT, BYTECOUNT, TriColor> {

fn bw_buffer(&self) -> &[u8] {
self.bw_buffer()
}

fn chromatic_buffer(&self) -> &[u8] {
self.chromatic_buffer()
}
}

struct Display<SPI, EPD, DRAWTARGET, DELAY, THEME>
where
SPI: SpiDevice,
EPD: WaveshareThreeColorDisplayV2<SPI, DELAY>,
DRAWTARGET: DrawTarget<Color = TriColor> + ChromaticBuffer,
DRAWTARGET::Error: fmt::Debug,
DELAY: DelayUs,
THEME: Theme<TriColor>

{
spi: SPI,
epd: EPD,
draw_target: DRAWTARGET,
delay: DELAY,
theme: THEME,
}

trait DisplayTheme {

type Error;

fn draw(&mut self, data: &Data) -> Result<(), Self::Error>;
}

impl<SPI, EPD, DRAWTARGET, DELAY, THEME> DisplayTheme for Display<SPI, EPD, DRAWTARGET, DELAY, THEME>
where
SPI: SpiDevice,
EPD: WaveshareThreeColorDisplayV2<SPI, DELAY>,
SPI: SpiDevice,
DRAWTARGET: DrawTarget<Color = TriColor> + ChromaticBuffer + OriginDimensions,
DRAWTARGET::Error: fmt::Debug,
DELAY: DelayUs,
THEME: Theme<TriColor>
{
type Error = SPI::Error;

fn draw(&mut self, data: &Data) -> Result<(), Self::Error> {
let _ = self.theme.draw(data, &mut self.draw_target);
draw_to_epd(&mut self.spi, &mut self.epd, &mut self.draw_target, &mut self.delay)?;
Ok(())
}
}

fn draw_to_epd<'a, SPI, EPD, DRAWTARGET, DELAY>(spi: &mut SPI, epd: &mut EPD, draw_target: &mut DRAWTARGET, delay: &mut DELAY) -> Result<(), SPI::Error>
where
SPI: SpiDevice,
EPD: WaveshareThreeColorDisplayV2<SPI, DELAY>,
DRAWTARGET: DrawTarget<Color = TriColor> + ChromaticBuffer,
DELAY: DelayUs {
info!("waking up display");
epd.wake_up(spi, delay)?;

epd.wait_until_idle(spi, delay)?;


info!("updating display frame");
epd.update_color_frame(spi, delay, draw_target.bw_buffer(), draw_target.chromatic_buffer())?;
epd.display_frame(spi, delay)?;

// Set the EPD to sleep
epd.sleep(spi, delay)?;
Ok(())
}

0 comments on commit b682b00

Please sign in to comment.