rog-aura: reorganise per-key effects

This commit is contained in:
Luke D. Jones
2022-08-25 18:25:04 +12:00
parent 503aa20257
commit a8a99ac1d1
6 changed files with 177 additions and 129 deletions

View File

@@ -1,52 +1,56 @@
//! Using a combination of key-colour array plus a key layout to generate outputs.
use rog_aura::{keys::Key, Colour, PerKey, Sequences, Speed};
use rog_aura::{keys::Key, layouts::KeyLayout, ActionData, Colour, Sequences, Speed};
use rog_dbus::RogDbusClientBlocking;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let layout = KeyLayout::gx502_layout();
let (client, _) = RogDbusClientBlocking::new().unwrap();
let mut seq = Sequences::new();
let mut key = PerKey::new_breathe(Key::W, Colour(255, 127, 0), Colour(127, 0, 255), Speed::Med);
let mut key =
ActionData::new_breathe(Key::W, Colour(255, 127, 0), Colour(127, 0, 255), Speed::Med);
seq.push(key.clone());
key.key = Key::A;
key.set_key(Key::A);
seq.push(key.clone());
key.key = Key::S;
key.set_key(Key::S);
seq.push(key.clone());
key.key = Key::D;
key.set_key(Key::D);
seq.push(key.clone());
let mut key = PerKey::new_breathe(
let mut key = ActionData::new_breathe(
Key::Q,
Colour(127, 127, 127),
Colour(127, 255, 255),
Speed::Low,
);
seq.push(key.clone());
key.key = Key::E;
key.set_key(Key::E);
seq.push(key.clone());
let mut key = PerKey::new_breathe(
let mut key = ActionData::new_breathe(
Key::N1,
Colour(166, 127, 166),
Colour(127, 155, 20),
Speed::High,
);
key.key = Key::Tilde;
key.set_key(Key::Tilde);
seq.push(key.clone());
key.key = Key::N2;
key.set_key(Key::N2);
seq.push(key.clone());
key.key = Key::N3;
key.set_key(Key::N3);
seq.push(key.clone());
key.key = Key::N4;
key.set_key(Key::N4);
seq.push(key.clone());
loop {
seq.next_state();
seq.next_state(&layout);
let packets = seq.create_packets();
println!("{:#0x?}", &packets[0]);
client.proxies().led().per_key_raw(packets)?;
std::thread::sleep(std::time::Duration::from_millis(100));
std::thread::sleep(std::time::Duration::from_millis(300));
}
}

View File

@@ -295,27 +295,35 @@ impl CtrlKbdLed {
Ok(())
}
/// Write an effect block. This is for per-key
pub fn write_per_key_block(&mut self, effect: &PerKeyRaw) -> Result<(), RogError> {
if !self.per_key_mode_active {
/// Write an effect block. This is for per-key, but can be repurposed to
/// write the raw factory mode packets - when doing this it is expected that
/// only the first `Vec` (`effect[0]`) is valid.
pub fn write_effect_block(&mut self, effect: &PerKeyRaw) -> Result<(), RogError> {
let pkt_type = effect[0][1];
const PER_KEY_TYPE: u8 = 0xbc;
if pkt_type != PER_KEY_TYPE {
self.per_key_mode_active = false;
if let LEDNode::Rog(hid_raw) = &self.led_node {
let init = KeyColourArray::get_init_msg();
hid_raw.write_bytes(&init)?;
hid_raw.write_bytes(&effect[0])?;
hid_raw.write_bytes(&LED_SET)?;
// hid_raw.write_bytes(&LED_APPLY)?;
}
self.per_key_mode_active = true;
}
if let LEDNode::Rog(hid_raw) = &self.led_node {
// if self.flip_effect_write {
// for row in effect.iter().rev() {
// hid_raw.write_bytes(row)?;
// }
// } else {
for row in effect.iter() {
hid_raw.write_bytes(row)?;
} else {
if !self.per_key_mode_active {
if let LEDNode::Rog(hid_raw) = &self.led_node {
let init = KeyColourArray::get_init_msg();
hid_raw.write_bytes(&init)?;
}
self.per_key_mode_active = true;
}
// }
if let LEDNode::Rog(hid_raw) = &self.led_node {
for row in effect.iter() {
hid_raw.write_bytes(row)?;
}
}
self.flip_effect_write = !self.flip_effect_write;
}
self.flip_effect_write = !self.flip_effect_write;
Ok(())
}

View File

@@ -212,7 +212,7 @@ impl CtrlKbdLedZbus {
async fn per_key_raw(&self, data: PerKeyRaw) -> zbus::fdo::Result<()> {
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.write_per_key_block(&data)?;
ctrl.write_effect_block(&data)?;
}
Ok(())
}

View File

@@ -437,6 +437,25 @@ impl From<&AuraEffect> for [u8; LED_MSG_LEN] {
}
}
impl From<&AuraEffect> for Vec<u8> {
fn from(aura: &AuraEffect) -> Self {
let mut msg = vec![0u8; LED_MSG_LEN];
msg[0] = 0x5d;
msg[1] = 0xb3;
msg[2] = aura.zone as u8;
msg[3] = aura.mode as u8;
msg[4] = aura.colour1.0;
msg[5] = aura.colour1.1;
msg[6] = aura.colour1.2;
msg[7] = aura.speed as u8;
msg[8] = aura.direction as u8;
msg[10] = aura.colour2.0;
msg[11] = aura.colour2.1;
msg[12] = aura.colour2.2;
msg
}
}
#[cfg(test)]
mod tests {
use crate::{AuraEffect, AuraModeNum, AuraZone, Colour, Direction, Speed, LED_MSG_LEN};

View File

@@ -26,6 +26,7 @@ impl Default for KeyColourArray {
impl KeyColourArray {
pub fn new() -> Self {
let mut set = vec![vec![0u8; 64]; 11];
// set[0].copy_from_slice(&KeyColourArray::get_init_msg());
for (count, row) in set.iter_mut().enumerate() {
row[0] = 0x5d; // Report ID
row[1] = 0xbc; // Mode = custom??, 0xb3 is builtin
@@ -239,10 +240,6 @@ impl KeyColourArray {
}
}
pub trait KeyLayout {
fn get_rows(&self) -> &Vec<[Key; 17]>;
}
impl From<KeyColourArray> for PerKeyRaw {
fn from(k: KeyColourArray) -> Self {
k.0

View File

@@ -1,28 +1,9 @@
use serde_derive::{Deserialize, Serialize};
use crate::{keys::Key, Colour, KeyColourArray, PerKeyRaw, Speed};
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct PerKey {
pub key: Key,
action: ActionData,
/// The end resulting colour after stepping through effect
#[serde(skip)]
colour: Colour,
}
impl PerKey {
pub fn new_breathe(key: Key, colour1: Colour, colour2: Colour, speed: Speed) -> Self {
Self {
key,
action: ActionData::new_breathe(colour1, colour2, speed),
colour: Default::default(),
}
}
}
use crate::{keys::Key, layouts::KeyLayout, Colour, KeyColourArray, PerKeyRaw, Speed};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub(super) enum ActionData {
pub(super) enum Action {
Static(Colour),
Breathe {
/// The starting colour
@@ -41,27 +22,101 @@ pub(super) enum ActionData {
},
}
impl ActionData {
fn new_breathe(colour1: Colour, colour2: Colour, speed: Speed) -> Self {
Self::Breathe {
colour1,
colour2,
speed,
colour_actual: colour1,
count_flipped: false,
use_colour1: true,
}
}
}
impl Default for ActionData {
impl Default for Action {
fn default() -> Self {
Self::Static(Colour::default())
}
}
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct ActionData {
key: Key,
action: Action,
// TODO: time
/// The end resulting colour after stepping through effect
#[serde(skip)]
colour: Colour,
}
impl ActionData {
pub fn set_key(&mut self, key: Key) {
self.key = key
}
pub fn new_static(key: Key, colour: Colour) -> Self {
Self {
key,
action: Action::Static(colour),
colour: Default::default(),
}
}
pub fn new_breathe(key: Key, colour1: Colour, colour2: Colour, speed: Speed) -> Self {
Self {
key,
action: Action::Breathe {
colour1,
colour2,
speed,
colour_actual: colour1,
count_flipped: false,
use_colour1: true,
},
colour: Default::default(),
}
}
pub fn next_state(&mut self, _layout: &KeyLayout) {
match &mut self.action {
Action::Static(c) => self.colour = *c,
Action::Breathe {
colour1,
colour2,
speed,
colour_actual,
count_flipped: flipped,
use_colour1,
} => {
let speed = 4 - <u8>::from(*speed);
let colour: &mut Colour;
if *colour_actual == Colour(0, 0, 0) {
*use_colour1 = !*use_colour1;
}
if !*use_colour1 {
colour = colour2;
} else {
colour = colour1;
}
let r1_scale = colour.0 / speed / 2;
let g1_scale = colour.1 / speed / 2;
let b1_scale = colour.2 / speed / 2;
if *colour_actual == Colour(0, 0, 0) {
*flipped = true;
} else if colour_actual >= colour {
*flipped = false;
}
if !*flipped {
colour_actual.0 = colour_actual.0.saturating_sub(r1_scale);
colour_actual.1 = colour_actual.1.saturating_sub(g1_scale);
colour_actual.2 = colour_actual.2.saturating_sub(b1_scale);
} else {
colour_actual.0 = colour_actual.0.saturating_add(r1_scale);
colour_actual.1 = colour_actual.1.saturating_add(g1_scale);
colour_actual.2 = colour_actual.2.saturating_add(b1_scale);
}
self.colour = *colour_actual;
}
}
}
}
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct Sequences(Vec<PerKey>);
pub struct Sequences(Vec<ActionData>);
impl Sequences {
#[inline]
@@ -70,12 +125,12 @@ impl Sequences {
}
#[inline]
pub fn push(&mut self, action: PerKey) {
pub fn push(&mut self, action: ActionData) {
self.0.push(action);
}
#[inline]
pub fn insert(&mut self, index: usize, action: PerKey) {
pub fn insert(&mut self, index: usize, action: ActionData) {
self.0.insert(index, action);
}
@@ -83,60 +138,16 @@ impl Sequences {
/// is not in range then `None` is returned, otherwise the `ActionData` at that location
/// is yeeted and returned.
#[inline]
pub fn remove_item(&mut self, index: usize) -> Option<PerKey> {
pub fn remove_item(&mut self, index: usize) -> Option<ActionData> {
if index < self.0.len() {
return Some(self.0.remove(index));
}
None
}
pub fn next_state(&mut self) {
pub fn next_state(&mut self, layout: &KeyLayout) {
for effect in self.0.iter_mut() {
match &mut effect.action {
ActionData::Static(c) => effect.colour = *c,
ActionData::Breathe {
colour1,
colour2,
speed,
colour_actual,
count_flipped: flipped,
use_colour1,
} => {
let speed = 4 - <u8>::from(*speed);
let colour: &mut Colour;
if *colour_actual == Colour(0, 0, 0) {
*use_colour1 = !*use_colour1;
}
if !*use_colour1 {
colour = colour2;
} else {
colour = colour1;
}
let r1_scale = colour.0 / speed / 2;
let g1_scale = colour.1 / speed / 2;
let b1_scale = colour.2 / speed / 2;
if *colour_actual == Colour(0, 0, 0) {
*flipped = true;
} else if colour_actual >= colour {
*flipped = false;
}
if !*flipped {
colour_actual.0 = colour_actual.0.saturating_sub(r1_scale);
colour_actual.1 = colour_actual.1.saturating_sub(g1_scale);
colour_actual.2 = colour_actual.2.saturating_sub(b1_scale);
} else {
colour_actual.0 = colour_actual.0.saturating_add(r1_scale);
colour_actual.1 = colour_actual.1.saturating_add(g1_scale);
colour_actual.2 = colour_actual.2.saturating_add(b1_scale);
}
effect.colour = *colour_actual;
}
}
effect.next_state(layout);
}
}
@@ -155,18 +166,19 @@ impl Sequences {
#[cfg(test)]
mod tests {
use crate::{keys::Key, ActionData, Colour, PerKey, Sequences, Speed};
use crate::{keys::Key, layouts::KeyLayout, Action, ActionData, Colour, Sequences, Speed};
#[test]
fn single_key_next_state_then_create() {
let layout = KeyLayout::gx502_layout();
let mut seq = Sequences::new();
seq.0.push(PerKey {
seq.0.push(ActionData {
key: Key::F,
action: ActionData::Static(Colour(255, 127, 0)),
action: Action::Static(Colour(255, 127, 0)),
colour: Default::default(),
});
seq.next_state();
seq.next_state(&layout);
let packets = seq.create_packets();
assert_eq!(packets[0][0], 0x5d);
@@ -177,14 +189,22 @@ mod tests {
#[test]
fn cycle_breathe() {
let layout = KeyLayout::gx502_layout();
let mut seq = Sequences::new();
seq.0.push(PerKey {
seq.0.push(ActionData {
key: Key::F,
action: ActionData::new_breathe(Colour(255, 127, 0), Colour(127, 0, 255), Speed::Med),
action: Action::Breathe {
colour1: Colour(255, 127, 0),
colour2: Colour(127, 0, 255),
speed: Speed::Med,
colour_actual: Colour(255, 127, 0),
count_flipped: false,
use_colour1: true,
},
colour: Default::default(),
});
seq.next_state();
seq.next_state(&layout);
let packets = seq.create_packets();
assert_eq!(packets[0][0], 0x5d);
@@ -194,7 +214,7 @@ mod tests {
// dbg!(&packets[5][33..=35]);
seq.next_state();
seq.next_state(&layout);
let packets = seq.create_packets();
assert_eq!(packets[0][0], 0x5d);