Finished read_pedals() function and made listkeys an Option

This commit is contained in:
Dennis Potter 2018-07-11 12:02:10 +02:00
parent ad132db462
commit a8169788c5

View File

@ -11,6 +11,7 @@ extern crate users;
use std::path::PathBuf; use std::path::PathBuf;
use std::process;
use structopt::StructOpt; use structopt::StructOpt;
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
@ -22,7 +23,7 @@ struct Opt {
/// Prints a table of all keys with <listkeys> rows /// Prints a table of all keys with <listkeys> rows
#[structopt(short = "l", long = "listkeys")] #[structopt(short = "l", long = "listkeys")]
listkeys: usize, listkeys: Option<usize>,
/// Select pedal (left: 1, middle: 2, right: 3) /// Select pedal (left: 1, middle: 2, right: 3)
#[structopt(short = "p", long = "pedal", parse(from_os_str))] #[structopt(short = "p", long = "pedal", parse(from_os_str))]
@ -35,40 +36,40 @@ fn main() {
let opt = Opt::from_args(); let opt = Opt::from_args();
// All options that don't need the device to be open
// Print all keys and exit application
if let Some(x) = opt.listkeys {
key_operations::print_key_map(x);
process::exit(0);
}
// Open device
let vld_dev = [ let vld_dev = [
(0x0c45u16, 0x7403u16), (0x0c45u16, 0x7403u16),
(0x0c45 , 0x7404), (0x0c45 , 0x7404),
(0x413d , 0x2107) (0x413d , 0x2107)
]; ];
let api = hidapi::HidApi::new().expect("Hidapi init failed!");
let mut dev_path = String::new();
for device in &api.devices() {
//println!("{}:{}, {}, {}", device.vendor_id, device.product_id, device.interface_number, device.path);
for val in vld_dev.iter() {
if *val == (device.vendor_id, device.product_id) && device.interface_number == 1 {
println!("Found device {:x}:{:x} ({})", device.vendor_id, device.product_id, device.path);
dev_path = device.path.clone();
}
}
}
// let api = hidapi::HidApi::new().expect("Hidapi init failed!"); let dev = api.open_path(dev_path.as_str()).unwrap();
// let mut dev_path = String::new(); println!("Succesfully opened device.");
//
// for device in &api.devices() {
// //println!("{}:{}, {}, {}", device.vendor_id, device.product_id, device.interface_number, device.path);
// for val in vld_dev.iter() {
// if *val == (device.vendor_id, device.product_id) && device.interface_number == 1 {
// println!("Found device {:x}:{:x} ({})", device.vendor_id, device.product_id, device.path);
// dev_path = device.path.clone();
// }
// }
// }
//
//
// let dev = api.open_path(dev_path.as_str()).unwrap();
// println!("Succesfully opened device.");
//
// //ToDo: Replace by match
// if opt.read == true {
// // Read data from device
// read_pedals(& dev);
// }
if opt.listkeys > 0 { // All options that need the device to be open
println!("{:?}", opt.listkeys); if opt.read {
key_operations::print_key_map(opt.listkeys); read_pedals(& dev);
} }
} }
@ -81,15 +82,34 @@ fn check_sudo() {
/// Read the current values of the pedals /// Read the current values of the pedals
fn read_pedals(dev: & hidapi::HidDevice) { fn read_pedals(dev: & hidapi::HidDevice) {
let query = [0x01u8, 0x82, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00]; let column_width = 15;
println!("{:?}", query); let total_width = (column_width+3)*3;
let res = dev.write(&query).expect("test");
println!("Wrote: {:?} byte(s)", res);
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
let mut query = [0x01u8, 0x82, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00];
let res = dev.read(&mut buf[..]).unwrap(); // Print header
println!("Read: {:?}", &buf[..res]); println!(" {}", "-".repeat(total_width));
println!("{name:^width$}", name = "Programmed Keys", width = total_width - 2);
println!(" {}", "-".repeat(total_width));
// Read and print keys
for i in 0..3 {
query[3] = i + 1;
dev.write(&query).unwrap();
let res = dev.read(&mut buf[..]).unwrap();
let key_name = match key_operations::print_key(&buf[..res]) {
Some(key) => key,
None => "< None >".to_string(),
};
print!("{name:^-width$}", name = key_name, width = column_width);
}
// Print simple footer
println!("\n {}", "-".repeat(total_width));
} }