diff options
Diffstat (limited to '')
-rw-r--r-- | src/testbench.rs | 114 |
1 files changed, 82 insertions, 32 deletions
diff --git a/src/testbench.rs b/src/testbench.rs index 5dd18fd..52f5287 100644 --- a/src/testbench.rs +++ b/src/testbench.rs @@ -7,7 +7,7 @@ use std::path::PathBuf; use imgui; use rlua::Lua; -// use serialport; +use serialport; use crate::rusbtmc; @@ -43,15 +43,16 @@ impl Test { #[derive(Default)] pub struct Bench { - // tests window + /* tests window */ lua: Lua, tests: Vec<Test>, tests_path: PathBuf, + /* devices window */ selected_test: Option<usize>, selected_usb_dev: Option<usize>, usb_devices: HashMap<imgui::ImString, rusbtmc::Instrument<rusb::GlobalContext>>, selected_serial_dev: Option<usize>, - // serial_devices: Vec<?> + // serial_devices: HashMap<imgui::ImString, (serialport::SerialPortBuilder, Option<serialport::SerialPort>>, lua_console: Vec<imgui::ImString>, instr_console: Vec<imgui::ImString>, instr_input: imgui::ImString, @@ -103,6 +104,7 @@ impl Bench { return b; } + /// Loads lua tests from tests_path pub fn load_tests(&mut self) -> io::Result<()> { let entries: Result<Vec<PathBuf>, io::Error> = fs::read_dir(&self.tests_path)? .into_iter() @@ -124,6 +126,25 @@ impl Bench { } } + fn get_selected_device<'a>( + index: Option<usize>, + list: &'a mut HashMap<imgui::ImString, rusbtmc::Instrument<rusb::GlobalContext>>, + ) -> Option<&'a mut rusbtmc::Instrument<rusb::GlobalContext>> { + let selected_index = match index { + Some(i) => i, + None => return None, + }; + + for (index, (_name, dev)) in list.iter_mut().enumerate() { + if matches!(selected_index, i if i == index) { + return Some(dev); + } + } + + return None; + } + + /// draws the entire interface pub fn draw(&mut self, _: &mut bool, ui: &mut imgui::Ui) { use imgui::*; @@ -179,7 +200,8 @@ impl Bench { .position([520., 20.], Condition::Appearing); dev_win.build(&ui, || { - // usb devices + /* usb devices */ + // buttons ui.text_wrapped(im_str!("USB Devices")); ui.same_line(0.); if ui.button(im_str!("Refresh"), [0., 0.]) { @@ -199,8 +221,8 @@ impl Bench { let handle = match instr.device.open() { Ok(handle) => handle, - Err(_) => { - // dbg!("failed to get handle"); + Err(e) => { + dbg!("failed to get handle", e); continue; } }; @@ -219,46 +241,74 @@ impl Bench { } } - if self.selected_usb_dev.is_some() { - // search dev - let mut instr = None; - for (index, (_name, dev)) in self.usb_devices.iter_mut().enumerate() { - if matches!(self.selected_usb_dev, Some(i) if i == index) { - instr = Some(dev); - break; - } + if let Some(instr) = Bench::get_selected_device( + self.selected_usb_dev.clone(), + &mut self.usb_devices + ) { + // TODO: handle errors + ui.same_line(0.); + if ui.button(im_str!("Open"), [0., 0.]) { + let _ = dbg!(instr.open()); } - if let Some(instr) = instr { - ui.same_line(0.); - if ui.button(im_str!("Open"), [0., 0.]) { - let _ = dbg!(instr.open()); - } - - ui.same_line(0.); - if ui.button(im_str!("Close"), [0., 0.]) { - let _ = dbg!(instr.close()); - } + ui.same_line(0.); + if ui.button(im_str!("Close"), [0., 0.]) { + let _ = dbg!(instr.close()); + } - ui.same_line(0.); - if ui.button(im_str!("Pulse"), [0., 0.]) { - let _ = dbg!(instr.pulse()); - } + ui.same_line(0.); + if ui.button(im_str!("Pulse"), [0., 0.]) { + let _ = dbg!(instr.pulse()); } } - for (index, (name, _dev)) in self.usb_devices.iter().enumerate() { + ui.separator(); + + // device list + for (index, (name, dev)) in self.usb_devices.iter().enumerate() { let selected = matches!(self.selected_usb_dev, Some(i) if i == index); + let mut label = name.clone(); + if dev.is_connected() { + label.push_str(" (connected)"); + } - if Selectable::new(&name).selected(selected).build(ui) { + if Selectable::new(&label).selected(selected).build(ui) { self.selected_usb_dev = Some(index); } + } - ui.same_line(0.); + ui.separator(); + + // serial devices + /* + ui.text_wrapped(im_str!("Serial Devices")); + ui.same_line(0.); + if ui.button(im_str!("Refresh"), [0., 0.]) { + if let Ok(ports) = serialport::available_ports() { + for port in ports { + + } + } } ui.separator(); - ui.input_text(im_str!("Device"), &mut self.instr_input).build(); + */ + + // interactive device console + ui.input_text(im_str!("Device"), &mut self.instr_input) + .build(); + ui.same_line(0.); + + if let Some(instr) = Bench::get_selected_device( + self.selected_usb_dev, + &mut self.usb_devices + ) { + if ui.button(im_str!("Send"), [0., 0.]) { + let _ = dbg!(instr.write(self.instr_input.to_str())); + self.instr_input.clear(); + } + } + ChildWindow::new("instrument console") .size([0., 0.]) .scrollable(true) |