Skip to content

Commit

Permalink
Add the heap allocation feature
Browse files Browse the repository at this point in the history
Some platforms (like ESP32) have a limited stack size, but a bigger heap
which can range 2-4MBytes for spiram. In such platforms it's impossible
to allocate most of the displays in the stack, but it's possible to
get them heap allocated (as long as we are compiling with std).

This commit adds the "heap" feature, disabled by default, which will
import std, and add the ability to create the display graphics in
heap instead of the stack.
  • Loading branch information
mangelajo committed Nov 22, 2022
1 parent b909be6 commit 44f812c
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ required-features = ["linux-dev"]
# Remove the linux-dev feature to build the tests on non unix systems
default = ["graphics", "linux-dev"]

# Allow allocating displays from heap instead of the stack
heap = []

graphics = ["embedded-graphics-core"]
linux-dev = []

Expand Down
14 changes: 14 additions & 0 deletions src/epd1in54/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use crate::graphics::{Display, DisplayRotation};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Full size buffer for use with the 1in54 EPD
///
/// Can also be manuall constructed:
Expand All @@ -24,6 +27,17 @@ impl Default for Display1in54 {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display1in54 {
fn new() -> Box<Self> {
Box::<Display1in54>::new(Display1in54 {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
WIDTH as usize * HEIGHT as usize / 8],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display1in54 {
type Color = BinaryColor;
type Error = core::convert::Infallible;
Expand Down
14 changes: 14 additions & 0 deletions src/epd1in54b/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::graphics::{Display, DisplayRotation};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Full size buffer for use with the 1in54 EPD
///
/// Can also be manually constructed and be used together with VarDisplay
Expand All @@ -21,6 +24,17 @@ impl Default for Display1in54b {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display1in54b {
fn new() -> Box<Self> {
Box::<Display1in54b>::new(Display1in54b {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
WIDTH as usize * HEIGHT as usize / 8],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display1in54b {
type Color = BinaryColor;
type Error = core::convert::Infallible;
Expand Down
13 changes: 13 additions & 0 deletions src/epd1in54c/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::graphics::{Display, DisplayRotation};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Full size buffer for use with the 1in54c EPD
///
/// Can also be manually constructed and be used together with VarDisplay
Expand All @@ -20,6 +23,16 @@ impl Default for Display1in54c {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display1in54c {
fn new() -> Box<Self> {
Box::<Display1in54c>::new(Display1in54c {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); NUM_DISPLAY_BITS as usize],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display1in54c {
type Color = BinaryColor;
type Error = core::convert::Infallible;
Expand Down
14 changes: 14 additions & 0 deletions src/epd2in13_v2/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use crate::graphics::{Display, DisplayRotation};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Full size buffer for use with the 2in13 v2 EPD
///
/// Can also be manually constructed:
Expand All @@ -23,6 +26,17 @@ impl Default for Display2in13 {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display2in13 {
fn new() -> Box<Self> {
Box::<Display2in13>::new(Display2in13 {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
buffer_len(WIDTH as usize, HEIGHT as usize)],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display2in13 {
type Color = BinaryColor;
type Error = core::convert::Infallible;
Expand Down
13 changes: 13 additions & 0 deletions src/epd2in13bc/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::epd2in13bc::{DEFAULT_BACKGROUND_COLOR, HEIGHT, NUM_DISPLAY_BITS, WIDT
use crate::graphics::{DisplayColorRendering, DisplayRotation, TriDisplay};
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Full size buffer for use with the 2.13" b/c EPD
///
/// Can also be manually constructed and be used together with VarDisplay
Expand All @@ -23,6 +26,16 @@ impl Default for Display2in13bc {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display2in13bc {
fn new() -> Box<Self> {
Box::<Display2in13bc>::new(Display2in13bc {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); 2 * NUM_DISPLAY_BITS as usize],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display2in13bc {
type Color = TriColor;
type Error = core::convert::Infallible;
Expand Down
14 changes: 14 additions & 0 deletions src/epd2in7b/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::graphics::{Display, DisplayRotation};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Full size buffer for use with the 2in7B EPD
///
/// Can also be manuall constructed:
Expand All @@ -22,6 +25,17 @@ impl Default for Display2in7b {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display2in7b {
fn new() -> Box<Self> {
Box::<Display2in7b>::new(Display2in7b {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
WIDTH as usize * HEIGHT as usize / 8],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display2in7b {
type Color = BinaryColor;
type Error = core::convert::Infallible;
Expand Down
14 changes: 14 additions & 0 deletions src/epd2in9/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::graphics::{Display, DisplayRotation};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Display with Fullsize buffer for use with the 2in9 EPD
///
/// Can also be manuall constructed:
Expand All @@ -22,6 +25,17 @@ impl Default for Display2in9 {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display2in9 {
fn new() -> Box<Self> {
Box::<Display2in9>::new(Display2in9 {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
WIDTH as usize * HEIGHT as usize / 8],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display2in9 {
type Color = BinaryColor;
type Error = core::convert::Infallible;
Expand Down
14 changes: 14 additions & 0 deletions src/epd2in9_v2/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::graphics::{Display, DisplayRotation};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Display with Fullsize buffer for use with the 2in9 EPD V2
///
/// Can also be manuall constructed:
Expand All @@ -22,6 +25,17 @@ impl Default for Display2in9 {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display2in9 {
fn new() -> Box<Self> {
Box::<Display2in9>::new(Display2in9 {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
WIDTH as usize * HEIGHT as usize / 8],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display2in9 {
type Color = BinaryColor;
type Error = core::convert::Infallible;
Expand Down
13 changes: 13 additions & 0 deletions src/epd2in9bc/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::graphics::{Display, DisplayRotation};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Full size buffer for use with the 2in9b/c EPD
///
/// Can also be manually constructed and be used together with VarDisplay
Expand All @@ -20,6 +23,16 @@ impl Default for Display2in9bc {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display2in9bc {
fn new() -> Box<Self> {
Box::<Display2in9bc>::new(Display2in9bc {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); NUM_DISPLAY_BITS as usize],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display2in9bc {
type Color = BinaryColor;
type Error = core::convert::Infallible;
Expand Down
14 changes: 14 additions & 0 deletions src/epd4in2/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::graphics::{Display, DisplayRotation};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Full size buffer for use with the 4in2 EPD
///
/// Can also be manuall constructed:
Expand All @@ -22,6 +25,17 @@ impl Default for Display4in2 {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display4in2 {
fn new() -> Box<Self> {
Box::<Display4in2>::new(Display4in2 {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
WIDTH as usize * HEIGHT as usize / 8],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display4in2 {
type Color = BinaryColor;
type Error = core::convert::Infallible;
Expand Down
15 changes: 15 additions & 0 deletions src/epd5in65f/graphics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::color::OctColor;
use crate::epd5in65f::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
use crate::graphics::{DisplayRotation, OctDisplay};

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

use embedded_graphics_core::prelude::*;

/// Full size buffer for use with the 5in65f EPD
Expand All @@ -22,6 +26,17 @@ impl Default for Display5in65f {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display5in65f {
fn new() -> Box<Self> {
Box::<Display5in65f>::new(Display5in65f {
buffer: [OctColor::colors_byte(DEFAULT_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR);
WIDTH as usize * HEIGHT as usize / 2],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display5in65f {
type Color = OctColor;
type Error = core::convert::Infallible;
Expand Down
17 changes: 17 additions & 0 deletions src/epd5in83b_v2/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use crate::graphics::{DisplayColorRendering, DisplayRotation};
use crate::prelude::TriDisplay;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Full size buffer for use with the 5in83 EPD
///
/// Can also be manually constructed:
Expand All @@ -26,6 +29,20 @@ impl Default for Display5in83 {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display5in83 {
fn new() -> Box<Self> {
let mut display = Box::<Display5in83>::new(Display5in83 {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); 2 * NUM_DISPLAY_BITS as usize],
rotation: DisplayRotation::default(),
});
// We need to invert chromatic part to black so it will be render white
let offset = display.chromatic_offset();
display.buffer[offset..].fill(0x00);
display
}
}

impl DrawTarget for Display5in83 {
type Color = TriColor;
type Error = core::convert::Infallible;
Expand Down
14 changes: 14 additions & 0 deletions src/epd7in5/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::graphics::{Display, DisplayRotation};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::prelude::*;

#[cfg(feature = "heap")]
use {crate::graphics::HeapAllocated, std::boxed::Box};

/// Full size buffer for use with the 7in5 EPD
///
/// Can also be manually constructed:
Expand All @@ -22,6 +25,17 @@ impl Default for Display7in5 {
}
}

#[cfg(feature = "heap")]
impl HeapAllocated for Display7in5 {
fn new() -> Box<Self> {
Box::<Display7in5>::new(Display7in5 {
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
WIDTH as usize * HEIGHT as usize / 8],
rotation: DisplayRotation::default(),
})
}
}

impl DrawTarget for Display7in5 {
type Color = BinaryColor;
type Error = core::convert::Infallible;
Expand Down
Loading

0 comments on commit 44f812c

Please sign in to comment.