diff --git a/src/main.rs b/src/main.rs index bba46ac..b98f27b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ mod xm; mod hw; -use xm::command::{send_command, XMCommand}; +use text_io::scan; +use xm::packet::XMPacket; +use xm::command::{XMCommand, send_command}; use hw::serial::xm_pcr_serial; use std::time::Duration; -use std::io; +use std::io::{self, Write}; fn main() -> Result<(), io::Error> { let port_name: &str = "/dev/ttyUSB0"; // Adjust as per your system @@ -19,14 +21,77 @@ fn main() -> Result<(), io::Error> { } }; - match send_command(&mut *port, XMCommand::get_radio_id(), timeout) { - Ok(serial_number_str) => { - println!("Received valid XMRadioID: {}", serial_number_str); + println!("Select the command you want to send: \n"); + println!("1. Power Radio On"); + println!("2. Power Radio Off"); + println!("3. Select Channel"); + println!("4. Get Radio ID"); + println!("0. Exit"); + + let a: i32; + scan!("{}", a); + + match a { + 1 => { + match send_command(&mut *port, XMCommand::power_on(), timeout, false) { + Ok(_) => { + println!("Power on command sent."); + }, + Err(e) => { + eprintln!("Error: {}", e); + } + } }, - Err(e) => { - eprintln!("Error: {}", e); + 2 => { + match send_command(&mut *port, XMCommand::power_off(), timeout, false) { + Ok(_) => { + println!("Power off command sent."); + }, + Err(e) => { + eprintln!("Error: {}", e); + } + } + }, + 3 => { + println!("Enter the channel number (0-255): "); + let channel: u8; + scan!("{}", channel); + + match send_command(&mut *port, XMCommand::select_channel(channel), timeout, false) { + Ok(_) => { + println!("Channel selection command sent."); + }, + Err(e) => { + eprintln!("Error: {}", e); + } + } + }, + 4 => { + match send_command(&mut *port, XMCommand::get_radio_id(), timeout, true) { + Ok(Some(serial_number_str)) => { + println!("Received valid XMRadioID: {}", serial_number_str); + }, + Ok(None) => { + println!("No response expected."); + }, + Err(e) => { + eprintln!("Error: {}", e); + } + } + }, + 0 => { + return Ok(()); + }, + _ => { + eprintln!("Invalid command."); + return Ok(()); } } + // Example of sending a command that expects a response + + + // Example of sending a command that does not expect a response + Ok(()) } diff --git a/src/xm/command.rs b/src/xm/command.rs index 2bae82b..3e18342 100644 --- a/src/xm/command.rs +++ b/src/xm/command.rs @@ -1,10 +1,9 @@ -use serialport::SerialPort; -use std::thread::sleep; -use std::time::Duration; use std::io; - -use super::packet::XMPacket; -use super::radio_id::XMRadioID; +use std::time::Duration; +use std::thread::sleep; +use serialport::SerialPort; +use crate::xm::packet::XMPacket; +use crate::xm::radio_id::XMRadioID; pub struct XMCommand; @@ -27,12 +26,16 @@ impl XMCommand { } } -pub fn send_command(port: &mut dyn SerialPort, command: XMPacket, timeout: Duration) -> Result { - loop { - // Send the command - port.write_all(&command.to_bytes())?; - port.flush()?; +pub fn send_command(port: &mut dyn SerialPort, command: XMPacket, timeout: Duration, expect_response: bool) -> Result, io::Error> { + // Send the command + port.write_all(&command.to_bytes())?; + port.flush()?; + if (!expect_response) { + return Ok(None); // Return immediately if no response is expected + } + + loop { // Read the response from the serial port let mut response: Vec = vec![0; 1024]; // Buffer for response match port.read(&mut response) { @@ -48,7 +51,7 @@ pub fn send_command(port: &mut dyn SerialPort, command: XMPacket, timeout: Durat Ok(radio_id) => { let serial_number_bytes = radio_id.get_radio_id(); let serial_number_str = String::from_utf8_lossy(serial_number_bytes).to_string(); - return Ok(serial_number_str); // Return the serial number string + return Ok(Some(serial_number_str)); // Return the serial number string }, Err(e) => { eprintln!("Error parsing XMRadioID: {}", e);