feat: 支持读取显示器配置。

This commit is contained in:
Ivan Li 2023-05-07 09:56:00 +08:00
parent 800c0d3fc4
commit 3a430716d6
4 changed files with 82 additions and 12 deletions

View File

@ -0,0 +1,67 @@
use std::{sync::Arc, time::SystemTime};
use ddc_hi::{Ddc, Display};
use tokio::sync::RwLock;
use super::DisplayState;
pub struct DisplayHandler {
pub state: Arc<RwLock<DisplayState>>,
pub controller: Arc<RwLock<Display>>,
}
impl DisplayHandler {
pub async fn fetch_state(&self) {
let mut controller = self.controller.write().await;
let mut temp_state = DisplayState::default();
match controller.handle.get_vcp_feature(0x10) {
Ok(value) => {
temp_state.max_brightness = value.maximum();
temp_state.min_brightness = 0;
temp_state.brightness = value.value();
}
Err(_) => {}
};
match controller.handle.get_vcp_feature(0x12) {
Ok(value) => {
temp_state.max_contrast = value.maximum();
temp_state.min_contrast = 0;
temp_state.contrast = value.value();
}
Err(_) => {}
};
match controller.handle.get_vcp_feature(0xdc) {
Ok(value) => {
temp_state.max_mode = value.maximum();
temp_state.min_mode = 0;
temp_state.mode = value.value();
}
Err(_) => {}
};
temp_state.last_fetched_at = SystemTime::now();
let mut state = self.state.write().await;
*state = temp_state;
}
pub async fn set_brightness(&self, brightness: u16) {
let mut state = self.state.write().await;
state.brightness = brightness;
state.last_modified_at = SystemTime::now();
}
pub async fn set_contrast(&self, contrast: u16) {
let mut state = self.state.write().await;
state.contrast = contrast;
state.last_modified_at = SystemTime::now();
}
pub async fn set_mode(&self, mode: u16) {
let mut state = self.state.write().await;
state.mode = mode;
state.last_modified_at = SystemTime::now();
}
}

View File

@ -14,6 +14,7 @@ pub struct DisplayState {
pub max_mode: u16, pub max_mode: u16,
pub min_mode: u16, pub min_mode: u16,
pub last_modified_at: SystemTime, pub last_modified_at: SystemTime,
pub last_fetched_at: SystemTime,
} }
impl DisplayState { impl DisplayState {
@ -22,13 +23,14 @@ impl DisplayState {
brightness: 30, brightness: 30,
contrast: 50, contrast: 50,
mode: 0, mode: 0,
last_modified_at: SystemTime::now(), last_modified_at: SystemTime::UNIX_EPOCH,
max_brightness: 100, max_brightness: 100,
min_brightness: 0, min_brightness: 0,
max_contrast: 100, max_contrast: 100,
min_contrast: 0, min_contrast: 0,
max_mode: 15, max_mode: 15,
min_mode: 0, min_mode: 0,
last_fetched_at: SystemTime::UNIX_EPOCH,
} }
} }
} }

View File

@ -1,22 +1,15 @@
use std::{ use std::{
borrow::Borrow,
collections::HashMap,
ops::Sub,
sync::Arc, sync::Arc,
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
use ddc_hi::Display; use ddc_hi::Display;
use paris::{error, info, warn}; use paris::{error, info, warn};
use tokio::sync::{OnceCell, OwnedMutexGuard, RwLock}; use tokio::sync::{OnceCell, RwLock};
use super::{display_state::DisplayState, display_handler::DisplayHandler};
use super::display_state::DisplayState;
use ddc_hi::Ddc;
pub struct DisplayHandler {
pub state: Arc<RwLock<DisplayState>>,
pub controller: Arc<RwLock<Display>>,
}
pub struct DisplayManager { pub struct DisplayManager {
displays: Arc<RwLock<Vec<Arc<RwLock<DisplayHandler>>>>>, displays: Arc<RwLock<Vec<Arc<RwLock<DisplayHandler>>>>>,
@ -46,7 +39,14 @@ impl DisplayManager {
for display in controllers { for display in controllers {
let controller = Arc::new(RwLock::new(display)); let controller = Arc::new(RwLock::new(display));
let state = Arc::new(RwLock::new(DisplayState::default())); let state = Arc::new(RwLock::new(DisplayState::default()));
displays.push(Arc::new(RwLock::new(DisplayHandler { controller, state }))); let handler = DisplayHandler {
state: state.clone(),
controller: controller.clone(),
};
handler.fetch_state().await;
displays.push(Arc::new(RwLock::new(handler)));
} }
} }

View File

@ -2,6 +2,7 @@
// mod manager; // mod manager;
mod display_state; mod display_state;
mod manager; mod manager;
mod display_handler;
pub use display_state::*; pub use display_state::*;