From 8fb7c31f88178d01778d5d7e21bbaa7d6611da15 Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Sat, 14 Oct 2023 10:15:55 +0900 Subject: [PATCH] Make mixer element names configurable Doesn't really matter for us since all machines we plan to use so far have the same chip, but might matter in the future. Signed-off-by: Hector Martin --- conf/apple/j314.conf | 6 ++++++ conf/apple/j413.conf | 6 ++++++ src/helpers.rs | 11 +++++++++++ src/types.rs | 24 ++++++++++++++++-------- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/conf/apple/j314.conf b/conf/apple/j314.conf index 621b0f2..175cd29 100644 --- a/conf/apple/j314.conf +++ b/conf/apple/j314.conf @@ -6,6 +6,12 @@ t_hysteresis = 10 channels = 12 period = 4096 +[Controls] +vsense = VSENSE Switch +isense = ISENSE Switch +amp_gain = Amp Gain Volume +volume = Speaker Volume + [Speaker/Left Woofer 1] group = 1 tr_coil = 28.09 diff --git a/conf/apple/j413.conf b/conf/apple/j413.conf index 132507a..5fbd502 100644 --- a/conf/apple/j413.conf +++ b/conf/apple/j413.conf @@ -6,6 +6,12 @@ t_hysteresis = 10 channels = 8 period = 4096 +[Controls] +vsense = VSENSE Switch +isense = ISENSE Switch +amp_gain = Amp Gain Volume +volume = Speaker Volume + [Speaker/Left Woofer] group = 1 tr_coil = 32.1 diff --git a/src/helpers.rs b/src/helpers.rs index 7fa52db..3bb0846 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -85,6 +85,17 @@ pub fn parse_float(config: &Ini, section: &str, key: &str) -> f32 { val } +/** + Wrapper around configparser::ini::Ini.getfloat() + to safely unwrap the Result, E> returned by + it. +*/ +pub fn parse_string(config: &Ini, section: &str, key: &str) -> String { + config + .get(section, key) + .expect(&format!("{}/{}: Missing key", section, key)) +} + /** Wrapper around alsa::ctl::ElemValue::new(). Lets us bail on errors and pass in the Bytes type for V/ISENSE diff --git a/src/types.rs b/src/types.rs index a63a25d..f5b654c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -78,9 +78,9 @@ struct Mixer { impl Mixer { // TODO: implement turning on V/ISENSE - fn new(name: &str, card: &Ctl) -> Mixer { + fn new(name: &str, card: &Ctl, globals: &Globals) -> Mixer { let mut vs = Elem::new( - name.to_owned() + " VSENSE Switch", + name.to_owned() + " " + &globals.ctl_vsense, card, alsa::ctl::ElemType::Boolean, ); @@ -91,7 +91,7 @@ impl Mixer { assert!(vs.val.get_boolean(0).unwrap()); let mut is = Elem::new( - name.to_owned() + " ISENSE Switch", + name.to_owned() + " " + &globals.ctl_isense, card, alsa::ctl::ElemType::Boolean, ); @@ -104,12 +104,12 @@ impl Mixer { Mixer { drv: name.to_owned(), level: Elem::new( - name.to_owned() + " Speaker Volume", + name.to_owned() + " " + &globals.ctl_volume, card, alsa::ctl::ElemType::Integer, ), amp_gain: Elem::new( - name.to_owned() + " Amp Gain Volume", + name.to_owned() + " " + &globals.ctl_amp_gain, card, alsa::ctl::ElemType::Integer, ), @@ -158,7 +158,7 @@ impl Mixer { } } -#[derive(Copy, Clone)] +#[derive(Clone)] pub struct Globals { pub visense_pcm: usize, pub channels: usize, @@ -166,6 +166,10 @@ pub struct Globals { pub t_ambient: f32, pub t_safe_max: f32, pub t_hysteresis: f32, + pub ctl_vsense: String, + pub ctl_isense: String, + pub ctl_amp_gain: String, + pub ctl_volume: String, } impl Globals { @@ -177,6 +181,10 @@ impl Globals { t_ambient: helpers::parse_float(config, "Globals", "t_ambient"), t_safe_max: helpers::parse_float(config, "Globals", "t_safe_max"), t_hysteresis: helpers::parse_float(config, "Globals", "t_hysteresis"), + ctl_vsense: helpers::parse_string(config, "Controls", "vsense"), + ctl_isense: helpers::parse_string(config, "Controls", "isense"), + ctl_amp_gain: helpers::parse_string(config, "Controls", "amp_gain"), + ctl_volume: helpers::parse_string(config, "Controls", "volume"), } } } @@ -235,7 +243,7 @@ impl Speaker { let section = "Speaker/".to_owned() + name; let mut new_speaker: Speaker = Speaker { name: name.to_string(), - alsa_iface: Mixer::new(&name, ctl), + alsa_iface: Mixer::new(&name, ctl, globals), group: helpers::parse_int(config, §ion, "group"), tau_coil: helpers::parse_float(config, §ion, "tau_coil"), tau_magnet: helpers::parse_float(config, §ion, "tau_magnet"), @@ -248,7 +256,7 @@ impl Speaker { vs_scale: helpers::parse_float(config, §ion, "vs_scale"), is_chan: helpers::parse_int(config, §ion, "is_chan"), vs_chan: helpers::parse_int(config, §ion, "vs_chan"), - g: *globals, + g: globals.clone(), s: Default::default(), };