Skip to content

Commit

Permalink
Merge pull request zzeneg#1 from danil-tolkachev/ergohaven
Browse files Browse the repository at this point in the history
Change default settings to support Ergohaven keyboards
  • Loading branch information
kissetfall authored Jul 5, 2024
2 parents b79e976 + 02514ed commit 091e1e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Config {
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Device {
pub vendor_id: u16,
pub product_id: u16,
pub usage: u16,
pub usage_page: u16,
Expand All @@ -17,11 +18,12 @@ pub struct Device {
pub fn get_config() -> Config {
let default_config = Config {
device: Device {
product_id: 0x0844,
vendor_id: 0xe126,
product_id: 0x0,
usage: 0x61,
usage_page: 0xff60,
},
layouts: vec!["pl".to_string()],
layouts: vec!["en".to_string(), "ru".to_string()],
reconnect_delay: 5000,
};

Expand Down
13 changes: 10 additions & 3 deletions src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use tokio::sync::{broadcast, mpsc};
use crate::config::Device;

pub struct Keyboard {
vendor_id: u16,
product_id: u16,
usage: u16,
usage_page: u16,
Expand All @@ -13,18 +14,23 @@ pub struct Keyboard {
impl Keyboard {
pub fn new(device: Device, reconnect_delay: u64) -> Self {
return Self {
vendor_id: device.vendor_id,
product_id: device.product_id,
usage: device.usage,
usage_page: device.usage_page,
reconnect_delay,
};
}

fn get_device(product_id: &u16, usage: &u16, usage_page: &u16) -> Result<HidDevice, HidError> {
fn get_device(vendor_id: &u16, product_id: &u16, usage: &u16, usage_page: &u16) -> Result<HidDevice, HidError> {
let hid_api = HidApi::new()?;
let devices = hid_api.device_list();
for device_info in devices {
if device_info.product_id() == *product_id && device_info.usage() == *usage && device_info.usage_page() == *usage_page {
if (*vendor_id == 0 || device_info.vendor_id() == *vendor_id)
&& (*product_id == 0 || device_info.product_id() == *product_id)
&& device_info.usage() == *usage
&& device_info.usage_page() == *usage_page
{
let device = device_info.open_device(&hid_api)?;
return Ok(device);
}
Expand All @@ -34,6 +40,7 @@ impl Keyboard {
}

pub fn connect(&self) -> (broadcast::Sender<bool>, mpsc::Sender<Vec<u8>>) {
let vid = self.vendor_id;
let pid = self.product_id;
let usage = self.usage;
let usage_page = self.usage_page;
Expand All @@ -45,7 +52,7 @@ impl Keyboard {
tracing::info!("Waiting for keyboard...");
loop {
tracing::debug!("Trying to connect...");
if let Ok(device) = Self::get_device(&pid, &usage, &usage_page) {
if let Ok(device) = Self::get_device(&vid, &pid, &usage, &usage_page) {
let _ = &internal_connected_sender.send(true).unwrap();
tracing::info!("Connected to keyboard");
loop {
Expand Down

0 comments on commit 091e1e2

Please sign in to comment.