fix lcd-writer with rppal

This commit is contained in:
Simon 2023-12-20 09:14:32 +01:00
parent 748db285a6
commit de278aaa52
2 changed files with 20 additions and 24 deletions

View File

@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
linux-embedded-hal = "0.3.2" rppal = "0.16.0"

View File

@ -1,11 +1,5 @@
use linux_embedded_hal::{ use rppal::{i2c, i2c::I2c};
i2cdev::{core::I2CDevice, linux::LinuxI2CError}, use std::io::{self, stdin, stdout, BufRead, Write};
I2cdev,
};
use std::{
io::{self, stdin, stdout, BufRead, Write},
path::Path,
};
const C0: u8 = 7; const C0: u8 = 7;
const A0: u8 = 6; const A0: u8 = 6;
@ -29,12 +23,13 @@ enum ControlByte {
} }
struct Lcd { struct Lcd {
bus: I2cdev, bus: I2c,
} }
impl Lcd { impl Lcd {
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, LinuxI2CError> { pub fn new() -> Result<Self, i2c::Error> {
let mut bus = I2cdev::new(path)?; let mut bus = I2c::with_bus(1)?;
bus.set_slave_address(60)?;
let setup = [ let setup = [
ControlByte::Last as u8, ControlByte::Last as u8,
LcdCommand::FunctionSet as u8, LcdCommand::FunctionSet as u8,
@ -46,38 +41,39 @@ impl Lcd {
Ok(Self { bus }) 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]; let mut data = vec![ControlByte::Data as u8];
data.extend(value.bytes()); 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 pos = col | (row * 0x40) | 1 << 7;
let data = vec![ControlByte::Last as u8, pos]; 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]; 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<()> { fn main() -> io::Result<()> {
let device = "/dev/i2c-21"; let mut lcd = Lcd::new().unwrap();
println!("device = {device}");
let mut lcd = Lcd::new(device)?;
loop { loop {
print!("> "); print!("> ");
stdout().lock().flush()?; stdout().lock().flush()?;
let mut line = String::new(); let mut line = String::new();
stdin().lock().read_line(&mut line)?; stdin().lock().read_line(&mut line)?;
if line.starts_with("clear") { if line.starts_with("clear") {
lcd.clear()?; lcd.clear().unwrap();
lcd.set_cursor(0, 0)?; lcd.set_cursor(0, 0).unwrap();
} else if line.starts_with("print") { } else if line.starts_with("print") {
lcd.write(&line[6..])?; lcd.write(&line[6..]).unwrap();
} else { } else {
println!("unrecognized command \"{line}\""); println!("unrecognized command \"{line}\"");
} }