Created new functions for handling commands

This commit is contained in:
2021-05-08 22:15:54 +02:00
parent d2d324485e
commit 5a6131d0c3
3 changed files with 106 additions and 27 deletions

View File

@@ -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
}
}
match dev.send_feature_report(usb_buf) {
Ok(_) {
()
},
Err(_) {
idx = 0;
if err_cnt == 0 {
return ();
}
err_cnt -= 1;
()
}
}
},
Err(e) => {
e
}
}
})
}
}

53
src/controllers/Usb.rs Normal file
View File

@@ -0,0 +1,53 @@
/*****************************************************************************
* Module: UsbDevice
* Created On: Sat 08 May 2021 08:18:46 PM CEST
* Last Modified: Sat 08 May 2021 09:08:34 PM CEST
*
* Driver for DasKeyboard keyboards light
* Jali <jali@orca-central.de>
*****************************************************************************/
use hidapi::{DeviceInfo, HidApi, HidDevice HidResult};
/** Wrapper class for a usb device.
* Provides an instance of the HidApi and
* the device info for a specific device.
*
* This allows opening and closing the device at
* any time.
* This class is local to teh controllers module.
*/
pub struct UsbDevice {
_api: HidApi,
_device_info: DeviceInfo
}
/* implementation for the UsbDevice struct */
impl UsbDevice {
/** Creates a new instance of the UsbDevice
*
* # Arguments
*
* `vendor_id` - A unique id that identifies the vendor of the USB device.
* `product_id` - A unique id that identifies a product from the given vendor.
*
* # Returns
*
* A new instance of USBDevice, or None, if the device does not exist.
*/
pub fn new(vendor_id: u16, product_id: u16) -> HidResult<UsbDevice> {
let api = HidApi::new();
api.and_then(|api: HidResult<HidApi>| api)
.and_then(|api: HidApi| api.device_list())
.find(|device| device.vendor_id() == vendor_id && device.product_id() == product_id)
.and_then(|device| UsbDevice {
_api = api,
_device_info = device
})
}
/** Opens the device and returns a handle to work with. */
pub fn open(&self) -> HidResult<HidDevice> {
return self._device_info.open_device(self._api);
}
}

View File

@@ -2,9 +2,10 @@
* Definition of module controllers
*
* Created On: Fri 07 May 2021 09:19:14 PM CEST
* Last Modified: Fri 07 May 2021 09:19:14 PM CEST
* Last Modified: Sat 08 May 2021 09:09:46 PM CEST
*/
mod DasKeyboard;
pub use DasKeyboard::DasKeyboardController;
mod Usb;