Better structure

This commit is contained in:
Luke
2020-04-15 22:10:13 +12:00
parent 732295f8b7
commit 89158cdc98
5 changed files with 137 additions and 112 deletions

6
Cargo.lock generated
View File

@@ -57,8 +57,7 @@ dependencies = [
[[package]] [[package]]
name = "gumdrop" name = "gumdrop"
version = "0.7.0" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/murarth/gumdrop.git#61a279eab342381e417b36d06f3f2b67eb21fa3b"
checksum = "ee50908bc1beeac1f2902e0b4e0cd0d844e716f5ebdc6f0cfc1163fe5e10bcde"
dependencies = [ dependencies = [
"gumdrop_derive", "gumdrop_derive",
] ]
@@ -66,8 +65,7 @@ dependencies = [
[[package]] [[package]]
name = "gumdrop_derive" name = "gumdrop_derive"
version = "0.7.0" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/murarth/gumdrop.git#61a279eab342381e417b36d06f3f2b67eb21fa3b"
checksum = "90454ce4de40b7ca6a8968b5ef367bdab48413962588d0d2b1638d60090c35d7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@@ -6,4 +6,4 @@ edition = "2018"
[dependencies] [dependencies]
rusb = "0.5" rusb = "0.5"
gumdrop = "0.7" gumdrop = { git = "https://github.com/murarth/gumdrop.git" }

View File

@@ -38,8 +38,17 @@ impl Error for AuraError {
} }
} }
#[derive(Default, Debug, PartialEq)] #[derive(Default, Debug, PartialEq, Options)]
pub struct Colour(u8, u8, u8); pub struct Colour {
#[options(help = "print help message")]
help: bool,
#[options(help = "red: eg, 255")]
red: u8,
#[options(help = "green: eg, 123")]
green: u8,
#[options(help = "blue: eg, 166")]
blue: u8,
}
impl FromStr for Colour { impl FromStr for Colour {
type Err = AuraError; type Err = AuraError;
@@ -51,7 +60,12 @@ impl FromStr for Colour {
let r = u8::from_str_radix(&s[0..2], 16).or(Err(AuraError::ParseColour))?; let r = u8::from_str_radix(&s[0..2], 16).or(Err(AuraError::ParseColour))?;
let g = u8::from_str_radix(&s[2..4], 16).or(Err(AuraError::ParseColour))?; let g = u8::from_str_radix(&s[2..4], 16).or(Err(AuraError::ParseColour))?;
let b = u8::from_str_radix(&s[4..6], 16).or(Err(AuraError::ParseColour))?; let b = u8::from_str_radix(&s[4..6], 16).or(Err(AuraError::ParseColour))?;
Ok(Colour(r, g, b)) Ok(Colour {
help: false,
red: r,
green: g,
blue: b,
})
} }
} }
@@ -72,9 +86,9 @@ impl FromStr for Speed {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.to_lowercase(); let s = s.to_lowercase();
match s.as_str() { match s.as_str() {
"slow" => Ok(Speed::Slow), "low" => Ok(Speed::Slow),
"medium" => Ok(Speed::Medium), "med" => Ok(Speed::Medium),
"fast" => Ok(Speed::Fast), "high" => Ok(Speed::Fast),
_ => Err(AuraError::ParseSpeed), _ => Err(AuraError::ParseSpeed),
} }
} }
@@ -113,74 +127,43 @@ impl FromStr for Direction {
/// Byte value for setting the built-in mode. /// Byte value for setting the built-in mode.
/// ///
/// Enum corresponds to the required integer value /// Enum corresponds to the required integer value
#[derive(Debug, PartialEq)] #[derive(Debug, Options)]
pub enum ModeByte { pub enum SetAuraBuiltin {
Stable, // colour1 #[options(help = "set a single static colour")]
Breathe, // colour1, colour2, speed Stable(Colour), // colour1
Cycle, // speed Breathe(Breathe), // colour1, colour2, speed
Rainbow, // speed, direction // Cycle, // speed
Rain, // colour1, speed // Rainbow, // speed, direction
Random, // speed // Rain, // colour1, speed
Highlight, // colour1, speed // Random, // speed
Laser, // colour1, speed // Highlight, // colour1, speed
Ripple, // colour1, speed // Laser, // colour1, speed
Off, // none // Ripple, // colour1, speed
Pulse, // colour1 // Off, // none
LineRace, // colour1 #[options(help = "set a rapid pulse")]
WideLineRace, // colour1 Pulse(Colour), // colour1
#[options(help = "set a vertical line racing from left")]
LineRace(Colour), // colour1
#[options(help = "set a wide vertical line racing from left")]
WideLineRace(Colour), // colour1
} }
impl Default for ModeByte {
fn default() -> Self {
ModeByte::Stable
}
}
impl FromStr for ModeByte {
type Err = AuraError;
fn from_str(s: &str) -> Result<Self, Self::Err> { impl Default for SetAuraBuiltin {
match s { fn default() -> Self {
"stable" => Ok(ModeByte::Stable), SetAuraBuiltin::Stable(Colour::default())
"breathe" => Ok(ModeByte::Breathe),
"cycle" => Ok(ModeByte::Cycle),
"rainbow" => Ok(ModeByte::Rainbow),
"rain" => Ok(ModeByte::Rain),
"random" => Ok(ModeByte::Random),
"highlight" => Ok(ModeByte::Highlight),
"laser" => Ok(ModeByte::Laser),
"ripple" => Ok(ModeByte::Ripple),
"off" => Ok(ModeByte::Off),
"pulse" => Ok(ModeByte::Pulse),
"linerace" => Ok(ModeByte::LineRace),
"widelinerace" => Ok(ModeByte::WideLineRace),
_ => Err(AuraError::ParseMode),
}
} }
} }
#[derive(Debug, PartialEq, Options)] #[derive(Debug, PartialEq, Options)]
pub struct BuiltInMode { pub struct Breathe {
#[options(help = "selects from the built-in keyboard modes")] #[options(help = "print help message")]
mode: ModeByte, help: bool,
#[options(help = "set the first colour if a mode uses it, must be in hex: ffffff")] #[options(help = "set the first colour, must be hex string e.g, ff00ff")]
colour1: Colour, colour1: Colour,
#[options(help = "set the second colour if a mode uses it, must be in hex: ffffff")] #[options(help = "set the second colour, must be hex string e.g, ff00ff")]
colour2: Colour, colour2: Colour,
#[options(help = "set the speed of an effect if a mode uses it")] #[options(help = "set the speed")]
speed: Speed, speed: Speed,
#[options(help = "set the direction of an effect if a mode uses it")]
direction: Direction,
}
impl Default for BuiltInMode {
fn default() -> BuiltInMode {
BuiltInMode {
mode: ModeByte::Stable,
colour1: Colour(0xff, 0x00, 0x00),
colour2: Colour(0x00, 0x00, 0x00),
speed: Speed::Slow,
direction: Direction::Right,
}
}
} }
/// Packet Data: /// Packet Data:
@@ -225,20 +208,41 @@ impl Default for BuiltInMode {
/// Bytes 10, 11, 12 are Red, Green, Blue for second colour if mode supports it /// Bytes 10, 11, 12 are Red, Green, Blue for second colour if mode supports it
pub struct ModeMessage(pub [u8; LED_MSG_LEN]); pub struct ModeMessage(pub [u8; LED_MSG_LEN]);
impl From<BuiltInMode> for ModeMessage { impl From<SetAuraBuiltin> for ModeMessage {
fn from(mode: BuiltInMode) -> Self { fn from(mode: SetAuraBuiltin) -> Self {
let mut msg = [0u8; LED_MSG_LEN]; let mut msg = [0u8; LED_MSG_LEN];
msg[0] = 0x5d; msg[0] = 0x5d;
msg[1] = 0xb3; msg[1] = 0xb3;
msg[3] = mode.mode as u8; match mode {
msg[4] = mode.colour1.0; SetAuraBuiltin::Stable(settings) => {
msg[5] = mode.colour1.1; msg[3] = 0x00;
msg[6] = mode.colour1.2; msg[4] = settings.red;
msg[7] = mode.speed as u8; msg[5] = settings.green;
msg[8] = mode.direction as u8; msg[6] = settings.blue;
msg[10] = mode.colour2.0; return ModeMessage(msg);
msg[11] = mode.colour2.1; }
msg[12] = mode.colour2.2; SetAuraBuiltin::Breathe(settings) => {
msg[3] = 0x01;
msg[4] = settings.colour1.red;
msg[5] = settings.colour1.green;
msg[6] = settings.colour1.blue;
msg[7] = settings.speed as u8;
msg[10] = settings.colour2.red;
msg[11] = settings.colour2.green;
msg[12] = settings.colour2.blue;
return ModeMessage(msg);
}
_ => {}
}
// msg[3] = mode.mode as u8;
// msg[4] = mode.colour1.0;
// msg[5] = mode.colour1.1;
// msg[6] = mode.colour1.2;
// msg[7] = mode.speed as u8;
// msg[8] = mode.direction as u8;
// msg[10] = mode.colour2.0;
// msg[11] = mode.colour2.1;
// msg[12] = mode.colour2.2;
ModeMessage(msg) ModeMessage(msg)
} }

View File

@@ -1,5 +1,8 @@
use crate::aura::AuraError;
use crate::aura::ModeMessage; use crate::aura::ModeMessage;
use gumdrop::Options;
use rusb::{DeviceHandle, Error}; use rusb::{DeviceHandle, Error};
use std::str::FromStr;
use std::time::Duration; use std::time::Duration;
pub const LED_MSG_LEN: usize = 17; pub const LED_MSG_LEN: usize = 17;
@@ -30,6 +33,27 @@ static LED_SPECIAL: [u8; 64] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]; ];
#[derive(Debug, Options)]
pub struct LedBrightness {
pub level: u8,
}
impl FromStr for LedBrightness {
type Err = AuraError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.to_lowercase();
match s.as_str() {
"off" => Ok(LedBrightness { level: 0x00 }),
"low" => Ok(LedBrightness { level: 0x01 }),
"med" => Ok(LedBrightness { level: 0x02 }),
"high" => Ok(LedBrightness { level: 0x03 }),
_ => {
println!("Missing required argument, must be one of:\noff,low,med,high\n");
Err(AuraError::ParseSpeed)
}
}
}
}
/// ROG device controller /// ROG device controller
/// ///

View File

@@ -1,54 +1,53 @@
mod aura; mod aura;
mod core; mod core;
use crate::aura::{BuiltInMode, ModeMessage}; use crate::aura::*;
use crate::core::RogCore; use crate::core::{LedBrightness, RogCore};
use gumdrop::{Options, ParsingStyle}; use gumdrop::Options;
use std::env;
#[derive(Debug, Options)] #[derive(Debug, Options)]
struct CLIStart { struct CLIStart {
#[options(command, required)] #[options(help = "print help message")]
help: bool,
#[options(help = "<off,low,med,high>")]
bright: Option<LedBrightness>,
#[options(command)]
command: Option<Command>, command: Option<Command>,
} }
#[derive(Debug, Options)] #[derive(Debug, Options)]
enum Command { enum Command {
#[options(help = "show help for a command (eg: help aura)")]
Help(FreeOptions),
#[options(help = "Set the keyboard lighting from built-in modes")] #[options(help = "Set the keyboard lighting from built-in modes")]
LedMode(BuiltInMode), LedMode(LedModeCommand),
} }
#[derive(Debug, Options)] #[derive(Debug, Options)]
struct FreeOptions { struct LedModeCommand {
#[options(free)] #[options(help = "print help message")]
free: Vec<String>, help: bool,
#[options(command, required)]
command: Option<SetAuraBuiltin>,
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = env::args().collect(); let parsed = CLIStart::parse_args_default_or_exit();
match CLIStart::parse_args(&args[1..], ParsingStyle::AllOptions) { match parsed.command {
Ok(okk) => { Some(Command::LedMode(okk)) => match okk.command {
dbg!(&okk); Some(command) => {
match okk.command { let mut core = RogCore::new()?;
Some(Command::Help(help)) => { let mode = ModeMessage::from(command);
if help.free.contains(&"aura".to_string()) { core.aura_set_mode(mode)?;
println!("\n{}", BuiltInMode::usage())
}
}
Some(Command::LedMode(aura)) => {
let mut core = RogCore::new()?;
let mode = ModeMessage::from(aura);
core.aura_set_mode(mode)?;
}
None => {}
} }
_ => {}
},
_ => {}
}
match parsed.bright {
Some(brightness) => {
let mut core = RogCore::new()?;
core.aura_set_brightness(brightness.level as u8)?;
} }
Err(err) => { _ => {}
println!("\n{}", err);
println!("\nUsage:\n{}", Command::usage());
}
} }
//core.aura_set_brightness(3)?; //core.aura_set_brightness(3)?;