Skip to content

Commit 66aa115

Browse files
committed
feat(cli): Added option to inc and dec brightness
1 parent 1de1ac3 commit 66aa115

3 files changed

Lines changed: 87 additions & 22 deletions

File tree

flake.lock

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,34 @@ pub struct Args {
66
pub op: Op,
77
#[arg(short, long, action = clap::ArgAction::Count)]
88
pub verbosity: u8,
9-
#[arg(short, long)]
10-
pub bus: Option<u8>,
119
}
1210
#[derive(Debug, Subcommand)]
1311
pub enum Op {
1412
#[clap(name = "set")]
1513
SetBrightness {
1614
brightness: u8,
15+
#[arg(short, long)]
16+
bus: Option<u8>,
1717
},
1818
#[clap(name = "get")]
19-
GetBrightnexs,
19+
GetBrightness {
20+
#[arg(short, long)]
21+
bus: Option<u8>,
22+
},
23+
#[clap(name = "inc")]
24+
IncreaseBrightness {
25+
#[arg(default_value = "10")]
26+
amount: u8,
27+
#[arg(short, long)]
28+
bus: Option<u8>,
29+
},
30+
#[clap(name = "dec")]
31+
DecreaseBrightness {
32+
#[arg(default_value = "10")]
33+
amount: u8,
34+
#[arg(short, long)]
35+
bus: Option<u8>,
36+
},
2037
SetInput {
2138
bus: u8,
2239
input: crate::ddc::Input,

src/main.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ fn main() -> Result<()> {
3131
}
3232

3333
match cli.op {
34-
Op::GetBrightnexs => {
34+
Op::GetBrightness { bus } => {
3535
let list = DisplayList::probe(true)?;
36-
for dinfo in list.iter() {
36+
for dinfo in list.iter().filter(|info| {
37+
bus.map(|bus| info.io_path() == IOPath::I2C(bus as i32))
38+
.unwrap_or(true)
39+
}) {
3740
tracing::info!("Found display: {}", dinfo.model());
3841
let display = dinfo.open()?;
3942
let backlight = display.backlight_get()?;
@@ -45,9 +48,12 @@ fn main() -> Result<()> {
4548
);
4649
}
4750
}
48-
Op::SetBrightness { brightness } => {
51+
Op::SetBrightness { brightness, bus } => {
4952
let list = DisplayList::probe(true)?;
50-
for dinfo in list.iter() {
53+
for dinfo in list.iter().filter(|info| {
54+
bus.map(|bus| info.io_path() == IOPath::I2C(bus as i32))
55+
.unwrap_or(true)
56+
}) {
5157
tracing::info!("Found display: {}", dinfo.model());
5258
let display = dinfo.open()?;
5359
display.backlight_set(brightness.into())?;
@@ -60,6 +66,48 @@ fn main() -> Result<()> {
6066
);
6167
}
6268
}
69+
Op::IncreaseBrightness { amount, bus } => {
70+
let list = DisplayList::probe(true)?;
71+
for dinfo in list.iter().filter(|info| {
72+
bus.map(|bus| info.io_path() == IOPath::I2C(bus as i32))
73+
.unwrap_or(true)
74+
}) {
75+
tracing::info!("Found display: {}", dinfo.model());
76+
let display = dinfo.open()?;
77+
let current_backlight = display.backlight_get()?;
78+
let new_brightness = std::cmp::min(100, current_backlight.current + amount as u16);
79+
display.backlight_set(new_brightness)?;
80+
let backlight = display.backlight_get()?;
81+
println!(
82+
"{}: {}/{} (increased by {})",
83+
dinfo.model().blue(),
84+
backlight.current,
85+
backlight.max,
86+
amount
87+
);
88+
}
89+
}
90+
Op::DecreaseBrightness { amount, bus } => {
91+
let list = DisplayList::probe(true)?;
92+
for dinfo in list.iter().filter(|info| {
93+
bus.map(|bus| info.io_path() == IOPath::I2C(bus as i32))
94+
.unwrap_or(true)
95+
}) {
96+
tracing::info!("Found display: {}", dinfo.model());
97+
let display = dinfo.open()?;
98+
let current_backlight = display.backlight_get()?;
99+
let new_brightness = current_backlight.current.saturating_sub(amount as u16);
100+
display.backlight_set(new_brightness)?;
101+
let backlight = display.backlight_get()?;
102+
println!(
103+
"{}: {}/{} (decreased by {})",
104+
dinfo.model().blue(),
105+
backlight.current,
106+
backlight.max,
107+
amount
108+
);
109+
}
110+
}
63111
Op::GetInput { bus } => {
64112
let list = DisplayList::probe(true)?;
65113
for dinfo in list.iter().filter(|info| {

0 commit comments

Comments
 (0)