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,5 +9,5 @@ gumdrop = "0.8"
dbus = "0.7.1"
serde = "1.0"
serde_derive = "1.0"
toml = "0.5"
rog-lib = { path = "../rog-lib" }

View File

@@ -1,47 +0,0 @@
use crate::CONFIG_PATH;
use rog_lib::{aura::SetAuraBuiltin, core::LED_MSG_LEN};
use serde_derive::{Deserialize, Serialize};
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
#[derive(Default, Deserialize, Serialize)]
pub(crate) 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

@@ -1,15 +1,17 @@
use crate::{config::Config, DBUS_IFACE, DBUS_PATH};
use crate::{DBUS_IFACE, DBUS_PATH};
use dbus::{
blocking::Connection,
tree::{Factory, MethodErr},
};
use rog_lib::core::RogCore;
use rog_lib::hotkeys::*;
use rog_lib::{config::Config, core::RogCore};
use std::error::Error;
use std::time::Duration;
use std::{cell::RefCell, rc::Rc};
pub struct Daemon {
rogcore: RogCore,
hotkeys: Box<dyn Laptop>,
config: Config,
}
@@ -17,6 +19,7 @@ impl Daemon {
pub fn new() -> Self {
Daemon {
rogcore: RogCore::new().expect("Could not start RogCore"),
hotkeys: Box::new(LaptopGX502GW::new()),
config: Config::default().read(),
}
}
@@ -83,17 +86,14 @@ impl Daemon {
// We add the tree to the connection so that incoming method calls will be handled.
tree.start_receive(&connection);
let mut key_buf = [0u8; 32];
loop {
connection.process(Duration::from_millis(5))?;
connection.process(Duration::from_millis(1))?;
// READ KEYBOARD
// TODO: this needs to move to a thread
match daemon.borrow_mut().rogcore.poll_keyboard() {
Ok(buf) => {
// [5d, 1, 0, 0, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] up
// [5d, 1, 0, 0, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] down
// [5d, 1, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] left
// [5d, 1, 0, 0, 4f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] right
// TODO: this needs to move to a thread, but there is unsafety
match daemon.borrow_mut().rogcore.poll_keyboard(&mut key_buf) {
Ok(read) => {
// [5a, c4, ; 32 bytes long] fn+up
// [5a, c5, ; 32 bytes long] fn+down
// [5a, b2, ; 32 bytes long] fn+left
@@ -103,7 +103,15 @@ impl Daemon {
// read config + inc/dec brightness byte
// write to aura
// write config
println!("{:x?}", buf);
let hotkeys = unsafe { &mut (*daemon.as_ptr()).hotkeys };
let mut rogcore = unsafe { &mut (*daemon.as_ptr()).rogcore };
let mut config = unsafe { &mut (*daemon.as_ptr()).config };
if let Some(_count) = read {
if key_buf[0] == hotkeys.hotkey_group_byte() {
hotkeys.do_hotkey_action(&mut rogcore, &mut config, key_buf[1]);
}
}
}
Err(err) => println!("{:?}", err),
}

View File

@@ -1,5 +1,4 @@
// TODO: use /sys/class/dmi/id/board_name to detect model
mod config;
mod daemon;
use crate::daemon::*;
@@ -12,7 +11,6 @@ use rog_lib::core::{LedBrightness, RogCore, LED_MSG_LEN};
pub static DBUS_NAME: &'static str = "org.rogcore.Daemon";
pub static DBUS_PATH: &'static str = "/org/rogcore/Daemon";
pub static DBUS_IFACE: &'static str = "org.rogcore.Daemon";
pub static CONFIG_PATH: &'static str = "/etc/rogcore.conf";
#[derive(Debug, Options)]
struct CLIStart {