Showcase i2c writing from linux

This commit is contained in:
Frederik Menke 2023-08-26 23:15:11 +02:00
parent 54b51b50ba
commit 00ada18db5
2 changed files with 43 additions and 2 deletions

View file

@ -10,6 +10,7 @@ bytes = "1.2.1"
euclid = "0.22.7" euclid = "0.22.7"
futures = "0.3.24" futures = "0.3.24"
gilrs = "0.10.1" gilrs = "0.10.1"
i2c-linux = "0.1.2"
lazy_static = "1.4.0" lazy_static = "1.4.0"
regex = "1.6.0" regex = "1.6.0"
tokio = { version = "1.21.0", features = ["full"] } tokio = { version = "1.21.0", features = ["full"] }

View file

@ -1,18 +1,58 @@
#![warn(rust_2018_idioms)] #![warn(rust_2018_idioms)]
use futures::never::Never; use futures::never::Never;
use i2c_linux::I2c;
use red::jogger; use red::jogger;
use red::printer::gcode::*; use red::printer::gcode::*;
use red::printer::Printer; use red::printer::Printer;
use std::time::Duration; use std::time::Duration;
const DEFAULT_TTY: &str = "/dev/ttyUSB0"; const DEFAULT_TTY: &str = "/dev/ttyUSB0";
const I2C_ADDRESS_EXTENDER: u8 = 25;
const I2C_REGISTER_SPINDLE_SPEED: u8 = 0;
// available i2c functionality on the device as reported from
// the i2c library
// TENBIT_ADDR
// SMBUS_PEC
// SMBUS_QUICK
// SMBUS_READ_BYTE
// SMBUS_WRITE_BYTE
// SMBUS_READ_BYTE_DATA
// SMBUS_WRITE_BYTE_DATA
// SMBUS_READ_WORD_DATA
// SMBUS_WRITE_WORD_DATA
// SMBUS_PROC_CALL
// SMBUS_WRITE_BLOCK_DATA
// SMBUS_READ_I2C_BLOCK
// SMBUS_WRITE_I2C_BLOCK
// SMBUS_BYTE
// SMBUS_BYTE_DATA
// SMBUS_WORD_DATA
// SMBUS_I2C_BLOCK
// SMBUS_EMUL
#[tokio::main] #[tokio::main]
async fn main() -> Never { async fn main() -> Never {
communicate().await write_to_spindle().await
} }
async fn communicate() -> Never { async fn write_to_spindle() -> Never {
let mut i2c = I2c::from_path("/dev/i2c-0").unwrap();
let mut value = 0;
println!("functionality: {:?}", i2c.i2c_functionality());
i2c.smbus_set_slave_address(I2C_ADDRESS_EXTENDER.into(), false)
.unwrap();
loop {
value = u16::MAX - value;
match i2c.smbus_write_word_data(I2C_REGISTER_SPINDLE_SPEED, value) {
Ok(()) => println!("Wrote {} successfully", value),
Err(e) => println!("Error writing to device: {}", e),
};
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
async fn write_to_printer() -> Never {
let mut printer = Printer::connect(DEFAULT_TTY).await.unwrap(); let mut printer = Printer::connect(DEFAULT_TTY).await.unwrap();
printer printer
.send_gcode(Box::new(M114Command::new())) .send_gcode(Box::new(M114Command::new()))