Added types and append_string command

Application is now able to set the types (Key, Mouse, MouseKey, String),
which are saved in a struct, with a function. Furthermore, the
application can check the returned type of the board with a match.

This commit also implements the append string functionallity.
master
Dennis Potter 4 years ago
parent 6e05947498
commit 897bb28db5
  1. 19
      src/key_operations.rs
  2. 5
      src/main.rs
  3. 143
      src/pedal_operations.rs

@ -300,9 +300,9 @@ pub fn encode_byte(c: &str) -> Option<u8> {
None
}
pub fn decode_byte(u: u8) -> Option<String> {
pub fn decode_byte(u: &u8) -> Option<String> {
for key in KEY_MAP.iter() {
if key.1 == u {
if key.1 == *u {
return Some(key.0.to_string())
}
}
@ -310,11 +310,24 @@ pub fn decode_byte(u: u8) -> Option<String> {
None
}
pub fn encode_string(s: &str) -> Option<Vec<u8> > {
let mut char_vec = Vec::new();
for c in s.chars() {
match encode_byte(&c.to_string()) {
Some(x) => char_vec.push(x),
None => return None,
};
}
Some(char_vec)
}
pub fn print_key(response: &[u8]) -> Option<String> {
let mut key_combo = String::new();
if response[3] != 0 {
if let Some(key_str) = decode_byte(response[3]) {
if let Some(key_str) = decode_byte(&response[3]) {
key_combo.push_str(&key_str);
return Some(key_combo);

@ -128,14 +128,15 @@ fn main() {
for (i, cmd) in cmd_list.iter().enumerate() {
match cmd as &str {
"wr_key" => {
pedals.set_key(i, val_list[i].as_str());
"set_key" => {
pedals.set_key(ped_list[i] as usize, val_list[i].as_str());
}
"del_key" => {
}
"append_key" => {
}
"append_str" => {
pedals.set_string(ped_list[i] as usize, val_list[i].as_str());
}
_ => {
error!("Unkonwn command!");

@ -6,6 +6,28 @@ extern crate hidapi;
use std::process;
use colored::*;
#[derive(Copy, Clone)]
enum Type {
Unconfigured = 0,
Key = 1,
Mouse = 2,
MouseKey = 3,
String = 4
}
impl Type {
fn value(value:u8) -> Option<Type> {
match value {
0 => Some(Type::Unconfigured),
1 => Some(Type::Key),
2 => Some(Type::Mouse),
3 => Some(Type::MouseKey),
4 => Some(Type::String),
_ => None
}
}
}
pub struct PedalsData {
header: [u8; 8],
data: [u8; 48],
@ -14,7 +36,7 @@ pub struct PedalsData {
pub struct Pedals {
start: [u8; 8],
pub ped_data: Vec<PedalsData>,
ped_data: Vec<PedalsData>,
}
impl Pedals {
@ -47,8 +69,7 @@ impl Pedals {
PedalsData {
header: header_2,
data: default_data,
length: 8,
},
length: 8, },
]
}
}
@ -95,7 +116,18 @@ impl Pedals {
// Read and print keys
for i in peds.iter() {
// Read value from pedal and directly translate it to a key
let key_name = match key_operations::print_key(&self.read_pedal(dev, i)) {
let mut key_value = self.read_pedal(dev, i);
let key_name_option = match Type::value(key_value[1]) {
Some(Type::Unconfigured) => None,
Some(Type::Key) => key_operations::print_key(&key_value),
Some(Type::Mouse) => 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),
None => error!("The key type which was returned by the pedal was invalid!")
};
let key_name = match key_name_option {
Some(key) => key,
None => "< None >".to_string(),
};
@ -107,6 +139,47 @@ impl Pedals {
println!("│\n├{}┘", "─".repeat(total_width));
}
/// Sets the type of the function. False (0) if everything went fine, True (1) if
/// an error occurred.
fn set_type(& mut self, ped:usize, typ:Type) {
let set_value = if self.ped_data[ped].data[1] == 0 { true } else { false };
if set_value {
self.ped_data[ped].data[1] = typ as u8;
}
let ret = match typ {
Type::String => {
// If nothing is set, set type to string and length to 2
if set_value {
self.ped_data[ped].length = 2;
}
// Check if pedal type is set to String, otherwise error
self.ped_data[ped].data[1] != Type::String as u8
}
_ => {
let ret;
if self.ped_data[ped].data[1] == Type::String as u8 {
// if type is Key or Mouse, and String is already set, return false
ret = true;
}
else {
// else, set type to new type and return true
self.ped_data[ped].data[1] |= typ as u8;
ret = false;
}
ret
}
};
if ret {
error!("Invalid combination of options!");
}
}
fn write_pedal(&self, dev: & hidapi::HidDevice, ped:usize) {
// First, write header
dev.write(&self.ped_data[ped].header).unwrap();
@ -139,9 +212,9 @@ impl Pedals {
}
pub fn set_key(& mut self, ped:usize, key:&str) {
if let Some(encoded_key) = key_operations::encode_byte(key) {
self.ped_data[ped].data[1] = 1;
self.set_type(ped, Type::Key);
self.ped_data[ped].data[3] = encoded_key;
}
else {
@ -149,4 +222,62 @@ impl Pedals {
}
}
pub fn print_string(&self, dev: & hidapi::HidDevice, response: & mut [u8]) -> Option<String> {
let mut string = String::new();
let mut len = response[0] - 2;
let mut ind = 2;
while len > 0 {
if ind == 8 {
dev.read(&mut response[..]).unwrap();
ind = 0;
}
if let Some(key_str) = key_operations::decode_byte(&response[ind]) {
string.push_str(&key_str[..]);
}
len -= 1;
ind += 1;
}
Some(string)
}
pub fn set_string(& mut self, ped:usize, key:&str) {
self.set_type(ped, Type::String);
if key.len() > 38 {
error!("The size of each string must be smaller than or equal to 38.");
}
let encoded_vector = match key_operations::encode_string(&key) {
Some(x) => x,
None => error!("Could not encode string!"),
};
self.compile_string_data(ped, encoded_vector);
}
fn compile_string_data(& mut self, ped:usize, enc_vec:Vec<u8>) {
let len = enc_vec.len() as u8;
if self.ped_data[ped].length + len > 38 {
error!("The size of the accumulated string must be smaller than or equal to 38.")
}
let start_byte = self.ped_data[ped].length as usize;
for (i, c) in enc_vec.iter().enumerate() {
self.ped_data[ped].data[start_byte + i] = *c;
}
self.ped_data[ped].length += len;
self.ped_data[ped].header[2] = self.ped_data[ped].length;
self.ped_data[ped].data[0] = self.ped_data[ped].length;
}
}

Loading…
Cancel
Save