Skip to content

Commit dbb5bb5

Browse files
committed
version 0.1.1
2 parents 484b91d + b2f8359 commit dbb5bb5

File tree

7 files changed

+178
-37
lines changed

7 files changed

+178
-37
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
22
name = "roccat-vulcan-api-rs"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["ChickenStorm <[email protected]>"]
55
edition = "2018"
66
readme = "README.md"
77
repository = "https://github.com/ChickenStorm/roccat-vulcan-api-rs"
88
license = "MIT"
9-
categories = ["api-bindings"]
9+
categories = ["hardware-support"]
1010
description = "Roccat Vulcan keyboard illumination API"
1111
keywords = ["roccat", "vulcan", "keyboard", "lighting", "api"]
1212

@@ -17,4 +17,4 @@ keywords = ["roccat", "vulcan", "keyboard", "lighting", "api"]
1717
hidapi = "1.2.5"
1818

1919
[dev-dependencies]
20-
once_cell = "1.5.2"
20+
once_cell = "1.5.2"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Provide an API to control the lighting of the Roccat Vulcan 100 and 120.
77
# Usage
88

99
add on your dependencies of Cargo.toml
10-
`roccat_vulcan_api_rs = { version = "0.1.0", git = "https://github.com/ChickenStorm/roccat-vulcan-api-rs" }`.
10+
`roccat-vulcan-api-rs = { version = "0.1.1", git = "https://github.com/ChickenStorm/roccat-vulcan-api-rs", branch = "main" }`.
1111

1212

1313
The main way to interact with the API is through `KeyboardApi`.

examples/render_loop.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
extern crate roccat_vulcan_api_rs;
2+
3+
use roccat_vulcan_api_rs::{
4+
constants,
5+
ColorRgb,
6+
ColorBuffer,
7+
Color,
8+
KeyboardApi,
9+
LayoutFrCh,
10+
Layout,
11+
Key,
12+
};
13+
use std::time::Duration;
14+
15+
fn main() {
16+
17+
let mut key_press_mask: [bool; constants::NUMBER_KEY_LED_BUFFER] = [false; constants::NUMBER_KEY_LED_BUFFER];
18+
let keyboard = KeyboardApi::get_api().unwrap();
19+
let base_color = ColorRgb::new(0,255,255);
20+
let press_color = ColorRgb::new(255,0,255);
21+
let mut buffer = ColorBuffer::<ColorRgb>::new(base_color);
22+
let layout = LayoutFrCh::new();
23+
keyboard.render(&buffer).unwrap();
24+
'mainloop: loop {
25+
let result = keyboard.read_key_press(Duration::from_millis(62));
26+
if let Ok(val) = result {
27+
for keypress in val {
28+
let a = layout.find_key_info_from_press_code(&keypress.key_code());
29+
if let Some(key) = a {
30+
if *key.key() == Key::Escape {
31+
break 'mainloop;
32+
}
33+
let index_key = *key.key_code_light() as usize;
34+
if index_key < buffer.buffer().len(){
35+
buffer.buffer_mut()[index_key] = press_color;
36+
key_press_mask[index_key] = keypress.is_pressed()
37+
}
38+
}
39+
}
40+
}
41+
keyboard.render(&buffer).unwrap();
42+
for index in 0..buffer.buffer().len() {
43+
let el = &mut buffer.buffer_mut()[index];
44+
if *el != base_color && !key_press_mask[index] {
45+
if el.r() > base_color.r(){
46+
*el.r_mut() -= 5.min(el.r() - base_color.r());
47+
}
48+
else {
49+
*el.r_mut() += 5.min(base_color.r() - el.r());
50+
}
51+
if el.g() > base_color.g(){
52+
*el.g_mut() -= 5.min(el.g() - base_color.g());
53+
}
54+
else {
55+
*el.g_mut() += 5.min(base_color.g() - el.g());
56+
}
57+
if el.b() > base_color.b(){
58+
*el.b_mut() -= 5.min(el.b() - base_color.b());
59+
}
60+
else {
61+
*el.b_mut() += 5.min(base_color.b() - el.b());
62+
}
63+
}
64+
}
65+
}
66+
}

src/color.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ impl ColorRgb {
4848
b: array[2],
4949
}
5050
}
51+
52+
pub fn r_mut(&mut self) -> &mut u8 {
53+
return &mut self.r;
54+
}
55+
56+
pub fn g_mut(&mut self) -> &mut u8 {
57+
return &mut self.g;
58+
}
59+
60+
pub fn b_mut(&mut self) -> &mut u8 {
61+
return &mut self.b;
62+
}
63+
64+
pub fn set_r(&mut self, r: u8){
65+
self.r = r;
66+
}
67+
68+
pub fn set_g(&mut self, g: u8){
69+
self.g = g;
70+
}
71+
72+
pub fn set_b(&mut self, b: u8){
73+
self.b = b;
74+
}
75+
5176
}
5277

