It is now possible to add mouse directions.

Furthermore, it is now possible to read keys, mousedirections, and
mousepedals on the same pedal with print_mouse_key(1)
This commit is contained in:
Dennis Potter 2018-08-07 00:48:16 +02:00
parent 0d55a26626
commit 6fd43a14ed
3 changed files with 66 additions and 7 deletions

View File

@ -408,7 +408,6 @@ pub fn print_key(response: &[u8]) -> Option<String> {
None None
} }
pub fn print_mousebutton(response: &[u8]) -> Option<String> { pub fn print_mousebutton(response: &[u8]) -> Option<String> {
let mut mouse_string = String::new(); let mut mouse_string = String::new();
@ -417,9 +416,9 @@ pub fn print_mousebutton(response: &[u8]) -> Option<String> {
None => {} None => {}
} }
let mut x:i16 = if response[5] > 127 { response[5] as i16 - 256 } else { response[5] as i16 }; let x:i16 = if response[5] > 127 { response[5] as i16 - 256 } else { response[5] as i16 };
let mut y:i16 = if response[6] > 127 { response[6] as i16 - 256 } else { response[6] as i16 }; let y:i16 = if response[6] > 127 { response[6] as i16 - 256 } else { response[6] as i16 };
let mut w:i16 = if response[7] > 127 { response[7] as i16 - 256 } else { response[7] as i16 }; let w:i16 = if response[7] > 127 { response[7] as i16 - 256 } else { response[7] as i16 };
mouse_string.push_str(&format!(" X = {}", x)[..]); mouse_string.push_str(&format!(" X = {}", x)[..]);
mouse_string.push_str(&format!(" Y = {}", y)[..]); mouse_string.push_str(&format!(" Y = {}", y)[..]);
@ -429,6 +428,28 @@ pub fn print_mousebutton(response: &[u8]) -> Option<String> {
} }
pub fn print_mouse_key(response: &[u8]) -> Option<String> {
let mut key_mouse_string = String::new();
match print_key(&response) {
Some(x) => {
key_mouse_string.push_str(&x[..]);
key_mouse_string.push_str(" ");
},
None => {}
}
match print_mousebutton(&response) {
Some(x) => {
key_mouse_string.push_str(&x[..]);
key_mouse_string.push_str(" ");
},
None => {}
}
Some(key_mouse_string)
}
pub fn print_key_map(rows: usize) { pub fn print_key_map(rows: usize) {
print!("{}", " ‖ Key Name ¦ Value ".repeat(rows)); print!("{}", " ‖ Key Name ¦ Value ".repeat(rows));
println!(""); println!("");

View File

@ -131,7 +131,7 @@ fn main() {
"set_key" => { "set_key" => {
pedals.set_key(ped_list[i] as usize, val_list[i].as_str()); pedals.set_key(ped_list[i] as usize, val_list[i].as_str());
} }
"set_mouse" => { "set_mousebutton" => {
pedals.set_mousebutton(ped_list[i] as usize, val_list[i].as_str()); pedals.set_mousebutton(ped_list[i] as usize, val_list[i].as_str());
} }
"del_key" => { "del_key" => {
@ -144,6 +144,15 @@ fn main() {
"app_mod" => { "app_mod" => {
pedals.set_modifier(ped_list[i] as usize, val_list[i].as_str()); pedals.set_modifier(ped_list[i] as usize, val_list[i].as_str());
} }
"set_x" => {
pedals.set_mouse_xyw(ped_list[i] as usize, val_list[i].as_str(), 5)
}
"set_y" => {
pedals.set_mouse_xyw(ped_list[i] as usize, val_list[i].as_str(), 6)
}
"set_w" => {
pedals.set_mouse_xyw(ped_list[i] as usize, val_list[i].as_str(), 7)
}
_ => { _ => {
error!("Unknown command!"); error!("Unknown command!");
} }

View File

@ -116,7 +116,7 @@ impl Pedals {
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_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(dev, & 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!")
}; };
@ -235,7 +235,7 @@ impl Pedals {
pub fn set_mousebutton(& mut self, ped:usize, mousebutton:&str) { pub fn set_mousebutton(& mut self, ped:usize, mousebutton:&str) {
let mousebutton = match key_operations::MouseButton::str_to_enum(mousebutton) { let mousebutton = match key_operations::MouseButton::str_to_enum(mousebutton) {
Some(x) => x, Some(x) => x,
None => error!("Unkown mousebutton! Please use one of the following: mouse_left, mouse_middle, mouse_right, mouse_double."), None => error!("Unknown mousebutton! Please use one of the following: mouse_left, mouse_middle, mouse_right, mouse_double."),
}; };
self.set_type(ped, Type::Mouse); self.set_type(ped, Type::Mouse);
@ -243,6 +243,35 @@ impl Pedals {
self.ped_data[ped].data[4] |= mousebutton as u8; self.ped_data[ped].data[4] |= mousebutton as u8;
} }
pub fn set_mouse_xyw(& mut self, ped:usize, value:&str, direction:usize) {
// The values of the directions match the array index of ped_data[].data[]
// X = 5
// Y = 6
// W = 7
// Translate value passed by user to integer
let value_i8 = match value.parse::<i8>() {
Ok(x) => x,
Err(x) => {
error!("The value of {} ({}) must be in [-128, 127]! Message: {}.", direction, value, x)
}
};
// Translate to u8
let mut value_u8 = value_i8 as u8;
// Check if mouse wheel movement is smaller than 0, if so, add 256
if (direction == 7) & (value_i8 < 0) {
value_u8 += value_i8 as u8 + 256;
}
// Set Mouse Type
self.set_type(ped, Type::Mouse);
// Actually write data
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, dev: & hidapi::HidDevice, 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;