Right now, I'm using serial_port crate, but I don't really know how to use it correctly. The code so far looks like this:
let mut serial = File::open("/dev/ttyACM0").unwrap();
let timeout = Duration::from_secs(1);
let mut reader = BufReader::new(serial);
let mut buffer = String::new();
buffer.clear();
match reader.read_line(&mut buffer) {
Ok(_) => {
let data: Vec<&str> = buffer.split(",").collect();
if data.len() != 7 {
println!("Invalid data received: {:?}", buffer);
}
let x_joystick: f32 = data[0].parse().unwrap();
let y_joystick: f32 = data[1].parse().unwrap();
let x_accelerometer: f32 = data[2].parse().unwrap();
let y_accelerometer: f32 = data[3].parse().unwrap();
let z_accelerometer: f32 = data[4].parse().unwrap();
let c_button: f32 = data[5].parse().unwrap();
let z_button: f32 = data[6].parse().unwrap();
},
Err(e) => {
match e.kind() {
std::io::ErrorKind::TimedOut => {
println!("Timeout error: {:?}", e);
// You can handle the timeout error here
}
_ => {
println!("Other error: {:?}", e);
}
}
}
}
This is the main connectio part of the code. The rest is using the input. I'm REALLY new to Rust, and a lot of this code I looked at and tried to piece it together. Pretty much, I want the data from the Arduino to be read and then to function pretty much as a joystick / game controller. Thing is, when I try to use x_joystick somewhere like this:
let mut lAxisX = x_joystick;
it doesn't work and tells me it can't be found or is in wrong scope.
Any help would be appreciated