Skip to content

Commit

Permalink
Make sure sw_enablement doesn't overwrite
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Schaefer <[email protected]>
  • Loading branch information
JohnAZoidberg committed Sep 18, 2023
1 parent fb88ad1 commit 5a39462
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{Error, IS31FL3741};
use core::convert::TryFrom;
#[allow(unused_imports)]
use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::blocking::i2c::Read;
#[allow(unused_imports)]
use embedded_hal::blocking::i2c::Write;

Expand All @@ -20,6 +21,7 @@ use embedded_graphics_core::{pixelcolor::Rgb888, prelude::*, primitives::Rectang
impl<I2C, I2cError> Dimensions for AdafruitRGB13x9<I2C>
where
I2C: Write<Error = I2cError>,
I2C: Read<Error = I2cError>,
{
fn bounding_box(&self) -> Rectangle {
Rectangle::new(Point::zero(), Size::new(13, 9))
Expand All @@ -30,6 +32,7 @@ where
impl<I2C, I2cError> DrawTarget for AdafruitRGB13x9<I2C>
where
I2C: Write<Error = I2cError>,
I2C: Read<Error = I2cError>,
I2cError:,
{
type Color = Rgb888;
Expand Down Expand Up @@ -57,6 +60,7 @@ where
impl<I2C, I2cError> AdafruitRGB13x9<I2C>
where
I2C: Write<Error = I2cError>,
I2C: Read<Error = I2cError>,
{
pub fn unwrap(self) -> I2C {
self.device.i2c
Expand Down
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pub mod devices;

use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::blocking::i2c::Read;
use embedded_hal::blocking::i2c::Write;

/// A struct to integrate with a new IS31FL3741 powered device.
Expand All @@ -25,6 +26,7 @@ pub struct IS31FL3741<I2C> {
impl<I2C, I2cError> IS31FL3741<I2C>
where
I2C: Write<Error = I2cError>,
I2C: Read<Error = I2cError>,
{
/// Fill all pixels of the display at once. The brightness should range from 0 to 255.
pub fn fill_matrix(&mut self, brightnesses: &[u8]) -> Result<(), I2cError> {
Expand Down Expand Up @@ -125,7 +127,10 @@ where

/// How many SW rows to enable
pub fn sw_enablement(&mut self, setting: SwSetting) -> Result<(), I2cError> {
let config_register = 1; // Shutdown disable
let config_register = self.read_register(
Page::Config,
addresses::CONFIG_REGISTER,
)?;

let new_val = (config_register & 0x0F) | (setting as u8) << 4;
self.write_register(Page::Config, addresses::CONFIG_REGISTER, new_val)?;
Expand All @@ -147,6 +152,19 @@ where
Ok(())
}

fn read_u8(&mut self, register: u8) -> Result<u8, I2cError> {
let mut buf = [0x00];
self.i2c.write(self.address, &[register])?;
self.i2c.read(self.address, &mut buf)?;
Ok(buf[0])
}

fn read_register(&mut self, bank: Page, register: u8) -> Result<u8, I2cError> {
self.bank(bank)?;
let value = self.read_u8(register)?;
Ok(value)
}

fn bank(&mut self, bank: Page) -> Result<(), I2cError> {
self.unlock()?;
self.write(&[addresses::PAGE_SELECT_REGISTER, bank as u8])?;
Expand Down

0 comments on commit 5a39462

Please sign in to comment.