Added color per row support

This commit is contained in:
Sebastian H. Gabrielli 2022-09-02 12:12:38 +02:00
parent f6c4f5f82e
commit 3c3d0ac036
2 changed files with 16 additions and 57 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"cmake.configureOnOpen": false
}

View File

@ -8,8 +8,8 @@ use std::{cell::Cell, rc::Rc};
use colored::Colorize;
use gtk::glib::clone;
use gtk::{prelude::*, Window};
use gtk::{Application, ApplicationWindow, Button, ListBox};
use gtk::{prelude::*};
use gtk::{Application, ApplicationWindow, Button};
#[derive(Debug)]
struct KeyboardLighting {
@ -34,6 +34,7 @@ struct KeyboardConfig {
lighting: KeyboardLighting
}
/*
#[derive(Debug)]
struct LightingMessage {
row: u16,
@ -42,6 +43,7 @@ struct LightingMessage {
green: u8,
blue: u8
}
*/
fn parse_config(config_path: &Path) -> KeyboardConfig{
let mut parsed_json = json::parse(&std::fs::read_to_string(config_path).unwrap()).unwrap();
@ -96,64 +98,18 @@ fn parse_config(config_path: &Path) -> KeyboardConfig{
}
fn main() {
// Printing the parsed config for debug purposes
let config: KeyboardConfig = parse_config(&Path::new("numpad_config.json"));
println!("{:#?}", config);
/*
let context = libusb::Context::new().unwrap();
for device in context.devices().unwrap().iter() {
let device_desc = device.device_descriptor().unwrap();
println!("Bus {:03} Device {:03} ID {:04x}:{:04x}",
device.bus_number(),
device.address(),
device_desc.vendor_id(),
device_desc.product_id());
if device_desc.vendor_id() == config.vendor_id && device_desc.product_id() == config.product_id {
println!("Woah, this is my device! I'm going to open it!");
let config_desc = device.active_config_descriptor().unwrap();
let mut device_handle: libusb::DeviceHandle = device.open().unwrap();
println!("Number of interfaces: {}", config_desc.num_interfaces());
//println!("The device supports these languages: {:?}", &device_handle.read_languages(std::time::Duration::from_millis(100)));
if device_handle.kernel_driver_active(0).unwrap() {
println!("Interface has kernel driver, detaching...");
device_handle.detach_kernel_driver(0).unwrap();
}
device_handle.claim_interface(0).unwrap();
match device_handle.write_control(libusb::request_type(libusb::Direction::Out, libusb::RequestType::Class, libusb::Recipient::Interface), 0x09, 0x0300, 0, &[0x00], std::time::Duration::from_secs(1)) {
Ok(n) => {
println!("{}", format!("Successfully wrote {} bytes!", n).green());
},
Err(e) => {
eprintln!("{}{:?}", format!("Failed to write with error: ").red().bold(), e);
}
}
device_handle.release_interface(0).unwrap();
device_handle.attach_kernel_driver(0).unwrap();
println!("Goodbye!");
std::process::exit(0);
}
}
*/
// Creating the application and displaying it
let app = Application::builder().application_id("org.sebaweb.keyboard-control-software").build();
app.connect_activate(build_ui);
app.run();
}
// USB Code
fn send_lighting_update(col: u8, row: u8, r: u8, g: u8, b: u8, a: u8) {
fn send_lighting_update(col: u8, _row: u8, r: u8, g: u8, b: u8, _a: u8) {
let config: KeyboardConfig = parse_config(&Path::new("numpad_config.json"));
let context = libusb::Context::new().unwrap(); // Get a new USB context
@ -161,7 +117,7 @@ fn send_lighting_update(col: u8, row: u8, r: u8, g: u8, b: u8, a: u8) {
let device_desc = device.device_descriptor().unwrap(); // Get a descriptor for the device
if device_desc.vendor_id() == config.vendor_id && device_desc.product_id() == config.product_id { // Check if the devie is the device we are looking for
println!("Woah, this is my device! I'm going to open it!");
println!("Supported keyboard discovered, claiming it.");
let mut device_handle: libusb::DeviceHandle = device.open().unwrap(); // Get a handle for the device
@ -169,12 +125,12 @@ fn send_lighting_update(col: u8, row: u8, r: u8, g: u8, b: u8, a: u8) {
println!("Interface has kernel driver, detaching...");
device_handle.detach_kernel_driver(0).unwrap();
}
device_handle.claim_interface(0).unwrap(); // Now that the device is freed we claim it
device_handle.claim_interface(0).unwrap(); // Now that the device is free we claim it
match device_handle.write_control(
libusb::request_type(libusb::Direction::Out, libusb::RequestType::Class, libusb::Recipient::Interface), // The request is outband and uses HID specific features tergeting an interface
0x09, 0x0301, 0, // We want to send a SET_REPORT request, that is a custom feature, with the feature being lighting, we want to send it to endpoint 0
&[row, col, r, g, b, a], std::time::Duration::from_secs(1)) { // First we send the row, then the column, then the Red, Green, Blue, and lastly the brightness
&[r, g, b, col], std::time::Duration::from_secs(1)) { // First we send the row, then the column, then the Red, Green, Blue, and lastly the brightness
Ok(n) => {
println!("{}", format!("Successfully wrote {} bytes!", n).green());
},
@ -183,8 +139,8 @@ fn send_lighting_update(col: u8, row: u8, r: u8, g: u8, b: u8, a: u8) {
}
}
device_handle.release_interface(0).unwrap();
device_handle.attach_kernel_driver(0).unwrap();
device_handle.release_interface(0).unwrap(); // Release the interface
device_handle.attach_kernel_driver(0).unwrap(); // Re-attach the kernel driver
}
}
}
@ -240,7 +196,7 @@ fn build_ui(app: &Application) {
let col = num.get() as f32 - row*keys_per_row as f32;
println!("i: {}, row: {}, col: {}, red: {}, green: {}, blue: {}, alpha: {}", i, row, col, red as u8, green as u8, blue as u8, alpha as u8);
send_lighting_update(row as u8, col as u8, red as u8, green as u8, blue as u8, alpha as u8);
send_lighting_update(col as u8, row as u8, red as u8, green as u8, blue as u8, alpha as u8);
}
color.destroy();
}));