-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from hnez/backlight
backlight: dim the backlight when in screensaver mode
- Loading branch information
Showing
6 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// This file is part of tacd, the LXA TAC system daemon | ||
// Copyright (C) 2023 Pengutronix e.K. | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 2 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along | ||
// with this program; if not, write to the Free Software Foundation, Inc., | ||
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
|
||
use anyhow::Result; | ||
use async_std::prelude::*; | ||
use async_std::sync::Arc; | ||
use async_std::task::spawn; | ||
use log::warn; | ||
|
||
mod demo_mode; | ||
|
||
#[cfg(feature = "demo_mode")] | ||
use demo_mode::{Backlight as SysBacklight, Brightness, SysClass}; | ||
|
||
#[cfg(not(feature = "demo_mode"))] | ||
use sysfs_class::{Backlight as SysBacklight, Brightness, SysClass}; | ||
|
||
use crate::broker::{BrokerBuilder, Topic}; | ||
|
||
pub struct Backlight { | ||
pub brightness: Arc<Topic<f32>>, | ||
} | ||
|
||
impl Backlight { | ||
pub fn new(bb: &mut BrokerBuilder) -> Result<Self> { | ||
let brightness = bb.topic_rw("/v1/tac/display/backlight/brightness", Some(1.0)); | ||
|
||
let (mut rx, _) = brightness.clone().subscribe_unbounded(); | ||
|
||
let backlight = SysBacklight::new("backlight")?; | ||
let max_brightness = backlight.max_brightness()?; | ||
|
||
spawn(async move { | ||
while let Some(fraction) = rx.next().await { | ||
let brightness = (max_brightness as f32) * fraction; | ||
let mut brightness = brightness.clamp(0.0, max_brightness as f32) as u64; | ||
|
||
// A brightness of 0 turns the backlight off completely. | ||
// If the user selects something low but not zero they likely | ||
// want a dim glow, not completely off. | ||
if fraction > 0.01 && brightness == 0 { | ||
brightness = 1; | ||
} | ||
|
||
if let Err(e) = backlight.set_brightness(brightness) { | ||
warn!("Failed to set LED pattern: {}", e); | ||
} | ||
} | ||
}); | ||
|
||
Ok(Self { brightness }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// This file is part of tacd, the LXA TAC system daemon | ||
// Copyright (C) 2023 Pengutronix e.K. | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 2 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along | ||
// with this program; if not, write to the Free Software Foundation, Inc., | ||
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
|
||
use std::io::{Error, ErrorKind, Result}; | ||
use std::path::{Path, PathBuf}; | ||
use std::str::{from_utf8, FromStr}; | ||
|
||
use sysfs_class::{set_trait_method, trait_method}; | ||
|
||
pub trait SysClass: Sized { | ||
fn class() -> &'static str; | ||
unsafe fn from_path_unchecked(path: PathBuf) -> Self; | ||
fn path(&self) -> &Path; | ||
|
||
fn new(id: &str) -> Result<Self> { | ||
let inst = unsafe { Self::from_path_unchecked(id.into()) }; | ||
Ok(inst) | ||
} | ||
|
||
fn read_file<P: AsRef<Path>>(&self, name: P) -> Result<String> { | ||
let path = self.path().join(name); | ||
let path = path.to_str().unwrap(); | ||
|
||
if path == "backlight/max_brightness" { | ||
Ok("8".to_string()) | ||
} else { | ||
Err(Error::new(ErrorKind::NotFound, format!("{path} not found"))) | ||
} | ||
} | ||
|
||
fn parse_file<F: FromStr, P: AsRef<Path>>(&self, name: P) -> Result<F> { | ||
self.read_file(name)? | ||
.parse() | ||
.map_err(|_| Error::new(ErrorKind::InvalidData, "too bad")) | ||
} | ||
|
||
fn write_file<P: AsRef<Path>, S: AsRef<[u8]>>(&self, name: P, data: S) -> Result<()> { | ||
let path = self.path().join(name); | ||
let path = path.to_str().unwrap(); | ||
let data = from_utf8(data.as_ref()).unwrap(); | ||
|
||
log::info!("Backlight: Write {} to {}", data, path); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
pub trait Brightness { | ||
fn brightness(&self) -> Result<u64>; | ||
fn max_brightness(&self) -> Result<u64>; | ||
fn set_brightness(&self, val: u64) -> Result<()>; | ||
} | ||
|
||
pub struct Backlight { | ||
path: PathBuf, | ||
} | ||
|
||
impl SysClass for Backlight { | ||
fn class() -> &'static str { | ||
"backlight" | ||
} | ||
|
||
unsafe fn from_path_unchecked(path: PathBuf) -> Self { | ||
Self { path } | ||
} | ||
|
||
fn path(&self) -> &Path { | ||
&self.path | ||
} | ||
} | ||
|
||
impl Brightness for Backlight { | ||
trait_method!(brightness parse_file u64); | ||
trait_method!(max_brightness parse_file u64); | ||
set_trait_method!("brightness", set_brightness u64); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters