Refactor dbus commands and internal data structures

This commit is contained in:
Luke
2020-06-29 15:52:49 +12:00
parent f85c8cbc50
commit 32e9e63809
26 changed files with 720 additions and 653 deletions

View File

@@ -1,4 +1,5 @@
use super::*;
use crate::anime_matrix::AniMePacketType;
use crate::{DBUS_IFACE, DBUS_NAME, DBUS_PATH};
use dbus::channel::Sender;
use dbus::{blocking::Connection, Message};
use std::error::Error;

View File

@@ -153,7 +153,7 @@ impl From<AniMeMatrix> for AniMePacketType {
#[cfg(test)]
mod tests {
use crate::{AniMeMatrix, AniMePacketType};
use crate::anime_matrix::{AniMeMatrix, AniMePacketType};
#[test]
fn check_data_alignment() {

View File

@@ -1,150 +0,0 @@
use super::*;
use dbus::blocking::BlockingSender;
use dbus::channel::Sender;
use dbus::{blocking::Connection, Message};
use std::error::Error;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::{thread, time::Duration};
/// Simplified way to write a effect block
pub struct AuraDbusWriter {
connection: Box<Connection>,
block_time: u64,
stop: Arc<AtomicBool>,
}
impl AuraDbusWriter {
#[inline]
pub fn new() -> Result<Self, Box<dyn Error>> {
let connection = Connection::new_system()?;
Ok(AuraDbusWriter {
connection: Box::new(connection),
block_time: 33333,
stop: Arc::new(AtomicBool::new(false)),
})
}
/// This method must always be called before the very first write to initialise
/// the keyboard LED EC in the correct mode
#[inline]
pub fn init_effect(&self) -> Result<String, Box<dyn std::error::Error>> {
let match_rule = dbus::message::MatchRule::new_signal(DBUS_IFACE, "LedCancelEffect");
let stopper = self.stop.clone();
self.connection
.add_match(match_rule, move |_: (), _, msg| {
println!("GOT {:?}", msg);
if let Ok(stop) = msg.read1::<bool>() {
if stop {
stopper.store(true, Ordering::Relaxed);
}
}
true
})?;
let msg = Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "LedWriteBytes")?
.append1(KeyColourArray::get_init_msg());
let r = self
.connection
.send_with_reply_and_block(msg, Duration::from_millis(5000))?;
if let Some(reply) = r.get1::<&str>() {
return Ok(reply.to_owned());
}
Err(Box::new(dbus::Error::new_custom("name", "message")))
}
/// Write a single colour block.
///
/// Intentionally blocks for 10ms after sending to allow the block to
/// be written to the keyboard EC. This should not be async.
#[inline]
pub fn write_colour_block(
&mut self,
key_colour_array: &KeyColourArray,
) -> Result<(), Box<dyn Error>> {
let group = key_colour_array.get();
let mut msg = Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "LedWriteEffect")?
.append3(&group[0].to_vec(), &group[1].to_vec(), &group[2].to_vec())
.append3(&group[3].to_vec(), &group[4].to_vec(), &group[5].to_vec())
.append3(&group[6].to_vec(), &group[7].to_vec(), &group[8].to_vec())
.append2(&group[9].to_vec(), &group[10].to_vec());
msg.set_no_reply(true);
self.connection.send(msg).unwrap();
thread::sleep(Duration::from_micros(self.block_time));
if self.stop.load(Ordering::Relaxed) {
panic!("Got signal to stop!");
}
Ok(())
}
#[inline]
pub fn write_multizone(
&mut self,
group: &[[u8; LED_MSG_LEN]; 4],
) -> Result<(), Box<dyn std::error::Error>> {
let mut msg =
Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "LedWriteMultizone")?
.append1(&group[0].to_vec())
.append1(&group[1].to_vec())
.append1(&group[2].to_vec())
.append1(&group[3].to_vec());
msg.set_no_reply(true);
self.connection.send(msg).unwrap();
Ok(())
}
#[inline]
pub fn write_bytes(&self, bytes: &[u8]) -> Result<String, Box<dyn std::error::Error>> {
let msg = Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "LedWriteBytes")?
.append1(bytes.to_vec());
let r = self
.connection
.send_with_reply_and_block(msg, Duration::from_millis(5000))?;
if let Some(reply) = r.get1::<&str>() {
return Ok(reply.to_owned());
}
Err(Box::new(dbus::Error::new_custom("name", "message")))
}
#[inline]
pub fn write_fan_mode(&self, level: u8) -> Result<String, Box<dyn std::error::Error>> {
let msg = Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "SetFanMode")?
.append1(level);
let r = self
.connection
.send_with_reply_and_block(msg, Duration::from_millis(5000))?;
if let Some(reply) = r.get1::<&str>() {
return Ok(reply.to_owned());
}
Err(Box::new(dbus::Error::new_custom("name", "message")))
}
#[inline]
pub fn write_charge_limit(&self, level: u8) -> Result<String, Box<dyn std::error::Error>> {
let msg = Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "SetChargeLimit")?
.append1(level);
let r = self
.connection
.send_with_reply_and_block(msg, Duration::from_millis(5000))?;
if let Some(reply) = r.get1::<&str>() {
return Ok(reply.to_owned());
}
Err(Box::new(dbus::Error::new_custom("name", "message")))
}
#[inline]
pub fn write_builtin_mode(
&self,
mode: &SetAuraBuiltin,
) -> Result<String, Box<dyn std::error::Error>> {
let bytes = <[u8; LED_MSG_LEN]>::from(mode);
self.write_bytes(&bytes)
}
#[inline]
pub fn write_brightness(&self, level: u8) -> Result<String, Box<dyn std::error::Error>> {
self.write_bytes(&aura_brightness_bytes(level))
}
}

