Patch-Source: https://github.com/efernau/rot8/pull/61 From adb715259d7d999168be53fe06fb1125605c6be5 Mon Sep 17 00:00:00 2001 From: fabro122 Date: Wed, 25 Oct 2023 12:05:20 +0200 Subject: [PATCH 1/2] Added dynamic default normalization. - By default the normalization-factor will now be calculated in each loop as the length of the (x,y,z)-vector from the accelerometer - If the argument '--normalization-factor' gets supplied, it overrides this new default --- src/main.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index a228683..a0658fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,11 +87,10 @@ fn main() -> Result<(), String> { .possible_values(["xy", "yx", "zy", "yz", "xz", "zx"]) .takes_value(true), Arg::with_name("normalization-factor") - .default_value("1e6") .long("normalization-factor") .short('n') .value_name("NORMALIZATION_FACTOR") - .help("Set factor for sensor value normalization") + .help("Set factor for sensor value normalization manually. By default this factor is calculated dynamically using the sensor's data.") .takes_value(true), Arg::with_name("keyboard") .long("disable-keyboard") @@ -150,8 +149,13 @@ fn main() -> Result<(), String> { let x_source = xy.next().unwrap(); let y_source = xy.next().unwrap(); - let normalization_factor = matches.value_of("normalization-factor").unwrap_or("1e6"); - let normalization_factor = normalization_factor.parse::().unwrap_or(1e6); + let mut normalization_factor: Option = None; + if let Some(v) = matches.value_of("normalization-factor") { + match v.parse::() { + Ok(p) => { normalization_factor = Some(p) } + Err(_) => { return Err("The argument 'normalization-factor' is no valid float literal".to_string()); } + } + } let mut backend: Box = match WaylandBackend::new(display) { Ok(wayland_backend) => { @@ -226,14 +230,18 @@ fn main() -> Result<(), String> { let x_raw = fs::read_to_string(path_x.as_str()).unwrap(); let y_raw = fs::read_to_string(path_y.as_str()).unwrap(); let z_raw = fs::read_to_string(path_z.as_str()).unwrap(); - let x_clean = x_raw.trim_end_matches('\n').parse::().unwrap_or(0); - let y_clean = y_raw.trim_end_matches('\n').parse::().unwrap_or(0); - let z_clean = z_raw.trim_end_matches('\n').parse::().unwrap_or(0); + let x_clean = x_raw.trim_end_matches('\n').parse::().unwrap_or(0.); + let y_clean = y_raw.trim_end_matches('\n').parse::().unwrap_or(0.); + let z_clean = z_raw.trim_end_matches('\n').parse::().unwrap_or(0.); // Normalize vectors - let mut mut_x: f32 = (x_clean as f32) / normalization_factor; - let mut mut_y: f32 = (y_clean as f32) / normalization_factor; - let mut mut_z: f32 = (z_clean as f32) / normalization_factor; + let norm_factor = normalization_factor.unwrap_or_else(|| + f32::sqrt(x_clean * x_clean + y_clean * y_clean + z_clean * z_clean) + ); + + let mut mut_x: f32 = x_clean / norm_factor; + let mut mut_y: f32 = y_clean / norm_factor; + let mut mut_z: f32 = z_clean / norm_factor; // Apply inversions if flip_x { From b0a95d5eecde15775de5ef2727655bb3fbf778ab Mon Sep 17 00:00:00 2001 From: fabro122 Date: Wed, 25 Oct 2023 14:14:51 +0200 Subject: [PATCH 2/2] Formatted code using rustfmt --- src/main.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index a0658fa..4a9ad32 100644 --- a/src/main.rs +++ b/src/main.rs @@ -152,8 +152,12 @@ fn main() -> Result<(), String> { let mut normalization_factor: Option = None; if let Some(v) = matches.value_of("normalization-factor") { match v.parse::() { - Ok(p) => { normalization_factor = Some(p) } - Err(_) => { return Err("The argument 'normalization-factor' is no valid float literal".to_string()); } + Ok(p) => normalization_factor = Some(p), + Err(_) => { + return Err( + "The argument 'normalization-factor' is no valid float literal".to_string(), + ); + } } } @@ -235,9 +239,9 @@ fn main() -> Result<(), String> { let z_clean = z_raw.trim_end_matches('\n').parse::().unwrap_or(0.); // Normalize vectors - let norm_factor = normalization_factor.unwrap_or_else(|| + let norm_factor = normalization_factor.unwrap_or_else(|| { f32::sqrt(x_clean * x_clean + y_clean * y_clean + z_clean * z_clean) - ); + }); let mut mut_x: f32 = x_clean / norm_factor; let mut mut_y: f32 = y_clean / norm_factor;