feat: add set gain
This commit is contained in:
2
src-tauri/Cargo.lock
generated
2
src-tauri/Cargo.lock
generated
@ -796,6 +796,8 @@ dependencies = [
|
||||
name = "dsp_thomann"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"strum",
|
||||
"strum_macros",
|
||||
"thiserror 2.0.19",
|
||||
|
||||
@ -22,4 +22,4 @@ tauri = { version = "2", features = [] }
|
||||
tauri-plugin-opener = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
dsp_thomann = { path = "../../dsp_thomann" }
|
||||
dsp_thomann = { path = "../../dsp_thomann", features = ["serialization"] }
|
||||
|
||||
@ -1,14 +1,58 @@
|
||||
use tauri::State;
|
||||
use crate::state::AppState;
|
||||
use crate::state::{AppState, DSPDevice};
|
||||
use dsp_thomann::dsp408::DSP408;
|
||||
use std::{net::IpAddr, str::FromStr};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[tauri::command]
|
||||
pub fn device_count(
|
||||
state: State<AppState>
|
||||
) -> usize {
|
||||
let devices = state.devices.lock().unwrap();
|
||||
devices.len()
|
||||
impl AppState {
|
||||
pub fn with_device_mut<F, R>(
|
||||
&self,
|
||||
id: u64,
|
||||
f: F,
|
||||
) -> Result<R, String>
|
||||
where
|
||||
F: FnOnce(&mut DSPDevice) -> Result<R, String>,
|
||||
{
|
||||
let device = {
|
||||
let devices = self
|
||||
.devices
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
devices
|
||||
.get(&id)
|
||||
.cloned()
|
||||
.ok_or_else(|| "DSP not found".to_string())?
|
||||
};
|
||||
|
||||
let mut device = device
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
f(&mut device)
|
||||
}
|
||||
|
||||
pub fn remove_device(
|
||||
&self,
|
||||
id: u64,
|
||||
) -> Result<(), String> {
|
||||
|
||||
let device = {
|
||||
let mut devices = self.devices
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
devices
|
||||
.remove(&id)
|
||||
.ok_or_else(|| format!("DSP {} not found", id))?
|
||||
};
|
||||
|
||||
// Drop happens here.
|
||||
// DSPDevice owns DSP408.
|
||||
drop(device);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@ -17,19 +61,114 @@ pub fn create_dsp408(
|
||||
ip: String,
|
||||
port: u16,
|
||||
device_id: u8,
|
||||
) -> Result<usize, String> {
|
||||
) -> Result<u64, String> {
|
||||
|
||||
let ip = IpAddr::from_str(&ip)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let dsp = DSP408::new(ip, port, device_id)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let mut next_id = state
|
||||
.next_id
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let id = *next_id;
|
||||
*next_id += 1;
|
||||
|
||||
let device = DSPDevice {
|
||||
dsp,
|
||||
};
|
||||
|
||||
let mut devices = state
|
||||
.devices
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let ip = IpAddr::from_str(&ip).map_err(|e| e.to_string())?;
|
||||
devices.insert(
|
||||
id,
|
||||
Arc::new(Mutex::new(device)),
|
||||
);
|
||||
|
||||
let dsp = DSP408::new(ip, port, device_id)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
devices.push(dsp);
|
||||
#[tauri::command]
|
||||
pub async fn remove_dsp408(
|
||||
state: State<'_, AppState>,
|
||||
id: u64,
|
||||
) -> Result<(), String> {
|
||||
|
||||
Ok(devices.len())
|
||||
}
|
||||
state.with_device_mut(id, |device| {
|
||||
|
||||
if device.dsp.connected() {
|
||||
device.dsp
|
||||
.disconnect()
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
state.remove_device(id)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn connect_dsp408(
|
||||
state: State<'_, AppState>,
|
||||
id: u64,
|
||||
) -> Result<(), String> {
|
||||
|
||||
state.with_device_mut(id, |device| {
|
||||
device.dsp
|
||||
.connect()
|
||||
.map_err(|e| e.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn disconnect_dsp408(
|
||||
state: State<'_, AppState>,
|
||||
id: u64,
|
||||
) -> Result<(), String> {
|
||||
|
||||
state.with_device_mut(id, |device| {
|
||||
device.dsp
|
||||
.disconnect()
|
||||
.map_err(|e| e.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_dsp_state(
|
||||
state: State<'_, AppState>,
|
||||
id: u64,
|
||||
) -> Result<dsp_thomann::dsp408::types::DSPState, String> {
|
||||
|
||||
state.with_device_mut(id, |device| {
|
||||
let dsp_state = device.dsp
|
||||
.state()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(dsp_state.clone())
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn set_channel_gain(
|
||||
state: State<'_, AppState>,
|
||||
id: u64,
|
||||
channel: dsp_thomann::dsp408::types::Channel,
|
||||
db: f32,
|
||||
) -> Result<bool, String> {
|
||||
state.with_device_mut(id, |device| {
|
||||
let r = device.dsp
|
||||
.set_channel_gain(channel, db)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(r)
|
||||
})
|
||||
}
|
||||
|
||||
@ -3,21 +3,18 @@ mod commands;
|
||||
|
||||
use state::AppState;
|
||||
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.manage(AppState::new())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
greet,
|
||||
commands::device_count,
|
||||
commands::create_dsp408,
|
||||
commands::connect_dsp408,
|
||||
commands::remove_dsp408,
|
||||
commands::disconnect_dsp408,
|
||||
commands::get_dsp_state,
|
||||
commands::set_channel_gain,
|
||||
]
|
||||
)
|
||||
.run(tauri::generate_context!())
|
||||
|
||||
@ -1,14 +1,21 @@
|
||||
use std::sync::Mutex;
|
||||
use std::sync::{Mutex, Arc};
|
||||
use std::collections::HashMap;
|
||||
use dsp_thomann::dsp408::DSP408;
|
||||
|
||||
pub struct DSPDevice {
|
||||
pub dsp: DSP408,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub devices: Mutex<Vec<DSP408>>,
|
||||
pub devices: Mutex<HashMap<u64, Arc<Mutex<DSPDevice>>>>,
|
||||
pub next_id: Mutex<u64>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
devices: Mutex::new(Vec::new()),
|
||||
devices: Mutex::new(HashMap::new()),
|
||||
next_id: Mutex::new(1),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user