Avoid conversions to and from percent

This commit is contained in:
Kenny Levinsen
2019-08-04 14:11:21 +02:00
parent 339b069354
commit a4f661ffa7
2 changed files with 12 additions and 12 deletions
+5 -5
View File
@@ -51,14 +51,14 @@ impl Backlight {
pub fn brightness(&self) -> f32 { pub fn brightness(&self) -> f32 {
if self.cur_brightness > self.max_brightness { if self.cur_brightness > self.max_brightness {
// what. // 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> { 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 { self.cur_brightness = if self.cur_brightness as i64 + inc < 1 {
1 1
@@ -116,7 +116,7 @@ impl ModuleImpl for Backlight {
&c, &c,
432, 432,
24, 24,
self.brightness() / 100.0, self.brightness(),
)?; )?;
draw_box( draw_box(
&mut buf.subdimensions((128, 0, 432, 24))?, &mut buf.subdimensions((128, 0, 432, 24))?,
@@ -142,7 +142,7 @@ impl ModuleImpl for Backlight {
x: _x, x: _x,
y, y,
} => { } => {
self.add(y as f32 / 8.0).unwrap(); self.add(y as f32 / 800.0).unwrap();
match self.sync() { match self.sync() {
Ok(val) => val, Ok(val) => val,
Err(err) => { Err(err) => {
+7 -7
View File
@@ -54,7 +54,7 @@ struct PulseAudioSoundDevice {
client: Arc<Mutex<PulseAudioClient>>, client: Arc<Mutex<PulseAudioClient>>,
name: Option<String>, name: Option<String>,
volume: Option<ChannelVolumes>, volume: Option<ChannelVolumes>,
volume_avg: u32, volume_avg: f32,
muted: bool, muted: bool,
} }
@@ -313,7 +313,7 @@ impl PulseAudioSoundDevice {
client: cl, client: cl,
name: Some(name.to_string()), name: Some(name.to_string()),
volume: None, volume: None,
volume_avg: 0, volume_avg: 0.0,
muted: false, muted: false,
})); }));
let (tx, rx) = channel(); let (tx, rx) = channel();
@@ -335,10 +335,10 @@ impl PulseAudioSoundDevice {
fn update_volume(&mut self, volume: ChannelVolumes) { fn update_volume(&mut self, volume: ChannelVolumes) {
self.volume = Some(volume); 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 self.volume_avg
} }
@@ -365,7 +365,7 @@ impl PulseAudioSoundDevice {
}; };
// apply step to volumes // 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() { for vol in volume.values.iter_mut() {
vol.0 = min(max(0, vol.0 as i32 + step) as u32, VOLUME_MAX.0); vol.0 = min(max(0, vol.0 as i32 + step) as u32, VOLUME_MAX.0);
} }
@@ -433,7 +433,7 @@ impl ModuleImpl for Arc<Mutex<PulseAudio>> {
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> { ) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
let s = self.lock().unwrap(); let s = self.lock().unwrap();
let muted = s.device.lock().unwrap().muted; 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); buf.memset(bg);
let c = if muted { let c = if muted {
Color::new(1.0, 1.0, 0.0, 1.0) Color::new(1.0, 1.0, 0.0, 1.0)
@@ -499,7 +499,7 @@ impl ModuleImpl for Arc<Mutex<PulseAudio>> {
.device .device
.lock() .lock()
.unwrap() .unwrap()
.set_volume(y as f32 / 8.0) .set_volume(y as f32 / 800.0)
.unwrap(); .unwrap();
} }
Input::Click { pos: _pos, button } => match button { Input::Click { pos: _pos, button } => match button {