Skip to content
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

Update embedded-hal to 1.0 (alpha) #5

Draft
wants to merge 2 commits into
base: main
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
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ repository = "https://github.com/stillinbeta/is31fl3741"
readme = "README.md"

[dependencies]
embedded-hal = "0.2.6"
embedded-graphics-core = { optional = true, version = "0.3.3" }
embedded-hal = "1.0.0-alpha.11"
embedded-graphics-core = { optional = true, version = "0.4.0" }

[package.metadata.docs.rs]
all-features = true

[dev-dependencies]
cortex-m-rt = "0.6.10"
cortex-m = "0.7.1"
cortex-m-rt = "0.7.3"
cortex-m = "0.7.7"
panic-halt = "0.2.0"
stm32g0xx-hal = {version = "0.1.3", features = ["rt", "stm32g071"]}
tinybmp = "0.3.3"
embedded-graphics = "0.7.1"
stm32g0xx-hal = {version = "0.2.0", features = ["rt", "stm32g071"]}
tinybmp = "0.5.0"
embedded-graphics = "0.8.1"

[features]
adafruit_rgb_13x9 = []
Expand Down
28 changes: 13 additions & 15 deletions src/devices.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// #[cfg_attr(docsrs, doc(cfg(feature = "adafruit_rgb_13x9")))]
#[allow(unused_imports)]
use crate::{Error, IS31FL3741};
use crate::{Is31Error, IS31FL3741};
#[allow(unused_imports)]
use core::convert::TryFrom;
#[allow(unused_imports)]
use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::delay::DelayUs;
#[allow(unused_imports)]
use embedded_hal::blocking::i2c::Write;
use embedded_hal::i2c::{Error, I2c};

