use std::str::{from_utf8, Utf8Error};
use std::time::Duration;
use hidapi::{HidDevice, HidError};
use crate::{Kind, AjazzError, AjazzInput};
pub fn get_feature_report(device: &HidDevice, report_id: u8, length: usize) -> Result<Vec<u8>, HidError> {
let mut buff = vec![0u8; length];
buff.insert(0, report_id);
device.get_feature_report(buff.as_mut_slice())?;
Ok(buff)
}
pub fn send_feature_report(device: &HidDevice, payload: &[u8]) -> Result<(), HidError> {
device.send_feature_report(payload)
}
pub fn read_data(device: &HidDevice, length: usize, timeout: Option<Duration>) -> Result<Vec<u8>, HidError> {
device.set_blocking_mode(timeout.is_some())?;
let mut buf = vec![0u8; length];
match timeout {
Some(timeout) => device.read_timeout(buf.as_mut_slice(), timeout.as_millis() as i32),
None => device.read(buf.as_mut_slice()),
}?;
Ok(buf)
}
pub fn write_data(device: &HidDevice, payload: &[u8]) -> Result<usize, HidError> {
device.write(payload)
}
pub fn extract_str(bytes: &[u8]) -> Result<String, Utf8Error> {
Ok(from_utf8(bytes)?.replace('\0', "").to_string())
}
pub fn elgato_to_ajazz153(kind: &Kind, key: u8) -> u8 {
if key < kind.key_count() {
[12, 9, 6, 3, 0, 15, 13, 10, 7, 4, 1, 16, 14, 11, 8, 5, 2, 17][key as usize]
} else {
key
}
}
pub fn ajazz153_to_elgato_input(kind: &Kind, key: u8) -> u8 {
if key < kind.key_count() {
[4, 10, 16, 3, 9, 15, 2, 8, 14, 1, 7, 13, 0, 6, 12, 5, 11, 17][key as usize]
} else {
key
}
}
pub fn inverse_key_index(kind: &Kind, key: u8) -> u8 {
if key < kind.key_count() {
kind.key_count() - 1 - key
} else {
key
}
}
pub fn mirabox_extend_packet(kind: &Kind, buf: &mut Vec<u8>) {
let length = if kind.is_ajazz_v2() { 1025 } else { 513 };
buf.extend(vec![0u8; length - buf.len()]);
}
pub fn read_button_states(kind: &Kind, states: &[u8]) -> Vec<bool> {
if states[0] == 0 {
return vec![];
}
let mut bools = vec![];
for i in 0..kind.key_count() {
bools.push(states[(i + 1) as usize] != 0);
}
bools
}
pub fn read_encoder_input(kind: &Kind, data: &[u8]) -> Result<AjazzInput, AjazzError> {
match &data[4] {
0x0 => Ok(AjazzInput::EncoderStateChange(data[5..5 + kind.encoder_count() as usize].iter().map(|s| *s != 0).collect())),
0x1 => Ok(AjazzInput::EncoderTwist(
data[5..5 + kind.encoder_count() as usize].iter().map(|s| i8::from_le_bytes([*s])).collect(),
)),
_ => Err(AjazzError::BadData),
}
}
pub fn ajazz03_read_input(kind: &Kind, input: u8, state: u8) -> Result<AjazzInput, AjazzError> {
match input {
(0..=6) | 0x25 | 0x30 | 0x31 => ajazz03_read_button_press(kind, input, state),
0x90 | 0x91 | 0x50 | 0x51 | 0x60 | 0x61 => ajazz03_read_encoder_value(kind, input),
0x33..=0x35 => ajazz03_read_encoder_press(kind, input, state),
_ => Err(AjazzError::BadData),
}
}
fn ajazz03_read_button_press(kind: &Kind, input: u8, state: u8) -> Result<AjazzInput, AjazzError> {
let mut button_states = vec![0x01];
button_states.extend(vec![0u8; (kind.key_count() + 1) as usize]);
if input == 0 {
return Ok(AjazzInput::ButtonStateChange(read_button_states(kind, &button_states)));
}
let pressed_index: usize = match input {
(1..=6) => input as usize,
0x25 => 7,
0x30 => 8,
0x31 => 9,
_ => return Err(AjazzError::BadData),
};
button_states[pressed_index] = state;
Ok(AjazzInput::ButtonStateChange(read_button_states(kind, &button_states)))
}
fn ajazz03_read_encoder_value(kind: &Kind, input: u8) -> Result<AjazzInput, AjazzError> {
let mut encoder_values = vec![0i8; kind.encoder_count() as usize];
let (encoder, value): (usize, i8) = match input {
0x90 => (0, -1),
0x91 => (0, 1),
0x50 => (1, -1),
0x51 => (1, 1),
0x60 => (2, -1),
0x61 => (2, 1),
_ => return Err(AjazzError::BadData),
};
encoder_values[encoder] = value;
Ok(AjazzInput::EncoderTwist(encoder_values))
}
fn ajazz03_read_encoder_press(kind: &Kind, input: u8, state: u8) -> Result<AjazzInput, AjazzError> {
let mut encoder_states = vec![false; kind.encoder_count() as usize];
let encoder: usize = match input {
0x33 => 0, 0x35 => 1, 0x34 => 2, _ => return Err(AjazzError::BadData),
};
encoder_states[encoder] = state != 0;
Ok(AjazzInput::EncoderStateChange(encoder_states))
}