View File

@@ -0,0 +1,107 @@
use crate::cli_options::*;
use serde_derive::{Deserialize, Serialize};
pub const SINGLE: u8 = 0x00;
pub const BREATHING: u8 = 0x01;
pub const STROBE: u8 = 0x02;
pub const RAINBOW: u8 = 0x03;
pub const STAR: u8 = 0x04;
pub const RAIN: u8 = 0x05;
pub const HIGHLIGHT: u8 = 0x06;
pub const LASER: u8 = 0x07;
pub const RIPPLE: u8 = 0x08;
pub const PULSE: u8 = 0x0a;
pub const COMET: u8 = 0x0b;
pub const FLASH: u8 = 0x0c;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum AuraModes {
Stable(SingleColour),
Breathe(TwoColourSpeed),
Strobe(SingleSpeed),
Rainbow(SingleSpeedDirection),
Star(TwoColourSpeed),
Rain(SingleSpeed),
Highlight(SingleColourSpeed),
Laser(SingleColourSpeed),
Ripple(SingleColourSpeed),
Pulse(SingleColour),
Comet(SingleColour),
Flash(SingleColour),
MultiStatic(MultiColour),
LedBrightness(u8),
Aura,
}
impl From<SetAuraBuiltin> for AuraModes {
fn from(mode: SetAuraBuiltin) -> Self {
match mode {
SetAuraBuiltin::Stable(x) => AuraModes::Stable(x),
SetAuraBuiltin::Breathe(x) => AuraModes::Breathe(x),
SetAuraBuiltin::Strobe(x) => AuraModes::Strobe(x),
SetAuraBuiltin::Rainbow(x) => AuraModes::Rainbow(x),
SetAuraBuiltin::Star(x) => AuraModes::Star(x),
SetAuraBuiltin::Rain(x) => AuraModes::Rain(x),
SetAuraBuiltin::Highlight(x) => AuraModes::Highlight(x),
SetAuraBuiltin::Laser(x) => AuraModes::Laser(x),
SetAuraBuiltin::Ripple(x) => AuraModes::Ripple(x),
SetAuraBuiltin::Pulse(x) => AuraModes::Pulse(x),
SetAuraBuiltin::Comet(x) => AuraModes::Comet(x),
SetAuraBuiltin::Flash(x) => AuraModes::Flash(x),
SetAuraBuiltin::MultiStatic(x) => AuraModes::MultiStatic(x),
}
}
}
impl From<AuraModes> for u8 {
fn from(mode: AuraModes) -> Self {
u8::from(&mode)
}
}
impl From<&mut AuraModes> for u8 {
fn from(mode: &mut AuraModes) -> Self {
u8::from(&*mode)
}
}
impl From<&AuraModes> for u8 {
fn from(mode: &AuraModes) -> Self {
match mode {
AuraModes::Stable(_) => 0x00,
AuraModes::Breathe(_) => 0x01,
AuraModes::Strobe(_) => 0x02,
AuraModes::Rainbow(_) => 0x03,
AuraModes::Star(_) => 0x04,
AuraModes::Rain(_) => 0x05,
AuraModes::Highlight(_) => 0x06,
AuraModes::Laser(_) => 0x07,
AuraModes::Ripple(_) => 0x08,
AuraModes::Pulse(_) => 0x0a,
AuraModes::Comet(_) => 0x0b,
AuraModes::Flash(_) => 0x0c,
AuraModes::MultiStatic(_) => 0x0d,
_ => panic!("Invalid mode"),
}
}
}
impl From<u8> for AuraModes {
fn from(byte: u8) -> Self {
match byte {
SINGLE => AuraModes::Stable(SingleColour::default()),
BREATHING => AuraModes::Breathe(TwoColourSpeed::default()),
STROBE => AuraModes::Strobe(SingleSpeed::default()),
RAINBOW => AuraModes::Rainbow(SingleSpeedDirection::default()),
STAR => AuraModes::Star(TwoColourSpeed::default()),
RAIN => AuraModes::Rain(SingleSpeed::default()),
HIGHLIGHT => AuraModes::Highlight(SingleColourSpeed::default()),
LASER => AuraModes::Laser(SingleColourSpeed::default()),
RIPPLE => AuraModes::Ripple(SingleColourSpeed::default()),
PULSE => AuraModes::Pulse(SingleColour::default()),
COMET => AuraModes::Comet(SingleColour::default()),
FLASH => AuraModes::Flash(SingleColour::default()),
_ => panic!("Invalid mode byte"),
}
}
}

