From a4f661ffa74f6e1d45317bdb23f686da3dfac4e5 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Sun, 4 Aug 2019 14:11:21 +0200 Subject: [PATCH] Avoid conversions to and from percent --- src/backlight.rs | 10 +++++----- src/sound.rs | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/backlight.rs b/src/backlight.rs index c142e3f..df9a73c 100644 --- a/src/backlight.rs +++ b/src/backlight.rs @@ -51,14 +51,14 @@ impl Backlight { pub fn brightness(&self) -> f32 { if self.cur_brightness > self.max_brightness { // what. - return 100.0; + return 1.0; } - (self.cur_brightness as f32 / self.max_brightness as f32) * 100.0 + (self.cur_brightness as f32 / self.max_brightness as f32) } pub fn add(&mut self, diff: f32) -> Result<(), Error> { - let inc = ((self.max_brightness as f32) / 100.0 * diff) as i64; + let inc = (self.max_brightness as f32 * diff) as i64; self.cur_brightness = if self.cur_brightness as i64 + inc < 1 { 1 @@ -116,7 +116,7 @@ impl ModuleImpl for Backlight { &c, 432, 24, - self.brightness() / 100.0, + self.brightness(), )?; draw_box( &mut buf.subdimensions((128, 0, 432, 24))?, @@ -142,7 +142,7 @@ impl ModuleImpl for Backlight { x: _x, y, } => { - self.add(y as f32 / 8.0).unwrap(); + self.add(y as f32 / 800.0).unwrap(); match self.sync() { Ok(val) => val, Err(err) => { diff --git a/src/sound.rs b/src/sound.rs index 8838796..2117f45 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -54,7 +54,7 @@ struct PulseAudioSoundDevice { client: Arc>, name: Option, volume: Option, - volume_avg: u32, + volume_avg: f32, muted: bool, } @@ -313,7 +313,7 @@ impl PulseAudioSoundDevice { client: cl, name: Some(name.to_string()), volume: None, - volume_avg: 0, + volume_avg: 0.0, muted: false, })); let (tx, rx) = channel(); @@ -335,10 +335,10 @@ impl PulseAudioSoundDevice { fn update_volume(&mut self, volume: ChannelVolumes) { self.volume = Some(volume); - self.volume_avg = (volume.avg().0 as f32 / VOLUME_NORM.0 as f32 * 100.0).round() as u32; + self.volume_avg = volume.avg().0 as f32 / VOLUME_NORM.0 as f32; } - fn volume(&self) -> u32 { + fn volume(&self) -> f32 { self.volume_avg } @@ -365,7 +365,7 @@ impl PulseAudioSoundDevice { }; // apply step to volumes - let step = (step * VOLUME_NORM.0 as f32 / 100.0).round() as i32; + let step = (step * VOLUME_NORM.0 as f32).round() as i32; for vol in volume.values.iter_mut() { vol.0 = min(max(0, vol.0 as i32 + step) as u32, VOLUME_MAX.0); } @@ -433,7 +433,7 @@ impl ModuleImpl for Arc> { ) -> Result, ::std::io::Error> { let s = self.lock().unwrap(); let muted = s.device.lock().unwrap().muted; - let mut vol =(s.device.lock().unwrap().volume() as f32) / 100.0; + let mut vol = s.device.lock().unwrap().volume(); buf.memset(bg); let c = if muted { Color::new(1.0, 1.0, 0.0, 1.0) @@ -499,7 +499,7 @@ impl ModuleImpl for Arc> { .device .lock() .unwrap() - .set_volume(y as f32 / 8.0) + .set_volume(y as f32 / 800.0) .unwrap(); } Input::Click { pos: _pos, button } => match button {