Skip to content

embedded_hal 1.0 #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ version = "0.3.0"
edition = "2018"

[package.metadata.docs.rs]
targets = [ "thumbv7m-none-eabi" ]
targets = ["thumbv7m-none-eabi"]
all-features = true

[badges]
circle-ci = { repository = "jamwaffles/ssd1331", branch = "master" }

[dependencies]
embedded-hal = "0.2.3"
embedded-graphics-core = { version = "0.3.2", optional = true }
embedded-hal = "1.0.0"
embedded-graphics-core = { version = "0.4.0", optional = true }

[dev-dependencies]
cortex-m = "0.7.3"
cortex-m-rt = "0.6.11"
panic-semihosting = "0.5.3"
embedded-graphics = "0.7.1"
tinybmp = "0.3.1"
stm32f1xx-hal = { version = "0.7.0", features = [ "rt", "stm32f103" ] }
cortex-m = "0.7.7"
cortex-m-rt = "0.7.5"
panic-semihosting = "0.6.0"
embedded-graphics = "0.8.1"
tinybmp = "0.6.0"
stm32f1xx-hal = { version = "0.10.0", features = ["rt", "stm32f103"] }

[features]
default = ["graphics"]
Expand Down
17 changes: 8 additions & 9 deletions examples/bmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
#![no_std]
#![no_main]

