Added possibility to write and read mouse buttons to device

Furthermore, I did a small refactoring of the naming conventions for the
enum functions. Instead of vague terms liek value and string, they are
now called u8_to_enum, str_to_enum, or enum_to_str.
This commit is contained in:
Dennis Potter 2018-08-06 01:10:44 +02:00
parent a948232b3e
commit 0d55a26626
3 changed files with 89 additions and 22 deletions

View File

@ -284,7 +284,7 @@ pub enum Modifier {
} }
impl Modifier { impl Modifier {
pub fn value(modifier:&str) -> Option<Modifier> { pub fn str_to_enum(modifier:&str) -> Option<Modifier> {
match modifier { match modifier {
"ctrl" => Some(Modifier::Ctrl), "ctrl" => Some(Modifier::Ctrl),
"shift" => Some(Modifier::Shift), "shift" => Some(Modifier::Shift),
@ -294,7 +294,7 @@ impl Modifier {
} }
} }
pub fn string(modifier:Modifier) -> String { pub fn enum_to_string(modifier:Modifier) -> String {
match modifier { match modifier {
Modifier::Ctrl => "ctrl".to_string(), Modifier::Ctrl => "ctrl".to_string(),
Modifier::Shift => "shift".to_string(), Modifier::Shift => "shift".to_string(),
@ -304,13 +304,44 @@ impl Modifier {
} }
} }
enum MouseButton { pub enum MouseButton {
MouseLeft = 1, MouseLeft = 1,
MouseRight = 2, MouseRight = 2,
MouseMiddle = 4, MouseMiddle = 4,
MouseDouble = 8, MouseDouble = 8,
} }
impl MouseButton {
pub fn str_to_enum(mousebutton:&str) -> Option<MouseButton> {
match mousebutton {
"mouse_left" => Some(MouseButton::MouseLeft),
"mouse_right" => Some(MouseButton::MouseRight),
"mouse_middle" => Some(MouseButton::MouseMiddle),
"mouse_double" => Some(MouseButton::MouseDouble),
_ => None,
}
}
pub fn u8_to_enum(mousebutton:u8) -> Option<MouseButton> {
match mousebutton {
1 => Some(MouseButton::MouseLeft),
2 => Some(MouseButton::MouseRight),
3 => Some(MouseButton::MouseMiddle),
4 => Some(MouseButton::MouseDouble),
_ => None,
}
}
pub fn enum_to_string(mousebutton:MouseButton) -> String {
match mousebutton {
MouseButton::MouseLeft => "mouse_left".to_string(),
MouseButton::MouseRight => "mouse_right".to_string(),
MouseButton::MouseMiddle => "mouse_middle".to_string(),
MouseButton::MouseDouble => "mouse_double".to_string(),
}
}
}
pub fn encode_byte(c: &str) -> Option<u8> { pub fn encode_byte(c: &str) -> Option<u8> {
for key in KEY_MAP.iter() { for key in KEY_MAP.iter() {
if key.0 == c { if key.0 == c {
@ -349,19 +380,19 @@ pub fn print_key(response: &[u8]) -> Option<String> {
// Handle modifiers // Handle modifiers
if response[2] & Modifier::Ctrl as u8 != 0 { if response[2] & Modifier::Ctrl as u8 != 0 {
key_combo.push_str(&Modifier::string(Modifier::Ctrl)[..]); key_combo.push_str(&Modifier::enum_to_string(Modifier::Ctrl)[..]);
key_combo.push_str("+"); key_combo.push_str("+");
} }
if response[2] & Modifier::Shift as u8 != 0 { if response[2] & Modifier::Shift as u8 != 0 {
key_combo.push_str(&Modifier::string(Modifier::Shift)[..]); key_combo.push_str(&Modifier::enum_to_string(Modifier::Shift)[..]);
key_combo.push_str("+"); key_combo.push_str("+");
} }
if response[2] & Modifier::Alt as u8 != 0 { if response[2] & Modifier::Alt as u8 != 0 {
key_combo.push_str(&Modifier::string(Modifier::Alt)[..]); key_combo.push_str(&Modifier::enum_to_string(Modifier::Alt)[..]);
key_combo.push_str("+"); key_combo.push_str("+");
} }
if response[2] & Modifier::Win as u8 != 0 { if response[2] & Modifier::Win as u8 != 0 {
key_combo.push_str(&Modifier::string(Modifier::Win)[..]); key_combo.push_str(&Modifier::enum_to_string(Modifier::Win)[..]);
key_combo.push_str("+"); key_combo.push_str("+");
} }
@ -377,6 +408,27 @@ pub fn print_key(response: &[u8]) -> Option<String> {
None None
} }
pub fn print_mousebutton(response: &[u8]) -> Option<String> {
let mut mouse_string = String::new();
match MouseButton::u8_to_enum(response[4]) {
Some(x) => mouse_string.push_str(&MouseButton::enum_to_string(x)[..]),
None => {}
}
let mut 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 mut 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!(" Y = {}", y)[..]);
mouse_string.push_str(&format!(" W = {}", w)[..]);
Some(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

@ -39,7 +39,7 @@ enum Command {
#[structopt(short = "p", long = "pedal")] #[structopt(short = "p", long = "pedal")]
pedal: Vec<u8>, pedal: Vec<u8>,
/// Command to apply. Possible values: [set_key | del_key | append_key | append_str] /// Command to apply. Possible values: [set_key | set_mousebutton | del_key | app_key | app_str | app_mod]
#[structopt(short = "c", long = "command")] #[structopt(short = "c", long = "command")]
command: Vec<String>, command: Vec<String>,
@ -131,14 +131,17 @@ 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" => {
pedals.set_mousebutton(ped_list[i] as usize, val_list[i].as_str());
}
"del_key" => { "del_key" => {
} }
"append_key" => { "app_key" => {
} }
"append_str" => { "app_str" => {
pedals.set_string(ped_list[i] as usize, val_list[i].as_str()); pedals.set_string(ped_list[i] as usize, val_list[i].as_str());
} }
"append_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());
} }
_ => { _ => {
@ -150,7 +153,7 @@ 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(& dev);
info!("Succesfully 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

View File

@ -16,14 +16,15 @@ enum Type {
} }
impl Type { impl Type {
fn value(value:u8) -> Option<Type> { fn u8_to_enum(value:u8) -> Option<Type> {
match value { match value {
0 => Some(Type::Unconfigured), 0 => Some(Type::Unconfigured),
1 => Some(Type::Key), 1 => Some(Type::Key),
2 => Some(Type::Mouse), 2 => Some(Type::Mouse),
3 => Some(Type::MouseKey), 3 => Some(Type::MouseKey),
4 => Some(Type::String), 4 => Some(Type::String),
_ => None 0x81 => Some(Type::Key),
_ => None
} }
} }
} }
@ -111,10 +112,10 @@ impl Pedals {
// 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(dev, ped);
let key_name_option = match Type::value(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_key(&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_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!")
@ -221,7 +222,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::value(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!("Unkown modifier! Please use one of the following: ctrl, shift, alt, win."),
}; };
@ -231,6 +232,17 @@ impl Pedals {
self.ped_data[ped].data[2] |= modifier as u8; self.ped_data[ped].data[2] |= modifier as u8;
} }
pub fn set_mousebutton(& mut self, ped:usize, mousebutton:&str) {
let mousebutton = match key_operations::MouseButton::str_to_enum(mousebutton) {
Some(x) => x,
None => error!("Unkown mousebutton! Please use one of the following: mouse_left, mouse_middle, mouse_right, mouse_double."),
};
self.set_type(ped, Type::Mouse);
self.ped_data[ped].data[4] |= mousebutton as 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;