View File

@@ -1,164 +0,0 @@
use super::cli_options::*;
use super::LED_MSG_LEN;
use serde_derive::{Deserialize, Serialize};
/// Container for the byte strings used in modes. Generally useful for settings
/// and other usecases.
#[derive(Deserialize, Serialize)]
pub struct BuiltInModeBytes {
pub stable: [u8; LED_MSG_LEN],
pub breathe: [u8; LED_MSG_LEN],
pub strobe: [u8; LED_MSG_LEN],
pub rainbow: [u8; LED_MSG_LEN],
pub star: [u8; LED_MSG_LEN],
pub rain: [u8; LED_MSG_LEN],
pub highlight: [u8; LED_MSG_LEN],
pub laser: [u8; LED_MSG_LEN],
pub ripple: [u8; LED_MSG_LEN],
pub pulse: [u8; LED_MSG_LEN],
pub comet: [u8; LED_MSG_LEN],
pub flash: [u8; LED_MSG_LEN],
pub multi_static: [[u8; LED_MSG_LEN]; 4],
}
impl BuiltInModeBytes {
#[inline]
pub fn set_field_from(&mut self, bytes: &[u8]) {
if bytes[0] == 0x5d && bytes[1] == 0xb3 {
let b = BuiltInModeByte::from(bytes[3]);
match b {
BuiltInModeByte::Single => self.stable.copy_from_slice(bytes),
BuiltInModeByte::Breathing => self.breathe.copy_from_slice(bytes),
BuiltInModeByte::Strobe => self.strobe.copy_from_slice(bytes),
BuiltInModeByte::Rainbow => self.rainbow.copy_from_slice(bytes),
BuiltInModeByte::Star => self.star.copy_from_slice(bytes),
BuiltInModeByte::Rain => self.rain.copy_from_slice(bytes),
BuiltInModeByte::Highlight => self.highlight.copy_from_slice(bytes),
BuiltInModeByte::Laser => self.laser.copy_from_slice(bytes),
BuiltInModeByte::Ripple => self.ripple.copy_from_slice(bytes),
BuiltInModeByte::Pulse => self.pulse.copy_from_slice(bytes),
BuiltInModeByte::Comet => self.comet.copy_from_slice(bytes),
BuiltInModeByte::Flash => self.flash.copy_from_slice(bytes),
_ => {}
}
}
}
#[inline]
pub fn get_field_from(&self, byte: u8) -> Option<&[u8]> {
let bytes = match BuiltInModeByte::from(byte) {
BuiltInModeByte::Single => &self.stable,
BuiltInModeByte::Breathing => &self.breathe,
BuiltInModeByte::Strobe => &self.strobe,
BuiltInModeByte::Rainbow => &self.rainbow,
BuiltInModeByte::Star => &self.star,
BuiltInModeByte::Rain => &self.rain,
BuiltInModeByte::Highlight => &self.highlight,
BuiltInModeByte::Laser => &self.laser,
BuiltInModeByte::Ripple => &self.ripple,
BuiltInModeByte::Pulse => &self.pulse,
BuiltInModeByte::Comet => &self.comet,
BuiltInModeByte::Flash => &self.flash,
_ => return None,
};
Some(bytes)
}
}
impl Default for BuiltInModeBytes {
fn default() -> Self {
BuiltInModeBytes {
stable: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Stable(SingleColour::default())),
breathe: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Breathe(TwoColourSpeed::default())),
strobe: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Strobe(SingleSpeed::default())),
rainbow: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Rainbow(
SingleSpeedDirection::default(),
)),
star: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Star(TwoColourSpeed::default())),
rain: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Rain(SingleSpeed::default())),
highlight: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Highlight(
SingleColourSpeed::default(),
)),
laser: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Laser(SingleColourSpeed::default())),
ripple: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Ripple(SingleColourSpeed::default())),
pulse: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Pulse(SingleColour::default())),
comet: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Comet(SingleColour::default())),
flash: <[u8; LED_MSG_LEN]>::from(SetAuraBuiltin::Flash(SingleColour::default())),
multi_static: <[[u8; LED_MSG_LEN]; 4]>::from(SetAuraBuiltin::MultiStatic(
MultiColour::default(),
)),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
pub enum BuiltInModeByte {
Single = 0x00,
Breathing = 0x01,
Strobe = 0x02,
Rainbow = 0x03,
Star = 0x04,
Rain = 0x05,
Highlight = 0x06,
Laser = 0x07,
Ripple = 0x08,
Pulse = 0x0a,
Comet = 0x0b,
Flash = 0x0c,
MultiStatic,
None,
}
impl Default for BuiltInModeByte {
#[inline]
fn default() -> Self {
BuiltInModeByte::Single
}
}
impl From<u8> for BuiltInModeByte {
#[inline]
fn from(byte: u8) -> Self {
match byte {
0x00 => Self::Single,
0x01 => Self::Breathing,
0x02 => Self::Strobe,
0x03 => Self::Rainbow,
0x04 => Self::Star,
0x05 => Self::Rain,
0x06 => Self::Highlight,
0x07 => Self::Laser,
0x08 => Self::Ripple,
0x0a => Self::Pulse,
0x0b => Self::Comet,
0x0c => Self::Flash,
_ => Self::None,
}
}
}
impl From<&u8> for BuiltInModeByte {
#[inline]
fn from(byte: &u8) -> Self {
Self::from(*byte)
}
}
impl From<BuiltInModeByte> for u8 {
#[inline]
fn from(byte: BuiltInModeByte) -> Self {
match byte {
BuiltInModeByte::Single => 0x00,
BuiltInModeByte::Breathing => 0x01,
BuiltInModeByte::Strobe => 0x02,
BuiltInModeByte::Rainbow => 0x03,
BuiltInModeByte::Star => 0x04,
BuiltInModeByte::Rain => 0x05,
BuiltInModeByte::Highlight => 0x06,
BuiltInModeByte::Laser => 0x07,
BuiltInModeByte::Ripple => 0x08,
BuiltInModeByte::Pulse => 0x0a,
BuiltInModeByte::Comet => 0x0b,
BuiltInModeByte::Flash => 0x0c,
BuiltInModeByte::MultiStatic => 0x00,
BuiltInModeByte::None => 0xff,
}
}
}

