Bright go up. Bright go down.

This commit is contained in:
Luke
2020-04-17 15:21:26 +12:00
parent e49799e4d2
commit 1616633abf
9 changed files with 130 additions and 33 deletions

View File

@@ -9,3 +9,4 @@ rusb = "0.5"
gumdrop = "0.8"
serde = "1.0"
serde_derive = "1.0"
toml = "0.5"

48
rog-lib/src/config.rs Normal file
View File

@@ -0,0 +1,48 @@
use crate::{aura::SetAuraBuiltin, core::LED_MSG_LEN};
use serde_derive::{Deserialize, Serialize};
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
pub static CONFIG_PATH: &'static str = "/etc/rogcore.conf";
#[derive(Default, Deserialize, Serialize)]
pub struct Config {
pub brightness: u8,
pub builtin: Vec<u8>,
}
impl Config {
pub fn read(mut self) -> Self {
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&CONFIG_PATH)
.expect("config file error");
let mut buf = String::new();
if let Ok(l) = file.read_to_string(&mut buf) {
if l == 0 {
// create a default config here
let d = SetAuraBuiltin::default();
let c = Config {
brightness: 1u8,
builtin: (<[u8; LED_MSG_LEN]>::from(d)).to_vec(),
};
let toml = toml::to_string(&c).unwrap();
file.write_all(toml.as_bytes())
.expect("Writing default config failed");
self = c;
} else {
self = toml::from_str(&buf).unwrap();
}
}
self
}
pub fn write(&self) {
let mut file = File::create(CONFIG_PATH).expect("Couldn't overwrite config");
let toml = toml::to_string_pretty(self).expect("Parse config to JSON failed");
file.write_all(toml.as_bytes())
.expect("Saving config failed");
}
}

View File

@@ -55,38 +55,34 @@ pub struct RogCore {
handle: DeviceHandle<rusb::GlobalContext>,
initialised: bool,
led_interface_num: u8,
keys_interface_num: u8,
}
impl RogCore {
pub fn new() -> Result<RogCore, Error> {
// TODO: make this more configurable
let mut handle = RogCore::get_device(0x0B05, 0x1866)?;
handle.set_active_configuration(0).unwrap_or(());
let config = handle.device().config_descriptor(0).unwrap();
// Interface with outputs
let mut keys_interface_num = 0;
let mut led_interface_num = 0;
for iface in config.interfaces() {
for desc in iface.descriptors() {
for endpoint in desc.endpoint_descriptors() {
if endpoint.address() == 0x83 {
keys_interface_num = desc.interface_number();
} else if endpoint.address() == 0x81 {
if endpoint.address() == 0x81 {
led_interface_num = desc.interface_number();
break;
}
}
}
}
handle.set_auto_detach_kernel_driver(true).unwrap();
handle.set_auto_detach_kernel_driver(true).unwrap();
Ok(RogCore {
handle,
initialised: false,
led_interface_num,
keys_interface_num,
})
}
@@ -144,22 +140,21 @@ impl RogCore {
self.aura_write_messages(&messages)
}
pub fn poll_keyboard(&mut self) -> Result<[u8; 32], Error> {
self.handle.claim_interface(self.keys_interface_num)?;
let mut buf = [0; 32];
pub fn poll_keyboard(&mut self, buf: &mut [u8; 32]) -> Result<Option<usize>, Error> {
match self
.handle
.read_interrupt(0x83, &mut buf, Duration::from_micros(10))
.read_interrupt(0x83, buf, Duration::from_micros(10))
{
Ok(_) => {
self.handle.release_interface(self.keys_interface_num)?;
Ok(buf)
Ok(o) => {
if buf[0] == 0x5a {
return Ok(Some(o));
}
}
Err(err) => match err {
//Error::Timeout => {}
_ => return Err(err),
},
}
Ok(None)
}
}

92
rog-lib/src/hotkeys.rs Normal file
View File

@@ -0,0 +1,92 @@
use crate::config::Config;
use crate::core::RogCore;
pub trait Laptop {
fn do_hotkey_action(&self, core: &mut RogCore, config: &mut Config, key_byte: u8);
fn hotkey_group_byte(&self) -> u8;
}
pub struct LaptopGX502GW {
hotkey_group_byte: u8,
min_bright: u8,
max_bright: u8,
}
impl LaptopGX502GW {
pub fn new() -> Self {
LaptopGX502GW {
hotkey_group_byte: 0x5a,
min_bright: 0x00,
max_bright: 0x03,
}
}
}
impl Laptop for LaptopGX502GW {
fn do_hotkey_action(&self, core: &mut RogCore, config: &mut Config, key_byte: u8) {
match GX502GWKeys::from(key_byte) {
GX502GWKeys::Rog => {
println!("ROG!");
}
GX502GWKeys::LedBrightUp => {
let mut bright = config.brightness;
if bright < self.max_bright {
bright += 1;
config.brightness = bright;
}
let bytes = RogCore::aura_brightness_bytes(bright).unwrap();
core.aura_set_mode(&bytes).unwrap();
config.write();
}
GX502GWKeys::LedBrightDown => {
let mut bright = config.brightness;
if bright > self.min_bright {
bright -= 1;
config.brightness = bright;
}
let bytes = RogCore::aura_brightness_bytes(bright).unwrap();
core.aura_set_mode(&bytes).unwrap();
config.write();
}
_ => println!("{:X?}", key_byte),
}
}
fn hotkey_group_byte(&self) -> u8 {
self.hotkey_group_byte
}
}
pub enum GX502GWKeys {
Rog = 0x38,
MicToggle = 0x7C,
Fan = 0xAE,
ScreenToggle = 0x35,
ScreenBrightDown = 0x10,
ScreenBrightUp = 0x20,
TouchPadToggle = 0x6b,
Sleep = 0x6C,
AirplaneMode = 0x88,
LedBrightUp = 0xC4,
LedBrightDown = 0xC5,
AuraPrevious = 0xB2,
AuraNext = 0xB3,
None,
}
impl From<u8> for GX502GWKeys {
fn from(byte: u8) -> Self {
match byte {
0x38 => GX502GWKeys::Rog,
0x7C => GX502GWKeys::MicToggle,
0xAE => GX502GWKeys::Fan,
0x35 => GX502GWKeys::ScreenToggle,
0x10 => GX502GWKeys::ScreenBrightDown,
0x20 => GX502GWKeys::ScreenBrightUp,
0x6b => GX502GWKeys::TouchPadToggle,
0x6C => GX502GWKeys::Sleep,
0x88 => GX502GWKeys::AirplaneMode,
0xC4 => GX502GWKeys::LedBrightUp,
0xC5 => GX502GWKeys::LedBrightDown,
0xB2 => GX502GWKeys::AuraPrevious,
0xB3 => GX502GWKeys::AuraNext,
_ => GX502GWKeys::None,
}
}
}

View File

@@ -1,3 +1,5 @@
pub mod aura;
pub mod config;
pub mod core;
mod error;
pub mod hotkeys;