Skip to content

Commit 722428e

Browse files
Add the missing async I2C constructor (#46)
1 parent f264dd2 commit 722428e

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/non_blocking/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
use embedded_hal::digital::v2::OutputPin;
55
use embedded_hal_async::delay::DelayUs;
6+
use embedded_hal_async::i2c::I2c;
67

78
pub mod bus;
8-
use bus::{DataBus, EightBitBus, FourBitBus};
9+
use bus::{DataBus, EightBitBus, FourBitBus, I2CBus};
910

1011
pub use crate::error;
1112
use error::Result;
@@ -127,6 +128,34 @@ impl<
127128
}
128129
}
129130

131+
impl<I2C: I2c + 'static> HD44780<I2CBus<I2C>> {
132+
/// Create an instance of a `HD44780` from an I2C bus and a struct implementing the delay trait.
133+
/// - The delay instance is used to sleep between commands to
134+
/// ensure the `HD44780` has enough time to process commands.
135+
/// - The I2C bus is used to send and receive with
136+
/// the `HD44780`.
137+
///
138+
/// This mode operates differently than 8 bit and 4 bit modes by using
139+
/// an I2C expander, which is nice on devices with less I/O although
140+
/// the I/O takes a 'bit' longer, because of transmission delays
141+
///
142+
/// Instead of commands being sent byte by byte each command is
143+
/// broken up into it's upper and lower nibbles (4 bits) before
144+
/// being sent over the data bus
145+
///
146+
pub async fn new_i2c<'a, D: DelayUs>(i2c: I2C, address: u8, delay: &'a mut D) -> Result<HD44780<I2CBus<I2C>>> {
147+
let mut hd = HD44780 {
148+
bus: I2CBus::new(i2c, address),
149+
entry_mode: EntryMode::default(),
150+
display_mode: DisplayMode::default(),
151+
};
152+
153+
hd.init_4bit(delay).await?;
154+
155+
return Ok(hd);
156+
}
157+
}
158+
130159
impl<B> HD44780<B>
131160
where
132161
B: DataBus,

0 commit comments

Comments
 (0)