Created new functions for handling commands
This commit is contained in:
@@ -1,23 +1,24 @@
|
||||
/*****************************************************************************
|
||||
* Module: DasKeyboard
|
||||
* Created On: Fri 07 May 2021 10:17:24 PM CEST
|
||||
* Last Modified: Fri 07 May 2021 11:28:01 PM CEST
|
||||
* Last Modified: Sat 08 May 2021 10:14:52 PM CEST
|
||||
*
|
||||
* Driver for DasKeyboard keyboards light
|
||||
* Jali <jali@orca-central.de>
|
||||
*****************************************************************************/
|
||||
|
||||
use std::ffi::{CStr, CString};
|
||||
use hidapi::{DeviceInfo, HidApi, HidError, HidResult};
|
||||
use std::ffi::{CStr, CString};
|
||||
use Usb::UsbDevice;
|
||||
|
||||
/** Driver for DasKeyboard RGB keyboards
|
||||
* lighting controller
|
||||
*/
|
||||
pub struct DasKeyboardController {
|
||||
dev: DeviceInfo,
|
||||
location: CString,
|
||||
use_traditional_send_data: bool,
|
||||
version: String,
|
||||
_dev: UsbDevice,
|
||||
_location: CString,
|
||||
_use_traditional_send_data: bool,
|
||||
_version: String,
|
||||
}
|
||||
|
||||
/** Implementation of the controller. */
|
||||
@@ -31,28 +32,52 @@ impl DasKeyboardController {
|
||||
* * `vendor_id` - The unique vendor/manufactorer id of the device
|
||||
* * `product_id` - The unique product id of the device.
|
||||
*/
|
||||
pub fn new(vendor_id: u16, product_id: u16) -> HidResult<DasKeyboardController> {
|
||||
match HidApi::new() {
|
||||
Ok(api) => {
|
||||
let device = api.device_list()
|
||||
.find(d => d.vendor_id() == vendor_id && d.product_id() == product_id);
|
||||
pub fn new(vendor_id: u16, product_id: u16) -> HidResult<DasKeyboardController> {}
|
||||
|
||||
match device {
|
||||
Some(d) => {
|
||||
let c = DasKeyboardController {
|
||||
dev = d,
|
||||
location = d.path()
|
||||
};
|
||||
Ok(c)
|
||||
},
|
||||
None => {
|
||||
Err(HidApiError("Could not open device"))
|
||||
/** Send a command to the HID controller in the traditional way,
|
||||
* sending 8 bytes per call.
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `api` - Reference to the HidApi object.
|
||||
* * `device_info` - Reference to the device to write to.
|
||||
*/
|
||||
fn send_data_traditional(usb_device: &UsbDevice, data: &[u8]) {
|
||||
// traditional send_data (split into chunks of 8 bytes)
|
||||
let mut usb_buf = [0u8; 9];
|
||||
let err_cnt = 3;
|
||||
let chk_sum = 0;
|
||||
|
||||
usb_device.open().and_then(|dev| {
|
||||
for idx in (1..data.len() + 1).step_by(7) {
|
||||
usb_buf[0] = 1;
|
||||
for fld_idx in (1..7) {
|
||||
let tmp_idx = idx + fld_idx -1;
|
||||
if tmp_idx < length {
|
||||
usb_buf[fld_idx] = data[tmp_idx];
|
||||
chk_sum ^= data[tmp_idx];
|
||||
}
|
||||
else if tmp_idx == data.len() {
|
||||
usb_buf[fld_idx] = chk_sum;
|
||||
}
|
||||
else {
|
||||
usb_buf[fld_idx] = 0
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
e
|
||||
}
|
||||
}
|
||||
match dev.send_feature_report(usb_buf) {
|
||||
Ok(_) {
|
||||
()
|
||||
},
|
||||
Err(_) {
|
||||
idx = 0;
|
||||
if err_cnt == 0 {
|
||||
return ();
|
||||
}
|
||||
err_cnt -= 1;
|
||||
()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user