From 59f54b76f6bec85029540cb1fe619c9014aa43e9 Mon Sep 17 00:00:00 2001 From: Luke D Jones Date: Sun, 11 Apr 2021 20:13:07 +1200 Subject: [PATCH] aura: split out all aura related files to rog-aura crate --- daemon/src/config_aura.rs | 90 ++++++++++++- .../src/{aura_modes.rs => builtin_modes.rs} | 124 ++++-------------- rog-aura/src/data.rs | 73 ----------- rog-aura/src/lib.rs | 13 +- .../src/{aura_perkey.rs => per_key_rgb.rs} | 3 +- 5 files changed, 119 insertions(+), 184 deletions(-) rename rog-aura/src/{aura_modes.rs => builtin_modes.rs} (66%) delete mode 100644 rog-aura/src/data.rs rename rog-aura/src/{aura_perkey.rs => per_key_rgb.rs} (98%) diff --git a/daemon/src/config_aura.rs b/daemon/src/config_aura.rs index d5c1becf..15e5c55a 100644 --- a/daemon/src/config_aura.rs +++ b/daemon/src/config_aura.rs @@ -1,6 +1,6 @@ use crate::laptops::LaptopLedData; use log::{error, info, warn}; -use rog_aura::{AuraEffect, AuraModeNum, AuraMultiZone, AuraZone, LedBrightness}; +use rog_aura::{AuraEffect, AuraModeNum, AuraZone, LedBrightness}; use serde_derive::{Deserialize, Serialize}; use std::collections::BTreeMap; use std::fs::{File, OpenOptions}; @@ -146,3 +146,91 @@ impl AuraConfig { None } } + + +#[derive(Deserialize, Serialize)] +pub struct AuraMultiZone { + static_: [AuraEffect; 4], + breathe: [AuraEffect; 4], +} + +impl AuraMultiZone { + pub fn set(&mut self, effect: AuraEffect) { + if effect.mode == AuraModeNum::Static { + match effect.zone { + AuraZone::None => {} + AuraZone::One => self.static_[0] = effect, + AuraZone::Two => self.static_[1] = effect, + AuraZone::Three => self.static_[2] = effect, + AuraZone::Four => self.static_[3] = effect, + } + } else if effect.mode == AuraModeNum::Breathe { + match effect.zone { + AuraZone::None => {} + AuraZone::One => self.breathe[0] = effect, + AuraZone::Two => self.breathe[1] = effect, + AuraZone::Three => self.breathe[2] = effect, + AuraZone::Four => self.breathe[3] = effect, + } + } + } + + pub fn static_(&self) -> &[AuraEffect; 4] { + &self.static_ + } + + pub fn breathe(&self) -> &[AuraEffect; 4] { + &self.breathe + } +} + +impl Default for AuraMultiZone { + fn default() -> Self { + Self { + static_: [ + AuraEffect { + mode: AuraModeNum::Static, + zone: AuraZone::One, + ..Default::default() + }, + AuraEffect { + mode: AuraModeNum::Static, + zone: AuraZone::Two, + ..Default::default() + }, + AuraEffect { + mode: AuraModeNum::Static, + zone: AuraZone::Three, + ..Default::default() + }, + AuraEffect { + mode: AuraModeNum::Static, + zone: AuraZone::Four, + ..Default::default() + }, + ], + breathe: [ + AuraEffect { + mode: AuraModeNum::Breathe, + zone: AuraZone::One, + ..Default::default() + }, + AuraEffect { + mode: AuraModeNum::Breathe, + zone: AuraZone::Two, + ..Default::default() + }, + AuraEffect { + mode: AuraModeNum::Breathe, + zone: AuraZone::Three, + ..Default::default() + }, + AuraEffect { + mode: AuraModeNum::Breathe, + zone: AuraZone::Four, + ..Default::default() + }, + ], + } + } +} \ No newline at end of file diff --git a/rog-aura/src/aura_modes.rs b/rog-aura/src/builtin_modes.rs similarity index 66% rename from rog-aura/src/aura_modes.rs rename to rog-aura/src/builtin_modes.rs index 15d68705..16cc4a4e 100644 --- a/rog-aura/src/aura_modes.rs +++ b/rog-aura/src/builtin_modes.rs @@ -6,11 +6,13 @@ pub const LED_INIT5: [u8; 6] = [0x5e, 0x05, 0x20, 0x31, 0, 0x08]; use serde_derive::{Deserialize, Serialize}; use std::str::FromStr; +#[cfg(feature = "dbus")] use zvariant_derive::Type; use crate::{LED_MSG_LEN, error::Error}; -#[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize, Type)] +#[cfg_attr(feature = "dbus", derive(Type))] +#[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize)] pub enum LedBrightness { Off, Low, @@ -36,7 +38,8 @@ impl From for LedBrightness { } } -#[derive(Debug, Clone, PartialEq, Copy, Deserialize, Serialize, Type)] +#[cfg_attr(feature = "dbus", derive(Type))] +#[derive(Debug, Clone, PartialEq, Copy, Deserialize, Serialize)] pub struct Colour(pub u8, pub u8, pub u8); impl Default for Colour { @@ -59,7 +62,8 @@ impl FromStr for Colour { } } -#[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize, Type)] +#[cfg_attr(feature = "dbus", derive(Type))] +#[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize)] pub enum Speed { Low = 0xe1, Med = 0xeb, @@ -87,7 +91,8 @@ impl FromStr for Speed { /// Used for Rainbow mode. /// /// Enum corresponds to the required integer value -#[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize, Type)] +#[cfg_attr(feature = "dbus", derive(Type))] +#[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize)] pub enum Direction { Right, Left, @@ -114,8 +119,10 @@ impl FromStr for Direction { } } +/// Enum of modes that convert to the actual number required by a USB HID packet +#[cfg_attr(feature = "dbus", derive(Type))] #[derive( - Debug, Type, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Deserialize, Serialize, + Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Deserialize, Serialize, )] pub enum AuraModeNum { Static = 0, @@ -190,95 +197,9 @@ impl From for AuraModeNum { } } -#[derive(Deserialize, Serialize)] -pub struct AuraMultiZone { - static_: [AuraEffect; 4], - breathe: [AuraEffect; 4], -} - -impl AuraMultiZone { - pub fn set(&mut self, effect: AuraEffect) { - if effect.mode == AuraModeNum::Static { - match effect.zone { - AuraZone::None => {} - AuraZone::One => self.static_[0] = effect, - AuraZone::Two => self.static_[1] = effect, - AuraZone::Three => self.static_[2] = effect, - AuraZone::Four => self.static_[3] = effect, - } - } else if effect.mode == AuraModeNum::Breathe { - match effect.zone { - AuraZone::None => {} - AuraZone::One => self.breathe[0] = effect, - AuraZone::Two => self.breathe[1] = effect, - AuraZone::Three => self.breathe[2] = effect, - AuraZone::Four => self.breathe[3] = effect, - } - } - } - - pub fn static_(&self) -> &[AuraEffect; 4] { - &self.static_ - } - - pub fn breathe(&self) -> &[AuraEffect; 4] { - &self.breathe - } -} - -impl Default for AuraMultiZone { - fn default() -> Self { - Self { - static_: [ - AuraEffect { - mode: AuraModeNum::Static, - zone: AuraZone::One, - ..Default::default() - }, - AuraEffect { - mode: AuraModeNum::Static, - zone: AuraZone::Two, - ..Default::default() - }, - AuraEffect { - mode: AuraModeNum::Static, - zone: AuraZone::Three, - ..Default::default() - }, - AuraEffect { - mode: AuraModeNum::Static, - zone: AuraZone::Four, - ..Default::default() - }, - ], - breathe: [ - AuraEffect { - mode: AuraModeNum::Breathe, - zone: AuraZone::One, - ..Default::default() - }, - AuraEffect { - mode: AuraModeNum::Breathe, - zone: AuraZone::Two, - ..Default::default() - }, - AuraEffect { - mode: AuraModeNum::Breathe, - zone: AuraZone::Three, - ..Default::default() - }, - AuraEffect { - mode: AuraModeNum::Breathe, - zone: AuraZone::Four, - ..Default::default() - }, - ], - } - } -} - /// Base effects have no zoning, while multizone is 1-4 -#[derive(Debug, Type, Copy, Clone, PartialEq, Deserialize, Serialize)] +#[cfg_attr(feature = "dbus", derive(Type))] +#[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize)] pub enum AuraZone { None, One, @@ -287,8 +208,12 @@ pub enum AuraZone { Four, } -/// Default factory modes structure -#[derive(Debug, Type, Clone, Deserialize, Serialize)] +/// Default factory modes structure. This easily converts to an USB HID packet with: +/// ```rust +/// let bytes: [u8; LED_MSG_LEN] = mode.into(); +/// ``` +#[cfg_attr(feature = "dbus", derive(Type))] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct AuraEffect { /// The effect type pub mode: AuraModeNum, @@ -344,11 +269,11 @@ impl Default for AuraEffect { /// Parses `AuraEffect` in to packet data for writing to the USB interface /// -/// Byte structure: +/// Byte structure where colour is RGB, one byte per R, G, B: /// ```ignore -/// | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| -/// |---|---|---|---|---|---|---|---|---|---|---|---|---| -/// |5d |b3 |00 |03 |ff |00 |00 |00 |00 |00 |00 |ff |00 | +/// | 0 | 1 | 2 | 3 | 4, 5, 6 | 7 | 8 | 9 | 10, 11, 12| +/// |---|---|-----|-----|---------|------|----------|---|-----------| +/// |5d |b3 |Zone |Mode |Colour 1 |Speed |Direction |00 |Colour 2 | /// ``` impl From<&AuraEffect> for [u8; LED_MSG_LEN] { fn from(aura: &AuraEffect) -> Self { @@ -365,7 +290,6 @@ impl From<&AuraEffect> for [u8; LED_MSG_LEN] { msg[10] = aura.colour2.0; msg[11] = aura.colour2.1; msg[12] = aura.colour2.2; - msg } } diff --git a/rog-aura/src/data.rs b/rog-aura/src/data.rs deleted file mode 100644 index 9280be6b..00000000 --- a/rog-aura/src/data.rs +++ /dev/null @@ -1,73 +0,0 @@ -use serde_derive::{Deserialize, Serialize}; -#[cfg(feature = "dbus")] -use zvariant_derive::Type; - -/// The first 7 bytes of a USB packet are accounted for by `USB_PREFIX1` and `USB_PREFIX2` -const BLOCK_START: usize = 7; -/// *Not* inclusive, the byte before this is the final for each "pane" -const BLOCK_END: usize = 634; -/// Individual usable data length of each USB packet -const PANE_LEN: usize = BLOCK_END - BLOCK_START; -/// The length of usable data -pub const ANIME_DATA_LEN: usize = PANE_LEN * 2; - -const USB_PREFIX1: [u8; 7] = [0x5e, 0xc0, 0x02, 0x01, 0x00, 0x73, 0x02]; -const USB_PREFIX2: [u8; 7] = [0x5e, 0xc0, 0x02, 0x74, 0x02, 0x73, 0x02]; - -/// The minimal serializable data that can be transferred over wire types. -/// Other data structures in `rog_anime` will convert to this. -#[cfg_attr(feature = "dbus", derive(Type))] -#[derive(Debug, Clone, Deserialize, Serialize)] -pub struct AnimeDataBuffer(Vec); - -impl Default for AnimeDataBuffer { - fn default() -> Self { - Self::new() - } -} - -impl AnimeDataBuffer { - #[inline] - pub fn new() -> Self { - AnimeDataBuffer(vec![0u8; ANIME_DATA_LEN]) - } - - /// Get the inner data buffer - #[inline] - pub fn get(&self) -> &[u8] { - &self.0 - } - - /// Get a mutable slice of the inner buffer - #[inline] - pub fn get_mut(&mut self) -> &mut [u8] { - &mut self.0 - } - - /// Create from a vector of bytes - /// - /// # Panics - /// Will panic if the vector length is not `ANIME_DATA_LEN` - #[inline] - pub fn from_vec(input: Vec) -> Self { - assert_eq!(input.len(), ANIME_DATA_LEN); - Self(input) - } -} - -/// The two packets to be written to USB -pub type AnimePacketType = [[u8; 640]; 2]; - -impl From for AnimePacketType { - #[inline] - fn from(anime: AnimeDataBuffer) -> Self { - assert!(anime.0.len() == ANIME_DATA_LEN); - let mut buffers = [[0; 640]; 2]; - for (idx, chunk) in anime.0.as_slice().chunks(PANE_LEN).enumerate() { - buffers[idx][BLOCK_START..BLOCK_END].copy_from_slice(chunk); - } - buffers[0][..7].copy_from_slice(&USB_PREFIX1); - buffers[1][..7].copy_from_slice(&USB_PREFIX2); - buffers - } -} diff --git a/rog-aura/src/lib.rs b/rog-aura/src/lib.rs index 97a28f75..3a5355bd 100644 --- a/rog-aura/src/lib.rs +++ b/rog-aura/src/lib.rs @@ -1,18 +1,13 @@ -/// The main data conversion for transfering in shortform over dbus or other, -/// or writing directly to the USB device -mod data; -pub use data::*; - /// A container of images/grids/gifs/pauses which can be iterated over to generate /// cool effects mod sequencer; pub use sequencer::*; -mod aura_modes; -pub use aura_modes::*; +mod builtin_modes; +pub use builtin_modes::*; -mod aura_perkey; -pub use aura_perkey::*; +mod per_key_rgb; +pub use per_key_rgb::*; pub mod usb; diff --git a/rog-aura/src/aura_perkey.rs b/rog-aura/src/per_key_rgb.rs similarity index 98% rename from rog-aura/src/aura_perkey.rs rename to rog-aura/src/per_key_rgb.rs index 6c4da418..5fa7d48b 100644 --- a/rog-aura/src/aura_perkey.rs +++ b/rog-aura/src/per_key_rgb.rs @@ -33,7 +33,8 @@ impl KeyColourArray { KeyColourArray(set) } - /// Initialise and clear the keyboard for custom effects + /// Initialise and clear the keyboard for custom effects, this must be done for + /// every time mode switches from builtin to custom #[inline] pub const fn get_init_msg() -> [u8; 64] { let mut init = [0u8; 64];