From 89158cdc98a86e86099fd3ff9775bc55c7ea1343 Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 15 Apr 2020 22:10:13 +1200 Subject: [PATCH] Better structure --- Cargo.lock | 6 +- Cargo.toml | 2 +- src/aura.rs | 156 +++++++++++++++++++++++++++------------------------- src/core.rs | 24 ++++++++ src/main.rs | 61 ++++++++++---------- 5 files changed, 137 insertions(+), 112 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 064c8ff0..06405059 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,8 +57,7 @@ dependencies = [ [[package]] name = "gumdrop" version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee50908bc1beeac1f2902e0b4e0cd0d844e716f5ebdc6f0cfc1163fe5e10bcde" +source = "git+https://github.com/murarth/gumdrop.git#61a279eab342381e417b36d06f3f2b67eb21fa3b" dependencies = [ "gumdrop_derive", ] @@ -66,8 +65,7 @@ dependencies = [ [[package]] name = "gumdrop_derive" version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90454ce4de40b7ca6a8968b5ef367bdab48413962588d0d2b1638d60090c35d7" +source = "git+https://github.com/murarth/gumdrop.git#61a279eab342381e417b36d06f3f2b67eb21fa3b" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 80f2b2e4..7f04162f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,4 @@ edition = "2018" [dependencies] rusb = "0.5" -gumdrop = "0.7" +gumdrop = { git = "https://github.com/murarth/gumdrop.git" } diff --git a/src/aura.rs b/src/aura.rs index 85f248df..20b5e2c1 100644 --- a/src/aura.rs +++ b/src/aura.rs @@ -38,8 +38,17 @@ impl Error for AuraError { } } -#[derive(Default, Debug, PartialEq)] -pub struct Colour(u8, u8, u8); +#[derive(Default, Debug, PartialEq, Options)] +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 { 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 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))?; - 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 { let s = s.to_lowercase(); match s.as_str() { - "slow" => Ok(Speed::Slow), - "medium" => Ok(Speed::Medium), - "fast" => Ok(Speed::Fast), + "low" => Ok(Speed::Slow), + "med" => Ok(Speed::Medium), + "high" => Ok(Speed::Fast), _ => Err(AuraError::ParseSpeed), } } @@ -113,74 +127,43 @@ impl FromStr for Direction { /// Byte value for setting the built-in mode. /// /// Enum corresponds to the required integer value -#[derive(Debug, PartialEq)] -pub enum ModeByte { - Stable, // colour1 - Breathe, // colour1, colour2, speed - Cycle, // speed - Rainbow, // speed, direction - Rain, // colour1, speed - Random, // speed - Highlight, // colour1, speed - Laser, // colour1, speed - Ripple, // colour1, speed - Off, // none - Pulse, // colour1 - LineRace, // colour1 - WideLineRace, // colour1 +#[derive(Debug, Options)] +pub enum SetAuraBuiltin { + #[options(help = "set a single static colour")] + Stable(Colour), // colour1 + Breathe(Breathe), // colour1, colour2, speed + // Cycle, // speed + // Rainbow, // speed, direction + // Rain, // colour1, speed + // Random, // speed + // Highlight, // colour1, speed + // Laser, // colour1, speed + // Ripple, // colour1, speed + // Off, // none + #[options(help = "set a rapid pulse")] + 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 { - match s { - "stable" => Ok(ModeByte::Stable), - "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), - } +impl Default for SetAuraBuiltin { + fn default() -> Self { + SetAuraBuiltin::Stable(Colour::default()) } } #[derive(Debug, PartialEq, Options)] -pub struct BuiltInMode { - #[options(help = "selects from the built-in keyboard modes")] - mode: ModeByte, - #[options(help = "set the first colour if a mode uses it, must be in hex: ffffff")] +pub struct Breathe { + #[options(help = "print help message")] + help: bool, + #[options(help = "set the first colour, must be hex string e.g, ff00ff")] 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, - #[options(help = "set the speed of an effect if a mode uses it")] + #[options(help = "set the 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: @@ -225,20 +208,41 @@ impl Default for BuiltInMode { /// Bytes 10, 11, 12 are Red, Green, Blue for second colour if mode supports it pub struct ModeMessage(pub [u8; LED_MSG_LEN]); -impl From for ModeMessage { - fn from(mode: BuiltInMode) -> Self { +impl From for ModeMessage { + fn from(mode: SetAuraBuiltin) -> Self { let mut msg = [0u8; LED_MSG_LEN]; msg[0] = 0x5d; msg[1] = 0xb3; - 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; + match mode { + SetAuraBuiltin::Stable(settings) => { + msg[3] = 0x00; + msg[4] = settings.red; + msg[5] = settings.green; + msg[6] = settings.blue; + return ModeMessage(msg); + } + 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) } diff --git a/src/core.rs b/src/core.rs index 477db6f6..c8bb2683 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,5 +1,8 @@ +use crate::aura::AuraError; use crate::aura::ModeMessage; +use gumdrop::Options; use rusb::{DeviceHandle, Error}; +use std::str::FromStr; use std::time::Duration; 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, ]; +#[derive(Debug, Options)] +pub struct LedBrightness { + pub level: u8, +} +impl FromStr for LedBrightness { + type Err = AuraError; + + fn from_str(s: &str) -> Result { + 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 /// diff --git a/src/main.rs b/src/main.rs index 90917dea..b668e9a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,54 +1,53 @@ mod aura; mod core; -use crate::aura::{BuiltInMode, ModeMessage}; -use crate::core::RogCore; -use gumdrop::{Options, ParsingStyle}; -use std::env; +use crate::aura::*; +use crate::core::{LedBrightness, RogCore}; +use gumdrop::Options; #[derive(Debug, Options)] struct CLIStart { - #[options(command, required)] + #[options(help = "print help message")] + help: bool, + #[options(help = "")] + bright: Option, + #[options(command)] command: Option, } #[derive(Debug, Options)] enum Command { - #[options(help = "show help for a command (eg: help aura)")] - Help(FreeOptions), #[options(help = "Set the keyboard lighting from built-in modes")] - LedMode(BuiltInMode), + LedMode(LedModeCommand), } #[derive(Debug, Options)] -struct FreeOptions { - #[options(free)] - free: Vec, +struct LedModeCommand { + #[options(help = "print help message")] + help: bool, + #[options(command, required)] + command: Option, } fn main() -> Result<(), Box> { - let args: Vec = env::args().collect(); - match CLIStart::parse_args(&args[1..], ParsingStyle::AllOptions) { - Ok(okk) => { - dbg!(&okk); - match okk.command { - Some(Command::Help(help)) => { - if help.free.contains(&"aura".to_string()) { - println!("\n{}", BuiltInMode::usage()) - } - } - Some(Command::LedMode(aura)) => { - let mut core = RogCore::new()?; - let mode = ModeMessage::from(aura); - core.aura_set_mode(mode)?; - } - None => {} + let parsed = CLIStart::parse_args_default_or_exit(); + match parsed.command { + Some(Command::LedMode(okk)) => match okk.command { + Some(command) => { + let mut core = RogCore::new()?; + let mode = ModeMessage::from(command); + core.aura_set_mode(mode)?; } + _ => {} + }, + _ => {} + } + 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)?;