use std::env::args;
use std::io::{BufRead, BufReader, stdin};
use std::io::Write;
use std::path::Path;
use serialport::SerialPortSettings;
use odrive_rs::commands::ODrive;
fn main() {
let args: Vec<String> = args().collect();
let mut settings = SerialPortSettings::default();
settings.baud_rate = 115_200;
let serial = serialport::posix::TTYPort::open(Path::new(&args[1]), &settings).expect("Failed to open port");
let mut odrive = ODrive::new(serial);
let mut input_reader = BufReader::new(stdin());
println!("Welcome to the odrive terminal.");
println!("Type a line and press enter to send a message.");
println!("Type !exit to exit.");
let mut line = String::with_capacity(20);
loop {
input_reader.read_line(&mut line).unwrap();
let trimmed = line.trim();
if trimmed != "!exit" {
writeln!(odrive, "{}", trimmed).expect("Failed to send command to odrive!");
if let Some(response) = odrive.read_string().unwrap() {
println!("{}", response);
}
line.clear()
} else {
break;
}
}
}