use cortex_m_rt::{entry, exception, ExceptionFrame};
use cortex_m::delay::Delay;
use cortex_m_rt::{entry, exception};
use embedded_graphics::{geometry::Point, image::Image, pixelcolor::Rgb565, prelude::*};
use panic_semihosting as _;
use ssd1331::{DisplayRotation, Ssd1331};
use stm32f1xx_hal::{
delay::Delay,
prelude::*,
spi::{Mode, Phase, Polarity, Spi},
stm32,
Expand All @@ -44,32 +44,31 @@ fn main() -> ! {

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
let mut afio = dp.AFIO.constrain();

let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
let mut gpioa = dp.GPIOA.split();
let mut gpiob = dp.GPIOB.split();

// SPI1
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
let miso = gpioa.pa6;
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);

let mut delay = Delay::new(cp.SYST, clocks);
let mut delay = Delay::new(cp.SYST, clocks.hclk().to_Hz());

let mut rst = gpiob.pb0.into_push_pull_output(&mut gpiob.crl);
let dc = gpiob.pb1.into_push_pull_output(&mut gpiob.crl);

let spi = Spi::spi1(
let mut spi = Spi::spi1(
dp.SPI1,
(sck, miso, mosi),
&mut afio.mapr,
Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
},
8.mhz(),
8.MHz(),
clocks,
&mut rcc.apb2,
);

let mut display = Ssd1331::new(spi, dc, DisplayRotation::Rotate0);
Expand Down
9 changes: 4 additions & 5 deletions examples/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ fn main() -> ! {

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
let mut afio = dp.AFIO.constrain();

let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
let mut gpioa = dp.GPIOA.split();
let mut gpiob = dp.GPIOB.split();

// SPI1
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
Expand All @@ -71,7 +71,6 @@ fn main() -> ! {
},
8.mhz(),
clocks,
&mut rcc.apb2,
);

let mut display = Ssd1331::new(spi, dc, Rotate0);
Expand Down Expand Up @@ -105,6 +104,6 @@ fn main() -> ! {
}

#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}
9 changes: 4 additions & 5 deletions examples/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ fn main() -> ! {

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
let mut afio = dp.AFIO.constrain();

let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
let mut gpioa = dp.GPIOA.split();
let mut gpiob = dp.GPIOB.split();

// SPI1
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
Expand All @@ -71,7 +71,6 @@ fn main() -> ! {
},
8.mhz(),
clocks,
&mut rcc.apb2,
);

// Initialise the display with a default rotation of 90 degrees
Expand All @@ -98,6 +97,6 @@ fn main() -> ! {
}

#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}
15 changes: 7 additions & 8 deletions examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ fn main() -> ! {

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
let mut afio = dp.AFIO.constrain();

let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
let mut gpioa = dp.GPIOA.split();
let mut gpiob = dp.GPIOB.split();

// SPI1
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
Expand All @@ -65,20 +65,19 @@ fn main() -> ! {
let mut rst = gpiob.pb0.into_push_pull_output(&mut gpiob.crl);
let dc = gpiob.pb1.into_push_pull_output(&mut gpiob.crl);

let spi = Spi::spi1(
let spi = Spi::new(
dp.SPI1,
(sck, miso, mosi),
&mut afio.mapr,
Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
},
8.mhz(),
8.MHz(),
clocks,
&mut rcc.apb2,
);

let mut display = Ssd1331::new(spi, dc, Rotate0);
let mut display = Ssd1331::new(spi.into(), dc, Rotate0);

display.reset(&mut rst, &mut delay).unwrap();
display.init().unwrap();
Expand Down Expand Up @@ -115,6 +114,6 @@ fn main() -> ! {
}

#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}
5 changes: 3 additions & 2 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Shamefully taken from https://github.com/EdgewaterDevelopment/rust-ssd1331

use crate::error::Error;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use hal::spi::SpiBus;

/// SSD1331 Commands
#[derive(Debug)]
Expand Down Expand Up @@ -57,7 +58,7 @@ impl Command {
dc: &mut DC,
) -> Result<(), Error<CommE, PinE>>
where
SPI: hal::blocking::spi::Write<u8, Error = CommE>,
SPI: SpiBus<Error = CommE>,
DC: OutputPin<Error = PinE>,
{
// Transform command into a fixed size array of 7 u8 and the real length for sending
Expand Down
12 changes: 6 additions & 6 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use hal::{blocking::delay::DelayMs, digital::v2::OutputPin};
use hal::{delay::DelayNs, digital::OutputPin, spi::SpiBus};

use crate::{
command::{AddressIncrementMode, ColorMode, Command, VcomhLevel},
Expand Down Expand Up @@ -101,7 +101,7 @@ pub struct Ssd1331<SPI, DC> {

impl<SPI, DC, CommE, PinE> Ssd1331<SPI, DC>
where
SPI: hal::blocking::spi::Write<u8, Error = CommE>,
SPI: SpiBus<Error = CommE>,
DC: OutputPin<Error = PinE>,
{
/// Create new display instance
Expand Down Expand Up @@ -161,7 +161,7 @@ where
) -> Result<(), Error<CommE, PinE>>
where
RST: OutputPin<Error = PinE>,
DELAY: DelayMs<u8>,
DELAY: DelayNs,
{
rst.set_high().map_err(Error::Pin)?;
delay.delay_ms(1);
Expand Down Expand Up @@ -195,7 +195,7 @@ where
end: (u8, u8),
) -> Result<(), Error<CommE, PinE>> {
Command::ColumnAddress(start.0, end.0).send(&mut self.spi, &mut self.dc)?;
Command::RowAddress(start.1.into(), (end.1).into()).send(&mut self.spi, &mut self.dc)?;
Command::RowAddress(start.1, end.1).send(&mut self.spi, &mut self.dc)?;
Ok(())
}

Expand Down Expand Up @@ -373,7 +373,7 @@ use embedded_graphics_core::{
#[cfg(feature = "graphics")]
impl<SPI, DC> DrawTarget for Ssd1331<SPI, DC>
where
SPI: hal::blocking::spi::Write<u8>,
SPI: SpiBus,
DC: OutputPin,
{
type Color = Rgb565;
Expand All @@ -399,7 +399,7 @@ where
#[cfg(feature = "graphics")]
impl<SPI, DC> OriginDimensions for Ssd1331<SPI, DC>
where
SPI: hal::blocking::spi::Write<u8>,
SPI: SpiBus,
DC: OutputPin,
{
fn size(&self) -> Size {
Expand Down
36 changes: 18 additions & 18 deletions src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
//! Helpers for use in examples and tests

use embedded_hal::{
blocking::spi::{self, Transfer},
digital::v2::OutputPin,
};
use hal::{digital::OutputPin, spi::SpiDevice};

#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
pub struct Spi;

impl spi::Write<u8> for Spi {
type Error = ();

fn write(&mut self, _buf: &[u8]) -> Result<(), ()> {
impl SpiDevice for Spi {
fn transaction(
&mut self,
_operations: &mut [hal::spi::Operation<'_, u8>],
) -> Result<(), Self::Error> {
Ok(())
}
}

impl Transfer<u8> for Spi {
type Error = ();

fn transfer<'a>(&mut self, buf: &'a mut [u8]) -> Result<&'a [u8], ()> {
Ok(buf)
fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> {
self.transaction(&mut [hal::spi::Operation::Transfer(read, write)])
}
}

impl hal::spi::ErrorType for Spi {
type Error = hal::spi::ErrorKind;
}

#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
pub struct Pin;

impl OutputPin for Pin {
type Error = ();
impl hal::digital::ErrorType for Pin {
type Error = hal::digital::ErrorKind;
}

fn set_high(&mut self) -> Result<(), ()> {
impl OutputPin for Pin {
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(())
}

fn set_low(&mut self) -> Result<(), ()> {
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}