Moved HidDevice to struct Pedals. Closes #2

A recent update of hidapi made this possible.
This commit is contained in:
Dennis Potter 2018-11-13 00:21:08 +01:00
parent bef171200e
commit 00070237e7
3 changed files with 67 additions and 64 deletions

View File

@ -5,7 +5,7 @@ authors = ["Dennis <dennis@dennispotter.eu>"]
[dependencies] [dependencies]
structopt = "0.2.10" structopt = "0.2.10"
hidapi = "0.4.1" hidapi = "0.5.0"
users = "0.7" users = "0.7"
colored = "1.6.1" colored = "1.6.1"

View File

@ -10,7 +10,6 @@ mod messages;
#[macro_use] #[macro_use]
extern crate structopt; extern crate structopt;
extern crate hidapi;
extern crate users; extern crate users;
extern crate colored; extern crate colored;
@ -61,8 +60,6 @@ enum Command {
} }
fn main() { fn main() {
let mut pedals = pedal_operations::Pedals::new();
let opt = Opt::from_args(); let opt = Opt::from_args();
welcome("footswitch-rs, Dennis Potter <dennis@dennispotter.eu>"); welcome("footswitch-rs, Dennis Potter <dennis@dennispotter.eu>");
@ -76,47 +73,8 @@ fn main() {
goodbye(); goodbye();
} }
// Open device let mut pedals = pedal_operations::Pedals::new();
// This is the reason, the device is not part of the struct: https://github.com/Osspial/hidapi-rs/issues/16
// Maybe this can be fixed, as soon as this is merged into the crate: https://github.com/Osspial/hidapi-rs/pull/12
let vld_dev = [
(0x0c45u16, 0x7403u16),
(0x0c45 , 0x7404),
(0x413d , 0x2107)
];
info!("Initializing HID object. This can take a moment.");
let api = match hidapi::HidApi::new() {
Ok(res) => {
info!("Succesfully initialized HID object.");
res
},
Err(_) => {
error!("Could not initialize HID object.")
},
};
let mut dev_path = String::new();
for device in &api.devices() {
for val in vld_dev.iter() {
if *val == (device.vendor_id, device.product_id) && device.interface_number == 1 {
info!("Found device {:x}:{:x} ({})", device.vendor_id, device.product_id, device.path);
dev_path = device.path.clone();
}
}
}
let dev = match api.open_path(dev_path.as_str()) {
Ok(res) => {
info!("Succesfully opened device.");
res
},
Err(_) => {
error!("Could not open device. Make sure your device is connected. Maybe try to reconnect it.")
},
};
// All options that need the device to be open // All options that need the device to be open
@ -160,13 +118,13 @@ fn main() {
} }
// Since we ran the Write command without any errors, we are now writing everything // Since we ran the Write command without any errors, we are now writing everything
pedals.write_pedals(& dev); pedals.write_pedals();
info!("Successfully wrote everything to footpedal!"); info!("Successfully wrote everything to footpedal!");
info!("The current state of the device is shown below."); info!("The current state of the device is shown below.");
// Show user current state of pedal // Show user current state of pedal
pedals.read_pedals(&dev, vec![0,1,2]); pedals.read_pedals(vec![0,1,2]);
goodbye(); goodbye();
@ -177,10 +135,10 @@ fn main() {
} }
if all_var { if all_var {
pedals.read_pedals(&dev, vec![0,1,2]); pedals.read_pedals(vec![0,1,2]);
} }
else if ped_list.len() > 0 { else if ped_list.len() > 0 {
pedals.read_pedals(&dev, ped_list) pedals.read_pedals(ped_list)
} }
else { else {
error!("You did not specify any command. Run './footswitch-rs read --help' for more information"); error!("You did not specify any command. Run './footswitch-rs read --help' for more information");

View File

@ -4,6 +4,7 @@
extern crate hidapi; extern crate hidapi;
use std::process; use std::process;
use std::ffi::CString;
use colored::*; use colored::*;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -36,12 +37,55 @@ pub struct PedalsData {
} }
pub struct Pedals { pub struct Pedals {
dev:hidapi::HidDevice,
start: [u8; 8], start: [u8; 8],
ped_data: Vec<PedalsData>, ped_data: Vec<PedalsData>,
} }
impl Pedals { impl Pedals {
pub fn new() -> Pedals { pub fn new() -> Pedals {
// Open device
let vld_dev = [
(0x0c45u16, 0x7403u16),
(0x0c45 , 0x7404),
(0x413d , 0x2107)
];
info!("Initializing HID object. This can take a moment.");
let api = match hidapi::HidApi::new() {
Ok(res) => {
info!("Successfully initialized HID object.");
res
},
Err(_) => {
error!("Could not initialize HID object.")
},
};
let mut dev_path = CString::new("").unwrap();
for device in api.devices() {
for val in vld_dev.iter() {
if *val == (device.vendor_id, device.product_id) && device.interface_number == 1 {
info!("Found device {:x}:{:x} ({:#?})", device.vendor_id, device.product_id, device.path);
dev_path = device.path.clone();
}
}
}
// Moved this out of loop, because of error of "possibly uninitialized `dev`. Don't try to move it in the loop.
let dev = match api.open_path(&dev_path) {
Ok(res) => {
info!("Successfully opened device.");
res
},
Err(_) => {
error!("Could not open device. Make sure your device is connected. Maybe try to reconnect it.")
},
};
// Prepare variables // Prepare variables
let start = [0x01u8, 0x80, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00]; let start = [0x01u8, 0x80, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00];
@ -54,6 +98,7 @@ impl Pedals {
// Initialize actual object // Initialize actual object
Pedals { Pedals {
dev: dev,
start: start, start: start,
ped_data: vec![ ped_data: vec![
@ -75,23 +120,23 @@ impl Pedals {
} }
} }
pub fn read_pedal(&self, dev: & hidapi::HidDevice, ped:& u8) -> [u8; 8] { pub fn read_pedal(&self, ped:& u8) -> [u8; 8] {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
let mut query = [0x01u8, 0x82, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00]; let mut query = [0x01u8, 0x82, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00];
query[3] += ped; query[3] += ped;
// Write query to device // Write query to device
dev.write(&query).unwrap(); self.dev.write(&query).unwrap();
// Read answer // Read answer
dev.read(&mut buf[..]).unwrap(); self.dev.read(&mut buf[..]).unwrap();
buf buf
} }
/// Read the current values of the pedals /// Read the current values of the pedals
pub fn read_pedals(&self, dev: & hidapi::HidDevice, peds: Vec<u8>) { pub fn read_pedals(&self, peds: Vec<u8>) {
let total_width = 55 as usize; let total_width = 55 as usize;
// Check if passed pedal number is valid // Check if passed pedal number is valid
@ -109,14 +154,14 @@ impl Pedals {
// Read and print keys // Read and print keys
for (i, ped) in peds.iter().enumerate() { for (i, ped) in peds.iter().enumerate() {
// Read value from pedal and directly translate it to a key // Read value from pedal and directly translate it to a key
let mut key_value = self.read_pedal(dev, ped); let mut key_value = self.read_pedal(ped);
let key_name_option = match Type::u8_to_enum(key_value[1]) { let key_name_option = match Type::u8_to_enum(key_value[1]) {
Some(Type::Unconfigured) => None, Some(Type::Unconfigured) => None,
Some(Type::Key) => key_operations::print_key(&key_value), Some(Type::Key) => key_operations::print_key(&key_value),
Some(Type::Mouse) => key_operations::print_mousebutton(&key_value), Some(Type::Mouse) => key_operations::print_mousebutton(&key_value),
Some(Type::MouseKey) => key_operations::print_mouse_key(&key_value), Some(Type::MouseKey) => key_operations::print_mouse_key(&key_value),
Some(Type::String) => self.print_string(dev, & mut key_value), Some(Type::String) => self.print_string(& mut key_value),
None => error!("The key type which was returned by the pedal was invalid!") None => error!("The key type which was returned by the pedal was invalid!")
}; };
@ -178,9 +223,9 @@ impl Pedals {
} }
} }
fn write_pedal(&self, dev: & hidapi::HidDevice, ped:usize) { fn write_pedal(&self, ped:usize) {
// First, write header // First, write header
dev.write(&self.ped_data[ped].header).unwrap(); self.dev.write(&self.ped_data[ped].header).unwrap();
// Write data to device in 8 byte chunks // Write data to device in 8 byte chunks
let mut up:usize = 0; let mut up:usize = 0;
@ -191,21 +236,21 @@ impl Pedals {
up = 8 * (i + 1) as usize; up = 8 * (i + 1) as usize;
// Write to device // Write to device
dev.write(&self.ped_data[ped].data[low..up]).unwrap(); self.dev.write(&self.ped_data[ped].data[low..up]).unwrap();
} }
// Write remaining values to device // Write remaining values to device
if self.ped_data[ped].length % 8 > 0 { if self.ped_data[ped].length % 8 > 0 {
dev.write(&self.ped_data[ped].data[up..(self.ped_data[ped].length as usize)]).unwrap(); self.dev.write(&self.ped_data[ped].data[up..(self.ped_data[ped].length as usize)]).unwrap();
} }
} }
/// This method writes all data from Pedals.peddata to the device /// This method writes all data from Pedals.peddata to the device
pub fn write_pedals(&self, dev: & hidapi::HidDevice) { pub fn write_pedals(&self) {
dev.write(&self.start).unwrap(); self.dev.write(&self.start).unwrap();
for (i, _pedal) in self.ped_data.iter().enumerate() { for (i, _pedal) in self.ped_data.iter().enumerate() {
self.write_pedal(dev, i) self.write_pedal(i)
} }
} }
@ -223,7 +268,7 @@ impl Pedals {
pub fn set_modifier(& mut self, ped:usize, modifier:&str) { pub fn set_modifier(& mut self, ped:usize, modifier:&str) {
let modifier = match key_operations::Modifier::str_to_enum(modifier) { let modifier = match key_operations::Modifier::str_to_enum(modifier) {
Some(x) => x, Some(x) => x,
None => error!("Unkown modifier! Please use one of the following: ctrl, shift, alt, win."), None => error!("Unknown modifier! Please use one of the following: ctrl, shift, alt, win."),
}; };
self.set_type(ped, Type::Key); self.set_type(ped, Type::Key);
@ -271,7 +316,7 @@ impl Pedals {
self.ped_data[ped].data[direction] = value_u8; self.ped_data[ped].data[direction] = value_u8;
} }
pub fn print_string(&self, dev: & hidapi::HidDevice, response: & mut [u8]) -> Option<String> { pub fn print_string(&self, response: & mut [u8]) -> Option<String> {
let mut string = String::new(); let mut string = String::new();
let mut len = response[0] - 2; let mut len = response[0] - 2;
let mut ind = 2; let mut ind = 2;
@ -279,7 +324,7 @@ impl Pedals {
while len > 0 { while len > 0 {
if ind == 8 { if ind == 8 {
dev.read(&mut response[..]).unwrap(); self.dev.read(&mut response[..]).unwrap();
ind = 0; ind = 0;
} }