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
impl Pcf8574
Sourcepub fn new(bus: u8, address: u16) -> Result<Self, LinuxI2CError>
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}Sourcepub fn on_error(&mut self, on_err: ErrorHandling)
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}Trait Implementations§
Source§impl Hardware for Pcf8574
impl Hardware for Pcf8574
fn rs(&mut self, bit: bool)
fn enable(&mut self, bit: bool)
fn data(&mut self, bits: u8)
Source§fn wait_address(&mut self)
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
fn mode(&self) -> FunctionMode
Override to pick 8-bit mode (4-bit mode by default)
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more