Created a controller for the keyboard
This commit is contained in:
58
src/controllers/DasKeyboard.rs
Normal file
58
src/controllers/DasKeyboard.rs
Normal file
@@ -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 <jali@orca-central.de>
|
||||
*****************************************************************************/
|
||||
|
||||
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<DasKeyboardController> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/controllers/mod.rs
Normal file
10
src/controllers/mod.rs
Normal file
@@ -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;
|
||||
|
||||
88
src/main.rs
88
src/main.rs
@@ -1,77 +1,25 @@
|
||||
extern crate hidapi;
|
||||
|
||||
use hidapi::*;
|
||||
|
||||
fn ReceiveData(device: &HidDevice) -> Result<Vec<u8>,()> {
|
||||
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("<Unknown>"),
|
||||
device.serial_number().unwrap_or("<None>")
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user