From de278aaa52c182528ab8aafb6ea063cc939ff60f Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 20 Dec 2023 09:14:32 +0100 Subject: [PATCH] fix lcd-writer with rppal --- lcd-writer/Cargo.toml | 2 +- lcd-writer/src/main.rs | 42 +++++++++++++++++++----------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/lcd-writer/Cargo.toml b/lcd-writer/Cargo.toml index 6d09212..66802a9 100644 --- a/lcd-writer/Cargo.toml +++ b/lcd-writer/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -linux-embedded-hal = "0.3.2" +rppal = "0.16.0" diff --git a/lcd-writer/src/main.rs b/lcd-writer/src/main.rs index 971c6a0..ed5f3fc 100644 --- a/lcd-writer/src/main.rs +++ b/lcd-writer/src/main.rs @@ -1,11 +1,5 @@ -use linux_embedded_hal::{ - i2cdev::{core::I2CDevice, linux::LinuxI2CError}, - I2cdev, -}; -use std::{ - io::{self, stdin, stdout, BufRead, Write}, - path::Path, -}; +use rppal::{i2c, i2c::I2c}; +use std::io::{self, stdin, stdout, BufRead, Write}; const C0: u8 = 7; const A0: u8 = 6; @@ -29,12 +23,13 @@ enum ControlByte { } struct Lcd { - bus: I2cdev, + bus: I2c, } impl Lcd { - pub fn new>(path: P) -> Result { - let mut bus = I2cdev::new(path)?; + pub fn new() -> Result { + let mut bus = I2c::with_bus(1)?; + bus.set_slave_address(60)?; let setup = [ ControlByte::Last as u8, LcdCommand::FunctionSet as u8, @@ -46,38 +41,39 @@ impl Lcd { Ok(Self { bus }) } - pub fn write(&mut self, value: &str) -> Result<(), LinuxI2CError> { + pub fn write(&mut self, value: &str) -> Result<(), i2c::Error> { let mut data = vec![ControlByte::Data as u8]; data.extend(value.bytes()); - self.bus.write(&data) + let _ = self.bus.write(&data)?; + Ok(()) } - pub fn set_cursor(&mut self, col: u8, row: u8) -> Result<(), LinuxI2CError> { + pub fn set_cursor(&mut self, col: u8, row: u8) -> Result<(), i2c::Error> { let pos = col | (row * 0x40) | 1 << 7; let data = vec![ControlByte::Last as u8, pos]; - self.bus.write(&data) + let _ = self.bus.write(&data)?; + Ok(()) } - pub fn clear(&mut self) -> Result<(), LinuxI2CError> { + pub fn clear(&mut self) -> Result<(), i2c::Error> { let data = vec![ControlByte::Last as u8, LcdCommand::Clear as u8]; - self.bus.write(&data) + let _ = self.bus.write(&data)?; + Ok(()) } } fn main() -> io::Result<()> { - let device = "/dev/i2c-21"; - println!("device = {device}"); - let mut lcd = Lcd::new(device)?; + let mut lcd = Lcd::new().unwrap(); loop { print!("> "); stdout().lock().flush()?; let mut line = String::new(); stdin().lock().read_line(&mut line)?; if line.starts_with("clear") { - lcd.clear()?; - lcd.set_cursor(0, 0)?; + lcd.clear().unwrap(); + lcd.set_cursor(0, 0).unwrap(); } else if line.starts_with("print") { - lcd.write(&line[6..])?; + lcd.write(&line[6..]).unwrap(); } else { println!("unrecognized command \"{line}\""); }