Skip to content

Commit

Permalink
Update embedded-hal to 1.0 (alpha)
Browse files Browse the repository at this point in the history
Wait for official release.

Signed-off-by: Daniel Schaefer <[email protected]>
  • Loading branch information
JohnAZoidberg committed Aug 29, 2023
1 parent 77dac9a commit 6e703c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/stillinbeta/is31fl3741"
readme = "README.md"

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

[package.metadata.docs.rs]
Expand Down
35 changes: 20 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,26 @@ 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

0 comments on commit 6e703c9

Please sign in to comment.