#[cfg(feature = "adafruit_rgb_13x9")]
pub struct AdafruitRGB13x9<I2C> {
Expand All @@ -17,23 +16,22 @@ pub struct AdafruitRGB13x9<I2C> {
use embedded_graphics_core::{pixelcolor::Rgb888, prelude::*, primitives::Rectangle};

#[cfg(all(feature = "adafruit_rgb_13x9", feature = "embedded_graphics"))]
impl<I2C, I2cError> Dimensions for AdafruitRGB13x9<I2C>
impl<I2C: I2c, I2cError: Error> Dimensions for AdafruitRGB13x9<I2C>
where
I2C: Write<Error = I2cError>,
I2C: I2c<Error = I2cError>,
{
fn bounding_box(&self) -> Rectangle {
Rectangle::new(Point::zero(), Size::new(13, 9))
}
}

#[cfg(all(feature = "adafruit_rgb_13x9", feature = "embedded_graphics"))]
impl<I2C, I2cError> DrawTarget for AdafruitRGB13x9<I2C>
impl<I2C: I2c, I2cError: Error> DrawTarget for AdafruitRGB13x9<I2C>
where
I2C: Write<Error = I2cError>,
I2cError:,
I2C: I2c<Error = I2cError>,
{
type Color = Rgb888;
type Error = Error<I2cError>;
type Error = Is31Error<I2cError>;

fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
Expand All @@ -54,9 +52,9 @@ where
}

#[cfg(feature = "adafruit_rgb_13x9")]
impl<I2C, I2cError> AdafruitRGB13x9<I2C>
impl<I2C: I2c, I2cError: Error> AdafruitRGB13x9<I2C>
where
I2C: Write<Error = I2cError>,
I2C: I2c<Error = I2cError>,
{
pub fn unwrap(self) -> I2C {
self.device.i2c
Expand Down Expand Up @@ -204,19 +202,19 @@ where
}
}

pub fn pixel_rgb(&mut self, x: u8, y: u8, r: u8, g: u8, b: u8) -> Result<(), Error<I2cError>> {
pub fn pixel_rgb(&mut self, x: u8, y: u8, r: u8, g: u8, b: u8) -> Result<(), Is31Error<I2cError>> {
let x = x + y * 13;
self.device.pixel(x, 2, r)?;
self.device.pixel(x, 1, g)?;
self.device.pixel(x, 0, b)?;
Ok(())
}

pub fn setup<DEL: DelayMs<u8>>(&mut self, delay: &mut DEL) -> Result<(), Error<I2cError>> {
pub fn setup<DEL: DelayUs>(&mut self, delay: &mut DEL) -> Result<(), Is31Error<I2cError>> {
self.device.setup(delay)
}

pub fn fill_rgb(&mut self, r: u8, g: u8, b: u8) -> Result<(), Error<I2cError>> {
pub fn fill_rgb(&mut self, r: u8, g: u8, b: u8) -> Result<(), Is31Error<I2cError>> {
for x in 0..13 {
for y in 0..9 {
self.pixel_rgb(x, y, r, g, b)?;
Expand Down
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#![doc = include_str!("../README.md")]
/// Preconfigured devices
pub mod devices;

use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::blocking::i2c::Write;
use embedded_hal::delay::DelayUs;
use embedded_hal::i2c::{Error, I2c};

/// A struct to integrate with a new IS31FL3741 powered device.
pub struct IS31FL3741<I2C> {
Expand All @@ -22,9 +21,9 @@ pub struct IS31FL3741<I2C> {
pub calc_pixel: fn(x: u8, y: u8) -> (u8, u8),
}

impl<I2C, I2cError> IS31FL3741<I2C>
impl<I2C: I2c, I2cError: Error> IS31FL3741<I2C>
where
I2C: Write<Error = I2cError>,
I2C: I2c<Error = I2cError>,
{
/// Fill the display with a single brightness. The brightness should range from 0 to 255.
pub fn fill(&mut self, brightness: u8) -> Result<(), I2cError> {
Expand All @@ -46,7 +45,7 @@ where
/// 2. The chip will be put in shutdown mode
/// 3. The chip will be configured to use the maximum voltage
/// 4. The chip will be taken out of shutdown mode
pub fn setup<DEL: DelayMs<u8>>(&mut self, delay: &mut DEL) -> Result<(), Error<I2cError>> {
pub fn setup<DEL: DelayUs>(&mut self, delay: &mut DEL) -> Result<(), Is31Error<I2cError>> {
self.reset(delay)?;
self.shutdown(true)?;
delay.delay_ms(10);
Expand All @@ -55,15 +54,16 @@ where
self.shutdown(false)?;
Ok(())
}

/// Set the brightness at a specific x,y coordinate. Just like the [fill method](Self::fill)
/// the brightness should range from 0 to 255. If the coordinate is out of range then the
/// function will return an error of [InvalidLocation](Error::InvalidLocation).
pub fn pixel(&mut self, x: u8, y: u8, brightness: u8) -> Result<(), Error<I2cError>> {
pub fn pixel(&mut self, x: u8, y: u8, brightness: u8) -> Result<(), Is31Error<I2cError>> {
if x > self.width {
return Err(Error::InvalidLocation(x));
return Err(Is31Error::InvalidLocation(x));
}
if y > self.height {
return Err(Error::InvalidLocation(y));
return Err(Is31Error::InvalidLocation(y));
}
let (pixel, frame) = (self.calc_pixel)(x, y);
let bank = if frame == 0 { Page::Pwm1 } else { Page::Pwm2 };
Expand All @@ -80,7 +80,7 @@ where
/// Send a reset message to the slave device. Delay is something that your device's HAL should
/// provide which allows for the process to sleep for a certain amount of time (in this case 10
/// MS to perform a reset).
pub fn reset<DEL: DelayMs<u8>>(&mut self, delay: &mut DEL) -> Result<(), I2cError> {
pub fn reset<DEL: DelayUs>(&mut self, delay: &mut DEL) -> Result<(), I2cError> {
self.write_register(Page::Config, addresses::RESET_REGISTER, addresses::RESET)?;
delay.delay_ms(10);
Ok(())
Expand Down Expand Up @@ -151,15 +151,15 @@ pub mod addresses {
}

#[derive(Clone, Copy, Debug)]
pub enum Error<I2cError> {
pub enum Is31Error<I2cError> {
I2cError(I2cError),
InvalidLocation(u8),
InvalidFrame(u8),
}

impl<E> From<E> for Error<E> {
impl<E> From<E> for Is31Error<E> {
fn from(error: E) -> Self {
Error::I2cError(error)
Is31Error::I2cError(error)
}
}

Expand Down