From d2d324485ebf319d348b38d90ed9b530a94d5da7 Mon Sep 17 00:00:00 2001 From: Jali Date: Fri, 7 May 2021 23:28:37 +0200 Subject: [PATCH] Created a controller for the keyboard --- src/controllers/DasKeyboard.rs | 58 ++++++++++++++++++++++ src/controllers/mod.rs | 10 ++++ src/main.rs | 88 +++++++--------------------------- 3 files changed, 86 insertions(+), 70 deletions(-) create mode 100644 src/controllers/DasKeyboard.rs create mode 100644 src/controllers/mod.rs diff --git a/src/controllers/DasKeyboard.rs b/src/controllers/DasKeyboard.rs new file mode 100644 index 0000000..9a60aab --- /dev/null +++ b/src/controllers/DasKeyboard.rs @@ -0,0 +1,58 @@ +/***************************************************************************** + * Module: DasKeyboard + * Created On: Fri 07 May 2021 10:17:24 PM CEST + * Last Modified: Fri 07 May 2021 11:28:01 PM CEST + * + * Driver for DasKeyboard keyboards light + * Jali + *****************************************************************************/ + +use std::ffi::{CStr, CString}; +use hidapi::{DeviceInfo, HidApi, HidError, HidResult}; + +/** Driver for DasKeyboard RGB keyboards + * lighting controller + */ +pub struct DasKeyboardController { + dev: DeviceInfo, + location: CString, + use_traditional_send_data: bool, + version: String, +} + +/** Implementation of the controller. */ +impl DasKeyboardController { + /** + * Creates a new instance of ```DasKeyboardController```, which is + * bound to a currently connected device. + * + * # Arguments + * + * * `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); + + match device { + Some(d) => { + let c = DasKeyboardController { + dev = d, + location = d.path() + }; + Ok(c) + }, + None => { + Err(HidApiError("Could not open device")) + } + } + }, + Err(e) => { + e + } + } + } +} diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs new file mode 100644 index 0000000..5540f36 --- /dev/null +++ b/src/controllers/mod.rs @@ -0,0 +1,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 + */ + +mod DasKeyboard; +pub use DasKeyboard::DasKeyboardController; + diff --git a/src/main.rs b/src/main.rs index cc5b478..ee0d807 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,77 +1,25 @@ extern crate hidapi; -use hidapi::*; - -fn ReceiveData(device: &HidDevice) -> Result,()> { - let mut usb_buf = vec![0u8; 65]; - let mut err_cnt = 3; - let mut chk_sum = 0; - - loop { - usb_buf[0] = 0x01; - match device.get_feature_report(&mut usb_buf[..]) { - Ok(res) => { - if usb_buf[0] == 0x1 { - for i in 0..res { - chk_sum ^= usb_buf[i]; - } - if chk_sum == 0 { - break Err(()); - } - break Ok(usb_buf); - } - }, - Err(_) => { - err_cnt -= 1; - if err_cnt == 0 { - break Err(()); - } - } - } - } -} - -fn SendData(device: &HidDevice, data: &[u8]) { - let mut usb_buf = [0u8; 65]; - let mut err_cnt = 3; - - loop { - let mut chk_sum = 0; - usb_buf[0] = 0x01; - for i in 1..data.len() { - usb_buf[i + 1] = data[i]; - chk_sum ^= data[i]; - } - - usb_buf[data.len() + 1] = chk_sum; - match device.send_feature_report(&usb_buf[..]) { - Ok(_) => break, - Err(_) => { - err_cnt -= 1; - if err_cnt == 0 { - break; - } - () - } - } - } -} +use hidapi::HidApi; fn main() { - println!("Get the version data from the keyboard:"); + println!("Get device data:"); - let api = HidApi::new().unwrap(); - - // connect to the device using VID and PID - let (VID, PID) = (0x24f0, 0x2037); - let device = api.open(VID, PID).unwrap(); - - let usb_init = [0xea, 0x02, 0xb0]; - SendData(&device, &usb_init[..]); - match ReceiveData(&device) { - Ok(data) => { - println!("It worked!") - }, - Err(_) => () + match HidApi::new() { + Ok(api) => { + for device in api.device_list() { + println!( + "{:04x}:{:04x} {} - {} {}", + device.vendor_id(), + device.product_id(), + device.path().to_string_lossy().into_owned(), + device.product_string().unwrap_or(""), + device.serial_number().unwrap_or("") + ); + } + } + Err(e) => { + eprintln!("Error: {}", e); + } } }