|
3 | 3 |
|
4 | 4 | use embedded_hal::digital::v2::OutputPin; |
5 | 5 | use embedded_hal_async::delay::DelayUs; |
| 6 | +use embedded_hal_async::i2c::I2c; |
6 | 7 |
|
7 | 8 | pub mod bus; |
8 | | -use bus::{DataBus, EightBitBus, FourBitBus}; |
| 9 | +use bus::{DataBus, EightBitBus, FourBitBus, I2CBus}; |
9 | 10 |
|
10 | 11 | pub use crate::error; |
11 | 12 | use error::Result; |
@@ -127,6 +128,34 @@ impl< |
127 | 128 | } |
128 | 129 | } |
129 | 130 |
|
| 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 | + |
130 | 159 | impl<B> HD44780<B> |
131 | 160 | where |
132 | 161 | B: DataBus, |
|
0 commit comments