diff --git a/Cargo.toml b/Cargo.toml index a20d9e9..039ec29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,10 @@ [workspace] resolver = "2" -members = [ - "firmware", "protocol", +members = [ + "hostsoftware", + "firmware", + "protocol", ] diff --git a/firmware/src/main.rs b/firmware/src/main.rs index 7dce88c..ddc2b6c 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -378,7 +378,7 @@ mod app { loop { if let Some((msg, rest)) = buffer.split_once(|&x| x == 0) { let mut message = [0u8; 128]; - message.clone_from_slice(msg); + message[0..msg.len()].clone_from_slice(msg); let host_msg = from_bytes_cobs::(&mut message); match host_msg { diff --git a/hostsoftware/Cargo.toml b/hostsoftware/Cargo.toml new file mode 100644 index 0000000..bd9d380 --- /dev/null +++ b/hostsoftware/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "controller" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cheapsdo-protocol = { path = "../protocol" } +postcard = {version = "1.0.8", features = ["use-std"]} +serialport = "4.3.0" diff --git a/hostsoftware/src/main.rs b/hostsoftware/src/main.rs new file mode 100644 index 0000000..5e0d356 --- /dev/null +++ b/hostsoftware/src/main.rs @@ -0,0 +1,38 @@ +use std::io; +use std::{io::Write, thread}; +use std::time::Duration; + +use cheapsdo_protocol::*; +use postcard::{to_stdvec_cobs, from_bytes_cobs}; + +fn main() { + let mut port = serialport::new("/dev/ttyACM1", 115_200) + .timeout(Duration::from_millis(10)) + .open().expect("Failed to open port"); + + + + loop { + let host_msg = HostMessage::RequestStatus; + let msg_bytes = to_stdvec_cobs(&host_msg).unwrap(); + + port.write_all(&msg_bytes).unwrap(); + + let mut serial_buf: Vec = vec![0; 128]; + match port.read(serial_buf.as_mut_slice()) { + Ok(t) => { + serial_buf.truncate(t); + println!("Data: {:?}", serial_buf); + + let dev_msg = from_bytes_cobs::(&mut serial_buf).unwrap(); + println!("Message: {:?}", dev_msg); + } + Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), + Err(e) => eprintln!("{:?}", e), + } + + + thread::sleep(Duration::from_millis(1000)); + } + +}