From 5a6131d0c35ec092b14af6a3687a68a659f3e2f2 Mon Sep 17 00:00:00 2001 From: Jali Date: Sat, 8 May 2021 22:15:54 +0200 Subject: [PATCH] Created new functions for handling commands --- src/controllers/DasKeyboard.rs | 77 ++++++++++++++++++++++------------ src/controllers/Usb.rs | 53 +++++++++++++++++++++++ src/controllers/mod.rs | 3 +- 3 files changed, 106 insertions(+), 27 deletions(-) create mode 100644 src/controllers/Usb.rs diff --git a/src/controllers/DasKeyboard.rs b/src/controllers/DasKeyboard.rs index 9a60aab..6264f3a 100644 --- a/src/controllers/DasKeyboard.rs +++ b/src/controllers/DasKeyboard.rs @@ -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 *****************************************************************************/ -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 { - 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 {} - 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; + () + } + } + } + }) } } diff --git a/src/controllers/Usb.rs b/src/controllers/Usb.rs new file mode 100644 index 0000000..a1e11ac --- /dev/null +++ b/src/controllers/Usb.rs @@ -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 + *****************************************************************************/ + +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 { + let api = HidApi::new(); + api.and_then(|api: HidResult| 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 { + return self._device_info.open_device(self._api); + } +} diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index 5540f36..ae886da 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -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;