Pcf8574

Struct Pcf8574 

Source
pub struct Pcf8574 { /* private fields */ }
Expand description

Represents an LCD display attached via PCF8574 I2C expander. Use the traits in the lcd crate to interact with it.

Implementations§

Source§

impl Pcf8574

Source

pub fn new(bus: u8, address: u16) -> Result<Self, LinuxI2CError>

Create a new instance, using the Linux I2C interface for communication. bus is the number of /dev/i2c-<bus> to use, and address is the I2C address of the device.

After opening the device, defaults to ignoring all I/O errors; see Self::on_error and ErrorHandling for how to change this behavior.

Examples found in repository?
examples/hello_world.rs (line 33)
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12    let mut args = args().skip(1);
13    let bus = args.next()
14        .map(|s| u8::from_str_radix(&s, 10))
15        .unwrap_or_else(|| usage())
16        .unwrap_or_else(|e| {
17            eprintln!("invalid bus number: {}", e);
18            usage();
19        });
20    let addr = args.next()
21        .map_or_else(|| Ok(0x27), |s| {
22            if let Some(hex) = s.strip_prefix("0x") {
23                u16::from_str_radix(hex, 16)
24            } else {
25                u16::from_str_radix(&s, 10)
26            }
27        })
28        .unwrap_or_else(|e| {
29            eprintln!("invalid i2c address: {}", e);
30            usage();
31        });
32
33    let mut dev = Pcf8574::new(bus, addr)?;
34    dev.on_error(ErrorHandling::Panic);
35
36    let mut display = lcd::Display::new(dev);
37    display.init(lcd::FunctionLine::Line2, lcd::FunctionDots::Dots5x8);
38    display.display(
39        lcd::DisplayMode::DisplayOn,
40        lcd::DisplayCursor::CursorOff,
41        lcd::DisplayBlink::BlinkOff);
42
43    display.clear();
44    display.home();
45    display.print("Hello, World!");
46    display.position(2, 1);
47    display.print("This is line two");
48    display.position(2, 2);
49    display.print("... and line three");
50    display.position(0, 3);
51    display.print("Good bye!");
52
53    Ok(())
54}
Source

pub fn on_error(&mut self, on_err: ErrorHandling)

Change the I/O error handling strategy.

lcd::Hardware doesn’t have any way to return errors to the caller, so error handling has to be done here, internally.

Examples found in repository?
examples/hello_world.rs (line 34)
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12    let mut args = args().skip(1);
13    let bus = args.next()
14        .map(|s| u8::from_str_radix(&s, 10))
15        .unwrap_or_else(|| usage())
16        .unwrap_or_else(|e| {
17            eprintln!("invalid bus number: {}", e);
18            usage();
19        });
20    let addr = args.next()
21        .map_or_else(|| Ok(0x27), |s| {
22            if let Some(hex) = s.strip_prefix("0x") {
23                u16::from_str_radix(hex, 16)
24            } else {
25                u16::from_str_radix(&s, 10)
26            }
27        })
28        .unwrap_or_else(|e| {
29            eprintln!("invalid i2c address: {}", e);
30            usage();
31        });
32
33    let mut dev = Pcf8574::new(bus, addr)?;
34    dev.on_error(ErrorHandling::Panic);
35
36    let mut display = lcd::Display::new(dev);
37    display.init(lcd::FunctionLine::Line2, lcd::FunctionDots::Dots5x8);
38    display.display(
39        lcd::DisplayMode::DisplayOn,
40        lcd::DisplayCursor::CursorOff,
41        lcd::DisplayBlink::BlinkOff);
42
43    display.clear();
44    display.home();
45    display.print("Hello, World!");
46    display.position(2, 1);
47    display.print("This is line two");
48    display.position(2, 2);
49    display.print("... and line three");
50    display.position(0, 3);
51    display.print("Good bye!");
52
53    Ok(())
54}
Source

pub fn backlight(&mut self, on: bool)

Set the display’s backlight on or off.

Trait Implementations§

Source§

impl Delay for Pcf8574

Source§

fn delay_us(&mut self, delay_usec: u32)

Delay for given amount of time (in microseconds).
Source§

impl Hardware for Pcf8574

Source§

fn rs(&mut self, bit: bool)

Source§

fn enable(&mut self, bit: bool)

Source§

fn data(&mut self, bits: u8)

Source§

fn apply(&mut self)

Send data to the device. Read more
Source§

fn wait_address(&mut self)

Address set up time is 40ns minimum (tAS) This function should be overridden in case processor is too fast for 40ns to pass.
Source§

fn mode(&self) -> FunctionMode

Override to pick 8-bit mode (4-bit mode by default)
Source§

fn can_read(&self) -> bool

If this implementation can read from the data port. Default is false. If true is returned, both rw and read_data need to be implemented.
Source§

fn rw(&mut self, _bit: bool)

Set R/W flag. Read more
Source§

fn read_data(&mut self) -> u8

Read data from the data pins of the LCD (D0-D7 in 8-bit mode and D4-D7 in 4-bit mode) Read more

Auto Trait Implementations§

§

impl Freeze for Pcf8574

§

impl !RefUnwindSafe for Pcf8574

§

impl !Send for Pcf8574

§

impl !Sync for Pcf8574

§

impl Unpin for Pcf8574

§

impl !UnwindSafe for Pcf8574

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.