Skip to content

Commit

Permalink
Version Bump to EmbeddedHal 1.0.0 (#52)
Browse files Browse the repository at this point in the history
* Advanced to Embedded-hal 1.0.0 Async Code untested
* removed I2C Error
  • Loading branch information
ichdenkenicht authored Aug 7, 2024
1 parent 722428e commit 3381df1
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 89 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ readme = "README.md"
async = ["embedded-hal-async"]

[dependencies]
embedded-hal = "0.2.3"
embedded-hal-async = { version = "1.0.0-rc.1", optional = true }
embedded-hal = {version = "1"}
embedded-hal-async = {version = "1", optional = true}

8 changes: 4 additions & 4 deletions src/bus/eightbit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::delay::DelayNs;
use embedded_hal::digital::OutputPin;

use crate::{
bus::DataBus,
Expand Down Expand Up @@ -134,7 +134,7 @@ impl<
D7: OutputPin,
> DataBus for EightBitBus<RS, EN, D0, D1, D2, D3, D4, D5, D6, D7>
{
fn write<D: DelayUs<u16> + DelayMs<u8>>(&mut self, byte: u8, data: bool, delay: &mut D) -> Result<()> {
fn write<D: DelayNs>(&mut self, byte: u8, data: bool, delay: &mut D) -> Result<()> {
if data {
self.rs.set_high().map_err(|_| Error)?;
} else {
Expand All @@ -144,7 +144,7 @@ impl<
self.set_bus_bits(byte)?;

self.en.set_high().map_err(|_| Error)?;
delay.delay_ms(2u8);
delay.delay_ms(2u32);
self.en.set_low().map_err(|_| Error)?;

if data {
Expand Down
10 changes: 5 additions & 5 deletions src/bus/fourbit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::delay::DelayNs;
use embedded_hal::digital::OutputPin;

use crate::bus::DataBus;
use crate::error::{Error, Result};
Expand Down Expand Up @@ -89,7 +89,7 @@ impl<RS: OutputPin, EN: OutputPin, D4: OutputPin, D5: OutputPin, D6: OutputPin,
impl<RS: OutputPin, EN: OutputPin, D4: OutputPin, D5: OutputPin, D6: OutputPin, D7: OutputPin> DataBus
for FourBitBus<RS, EN, D4, D5, D6, D7>
{
fn write<D: DelayUs<u16> + DelayMs<u8>>(&mut self, byte: u8, data: bool, delay: &mut D) -> Result<()> {
fn write<D: DelayNs>(&mut self, byte: u8, data: bool, delay: &mut D) -> Result<()> {
if data {
self.rs.set_high().map_err(|_| Error)?;
} else {
Expand All @@ -100,14 +100,14 @@ impl<RS: OutputPin, EN: OutputPin, D4: OutputPin, D5: OutputPin, D6: OutputPin,

// Pulse the enable pin to recieve the upper nibble
self.en.set_high().map_err(|_| Error)?;
delay.delay_ms(2u8);
delay.delay_ms(2u32);
self.en.set_low().map_err(|_| Error)?;

self.write_lower_nibble(byte)?;

// Pulse the enable pin to recieve the lower nibble
self.en.set_high().map_err(|_| Error)?;
delay.delay_ms(2u8);
delay.delay_ms(2u32);
self.en.set_low().map_err(|_| Error)?;

if data {
Expand Down
16 changes: 8 additions & 8 deletions src/bus/i2c.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
use embedded_hal::blocking::i2c::Write;
use embedded_hal::delay::DelayNs;
use embedded_hal::i2c::I2c;

use crate::{bus::DataBus, error::Result};

pub struct I2CBus<I2C: Write> {
pub struct I2CBus<I2C> {
i2c_bus: I2C,
address: u8,
}
Expand All @@ -13,28 +13,28 @@ const ENABLE: u8 = 0b0000_0100;
// const READ_WRITE: u8 = 0b0000_0010; // Not used as no reading of the `HD44780` is done
const REGISTER_SELECT: u8 = 0b0000_0001;

impl<I2C: Write> I2CBus<I2C> {
impl<I2C: I2c> I2CBus<I2C> {
pub fn new(i2c_bus: I2C, address: u8) -> I2CBus<I2C> {
I2CBus { i2c_bus, address }
}

/// Write a nibble to the lcd
/// The nibble should be in the upper part of the byte
fn write_nibble<D: DelayUs<u16> + DelayMs<u8>>(&mut self, nibble: u8, data: bool, delay: &mut D) {
fn write_nibble<D: DelayNs>(&mut self, nibble: u8, data: bool, delay: &mut D) {
let rs = match data {
false => 0u8,
true => REGISTER_SELECT,
};
let byte = nibble | rs | BACKLIGHT;

let _ = self.i2c_bus.write(self.address, &[byte, byte | ENABLE]);
delay.delay_ms(2u8);
delay.delay_ms(2u32);
let _ = self.i2c_bus.write(self.address, &[byte]);
}
}

impl<I2C: Write> DataBus for I2CBus<I2C> {
fn write<D: DelayUs<u16> + DelayMs<u8>>(&mut self, byte: u8, data: bool, delay: &mut D) -> Result<()> {
impl<I2C: I2c> DataBus for I2CBus<I2C> {
fn write<D: DelayNs>(&mut self, byte: u8, data: bool, delay: &mut D) -> Result<()> {
let upper_nibble = byte & 0xF0;
self.write_nibble(upper_nibble, data, delay);

Expand Down
4 changes: 2 additions & 2 deletions src/bus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
use embedded_hal::delay::DelayNs;

mod eightbit;
mod fourbit;
Expand All @@ -11,7 +11,7 @@ pub use self::i2c::I2CBus;
use crate::error::Result;

pub trait DataBus {
fn write<D: DelayUs<u16> + DelayMs<u8>>(&mut self, byte: u8, data: bool, delay: &mut D) -> Result<()>;
fn write<D: DelayNs>(&mut self, byte: u8, data: bool, delay: &mut D) -> Result<()>;

// TODO
// fn read(...)
Expand Down
60 changes: 30 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#![cfg_attr(feature = "async", feature(impl_trait_in_assoc_type))]

use display_size::DisplaySize;
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
use embedded_hal::blocking::i2c;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::delay::DelayNs;
use embedded_hal::i2c::I2c;
use embedded_hal::digital::OutputPin;

pub mod bus;
use bus::{DataBus, EightBitBus, FourBitBus, I2CBus};
Expand Down Expand Up @@ -81,7 +81,7 @@ impl<
///
#[allow(clippy::too_many_arguments)]
#[allow(clippy::type_complexity)]
pub fn new_8bit<D: DelayUs<u16> + DelayMs<u8>>(
pub fn new_8bit<D: DelayNs>(
rs: RS,
en: EN,
d0: D0,
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<RS: OutputPin, EN: OutputPin, D4: OutputPin, D5: OutputPin, D6: OutputPin,
/// being sent over the data bus
///
#[allow(clippy::type_complexity)]
pub fn new_4bit<D: DelayUs<u16> + DelayMs<u8>>(
pub fn new_4bit<D: DelayNs>(
rs: RS,
en: EN,
d4: D4,
Expand All @@ -174,7 +174,7 @@ impl<RS: OutputPin, EN: OutputPin, D4: OutputPin, D5: OutputPin, D6: OutputPin,
}
}

impl<I2C: i2c::Write> HD44780<I2CBus<I2C>> {
impl<I2C: I2c> HD44780<I2CBus<I2C>> {
/// Create an instance of a `HD44780` from an i2c write peripheral,
/// the `HD44780` I2C address and a struct implementing the delay trait.
/// - The delay instance is used to sleep between commands to
Expand All @@ -184,7 +184,7 @@ impl<I2C: i2c::Write> HD44780<I2CBus<I2C>> {
///
/// This mode operates on an I2C bus, using an I2C to parallel port expander
///
pub fn new_i2c<D: DelayUs<u16> + DelayMs<u8>>(
pub fn new_i2c<D: DelayNs>(
i2c_bus: I2C,
address: u8,
delay: &mut D,
Expand All @@ -211,7 +211,7 @@ where
/// ```rust,ignore
/// lcd.reset();
/// ```
pub fn reset<D: DelayUs<u16> + DelayMs<u8>>(&mut self, delay: &mut D) -> Result<()> {
pub fn reset<D: DelayNs>(&mut self, delay: &mut D) -> Result<()> {
self.write_command(0b0000_0010, delay)?;

Ok(())
Expand All @@ -222,7 +222,7 @@ where
///
/// Note: This is equivilent to calling all of the other relavent
/// methods however this operation does it all in one go to the `HD44780`
pub fn set_display_mode<D: DelayUs<u16> + DelayMs<u8>>(
pub fn set_display_mode<D: DelayNs>(
&mut self,
display_mode: DisplayMode,
delay: &mut D,
Expand All @@ -241,7 +241,7 @@ where
/// ```rust,ignore
/// lcd.clear();
/// ```
pub fn clear<D: DelayUs<u16> + DelayMs<u8>>(&mut self, delay: &mut D) -> Result<()> {
pub fn clear<D: DelayNs>(&mut self, delay: &mut D) -> Result<()> {
self.write_command(0b0000_0001, delay)?;

Ok(())
Expand All @@ -253,7 +253,7 @@ where
/// ```rust,ignore
/// lcd.set_autoscroll(true);
/// ```
pub fn set_autoscroll<D: DelayUs<u16> + DelayMs<u8>>(&mut self, enabled: bool, delay: &mut D) -> Result<()> {
pub fn set_autoscroll<D: DelayNs>(&mut self, enabled: bool, delay: &mut D) -> Result<()> {
self.entry_mode.shift_mode = enabled.into();

let cmd = self.entry_mode.as_byte();
Expand All @@ -264,7 +264,7 @@ where
}

/// Set if the cursor should be visible
pub fn set_cursor_visibility<D: DelayUs<u16> + DelayMs<u8>>(
pub fn set_cursor_visibility<D: DelayNs>(
&mut self,
visibility: Cursor,
delay: &mut D,
Expand All @@ -279,7 +279,7 @@ where
}

/// Set if the characters on the display should be visible
pub fn set_display<D: DelayUs<u16> + DelayMs<u8>>(&mut self, display: Display, delay: &mut D) -> Result<()> {
pub fn set_display<D: DelayNs>(&mut self, display: Display, delay: &mut D) -> Result<()> {
self.display_mode.display = display;

let cmd = self.display_mode.as_byte();
Expand All @@ -290,7 +290,7 @@ where
}

/// Set if the cursor should blink
pub fn set_cursor_blink<D: DelayUs<u16> + DelayMs<u8>>(&mut self, blink: CursorBlink, delay: &mut D) -> Result<()> {
pub fn set_cursor_blink<D: DelayNs>(&mut self, blink: CursorBlink, delay: &mut D) -> Result<()> {
self.display_mode.cursor_blink = blink;

let cmd = self.display_mode.as_byte();
Expand All @@ -309,7 +309,7 @@ where
/// // Move left when a new character is written
/// lcd.set_cursor_mode(CursorMode::Left)
/// ```
pub fn set_cursor_mode<D: DelayUs<u16> + DelayMs<u8>>(&mut self, mode: CursorMode, delay: &mut D) -> Result<()> {
pub fn set_cursor_mode<D: DelayNs>(&mut self, mode: CursorMode, delay: &mut D) -> Result<()> {
self.entry_mode.cursor_mode = mode;

let cmd = self.entry_mode.as_byte();
Expand All @@ -326,7 +326,7 @@ where
/// // for a 20 columns display
/// lcd.set_cursor_pos(40)
/// ```
pub fn set_cursor_pos<D: DelayUs<u16> + DelayMs<u8>>(&mut self, position: u8, delay: &mut D) -> Result<()> {
pub fn set_cursor_pos<D: DelayNs>(&mut self, position: u8, delay: &mut D) -> Result<()> {
let size = self.display_size.get();
let position = (position % size.0, position / size.0);
self.set_cursor_xy(position, delay)
Expand All @@ -338,7 +338,7 @@ where
/// // Move to the start of line 3
/// lcd.set_cursor_pos_xy(0,2)
/// ```
pub fn set_cursor_xy<D: DelayUs<u16> + DelayMs<u8>>(&mut self, position: (u8, u8), delay: &mut D) -> Result<()> {
pub fn set_cursor_xy<D: DelayNs>(&mut self, position: (u8, u8), delay: &mut D) -> Result<()> {
let size = self.display_size.get();
let pos = get_position(position, size)?;

Expand All @@ -355,7 +355,7 @@ where
/// lcd.shift_cursor(Direction::Left);
/// lcd.shift_cursor(Direction::Right);
/// ```
pub fn shift_cursor<D: DelayUs<u16> + DelayMs<u8>>(&mut self, dir: Direction, delay: &mut D) -> Result<()> {
pub fn shift_cursor<D: DelayNs>(&mut self, dir: Direction, delay: &mut D) -> Result<()> {
let bits = match dir {
Direction::Left => 0b0000_0000,
Direction::Right => 0b0000_0100,
Expand All @@ -372,7 +372,7 @@ where
/// lcd.shift_display(Direction::Left);
/// lcd.shift_display(Direction::Right);
/// ```
pub fn shift_display<D: DelayUs<u16> + DelayMs<u8>>(&mut self, dir: Direction, delay: &mut D) -> Result<()> {
pub fn shift_display<D: DelayNs>(&mut self, dir: Direction, delay: &mut D) -> Result<()> {
let bits = match dir {
Direction::Left => 0b0000_0000,
Direction::Right => 0b0000_0100,
Expand All @@ -391,27 +391,27 @@ where
/// ```rust,ignore
/// lcd.write_char('A', &mut delay)?; // prints 'A'
/// ```
pub fn write_char<D: DelayUs<u16> + DelayMs<u8>>(&mut self, data: char, delay: &mut D) -> Result<()> {
pub fn write_char<D: DelayNs>(&mut self, data: char, delay: &mut D) -> Result<()> {
self.write_byte(data as u8, delay)
}

fn write_command<D: DelayUs<u16> + DelayMs<u8>>(&mut self, cmd: u8, delay: &mut D) -> Result<()> {
fn write_command<D: DelayNs>(&mut self, cmd: u8, delay: &mut D) -> Result<()> {
self.bus.write(cmd, false, delay)?;

// Wait for the command to be processed
delay.delay_us(100);
Ok(())
}

fn init_4bit<D: DelayUs<u16> + DelayMs<u8>>(&mut self, delay: &mut D) -> Result<()> {
fn init_4bit<D: DelayNs>(&mut self, delay: &mut D) -> Result<()> {
// Wait for the LCD to wakeup if it was off
delay.delay_ms(15u8);
delay.delay_ms(15u32);

// Initialize Lcd in 4-bit mode
self.bus.write(0x33, false, delay)?;

// Wait for the command to be processed
delay.delay_ms(5u8);
delay.delay_ms(5u32);

// Sets 4-bit operation and enables 5x7 mode for chars
self.bus.write(0x32, false, delay)?;
Expand Down Expand Up @@ -451,15 +451,15 @@ where
}

// Follow the 8-bit setup procedure as specified in the HD44780 datasheet
fn init_8bit<D: DelayUs<u16> + DelayMs<u8>>(&mut self, delay: &mut D) -> Result<()> {
fn init_8bit<D: DelayNs>(&mut self, delay: &mut D) -> Result<()> {
// Wait for the LCD to wakeup if it was off
delay.delay_ms(15u8);
delay.delay_ms(15u32);

// Initialize Lcd in 8-bit mode
self.bus.write(0b0011_0000, false, delay)?;

// Wait for the command to be processed
delay.delay_ms(5u8);
delay.delay_ms(5u32);

// Sets 8-bit operation and enables 5x7 mode for chars
self.bus.write(0b0011_1000, false, delay)?;
Expand Down Expand Up @@ -500,7 +500,7 @@ where
/// ```rust,ignore
/// lcd.write_str("Hello, World!", &mut delay)?;
/// ```
pub fn write_str<D: DelayUs<u16> + DelayMs<u8>>(&mut self, string: &str, delay: &mut D) -> Result<()> {
pub fn write_str<D: DelayNs>(&mut self, string: &str, delay: &mut D) -> Result<()> {
self.write_bytes(string.as_bytes(), delay)
}

Expand All @@ -510,7 +510,7 @@ where
/// ```rust,ignore
/// lcd.write_bytes(b"Hello, World!", &mut delay)?;
/// ```
pub fn write_bytes<D: DelayUs<u16> + DelayMs<u8>>(&mut self, string: &[u8], delay: &mut D) -> Result<()> {
pub fn write_bytes<D: DelayNs>(&mut self, string: &[u8], delay: &mut D) -> Result<()> {
for &b in string {
self.write_byte(b, delay)?;
}
Expand All @@ -531,7 +531,7 @@ where
/// lcd.write_byte(b'~', &mut delay)?; // usually prints 🡢
/// lcd.write_byte(b'\x7f', &mut delay)?; // usually prints 🡠
/// ```
pub fn write_byte<D: DelayUs<u16> + DelayMs<u8>>(&mut self, data: u8, delay: &mut D) -> Result<()> {
pub fn write_byte<D: DelayNs>(&mut self, data: u8, delay: &mut D) -> Result<()> {
self.bus.write(data, true, delay)?;

// Wait for the command to be processed
Expand Down
8 changes: 4 additions & 4 deletions src/non_blocking/bus/eightbit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::future::Future;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal_async::delay::DelayUs;
use embedded_hal::digital::OutputPin;
use embedded_hal_async::delay::DelayNs;

use crate::{
error::{Error, Result},
Expand Down Expand Up @@ -134,9 +134,9 @@ impl<
D7: OutputPin + 'static,
> DataBus for EightBitBus<RS, EN, D0, D1, D2, D3, D4, D5, D6, D7>
{
type WriteFuture<'a, D: 'a + DelayUs> = impl Future<Output = Result<()>> + 'a;
type WriteFuture<'a, D: 'a + DelayNs> = impl Future<Output = Result<()>> + 'a;

fn write<'a, D: DelayUs + 'a>(&'a mut self, byte: u8, data: bool, delay: &'a mut D) -> Self::WriteFuture<'a, D> {
fn write<'a, D: DelayNs + 'a>(&'a mut self, byte: u8, data: bool, delay: &'a mut D) -> Self::WriteFuture<'a, D> {
async move {
if data {
self.rs.set_high().map_err(|_| Error)?;
Expand Down
Loading

0 comments on commit 3381df1

Please sign in to comment.