5378
impl Color for ColorRgb {

src/keyboard.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// todo verify that the device is not loaded.
66

77
use super::{
8-
layout,
8+
layout::Keypress,
99
color::{
1010
Color,
1111
ColorBuffer,
@@ -16,6 +16,7 @@ use super::{
1616
use std::{
1717
thread::sleep,
1818
time::{Duration, Instant},
19+
vec::Vec,
1920
};
2021

2122
/// Differents error returned by the API
@@ -32,6 +33,7 @@ pub enum ErrorRoccatVulcanApi {
3233
ToMuchTimeWaited(Duration),
3334
/// error while trying the get the hdiapi.
3435
HidApiError(hidapi::HidError),
36+
InvalidInput,
3537
}
3638

3739
/// get the product id of the detected divice
@@ -88,7 +90,7 @@ pub struct KeyboardApi {
8890
}
8991

9092
/// Sleep duration for KeyboardApi::wait_for_control_device.
91-
const WAIT_FOR_CONTROL_DURATION: Duration = Duration::from_micros(1);
93+
const WAIT_FOR_CONTROL_DURATION: Duration = Duration::from_millis(1);
9294
/// Max time wating for device in KeyboardApi::wait_for_control_device.
9395
const MAX_WAIT_DURATION: Duration = Duration::from_millis(100);
9496

@@ -189,7 +191,7 @@ impl KeyboardApi {
189191
}
190192

191193
/// Wait until a key event and return the [`super::Keypress`] associated with it.
192-
pub fn wait_for_key_press(&self) -> Result<layout::Keypress, ErrorRoccatVulcanApi> {
194+
pub fn wait_for_key_press(&self) -> Result<Keypress, ErrorRoccatVulcanApi> {
193195
listen_key_press(&self.read).map_err(|err| ErrorRoccatVulcanApi::ReadDeviceError(err))
194196
}
195197

@@ -198,11 +200,33 @@ impl KeyboardApi {
198200
let buffer_bite = buffer.get_led_buffer();
199201
let bite_to_write = constants::BITE_PACKET_SIZE + 1;
200202
for i in 0..(buffer_bite.len() / bite_to_write){
201-
let buffer_write = &buffer_bite[(i * ( bite_to_write)).. (i + 1) * bite_to_write];
203+
let buffer_write = &buffer_bite[(i * ( bite_to_write))..(i + 1) * bite_to_write];
202204
self.led.write(buffer_write).map_err(|error| ErrorRoccatVulcanApi::LedDeviceError(error))?;
203205
}
204206
Ok(())
205207
}
208+
209+
/// read key press for a time of at least duration and return a vector of the keypress that occured for this duration.
210+
pub fn read_key_press(&self, duration: Duration) -> Result<Vec<Keypress>, ErrorRoccatVulcanApi> {
211+
if duration.as_millis() > i32::MAX as u128 {
212+
return Err(ErrorRoccatVulcanApi::InvalidInput);
213+
}
214+
let mut vector_result: Vec<Keypress> = Vec::new();
215+
let now = Instant::now();
216+
loop {
217+
let elapsed = now.elapsed();
218+
if duration <= elapsed {
219+
break;
220+
}
221+
let mut buffer: [u8; 5] = [0; 5];
222+
self.read.read_timeout(&mut buffer, (duration - elapsed).as_millis() as i32)
223+
.map_err(|error| ErrorRoccatVulcanApi::ReadDeviceError(error))?;
224+
if buffer[2] > 0 {
225+
vector_result.push(Keypress::new_from_buffer(&buffer));
226+
}
227+
}
228+
return Ok(vector_result);
229+
}
206230
}
207231

208232
impl Drop for KeyboardApi {
@@ -213,12 +237,12 @@ impl Drop for KeyboardApi {
213237
}
214238

215239
fn listen_key_press_raw(device: &hidapi::HidDevice) -> Result<[u8; 5], hidapi::HidError> {
216-
let mut buffer: [u8; 5]= [0; 5];
240+
let mut buffer: [u8; 5] = [0; 5];
217241
device.read(&mut buffer)?;
218242
return Ok(buffer);
219243
}
220244

221-
fn listen_key_press(device: &hidapi::HidDevice) -> Result<layout::Keypress, hidapi::HidError> {
245+
fn listen_key_press(device: &hidapi::HidDevice) -> Result<Keypress, hidapi::HidError> {
222246
let buffer = listen_key_press_raw(device)?;
223-
return Ok(layout::Keypress::new_from_buffer(&buffer));
247+
return Ok(Keypress::new_from_buffer(&buffer));
224248
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! # Usage
55
//!
66
//! add on your dependencies of Cargo.toml
7-
//! `roccat_vulcan_api_rs = { version = "0.1.0", git = "https://github.com/ChickenStorm/roccat-vulcan-api-rs" }`.
7+
//! `roccat-vulcan-api-rs = { version = "0.1.1", git = "https://github.com/ChickenStorm/roccat-vulcan-api-rs", branch = "main" }`.
88
//!
99
//! The main way to interact with the API is through [`KeyboardApi`].
1010
//! Note that when the structure is dropped the keyboard will go back to the default rainbow behavior.

src/main.rs

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ use roccat_vulcan_api_rs::layout::{
66
Layout,
77
layout_fr_ch::LayoutFrCh
88
};
9-
use roccat_vulcan_api_rs::color::ColorBuffer;
10-
use roccat_vulcan_api_rs::color::ColorRgb;
9+
use roccat_vulcan_api_rs::color::{
10+
ColorRgb,
11+
ColorBuffer,
12+
Color,
13+
};
14+
use roccat_vulcan_api_rs::constants;
1115
use std::{
1216
thread::sleep,
1317
time::{Duration, Instant},
@@ -16,37 +20,59 @@ use std::{
1620
fn main() {
1721

1822
let api = hidapi::HidApi::new().unwrap();
19-
for device in api.device_list() {
20-
let product_id_list = roccat_vulcan_api_rs::config::get_products_id_default();
21-
if product_id_list.contains(&(device.product_id())){
22-
println!{"vendor : {}, usage page : {}, usage : {}, intreface : {}", device.vendor_id(), device.usage_page(), device.usage(), device.interface_number()}
23-
}
24-
}
25-
2623

24+
let mut key_press_mask: [bool; constants::NUMBER_KEY_LED_BUFFER] = [false; constants::NUMBER_KEY_LED_BUFFER];
2725
let keyboard = KeyboardApi::get_api_from_hidapi(&api).unwrap();
28-
29-
let mut buffer = ColorBuffer::<ColorRgb>::new(ColorRgb::new(0,255,255));
26+
let base_color = ColorRgb::new(255,255,255);
27+
let press_color = vec![ColorRgb::new(255,0,255), ColorRgb::new(0,0,255), ColorRgb::new(255,0,0), ColorRgb::new(0,255,255)];
28+
let mut press_count: usize = 0;
29+
let mut buffer = ColorBuffer::<ColorRgb>::new(base_color);
3030
let layout = LayoutFrCh::new();
31-
loop {
32-
keyboard.render(&buffer).unwrap();
33-
let result = keyboard.wait_for_key_press();
31+
keyboard.render(&buffer).unwrap();
32+
'mainloop: loop {
33+
let result = keyboard.read_key_press(Duration::from_millis(62));
3434
if let Ok(val) = result {
35-
let a = layout.find_key_info_from_press_code(&val.key_code());
36-
if let Some(key) = a {
37-
let index_key = *key.key_code_light() as usize;
38-
if index_key < buffer.buffer().len(){
39-
if !val.is_pressed(){
40-
if let Key::Escape = key.key(){
41-
break;
42-
}
43-
buffer.buffer_mut()[index_key] = ColorRgb::new(0,255,255)
35+
for keypress in val {
36+
let a = layout.find_key_info_from_press_code(&keypress.key_code());
37+
if let Some(key) = a {
38+
if *key.key() == Key::Escape {
39+
break 'mainloop;
4440
}
45-
else {
46-
buffer.buffer_mut()[index_key] = ColorRgb::new(0,0,255)
41+
let index_key = *key.key_code_light() as usize;
42+
if index_key < buffer.buffer().len(){
43+
if keypress.is_pressed(){
44+
buffer.buffer_mut()[index_key] = press_color[press_count % press_color.len()];
45+
press_count += 1;
46+
}
47+
key_press_mask[index_key] = keypress.is_pressed();
48+
4749
}
4850
}
4951
}
5052
}
53+
keyboard.render(&buffer).unwrap();
54+
for index in 0..buffer.buffer().len() {
55+
let el = &mut buffer.buffer_mut()[index];
56+
if *el != base_color && !key_press_mask[index] {
57+
if el.r() > base_color.r(){
58+
*el.r_mut() -= 5.min(el.r() - base_color.r());
59+
}
60+
else {
61+
*el.r_mut() += 5.min(base_color.r() - el.r());
62+
}
63+
if el.g() > base_color.g(){
64+
*el.g_mut() -= 5.min(el.g() - base_color.g());
65+
}
66+
else {
67+
*el.g_mut() += 5.min(base_color.g() - el.g());
68+
}
69+
if el.b() > base_color.b(){
70+
*el.b_mut() -= 5.min(el.b() - base_color.b());
71+
}
72+
else {
73+
*el.b_mut() += 5.min(base_color.b() - el.b());
74+
}
75+
}
76+
}
5177
}
5278
}

0 commit comments

Comments
 (0)