Browse Source

first steps to rustfmt althought it sometimes doesn#t exactly looks good

embedded-hal-1.0
Christoph Groß 7 years ago
parent
commit
cd98033204
  1. 3
      src/drawing/color.rs
  2. 31
      src/drawing/font.rs
  3. 87
      src/drawing/mod.rs
  4. 67
      src/epd1in54/mod.rs
  5. 71
      src/epd2in9/mod.rs
  6. 3
      src/epd4in2/command.rs
  7. 12
      src/epd4in2/mod.rs
  8. 41
      src/interface/connection_interface.rs
  9. 30
      src/interface/mod.rs
  10. 11
      src/lib.rs
  11. 6
      src/type_a/command.rs

3
src/drawing/color.rs

@ -1,7 +1,7 @@
/// Only for the B/W Displays atm /// Only for the B/W Displays atm
pub enum Color { pub enum Color {
Black, Black,
White White,
} }
impl Color { impl Color {
@ -21,7 +21,6 @@ impl Color {
} }
} }
/// Get the color encoding of a specific bit in a byte /// Get the color encoding of a specific bit in a byte
/// ///
/// input is the byte where one bit is gonna be selected /// input is the byte where one bit is gonna be selected

31
src/drawing/font.rs

@ -11,20 +11,33 @@ pub struct Font<'a> {
first_char: u8, first_char: u8,
last_char: u8, last_char: u8,
bitmap: &'a [u8], bitmap: &'a [u8],
widthmap: &'a [u8] widthmap: &'a [u8],
} }
impl<'a> Font<'a> { impl<'a> Font<'a> {
/// Panics if either Bitmap or Widthmap of the Font are to small for the amount and size of chars /// Panics if either Bitmap or Widthmap of the Font are to small for the amount and size of chars
pub fn new(width: u8, height: u8, first_char: u8, last_char: u8, bitmap: &'a [u8], widthmap: &'a [u8]) -> Font<'a> { pub fn new(
width: u8,
height: u8,
first_char: u8,
last_char: u8,
bitmap: &'a [u8],
widthmap: &'a [u8],
) -> Font<'a> {
//Assertion so it shouldn't be able to panic later //Assertion so it shouldn't be able to panic later
let length_of_char = width as usize / 8 * height as usize; let length_of_char = width as usize / 8 * height as usize;
let amount_of_chars = last_char as usize - first_char as usize + 1; let amount_of_chars = last_char as usize - first_char as usize + 1;
assert!(bitmap.len() >= amount_of_chars * length_of_char); assert!(bitmap.len() >= amount_of_chars * length_of_char);
assert!(widthmap.len() >= amount_of_chars); assert!(widthmap.len() >= amount_of_chars);
Font {width, height, first_char, last_char, bitmap, widthmap } Font {
width,
height,
first_char,
last_char,
bitmap,
widthmap,
}
} }
fn get_length_of_char(&self) -> usize { fn get_length_of_char(&self) -> usize {
@ -49,7 +62,6 @@ impl<'a> Font<'a> {
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -64,7 +76,8 @@ mod tests {
0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, // '!' 0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, // '!'
0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, // '"' 0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, // '"'
0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00, // '#' 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00, // '#'
0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00]; // '$' 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00, // '$'
];
let widthmap = [8, 8, 8, 8]; let widthmap = [8, 8, 8, 8];
@ -80,9 +93,6 @@ mod tests {
assert_eq!(font.get_char_width('$'), widthmap[3]); assert_eq!(font.get_char_width('$'), widthmap[3]);
} }
#[test] #[test]
fn bitmap_8x8_test() { fn bitmap_8x8_test() {
let and = [0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x00, 0x00]; let and = [0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x00, 0x00];
@ -102,9 +112,6 @@ mod tests {
} }
} }
//bad font as the order is not the one we want to use //bad font as the order is not the one we want to use
//goes from bottom left -> up -> right //goes from bottom left -> up -> right
pub(crate) fn bitmap_8x8(input: char) -> [u8; 8] { pub(crate) fn bitmap_8x8(input: char) -> [u8; 8] {

87
src/drawing/mod.rs

@ -1,11 +1,9 @@
pub mod font; pub mod font;
use self::font::Font; use self::font::Font;
pub mod color; pub mod color;
use self::color::Color; use self::color::Color;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum Displayorientation { pub enum Displayorientation {
/// No rotation /// No rotation
@ -42,20 +40,17 @@ impl Display {
/// - Neccessary Buffersize /// - Neccessary Buffersize
pub fn get_dimensions(&self) -> (u16, u16, u16) { pub fn get_dimensions(&self) -> (u16, u16, u16) {
match self { match self {
Display::Eink42BlackWhite => (400, 300, 15000) Display::Eink42BlackWhite => (400, 300, 15000),
} }
} }
} }
#[allow(dead_code)] #[allow(dead_code)]
pub struct Graphics<'a> { pub struct Graphics<'a> {
width: u16, width: u16,
height: u16, height: u16,
rotation: Displayorientation, rotation: Displayorientation,
buffer: &'a mut [u8] buffer: &'a mut [u8], //buffer: Box<u8>//[u8; 15000]
//buffer: Box<u8>//[u8; 15000],
} }
impl<'a> Graphics<'a> { impl<'a> Graphics<'a> {
@ -63,7 +58,12 @@ impl<'a> Graphics<'a> {
pub fn new(width: u16, height: u16, buffer: &'a mut [u8]) -> Graphics<'a> { pub fn new(width: u16, height: u16, buffer: &'a mut [u8]) -> Graphics<'a> {
let len = buffer.len(); let len = buffer.len();
assert!(width / 8 * height >= len as u16); assert!(width / 8 * height >= len as u16);
Graphics {width, height, rotation: Displayorientation::Rotate0, buffer} Graphics {
width,
height,
rotation: Displayorientation::Rotate0,
buffer,
}
} }
/// Clears/Fills the full buffer with `color` /// Clears/Fills the full buffer with `color`
@ -82,12 +82,14 @@ impl<'a> Graphics<'a> {
/// limited to i16::max images (buffer_size) at the moment /// limited to i16::max images (buffer_size) at the moment
pub fn draw_pixel(&mut self, x: u16, y: u16, color: &Color) { pub fn draw_pixel(&mut self, x: u16, y: u16, color: &Color) {
let (idx, bit) = match self.rotation { let (idx, bit) = match self.rotation {
Displayorientation::Rotate0 | Displayorientation::Rotate180 Displayorientation::Rotate0 | Displayorientation::Rotate180 => (
=> ((x as usize / 8 + (self.width as usize / 8) * y as usize) , (x as usize / 8 + (self.width as usize / 8) * y as usize),
0x80 >> (x % 8)), 0x80 >> (x % 8),
Displayorientation::Rotate90 | Displayorientation::Rotate270 ),
=> (y as usize / 8 * self.width as usize + x as usize, Displayorientation::Rotate90 | Displayorientation::Rotate270 => (
0x80 >> (y % 8)), y as usize / 8 * self.width as usize + x as usize,
0x80 >> (y % 8),
),
}; };
if idx >= self.buffer.len() { if idx >= self.buffer.len() {
@ -97,7 +99,7 @@ impl<'a> Graphics<'a> {
match color { match color {
Color::Black => { Color::Black => {
self.buffer[idx] &= !bit; self.buffer[idx] &= !bit;
}, }
Color::White => { Color::White => {
self.buffer[idx] |= bit; self.buffer[idx] |= bit;
} }
@ -110,10 +112,12 @@ impl<'a> Graphics<'a> {
#[allow(dead_code)] #[allow(dead_code)]
fn draw_byte(&mut self, x: u16, y: u16, filling: u8, color: &Color) { fn draw_byte(&mut self, x: u16, y: u16, filling: u8, color: &Color) {
let idx = match self.rotation { let idx = match self.rotation {
Displayorientation::Rotate0 | Displayorientation::Rotate180 Displayorientation::Rotate0 | Displayorientation::Rotate180 => {
=> x as usize / 8 + (self.width as usize / 8) * y as usize, x as usize / 8 + (self.width as usize / 8) * y as usize
Displayorientation::Rotate90 | Displayorientation::Rotate270 },
=> y as usize / 8 + (self.width as usize / 8) * x as usize, Displayorientation::Rotate90 | Displayorientation::Rotate270 => {
y as usize / 8 + (self.width as usize / 8) * x as usize
},
}; };
if idx >= self.buffer.len() { if idx >= self.buffer.len() {
@ -145,7 +149,6 @@ impl<'a> Graphics<'a> {
} }
} }
//TODO: add support for font_height = 0 //TODO: add support for font_height = 0
//TODO: add support for char offset in y direction to reduce font file size //TODO: add support for char offset in y direction to reduce font file size
fn draw_char_helper(&mut self, x0: u16, y0: u16, input: char, font: &Font, color: &Color) { fn draw_char_helper(&mut self, x0: u16, y0: u16, input: char, font: &Font, color: &Color) {
@ -156,16 +159,15 @@ impl<'a> Graphics<'a> {
let buff = font.get_char(input); let buff = font.get_char(input);
let char_width = font.get_char_width(input); let char_width = font.get_char_width(input);
let mut row_counter = 0; let mut row_counter = 0;
let mut width_counter = 0u8; let mut width_counter = 0u8;
for &elem in buff.iter() { for &elem in buff.iter() {
for _ in 0..8 { for _ in 0..8 {
self.draw_pixel( self.draw_pixel(
x0 + u16::from(width_counter), x0 + u16::from(width_counter),
y0 + row_counter, y0 + row_counter,
&Color::get_color(elem, width_counter % 8, color)); &Color::get_color(elem, width_counter % 8, color),
);
//Widthcounter shows how far we are in x direction //Widthcounter shows how far we are in x direction
width_counter += 1; width_counter += 1;
@ -185,7 +187,11 @@ impl<'a> Graphics<'a> {
// includes special draw_char instructions as this one is ordered columnwise and not rowwise (first byte == first 8 pixel columnwise) // includes special draw_char instructions as this one is ordered columnwise and not rowwise (first byte == first 8 pixel columnwise)
for &elem in (&font::bitmap_8x8(input)).iter() { for &elem in (&font::bitmap_8x8(input)).iter() {
for i in 0..8u8 { for i in 0..8u8 {
self.draw_pixel(x0 + counter, y0 + 7 - u16::from(i), &Color::convert_color(elem, i, color)) self.draw_pixel(
x0 + counter,
y0 + 7 - u16::from(i),
&Color::convert_color(elem, i, color),
)
} }
counter += 1; counter += 1;
} }
@ -198,7 +204,12 @@ impl<'a> Graphics<'a> {
/// no autobreak line yet /// no autobreak line yet
pub fn draw_string_8x8(&mut self, x0: u16, y0: u16, input: &str, color: &Color) { pub fn draw_string_8x8(&mut self, x0: u16, y0: u16, input: &str, color: &Color) {
for (counter, input_char) in input.chars().enumerate() { for (counter, input_char) in input.chars().enumerate() {
self.draw_char_8x8(x0 + counter as u16*8, y0, input_char, color); self.draw_char_8x8(
x0 + counter as u16 * 8,
y0,
input_char,
color,
);
} }
} }
@ -297,7 +308,6 @@ impl<'a> Graphics<'a> {
} }
} }
fn draw_circle_helper(&mut self, x0: u16, y0: u16, radius: u16, filled: bool, color: &Color) { fn draw_circle_helper(&mut self, x0: u16, y0: u16, radius: u16, filled: bool, color: &Color) {
let mut x = radius - 1; let mut x = radius - 1;
let mut y = 0; let mut y = 0;
@ -324,7 +334,6 @@ fn draw_circle_helper(&mut self, x0: u16, y0: u16, radius: u16, filled: bool, co
err += dx - 2 * radius as i16; err += dx - 2 * radius as i16;
} }
} }
} }
fn circle_helper_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, color: &Color) { fn circle_helper_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, color: &Color) {
@ -343,7 +352,6 @@ fn circle_helper_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, color: &
self.draw_horizontal_line(x0 - y, y0 - y, 2 * y, color); self.draw_horizontal_line(x0 - y, y0 - y, 2 * y, color);
// self.draw_pixel(buffer, x0 - y, y0 - x, color); // self.draw_pixel(buffer, x0 - y, y0 - x, color);
// self.draw_pixel(buffer, x0 + y, y0 - x, color); // self.draw_pixel(buffer, x0 + y, y0 - x, color);
} }
//TODO: Test //TODO: Test
@ -358,9 +366,6 @@ fn circle_helper_filled_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, c
self.draw_pixel(x0 + x, y0 - y, color); self.draw_pixel(x0 + x, y0 - y, color);
} }
///TODO: test if circle looks good ///TODO: test if circle looks good
/// Draws a circle /// Draws a circle
pub fn draw_circle(&mut self, x0: u16, y0: u16, radius: u16, color: &Color) { pub fn draw_circle(&mut self, x0: u16, y0: u16, radius: u16, color: &Color) {
@ -401,16 +406,12 @@ fn circle_helper_filled_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, c
} }
} }
///TODO: test! ///TODO: test!
pub fn draw_filled_circle(&mut self, x0: u16, y0: u16, radius: u16, color: &Color) { pub fn draw_filled_circle(&mut self, x0: u16, y0: u16, radius: u16, color: &Color) {
self.draw_circle_helper(x0, y0, radius, true, color); self.draw_circle_helper(x0, y0, radius, true, color);
} }
} }
/* /*
############ ############ ############ ############ ############ ############ ############ ############
@ -424,8 +425,6 @@ fn circle_helper_filled_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, c
*/ */
#[cfg(test)] #[cfg(test)]
mod graphics { mod graphics {
use super::*; use super::*;
@ -439,11 +438,8 @@ mod graphics {
assert_eq!(graphics.buffer[0], Color::Black.get_byte_value()); assert_eq!(graphics.buffer[0], Color::Black.get_byte_value());
for &elem in graphics.buffer.iter() { for &elem in graphics.buffer.iter() {
assert_eq!(elem, Color::Black.get_byte_value()); assert_eq!(elem, Color::Black.get_byte_value());
} }
} }
/// draw a 4x4 in the top left corner /// draw a 4x4 in the top left corner
@ -465,8 +461,6 @@ mod graphics {
assert_eq!(elem, Color::White.get_byte_value()); assert_eq!(elem, Color::White.get_byte_value());
} }
} }
} }
#[test] #[test]
@ -489,11 +483,9 @@ mod graphics {
graphics.draw_vertical_line(5, 0, 8, &Color::Black); graphics.draw_vertical_line(5, 0, 8, &Color::Black);
assert_eq!(graphics.buffer[0], 0x7b); assert_eq!(graphics.buffer[0], 0x7b);
for &elem in graphics.buffer.iter() { for &elem in graphics.buffer.iter() {
assert_eq!(elem, 0x7bu8); assert_eq!(elem, 0x7bu8);
} }
} }
@ -545,8 +537,6 @@ mod graphics {
} }
} }
#[test] #[test]
fn test_pixel() { fn test_pixel() {
let mut buffer = [Color::White.get_byte_value(); 8]; let mut buffer = [Color::White.get_byte_value(); 8];
@ -555,7 +545,6 @@ mod graphics {
assert_eq!(graphics.buffer[0], !0x40); assert_eq!(graphics.buffer[0], !0x40);
let mut buffer = [Color::White.get_byte_value(); 16]; let mut buffer = [Color::White.get_byte_value(); 16];
let mut graphics = Graphics::new(16, 8, &mut buffer); let mut graphics = Graphics::new(16, 8, &mut buffer);
graphics.draw_pixel(9, 0, &Color::Black); graphics.draw_pixel(9, 0, &Color::Black);
@ -581,7 +570,6 @@ mod graphics {
#[test] #[test]
fn test_char_with_8x8_font() { fn test_char_with_8x8_font() {
// Test ! // Test !
let mut buffer = [Color::White.get_byte_value(); 8]; let mut buffer = [Color::White.get_byte_value(); 8];
let mut graphics = Graphics::new(8, 8, &mut buffer); let mut graphics = Graphics::new(8, 8, &mut buffer);
@ -594,7 +582,6 @@ mod graphics {
assert_eq!(graphics.buffer[6], !0x20); assert_eq!(graphics.buffer[6], !0x20);
assert_eq!(graphics.buffer[7], Color::White.get_byte_value()); assert_eq!(graphics.buffer[7], Color::White.get_byte_value());
// Test H // Test H
let mut buffer = [Color::White.get_byte_value(); 8]; let mut buffer = [Color::White.get_byte_value(); 8];
let mut graphics = Graphics::new(8, 8, &mut buffer); let mut graphics = Graphics::new(8, 8, &mut buffer);
@ -612,7 +599,6 @@ mod graphics {
#[test] #[test]
fn test_string_with_8x8_font() { fn test_string_with_8x8_font() {
// Test !H // Test !H
let mut buffer = [Color::White.get_byte_value(); 16]; let mut buffer = [Color::White.get_byte_value(); 16];
let mut graphics = Graphics::new(16, 8, &mut buffer); let mut graphics = Graphics::new(16, 8, &mut buffer);
@ -625,7 +611,6 @@ mod graphics {
assert_eq!(graphics.buffer[6 * 2], !0x20); assert_eq!(graphics.buffer[6 * 2], !0x20);
assert_eq!(graphics.buffer[7 * 2], Color::White.get_byte_value()); assert_eq!(graphics.buffer[7 * 2], Color::White.get_byte_value());
for i in 0..3 { for i in 0..3 {
assert_eq!(graphics.buffer[i * 2 + 1], !0x88); assert_eq!(graphics.buffer[i * 2 + 1], !0x88);
} }

67
src/epd1in54/mod.rs

@ -25,31 +25,18 @@ const HEIGHT: u16 = 200;
const DEFAULT_BACKGROUND_COLOR: Color = Color::White; const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
use hal::{ use hal::{
blocking::{ blocking::{delay::*, spi::Write},
spi::Write, digital::*,
delay::*
},
digital::*
}; };
use type_a::{ use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE};
LUT_FULL_UPDATE,
LUT_PARTIAL_UPDATE,
command::Command
};
use drawing::color::Color; use drawing::color::Color;
use interface::*; use interface::*;
use interface::connection_interface::ConnectionInterface; use interface::connection_interface::ConnectionInterface;
/// EPD1in54 driver /// EPD1in54 driver
/// ///
pub struct EPD1in54<SPI, CS, BUSY, DataCommand, RST, Delay> { pub struct EPD1in54<SPI, CS, BUSY, DataCommand, RST, Delay> {
@ -68,11 +55,8 @@ where
BUSY: InputPin, BUSY: InputPin,
DataCommand: OutputPin, DataCommand: OutputPin,
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16> Delay: DelayUs<u16> + DelayMs<u16>,
{ {}
}
impl<SPI, CS, BUSY, DataCommand, RST, Delay, E> WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, E> impl<SPI, CS, BUSY, DataCommand, RST, Delay, E> WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, E>
for EPD1in54<SPI, CS, BUSY, DataCommand, RST, Delay> for EPD1in54<SPI, CS, BUSY, DataCommand, RST, Delay>
@ -84,7 +68,6 @@ where
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>, Delay: DelayUs<u16> + DelayMs<u16>,
{ {
fn get_width(&self) -> u16 { fn get_width(&self) -> u16 {
WIDTH WIDTH
} }
@ -93,23 +76,20 @@ where
HEIGHT HEIGHT
} }
fn new( fn new(
interface: ConnectionInterface<SPI, CS, BUSY, DataCommand, RST, Delay> interface: ConnectionInterface<SPI, CS, BUSY, DataCommand, RST, Delay>,
) -> Result<Self, E> { ) -> Result<Self, E> {
let mut epd = EPD1in54 {
let mut epd = EPD1in54 {interface, background_color: DEFAULT_BACKGROUND_COLOR}; interface,
background_color: DEFAULT_BACKGROUND_COLOR,
};
epd.init()?; epd.init()?;
Ok(epd) Ok(epd)
} }
fn init(&mut self) -> Result<(), E> { fn init(&mut self) -> Result<(), E> {
self.reset(); self.reset();
// 3 Databytes: // 3 Databytes:
@ -153,7 +133,6 @@ where
} }
fn sleep(&mut self) -> Result<(), E> { fn sleep(&mut self) -> Result<(), E> {
self.interface.send_command(Command::DEEP_SLEEP_MODE)?; self.interface.send_command(Command::DEEP_SLEEP_MODE)?;
// 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode // 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode
//TODO: is 0x00 needed here? //TODO: is 0x00 needed here?
@ -163,7 +142,6 @@ where
Ok(()) Ok(())
} }
fn reset(&mut self) { fn reset(&mut self) {
self.interface.reset() self.interface.reset()
} }
@ -172,8 +150,6 @@ where
self.interface.delay_ms(delay) self.interface.delay_ms(delay)
} }
fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> {
self.use_full_frame()?; self.use_full_frame()?;
@ -182,7 +158,14 @@ where
} }
//TODO: update description: last 3 bits will be ignored for width and x_pos //TODO: update description: last 3 bits will be ignored for width and x_pos
fn update_partial_frame(&mut self, buffer: &[u8], x: u16, y: u16, width: u16, height: u16) -> Result<(), E>{ fn update_partial_frame(
&mut self,
buffer: &[u8],
x: u16,
y: u16,
width: u16,
height: u16,
) -> Result<(), E> {
self.set_ram_area(x, y, x + width, y + height)?; self.set_ram_area(x, y, x + width, y + height)?;
self.set_ram_counter(x, y)?; self.set_ram_counter(x, y)?;
@ -190,7 +173,6 @@ where
self.interface.send_multiple_data(buffer) self.interface.send_multiple_data(buffer)
} }
fn display_frame(&mut self) -> Result<(), E> { fn display_frame(&mut self) -> Result<(), E> {
// enable clock signal, enable cp, display pattern -> 0xC4 (tested with the arduino version) // enable clock signal, enable cp, display pattern -> 0xC4 (tested with the arduino version)
//TODO: test control_1 or control_2 with default value 0xFF (from the datasheet) //TODO: test control_1 or control_2 with default value 0xFF (from the datasheet)
@ -203,13 +185,11 @@ where
self.interface.send_command(Command::TERMINATE_COMMANDS_AND_FRAME_WRITE) self.interface.send_command(Command::TERMINATE_COMMANDS_AND_FRAME_WRITE)
} }
fn update_and_display_frame(&mut self, buffer: &[u8]) -> Result<(), E> { fn update_and_display_frame(&mut self, buffer: &[u8]) -> Result<(), E> {
self.update_frame(buffer)?; self.update_frame(buffer)?;
self.display_frame() self.display_frame()
} }
fn clear_frame(&mut self) -> Result<(), E> { fn clear_frame(&mut self) -> Result<(), E> {
self.use_full_frame()?; self.use_full_frame()?;
@ -224,7 +204,6 @@ where
fn set_background_color(&mut self, background_color: Color) { fn set_background_color(&mut self, background_color: Color) {
self.background_color = background_color; self.background_color = background_color;
} }
} }
impl<SPI, CS, BUSY, DC, RST, D, E> EPD1in54<SPI, CS, BUSY, DC, RST, D> impl<SPI, CS, BUSY, DC, RST, D, E> EPD1in54<SPI, CS, BUSY, DC, RST, D>
@ -248,7 +227,13 @@ where
self.set_ram_counter(0, 0) self.set_ram_counter(0, 0)
} }
pub(crate) fn set_ram_area(&mut self, start_x: u16, start_y: u16, end_x: u16, end_y: u16) -> Result<(), E> { pub(crate) fn set_ram_area(
&mut self,
start_x: u16,
start_y: u16,
end_x: u16,
end_y: u16,
) -> Result<(), E> {
assert!(start_x < end_x); assert!(start_x < end_x);
assert!(start_y < end_y); assert!(start_y < end_y);
@ -296,11 +281,9 @@ where
// self.set_lut_helper(buffer) // self.set_lut_helper(buffer)
//} //}
fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> { fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> {
assert!(buffer.len() == 30); assert!(buffer.len() == 30);
self.interface.send_command(Command::WRITE_LUT_REGISTER)?; self.interface.send_command(Command::WRITE_LUT_REGISTER)?;
self.interface.send_multiple_data(buffer) self.interface.send_multiple_data(buffer)
} }
} }

71
src/epd2in9/mod.rs

@ -24,32 +24,18 @@ const HEIGHT: u16 = 296;
const DEFAULT_BACKGROUND_COLOR: Color = Color::White; const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
use hal::{ use hal::{
blocking::{ blocking::{delay::*, spi::Write},
spi::Write, digital::*,
delay::*
},
digital::*
}; };
use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE};
use type_a::{
LUT_FULL_UPDATE,
LUT_PARTIAL_UPDATE,
command::Command
};
use drawing::color::Color; use drawing::color::Color;
use interface::*; use interface::*;
use interface::connection_interface::ConnectionInterface; use interface::connection_interface::ConnectionInterface;
/// EPD2in9 driver /// EPD2in9 driver
/// ///
pub struct EPD2in9<SPI, CS, BUSY, DataCommand, RST, Delay> { pub struct EPD2in9<SPI, CS, BUSY, DataCommand, RST, Delay> {
@ -68,13 +54,11 @@ where
BUSY: InputPin, BUSY: InputPin,
DataCommand: OutputPin, DataCommand: OutputPin,
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16> Delay: DelayUs<u16> + DelayMs<u16>,
{ {}
}
impl<SPI, CS, BUSY, DataCommand, RST, Delay, E> WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, E> impl<SPI, CS, BUSY, DataCommand, RST, Delay, E>
WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, E>
for EPD2in9<SPI, CS, BUSY, DataCommand, RST, Delay> for EPD2in9<SPI, CS, BUSY, DataCommand, RST, Delay>
where where
SPI: Write<u8, Error = E>, SPI: Write<u8, Error = E>,
@ -84,7 +68,6 @@ where
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>, Delay: DelayUs<u16> + DelayMs<u16>,
{ {
fn get_width(&self) -> u16 { fn get_width(&self) -> u16 {
WIDTH WIDTH
} }
@ -93,26 +76,23 @@ where
HEIGHT HEIGHT
} }
fn new( fn new(
interface: ConnectionInterface<SPI, CS, BUSY, DataCommand, RST, Delay> interface: ConnectionInterface<SPI, CS, BUSY, DataCommand, RST, Delay>,
) -> Result<Self, E> { ) -> Result<Self, E> {
//let epd = EPD::new(WIDTH, HEIGHT); //let epd = EPD::new(WIDTH, HEIGHT);
//let background_color = Color::White; //let background_color = Color::White;
let mut epd = EPD2in9 {interface, background_color: DEFAULT_BACKGROUND_COLOR}; let mut epd = EPD2in9 {
interface,
background_color: DEFAULT_BACKGROUND_COLOR,
};
epd.init()?; epd.init()?;
Ok(epd) Ok(epd)
} }
fn init(&mut self) -> Result<(), E> { fn init(&mut self) -> Result<(), E> {
self.reset(); self.reset();
// 3 Databytes: // 3 Databytes:
@ -156,7 +136,6 @@ where
} }
fn sleep(&mut self) -> Result<(), E> { fn sleep(&mut self) -> Result<(), E> {
self.interface.send_command(Command::DEEP_SLEEP_MODE)?; self.interface.send_command(Command::DEEP_SLEEP_MODE)?;
// 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode // 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode
//TODO: is 0x00 needed here? //TODO: is 0x00 needed here?
@ -166,7 +145,6 @@ where
Ok(()) Ok(())
} }
fn reset(&mut self) { fn reset(&mut self) {
self.interface.reset() self.interface.reset()
} }
@ -175,8 +153,6 @@ where
self.interface.delay_ms(delay) self.interface.delay_ms(delay)
} }
fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> {
self.use_full_frame()?; self.use_full_frame()?;
@ -185,7 +161,14 @@ where
} }
//TODO: update description: last 3 bits will be ignored for width and x_pos //TODO: update description: last 3 bits will be ignored for width and x_pos
fn update_partial_frame(&mut self, buffer: &[u8], x: u16, y: u16, width: u16, height: u16) -> Result<(), E>{ fn update_partial_frame(
&mut self,
buffer: &[u8],
x: u16,
y: u16,
width: u16,
height: u16,
) -> Result<(), E> {
self.set_ram_area(x, y, x + width, y + height)?; self.set_ram_area(x, y, x + width, y + height)?;
self.set_ram_counter(x, y)?; self.set_ram_counter(x, y)?;
@ -193,7 +176,6 @@ where
self.interface.send_multiple_data(buffer) self.interface.send_multiple_data(buffer)
} }
fn display_frame(&mut self) -> Result<(), E> { fn display_frame(&mut self) -> Result<(), E> {
// enable clock signal, enable cp, display pattern -> 0xC4 (tested with the arduino version) // enable clock signal, enable cp, display pattern -> 0xC4 (tested with the arduino version)
//TODO: test control_1 or control_2 with default value 0xFF (from the datasheet) //TODO: test control_1 or control_2 with default value 0xFF (from the datasheet)
@ -206,13 +188,11 @@ where
self.interface.send_command(Command::TERMINATE_COMMANDS_AND_FRAME_WRITE) self.interface.send_command(Command::TERMINATE_COMMANDS_AND_FRAME_WRITE)
} }
fn update_and_display_frame(&mut self, buffer: &[u8]) -> Result<(), E> { fn update_and_display_frame(&mut self, buffer: &[u8]) -> Result<(), E> {
self.update_frame(buffer)?; self.update_frame(buffer)?;
self.display_frame() self.display_frame()
} }
fn clear_frame(&mut self) -> Result<(), E> { fn clear_frame(&mut self) -> Result<(), E> {
self.use_full_frame()?; self.use_full_frame()?;
@ -227,7 +207,6 @@ where
fn set_background_color(&mut self, background_color: Color) { fn set_background_color(&mut self, background_color: Color) {
self.background_color = background_color; self.background_color = background_color;
} }
} }
impl<SPI, CS, BUSY, DC, RST, D, E> EPD2in9<SPI, CS, BUSY, DC, RST, D> impl<SPI, CS, BUSY, DC, RST, D, E> EPD2in9<SPI, CS, BUSY, DC, RST, D>
@ -251,7 +230,13 @@ where
self.set_ram_counter(0, 0) self.set_ram_counter(0, 0)
} }
pub(crate) fn set_ram_area(&mut self, start_x: u16, start_y: u16, end_x: u16, end_y: u16) -> Result<(), E> { pub(crate) fn set_ram_area(
&mut self,
start_x: u16,
start_y: u16,
end_x: u16,
end_y: u16,
) -> Result<(), E> {
assert!(start_x < end_x); assert!(start_x < end_x);
assert!(start_y < end_y); assert!(start_y < end_y);
@ -299,11 +284,9 @@ where
// self.set_lut_helper(buffer) // self.set_lut_helper(buffer)
//} //}
fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> { fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> {
assert!(buffer.len() == 30); assert!(buffer.len() == 30);
self.interface.send_command(Command::WRITE_LUT_REGISTER)?; self.interface.send_command(Command::WRITE_LUT_REGISTER)?;
self.interface.send_multiple_data(buffer) self.interface.send_multiple_data(buffer)
} }
} }

3
src/epd4in2/command.rs

@ -144,8 +144,6 @@ pub(crate) enum Command {
POWER_SAVING = 0xE3, POWER_SAVING = 0xE3,
} }
impl interface::Command for Command { impl interface::Command for Command {
/// Returns the address of the command /// Returns the address of the command
fn address(self) -> u8 { fn address(self) -> u8 {
@ -153,7 +151,6 @@ impl interface::Command for Command {
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

12
src/epd4in2/mod.rs

@ -62,11 +62,9 @@ use drawing::color::Color;
pub mod command; pub mod command;
use self::command::Command; use self::command::Command;
/// EPD4in2 driver /// EPD4in2 driver
/// ///
pub struct EPD4in2<SPI, CS, BUSY, DC, RST, D> pub struct EPD4in2<SPI, CS, BUSY, DC, RST, D> {
{
/// Connection Interface /// Connection Interface
interface: ConnectionInterface<SPI, CS, BUSY, DC, RST, D>, interface: ConnectionInterface<SPI, CS, BUSY, DC, RST, D>,
/// Width /// Width
@ -77,9 +75,8 @@ pub struct EPD4in2<SPI, CS, BUSY, DC, RST, D>
color: Color, color: Color,
} }
impl<SPI, CS, BUSY, DataCommand, RST, Delay, SpiError>
WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, SpiError>
impl<SPI, CS, BUSY, DataCommand, RST, Delay, SpiError> WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, SpiError>
for EPD4in2<SPI, CS, BUSY, DataCommand, RST, Delay> for EPD4in2<SPI, CS, BUSY, DataCommand, RST, Delay>
where where
SPI: Write<u8, Error = SpiError>, SPI: Write<u8, Error = SpiError>,
@ -116,7 +113,6 @@ where
let width = WIDTH as u16; let width = WIDTH as u16;
let height = HEIGHT as u16; let height = HEIGHT as u16;
let color = Color::White; let color = Color::White;
let mut epd = EPD4in2 { let mut epd = EPD4in2 {
interface, interface,
@ -243,7 +239,6 @@ where
width: u16, width: u16,
height: u16, height: u16,
) -> Result<(), SpiError> { ) -> Result<(), SpiError> {
if buffer.len() as u16 != width / 8 * height { if buffer.len() as u16 != width / 8 * height {
//TODO: panic!! or sth like that //TODO: panic!! or sth like that
//return Err("Wrong buffersize"); //return Err("Wrong buffersize");
@ -284,7 +279,6 @@ where
self.display_frame() self.display_frame()
} }
fn display_frame(&mut self) -> Result<(), SpiError> { fn display_frame(&mut self) -> Result<(), SpiError> {
self.send_command(Command::DISPLAY_REFRESH)?; self.send_command(Command::DISPLAY_REFRESH)?;

41
src/interface/connection_interface.rs

@ -1,9 +1,6 @@
use hal::{ use hal::{
blocking::{ blocking::{delay::*, spi::Write},
spi::Write, digital::*,
delay::*
},
digital::*
}; };
use interface::Command; use interface::Command;
@ -25,8 +22,8 @@ pub struct ConnectionInterface<SPI, CS, BUSY, DC, RST, D> {
delay: D, delay: D,
} }
impl<SPI, CS, BUSY, DataCommand, RST, Delay, ErrorSpeziale>
impl<SPI, CS, BUSY, DataCommand, RST, Delay, ErrorSpeziale> ConnectionInterface<SPI, CS, BUSY, DataCommand, RST, Delay> ConnectionInterface<SPI, CS, BUSY, DataCommand, RST, Delay>
where where
SPI: Write<u8, Error = ErrorSpeziale>, SPI: Write<u8, Error = ErrorSpeziale>,
CS: OutputPin, CS: OutputPin,
@ -36,7 +33,14 @@ where
Delay: DelayUs<u16> + DelayMs<u16>, Delay: DelayUs<u16> + DelayMs<u16>,
{ {
pub fn new(spi: SPI, cs: CS, busy: BUSY, dc: DataCommand, rst: RST, delay: Delay) -> Self { pub fn new(spi: SPI, cs: CS, busy: BUSY, dc: DataCommand, rst: RST, delay: Delay) -> Self {
ConnectionInterface {spi, cs, busy, dc, rst, delay } ConnectionInterface {
spi,
cs,
busy,
dc,
rst,
delay,
}
} }
/// Basic function for sending [Commands](Command). /// Basic function for sending [Commands](Command).
@ -49,9 +53,7 @@ where
self.dc.set_low(); self.dc.set_low();
// Transfer the command over spi // Transfer the command over spi
self.with_cs(|epd| { self.with_cs(|epd| epd.spi.write(&[command.address()]))
epd.spi.write(&[command.address()])
})
} }
/// Basic function for sending a single u8 of data over spi /// Basic function for sending a single u8 of data over spi
@ -65,9 +67,7 @@ where
self.dc.set_high(); self.dc.set_high();
// Transfer data (u8) over spi // Transfer data (u8) over spi
self.with_cs(|epd| { self.with_cs(|epd| epd.spi.write(&[val]))
epd.spi.write(&[val])
})
} }
/// Basic function for sending a single u8 of data over spi /// Basic function for sending a single u8 of data over spi
@ -76,7 +76,11 @@ where
/// ///
/// Should rarely be needed! /// Should rarely be needed!
/// //TODO: make public? /// //TODO: make public?
pub(crate) fn send_data_x_times(&mut self, val: u8, repetitions: u16) -> Result<(), ErrorSpeziale> { pub(crate) fn send_data_x_times(
&mut self,
val: u8,
repetitions: u16,
) -> Result<(), ErrorSpeziale> {
// high for data // high for data
self.dc.set_high(); self.dc.set_high();
@ -100,9 +104,7 @@ where
self.dc.set_high(); self.dc.set_high();
// Transfer data (u8-array) over spi // Transfer data (u8-array) over spi
self.with_cs(|epd| { self.with_cs(|epd| epd.spi.write(data))
epd.spi.write(data)
})
} }
// spi write helper/abstraction function // spi write helper/abstraction function
@ -120,7 +122,6 @@ where
result result
} }
/// Waits until device isn't busy anymore (busy == HIGH) /// Waits until device isn't busy anymore (busy == HIGH)
/// ///
/// This is normally handled by the more complicated commands themselves, /// This is normally handled by the more complicated commands themselves,
@ -142,7 +143,6 @@ where
} }
} }
/// Abstraction of setting the delay for simpler calls /// Abstraction of setting the delay for simpler calls
/// ///
/// maximum delay ~65 seconds (u16:max in ms) /// maximum delay ~65 seconds (u16:max in ms)
@ -166,5 +166,4 @@ where
//TODO: same as 3 lines above //TODO: same as 3 lines above
self.delay_ms(200); self.delay_ms(200);
} }
} }

30
src/interface/mod.rs

@ -1,11 +1,8 @@
use core::marker::Sized;
use hal::{ use hal::{
blocking::{ blocking::{delay::*, spi::Write},
spi::Write, digital::*,
delay::*
},
digital::*
}; };
use core::marker::Sized;
use drawing::color::Color; use drawing::color::Color;
@ -13,16 +10,12 @@ use drawing::color::Color;
pub mod connection_interface; pub mod connection_interface;
use self::connection_interface::ConnectionInterface; use self::connection_interface::ConnectionInterface;
/// All commands need to have this trait which gives the address of the command /// All commands need to have this trait which gives the address of the command
/// which needs to be send via SPI with activated CommandsPin (Data/Command Pin in CommandMode) /// which needs to be send via SPI with activated CommandsPin (Data/Command Pin in CommandMode)
pub(crate) trait Command { pub(crate) trait Command {
fn address(self) -> u8; fn address(self) -> u8;
} }
//TODO: add LUT trait with set_fast_lut and set_manual_lut and set_normal_lut or sth like that? //TODO: add LUT trait with set_fast_lut and set_manual_lut and set_normal_lut or sth like that?
// for partial updates // for partial updates
trait LUTSupport<Error> { trait LUTSupport<Error> {
@ -31,7 +24,6 @@ trait LUTSupport<Error> {
fn set_lut_manual(&mut self, data: &[u8]) -> Result<(), Error>; fn set_lut_manual(&mut self, data: &[u8]) -> Result<(), Error>;
} }
pub trait WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, Error> pub trait WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, Error>
where where
SPI: Write<u8>, SPI: Write<u8>,
@ -51,9 +43,10 @@ pub trait WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, Error>
/// ///
/// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards /// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards
fn new( fn new(
interface: ConnectionInterface<SPI, CS, BUSY, DataCommand, RST, Delay> interface: ConnectionInterface<SPI, CS, BUSY, DataCommand, RST, Delay>,
) -> Result<Self, Error> ) -> Result<Self, Error>
where Self: Sized; where
Self: Sized;
/// This initialises the EPD and powers it up /// This initialises the EPD and powers it up
/// ///
@ -64,7 +57,6 @@ pub trait WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, Error>
/// after setting it to sleep. /// after setting it to sleep.
fn init(&mut self) -> Result<(), Error>; fn init(&mut self) -> Result<(), Error>;
// void DisplayFrame(const unsigned char* frame_buffer); // void DisplayFrame(const unsigned char* frame_buffer);
/// Transmit a full frame to the SRAM of the DPD /// Transmit a full frame to the SRAM of the DPD
/// ///
@ -78,7 +70,14 @@ pub trait WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, Error>
/// Normally it should be dtm2, so use false /// Normally it should be dtm2, so use false
/// ///
/// BUFFER needs to be of size: w / 8 * l ! /// BUFFER needs to be of size: w / 8 * l !
fn update_partial_frame(&mut self, buffer: &[u8], x: u16, y: u16, width: u16, height: u16) -> Result<(), Error>; fn update_partial_frame(
&mut self,
buffer: &[u8],
x: u16,
y: u16,
width: u16,
height: u16,
) -> Result<(), Error>;
/// Displays the frame data from SRAM /// Displays the frame data from SRAM
fn display_frame(&mut self) -> Result<(), Error>; fn display_frame(&mut self) -> Result<(), Error>;
@ -94,7 +93,6 @@ pub trait WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, Error>
/// Sets the backgroundcolor for various commands like [clear_frame()](WaveshareInterface::clear_frame()) /// Sets the backgroundcolor for various commands like [clear_frame()](WaveshareInterface::clear_frame())
fn set_background_color(&mut self, color: Color); fn set_background_color(&mut self, color: Color);
/// Let the device enter deep-sleep mode to save power. /// Let the device enter deep-sleep mode to save power.
/// ///
/// The deep sleep mode returns to standby with a hardware reset. /// The deep sleep mode returns to standby with a hardware reset.

11
src/lib.rs

@ -43,19 +43,14 @@
//TODO: Make more assertions about buffersizes? //TODO: Make more assertions about buffersizes?
extern crate embedded_hal as hal; extern crate embedded_hal as hal;
use hal::{ use hal::spi::{Mode, Phase, Polarity};
spi::{Mode, Phase, Polarity},
};
pub mod drawing; pub mod drawing;
mod interface; mod interface;
pub use interface::{ pub use interface::{connection_interface::ConnectionInterface, WaveshareInterface};
WaveshareInterface,
connection_interface::ConnectionInterface};
#[cfg(feature = "epd4in2")] #[cfg(feature = "epd4in2")]
mod epd4in2; mod epd4in2;
@ -67,7 +62,6 @@ mod epd1in54;
#[cfg(feature = "epd1in54")] #[cfg(feature = "epd1in54")]
pub use epd1in54::EPD1in54; pub use epd1in54::EPD1in54;
#[cfg(feature = "epd2in9")] #[cfg(feature = "epd2in9")]
mod epd2in9; mod epd2in9;
///2in9 eink ///2in9 eink
@ -78,7 +72,6 @@ pub use epd2in9::EPD2in9;
#[cfg(any(feature = "epd1in54", feature = "epd2in9"))] #[cfg(any(feature = "epd1in54", feature = "epd2in9"))]
pub mod type_a; pub mod type_a;
//TODO: test spi mode //TODO: test spi mode
/// SPI mode - /// SPI mode -
/// For more infos see [Requirements: SPI](index.html#spi) /// For more infos see [Requirements: SPI](index.html#spi)

6
src/type_a/command.rs

@ -2,7 +2,6 @@
use interface; use interface;
/// EPD1in54 and EPD2IN9 commands /// EPD1in54 and EPD2IN9 commands
/// ///
/// Should rarely (never?) be needed directly. /// Should rarely (never?) be needed directly.
@ -69,11 +68,9 @@ pub(crate) enum Command {
SET_RAM_Y_ADDRESS_COUNTER = 0x4F, SET_RAM_Y_ADDRESS_COUNTER = 0x4F,
TERMINATE_COMMANDS_AND_FRAME_WRITE = 0xFF TERMINATE_COMMANDS_AND_FRAME_WRITE = 0xFF,
} }
impl interface::Command for Command { impl interface::Command for Command {
/// Returns the address of the command /// Returns the address of the command
fn address(self) -> u8 { fn address(self) -> u8 {
@ -81,7 +78,6 @@ impl interface::Command for Command {
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Command; use super::Command;

Loading…
Cancel
Save