Skip to content

Commit

Permalink
Merge branch 'async'
Browse files Browse the repository at this point in the history
  • Loading branch information
Supernovatux committed Sep 4, 2022
2 parents 31129c6 + a0488c4 commit 779ef0c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 47 deletions.
64 changes: 64 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'auto_backlight'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=auto_backlight"
],
"filter": {
"name": "auto_backlight",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'auto_backlight'",
"cargo": {
"args": [
"build",
"--bin=auto_backlight",
"--package=auto_backlight"
],
"filter": {
"name": "auto_backlight",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'auto_backlight'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=auto_backlight",
"--package=auto_backlight"
],
"filter": {
"name": "auto_backlight",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
62 changes: 31 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ codegen-units = 1
panic = "abort"
opt-level = 3
overflow-checks = false
debug = 2

debug = 0
strip = "symbols"

[dependencies]
image = "*"
Expand Down
5 changes: 3 additions & 2 deletions src/brightness.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use glob::glob;
use log::{error, info};
use log::{error, info, debug};

use std::fs::{read_to_string, write};
#[derive(Debug)]
Expand Down Expand Up @@ -60,7 +60,7 @@ impl BrightnessDevice {
pub fn get_current_brightness_percent(&self) -> i16 {
let ret = (self.get_current_brightness() as f64 * 100.0 / self.get_max_brightness() as f64)
as i16;
info!("Current brightness is {}", ret);
debug !("Current brightness is {}", ret);
ret
}
pub fn increase_brightness(&self, change: i16) {
Expand All @@ -75,6 +75,7 @@ impl BrightnessDevice {
} else {
value + change
};
info!("Brightness changed from {} to {}",value,value_new);
write(&self.brightness, format!("{}", value_new)).expect("permission denied");
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::{Arc, atomic::{AtomicBool, Ordering}};

use futures::{channel::oneshot, FutureExt};
use futures_timer::Delay;
use log::info;
use log::{info, debug};

use crate::{cli_parser::{get_refresh, get_limit}, brightness::BrightnessDevices, screens::change_calc};
pub mod brightness;
Expand Down Expand Up @@ -39,9 +39,10 @@ pub async fn init() {
change = 0;
}
futures::select! {
_ = Delay::new(std::time::Duration::from_secs(refresh)).fuse() => info!("Current brightness {}",brightness),
_ = Delay::new(std::time::Duration::from_secs(refresh)).fuse() => debug!("Current brightness {}",brightness),
_ = &mut rx => {
brightness_dev.change_brightness(-change);
info!("Got exit signal");
break;},
}
}
Expand Down
19 changes: 9 additions & 10 deletions src/screens.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use fast_image_resize as fr;
use image::{DynamicImage, GenericImageView};
use log::{debug, info, trace};
use image::GenericImageView;
use log::{debug, trace};
use screenshots::DisplayInfo;
use std::num::NonZeroU32;

use crate::cli_parser::get_offset;
Expand All @@ -10,13 +11,13 @@ pub fn get_value_to_change(lim: u8, brightness: i16) -> i16 {
((-2.0 * lim as f64 / 255_f64) * brightness as f64 + lim as f64 + get_offset() as f64) as i16
}

pub fn get_average_brightness(img: DynamicImage) -> i16 {
let width = NonZeroU32::new(img.width()).unwrap();
let height = NonZeroU32::new(img.height()).unwrap();
pub fn get_average_brightness(img: Vec<u8>,dsp : DisplayInfo) -> i16 {
let width = NonZeroU32::new(dsp.width).unwrap();
let height = NonZeroU32::new(dsp.height).unwrap();
let src_image = fr::Image::from_vec_u8(
width,
height,
img.to_rgba8().into_raw(),
img,
fr::PixelType::U8x4,
)
.unwrap();
Expand All @@ -29,7 +30,6 @@ pub fn get_average_brightness(img: DynamicImage) -> i16 {
let new = image::RgbaImage::from_vec(dst_width.get(), dst_height.get(), dst_image.into_vec())
.unwrap();
let img = image::DynamicImage::ImageRgba8(new);
let img = img.grayscale();
let idk: Vec<u32> = img
.pixels()
.map(|x| (x.2[0] as u32 + x.2[1] as u32 + x.2[2] as u32) / 3)
Expand All @@ -43,10 +43,9 @@ pub fn change_calc(lim: u8) -> i16 {
for i in screens {
if i.display_info.is_primary {
trace!("{:?}", i.display_info);
let img = i.capture().unwrap();
ch = get_average_brightness(img);
let img = i.capture_raw().unwrap();
ch = get_average_brightness(img,i.display_info);
ch = get_value_to_change(lim, ch);
info!("Result of ch {}", ch);
break;
}
}
Expand Down

0 comments on commit 779ef0c

Please sign in to comment.