feat: GUI 配置支持添加和减少 LED。

This commit is contained in:
2023-01-02 16:53:20 +08:00
parent 4ad78ae5cc
commit 366b137258
21 changed files with 732 additions and 106 deletions

View File

@ -1,9 +1,9 @@
use futures::{future::join_all, stream::FuturesUnordered, StreamExt};
use futures::future::join_all;
use once_cell::sync::OnceCell;
use paris::info;
use serde::{Deserialize, Serialize};
use serde_json::value::Index;
use std::{collections::HashMap, iter::Map, sync::Arc, thread, time::Duration};
use std::{collections::HashMap, sync::Arc, time::Duration};
use tauri::async_runtime::RwLock;
use tokio::{
join,

View File

@ -11,6 +11,7 @@ mod rpc;
use crate::core::AmbientLightMode;
use crate::core::CoreManager;
use paris::*;
use picker::config::DisplayConfig;
use picker::manager::Picker;
use picker::screenshot::ScreenshotDto;
use std::vec;
@ -35,6 +36,26 @@ async fn take_snapshot() -> Vec<ScreenshotDto> {
base64_bitmap_list
}
#[tauri::command]
async fn get_screenshot_by_config(config: DisplayConfig) -> Result<ScreenshotDto, String> {
info!("Hi");
let manager = Picker::global();
let start = time::Instant::now();
let screenshot_dto = manager.get_screenshot_by_config(config).await;
info!("截图耗时 {} s", start.elapsed().as_seconds_f32());
match screenshot_dto {
Ok(screenshot_dto) => {
info!("截图耗时 {} s", start.elapsed().as_seconds_f32());
Ok(screenshot_dto)
}
Err(error) => {
error!("get_screenshot_by_config failed. {}", error);
Err(format!("get_screenshot_by_config failed. {}", error))
}
}
}
#[tauri::command]
fn get_picker_config() -> picker::config::Configuration {
let configuration = picker::config::Manager::global().get_config();
@ -57,6 +78,7 @@ async fn main() {
take_snapshot,
play_mode,
get_picker_config,
get_screenshot_by_config,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");

View File

@ -9,6 +9,7 @@ pub struct LedStripConfig {
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub struct DisplayConfig {
pub id: usize,
pub index_of_display: usize,
pub display_width: usize,
pub display_height: usize,
@ -19,8 +20,9 @@ pub struct DisplayConfig {
}
impl DisplayConfig {
pub fn default(index_of_display: usize, display_width: usize, display_height: usize) -> Self {
pub fn default(id: usize, index_of_display: usize, display_width: usize, display_height: usize) -> Self {
Self {
id,
index_of_display,
display_width,
display_height,

View File

@ -2,7 +2,7 @@ use futures::{stream::FuturesUnordered, StreamExt};
use once_cell::sync::OnceCell;
use paris::info;
use scrap::Display;
use std::{borrow::Borrow, sync::Arc};
use std::sync::Arc;
use tokio::{sync::Mutex, task};
use crate::picker::{config, screen::Screen};
@ -44,7 +44,7 @@ impl Picker {
for (index, display) in displays.iter().enumerate() {
let height = display.height();
let width = display.width();
let config = DisplayConfig::default(index, width, height);
let config = DisplayConfig::default(index, index, width, height);
configs.push(config);
}
@ -76,4 +76,16 @@ impl Picker {
anyhow::Ok(screenshot.to_dto().await)
}
pub async fn get_screenshot_by_config(
&self,
config: DisplayConfig,
) -> anyhow::Result<ScreenshotDto> {
let start = time::Instant::now();
let mut picker = DisplayPicker::from_config(config)?;
let screenshot = picker.take_screenshot()?;
info!("Take Screenshot Spend: {}", start.elapsed());
anyhow::Ok(screenshot.to_dto().await)
}
}

View File

@ -0,0 +1,83 @@
use futures::{stream::FuturesUnordered, StreamExt};
use once_cell::sync::OnceCell;
use paris::{info, warn};
use scrap::Display;
use std::{borrow::Borrow, sync::Arc};
use tokio::{sync::Mutex, task};
use crate::picker::{config, screen::Screen};
use super::{
config::DisplayConfig,
display_picker::DisplayPicker,
manager::Picker,
screenshot::{Screenshot, ScreenshotDto},
};
pub struct PreviewPicker {
pub pickers: Arc<Mutex<Vec<Arc<Mutex<DisplayPicker>>>>>,
pub screenshots: Arc<Mutex<Vec<Screenshot>>>,
}
impl PreviewPicker {
pub fn global() -> &'static PreviewPicker {
static SCREEN_COLOR_PREVIEW_PICKER: OnceCell<PreviewPicker> = OnceCell::new();
SCREEN_COLOR_PREVIEW_PICKER.get_or_init(|| PreviewPicker {
pickers: Arc::new(Mutex::new(vec![])),
screenshots: Arc::new(Mutex::new(vec![])),
})
}
pub async fn list_displays(&self) {
let mut pickers = self.pickers.lock().await;
let displays = Display::all()
.map_err(|error| anyhow::anyhow!("Can not get all of displays. {}", error))?;
let mut configs = vec![];
let mut futs = FuturesUnordered::new();
for (index, display) in displays.iter().enumerate() {
let height = display.height();
let width = display.width();
let config = DisplayConfig::default(index, width, height);
configs.push(config);
}
for config in configs.iter() {
let picker = DisplayPicker::from_config(*config);
match picker {
Ok(picker) => {
pickers.push(Arc::new(Mutex::new(picker)));
}
Err(_) => {
warn!(
"can not create DisplayPicker from config. config: {:?}",
config
);
}
}
}
}
pub async fn get_screenshot_by_config(
&self,
config: DisplayConfig,
) -> anyhow::Result<ScreenshotDto> {
let start = time::Instant::now();
let mut picker = DisplayPicker::from_config(config)?;
let screenshot = picker.take_screenshot()?;
info!("Take Screenshot Spend: {}", start.elapsed());
anyhow::Ok(screenshot.to_dto().await)
}
pub async fn preview_display_by_config(config: DisplayConfig) -> anyhow::Result<ScreenshotDto> {
let start = time::Instant::now();
let mut picker = DisplayPicker::from_config(config)?;
let screenshot = picker.take_screenshot()?;
info!("Take Screenshot Spend: {}", start.elapsed());
anyhow::Ok(screenshot.to_dto().await)
}
}

View File

@ -51,7 +51,7 @@ impl Screenshot {
}
};
let bottom: Vec<LedSamplePoints> = match config.top_led_strip {
let bottom: Vec<LedSamplePoints> = match config.bottom_led_strip {
Some(led_strip_config) => {
let points = Self::get_one_edge_sample_points(
config.display_height / 9,
@ -76,7 +76,7 @@ impl Screenshot {
}
};
let left: Vec<LedSamplePoints> = match config.top_led_strip {
let left: Vec<LedSamplePoints> = match config.left_led_strip {
Some(led_strip_config) => {
let points = Self::get_one_edge_sample_points(
config.display_width / 16,
@ -98,7 +98,7 @@ impl Screenshot {
}
};
let right: Vec<LedSamplePoints> = match config.top_led_strip {
let right: Vec<LedSamplePoints> = match config.right_led_strip {
Some(led_strip_config) => {
let points = Self::get_one_edge_sample_points(
config.display_width / 16,
@ -113,7 +113,7 @@ impl Screenshot {
.map(|groups| -> Vec<Point> {
groups
.into_iter()
.map(|(x, y)| (y, config.display_width - x))
.map(|(x, y)| (config.display_width - y, x))
.collect()
})
.collect()
@ -145,14 +145,15 @@ impl Screenshot {
let point_y_list: Vec<usize> = (point_start_y..width).step_by(cell_size_y).collect();
let point_x_list: Vec<usize> = iter::successors(Some(point_start_x), |i| {
let next = i + cell_size_x;
(next < (width as f64)).then_some(next)
(next < (length as f64)).then_some(next)
})
.map(|i| i as usize)
.collect();
let points: Vec<Point> = point_x_list
.into_iter()
.zip(point_y_list.into_iter())
.iter()
.map(|&x| point_y_list.iter().map(move |&y| (x, y)))
.flatten()
.collect();
points
.chunks(single_axis_points * single_axis_points)