View File

@@ -1,5 +1,6 @@
use crate::error::AuraError;
use gumdrop::Options;
use serde_derive::{Deserialize, Serialize};
use std::fmt::Debug;
use std::str::FromStr;
@@ -30,7 +31,7 @@ impl FromStr for LedBrightness {
}
}
#[derive(Debug)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Colour(pub u8, pub u8, pub u8);
impl Default for Colour {
fn default() -> Self {
@@ -51,7 +52,7 @@ impl FromStr for Colour {
}
}
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
pub enum Speed {
Low = 0xe1,
Med = 0xeb,
@@ -79,7 +80,7 @@ impl FromStr for Speed {
/// Used for Rainbow mode.
///
/// Enum corresponds to the required integer value
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
pub enum Direction {
Right,
Left,
@@ -106,7 +107,7 @@ impl FromStr for Direction {
}
}
#[derive(Debug, Default, Options)]
#[derive(Debug, Clone, Default, Options, Deserialize, Serialize)]
pub struct TwoColourSpeed {
#[options(help = "print help message")]
help: bool,
@@ -118,7 +119,7 @@ pub struct TwoColourSpeed {
pub speed: Speed,
}
#[derive(Debug, Default, Options)]
#[derive(Debug, Clone, Default, Options, Deserialize, Serialize)]
pub struct SingleSpeed {
#[options(help = "print help message")]
help: bool,
@@ -126,7 +127,7 @@ pub struct SingleSpeed {
pub speed: Speed,
}
#[derive(Debug, Default, Options)]
#[derive(Debug, Clone, Default, Options, Deserialize, Serialize)]
pub struct SingleColour {
#[options(help = "print help message")]
help: bool,
@@ -134,7 +135,7 @@ pub struct SingleColour {
pub colour: Colour,
}
#[derive(Debug, Default, Options)]
#[derive(Debug, Clone, Default, Options, Deserialize, Serialize)]
pub struct MultiColour {
#[options(help = "print help message")]
help: bool,
@@ -148,7 +149,7 @@ pub struct MultiColour {
pub colour4: Colour,
}
#[derive(Debug, Default, Options)]
#[derive(Debug, Clone, Default, Options, Deserialize, Serialize)]
pub struct SingleSpeedDirection {
#[options(help = "print help message")]
help: bool,
@@ -162,7 +163,7 @@ pub struct SingleSpeedDirection {
pub speed: Speed,
}
#[derive(Debug, Default, Options)]
#[derive(Debug, Clone, Default, Options, Deserialize, Serialize)]
pub struct SingleColourSpeed {
#[options(help = "print help message")]
help: bool,
@@ -172,10 +173,16 @@ pub struct SingleColourSpeed {
pub speed: Speed,
}
#[derive(Debug, Options, Clone, Deserialize, Serialize, Default)]
pub struct FreeOpts {
#[options(free)]
free: Vec<String>,
}
/// Byte value for setting the built-in mode.
///
/// Enum corresponds to the required integer value
#[derive(Debug, Options)]
#[derive(Debug, Clone, Options, Deserialize, Serialize)]
pub enum SetAuraBuiltin {
#[options(help = "set a single static colour")]
Stable(SingleColour),

140
rog-client/src/core_dbus.rs Normal file
View File

@@ -0,0 +1,140 @@
use super::*;
use crate::fancy::KeyColourArray;
use dbus::channel::Sender;
use dbus::{blocking::Connection, channel::Token, Message};
use std::error::Error;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::{thread, time::Duration};
/// Simplified way to write a effect block
pub struct AuraDbusWriter {
connection: Box<Connection>,
block_time: u64,
stop: Arc<AtomicBool>,
stop_token: Token,
}
impl AuraDbusWriter {
#[inline]
pub fn new() -> Result<Self, Box<dyn Error>> {
let connection = Connection::new_system()?;
let stop = Arc::new(AtomicBool::new(false));
let stopper2 = stop.clone();
let match_rule = dbus::message::MatchRule::new_signal(DBUS_IFACE, "KeyBacklightChanged");
let stop_token = connection.add_match(match_rule, move |_: (), _, msg| {
dbg!(&msg);
if let Ok(stop) = msg.read1::<bool>() {
if stop {
stopper2.store(true, Ordering::Relaxed);
}
}
true
})?;
Ok(AuraDbusWriter {
connection: Box::new(connection),
block_time: 33333,
stop,
stop_token,
})
}
/// This method must always be called before the very first write to initialise
/// the keyboard LED EC in the correct mode
#[inline]
pub fn init_effect(&self) -> Result<(), Box<dyn std::error::Error>> {
let mode = AuraModes::Aura;
let mut msg =
Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "SetKeyBacklight")?
.append1(serde_json::to_string(&mode)?);
msg.set_no_reply(true);
self.connection.send(msg).unwrap();
Ok(())
}
/// Write a single colour block.
///
/// Intentionally blocks for 10ms after sending to allow the block to
/// be written to the keyboard EC. This should not be async.
#[inline]
pub fn write_colour_block(
&mut self,
key_colour_array: &KeyColourArray,
) -> Result<(), Box<dyn Error>> {
let group = key_colour_array.get();
let mut msg = Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "LedWriteEffect")?
.append3(&group[0].to_vec(), &group[1].to_vec(), &group[2].to_vec())
.append3(&group[3].to_vec(), &group[4].to_vec(), &group[5].to_vec())
.append3(&group[6].to_vec(), &group[7].to_vec(), &group[8].to_vec())
.append2(&group[9].to_vec(), &group[10].to_vec());
msg.set_no_reply(true);
self.connection.send(msg).unwrap();
thread::sleep(Duration::from_micros(self.block_time));
self.connection.process(Duration::from_micros(500))?;
if self.stop.load(Ordering::Relaxed) {
self.connection.remove_match(self.stop_token)?;
println!("Keyboard backlight was changed, exiting");
std::process::exit(1)
}
Ok(())
}
#[inline]
pub fn write_multizone(
&mut self,
group: &[[u8; LED_MSG_LEN]; 4],
) -> Result<(), Box<dyn std::error::Error>> {
let mut msg =
Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "LedWriteMultizone")?
.append1(&group[0].to_vec())
.append1(&group[1].to_vec())
.append1(&group[2].to_vec())
.append1(&group[3].to_vec());
msg.set_no_reply(true);
self.connection.send(msg).unwrap();
Ok(())
}
#[inline]
pub fn write_bytes(&self, mode: &AuraModes) -> Result<(), Box<dyn std::error::Error>> {
let mut msg =
Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "SetKeyBacklight")?
.append1(serde_json::to_string(mode)?);
msg.set_no_reply(true);
self.connection.send(msg).unwrap();
Ok(())
}
#[inline]
pub fn write_fan_mode(&self, level: u8) -> Result<(), Box<dyn std::error::Error>> {
let mut msg = Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "SetFanMode")?
.append1(level);
msg.set_no_reply(true);
self.connection.send(msg).unwrap();
Ok(())
}
#[inline]
pub fn write_charge_limit(&self, level: u8) -> Result<(), Box<dyn std::error::Error>> {
let mut msg = Message::new_method_call(DBUS_NAME, DBUS_PATH, DBUS_IFACE, "SetChargeLimit")?
.append1(level);
msg.set_no_reply(true);
self.connection.send(msg).unwrap();
Ok(())
}
#[inline]
pub fn write_builtin_mode(&self, mode: &AuraModes) -> Result<(), Box<dyn std::error::Error>> {
self.write_bytes(mode)
}
#[inline]
pub fn write_brightness(&self, level: u8) -> Result<String, Box<dyn std::error::Error>> {
self.write_bytes(&AuraModes::LedBrightness(level))?;
Ok(String::new())
}
}

View File

@@ -3,26 +3,25 @@ pub static DBUS_PATH: &str = "/org/rogcore/Daemon";
pub static DBUS_IFACE: &str = "org.rogcore.Daemon";
pub const LED_MSG_LEN: usize = 17;
mod builtins;
pub use builtins::*;
pub mod aura_modes;
use aura_modes::AuraModes;
/// Contains mostly only what is required for parsing CLI options
pub mod cli_options;
mod fancy;
mod aura_dbus;
pub use aura_dbus::*;
/// Enables you to create fancy RGB effects
pub mod fancy;
pub use fancy::*;
/// The main dbus group for system controls, e.g, fan control, keyboard LED's
pub mod core_dbus;
mod animatrix_dbus;
pub use animatrix_dbus::*;
/// Specific dbus for writing to the AniMe Matrix display (if supported)
pub mod anime_dbus;
mod anime_matrix;
pub use anime_matrix::*;
/// Helper functions for the AniMe display
pub mod anime_matrix;
pub mod error;
use crate::cli_options::*;
/// Writes aout the correct byte string for brightness
///
@@ -56,7 +55,7 @@ pub fn aura_brightness_bytes(brightness: u8) -> [u8; 17] {
]
}
/// Parses `SetAuraBuiltin` in to packet data
/// Parses `AuraCommands` in to packet data
///
/// Byte structure:
///
@@ -133,40 +132,41 @@ pub fn aura_brightness_bytes(brightness: u8) -> [u8; 17] {
/// ```
///
/// This descriptor is also used for the per-key LED settings
impl From<&SetAuraBuiltin> for [u8; LED_MSG_LEN] {
fn from(mode: &SetAuraBuiltin) -> Self {
impl From<&AuraModes> for [u8; LED_MSG_LEN] {
fn from(mode: &AuraModes) -> Self {
let mut msg = [0u8; LED_MSG_LEN];
msg[0] = 0x5d;
msg[1] = 0xb3;
match mode {
SetAuraBuiltin::Stable(_) => msg[3] = 0x00,
SetAuraBuiltin::Breathe(_) => msg[3] = 0x01,
SetAuraBuiltin::Strobe(_) => msg[3] = 0x02,
SetAuraBuiltin::Rainbow(_) => msg[3] = 0x03,
SetAuraBuiltin::Star(_) => msg[3] = 0x04,
SetAuraBuiltin::Rain(_) => msg[3] = 0x05,
SetAuraBuiltin::Highlight(_) => msg[3] = 0x06,
SetAuraBuiltin::Laser(_) => msg[3] = 0x07,
SetAuraBuiltin::Ripple(_) => msg[3] = 0x08,
SetAuraBuiltin::Pulse(_) => msg[3] = 0x0a,
SetAuraBuiltin::Comet(_) => msg[3] = 0x0b,
SetAuraBuiltin::Flash(_) => msg[3] = 0x0c,
AuraModes::LedBrightness(n) => return aura_brightness_bytes(*n),
AuraModes::Stable(_) => msg[3] = 0x00,
AuraModes::Breathe(_) => msg[3] = 0x01,
AuraModes::Strobe(_) => msg[3] = 0x02,
AuraModes::Rainbow(_) => msg[3] = 0x03,
AuraModes::Star(_) => msg[3] = 0x04,
AuraModes::Rain(_) => msg[3] = 0x05,
AuraModes::Highlight(_) => msg[3] = 0x06,
AuraModes::Laser(_) => msg[3] = 0x07,
AuraModes::Ripple(_) => msg[3] = 0x08,
AuraModes::Pulse(_) => msg[3] = 0x0a,
AuraModes::Comet(_) => msg[3] = 0x0b,
AuraModes::Flash(_) => msg[3] = 0x0c,
_ => panic!("Mode not convertable to array"),
}
match mode {
SetAuraBuiltin::Rainbow(settings) => {
AuraModes::Rainbow(settings) => {
msg[7] = settings.speed as u8;
msg[8] = settings.direction as u8;
}
SetAuraBuiltin::Star(settings) => {
AuraModes::Star(settings) => {
msg[4] = settings.colour.0;
msg[5] = settings.colour.1;
msg[6] = settings.colour.2;
msg[7] = settings.speed as u8;
msg[9] = settings.colour2.2;
}
SetAuraBuiltin::Breathe(settings) => {
AuraModes::Breathe(settings) => {
msg[4] = settings.colour.0;
msg[5] = settings.colour.1;
msg[6] = settings.colour.2;
@@ -175,21 +175,21 @@ impl From<&SetAuraBuiltin> for [u8; LED_MSG_LEN] {
msg[11] = settings.colour2.1;
msg[12] = settings.colour2.2;
}
SetAuraBuiltin::Strobe(settings) | SetAuraBuiltin::Rain(settings) => {
AuraModes::Strobe(settings) | AuraModes::Rain(settings) => {
msg[7] = settings.speed as u8;
}
SetAuraBuiltin::Highlight(settings)
| SetAuraBuiltin::Laser(settings)
| SetAuraBuiltin::Ripple(settings) => {
AuraModes::Highlight(settings)
| AuraModes::Laser(settings)
| AuraModes::Ripple(settings) => {
msg[4] = settings.colour.0;
msg[5] = settings.colour.1;
msg[6] = settings.colour.2;
msg[7] = settings.speed as u8;
}
SetAuraBuiltin::Stable(settings)
| SetAuraBuiltin::Pulse(settings)
| SetAuraBuiltin::Comet(settings)
| SetAuraBuiltin::Flash(settings) => {
AuraModes::Stable(settings)
| AuraModes::Pulse(settings)
| AuraModes::Comet(settings)
| AuraModes::Flash(settings) => {
msg[4] = settings.colour.0;
msg[5] = settings.colour.1;
msg[6] = settings.colour.2;
@@ -200,16 +200,16 @@ impl From<&SetAuraBuiltin> for [u8; LED_MSG_LEN] {
}
}
impl From<SetAuraBuiltin> for [u8; LED_MSG_LEN] {
impl From<AuraModes> for [u8; LED_MSG_LEN] {
#[inline]
fn from(mode: SetAuraBuiltin) -> Self {
fn from(mode: AuraModes) -> Self {
<[u8; LED_MSG_LEN]>::from(&mode)
}
}
impl From<SetAuraBuiltin> for [[u8; LED_MSG_LEN]; 4] {
impl From<AuraModes> for [[u8; LED_MSG_LEN]; 4] {
#[inline]
fn from(mode: SetAuraBuiltin) -> Self {
fn from(mode: AuraModes) -> Self {
let mut msg = [[0u8; LED_MSG_LEN]; 4];
for (i, row) in msg.iter_mut().enumerate() {
row[0] = 0x5d;
@@ -218,7 +218,7 @@ impl From<SetAuraBuiltin> for [[u8; LED_MSG_LEN]; 4] {
}
match mode {
SetAuraBuiltin::MultiStatic(settings) => {
AuraModes::MultiStatic(settings) => {
msg[0][4] = settings.colour1.0;
msg[0][5] = settings.colour1.1;
msg[0][6] = settings.colour1.2;