From 03f8dea77faadd887df0de4ad4720d06a29e656f Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Mon, 18 Sep 2023 18:15:53 +0800 Subject: [PATCH 1/2] Add function enable/disable SW pins Useful if not all SW pins are connected. It's good to disable disconnected ones. Otherwise it causes higher voltages spikes which can cause audible noise. Signed-off-by: Daniel Schaefer --- src/lib.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 0ab8f42..7143e09 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -125,6 +125,15 @@ where Ok(()) } + /// How many SW rows to enable + pub fn sw_enablement(&mut self, setting: SwSetting) -> Result<(), I2cError> { + let config_register = 1; // Shutdown disable + + let new_val = (config_register & 0x0F) | (setting as u8) << 4; + self.write_register(Page::Config, addresses::CONFIG_REGISTER, new_val)?; + Ok(()) + } + /// Set the PWM frequency pub fn set_pwm_freq(&mut self, pwm: PwmFreq) -> Result<(), I2cError> { self.write_register(Page::Config, addresses::PWM_FREQ_REGISTER, pwm as u8) @@ -221,3 +230,25 @@ pub enum PwmFreq { /// 900Hz P900 = 0x0B, } + +#[repr(u8)] +pub enum SwSetting { + // SW1-SW9 active + Sw1Sw9 = 0b0000, + // SW1-SW8 active, SW9 not active + Sw1Sw8 = 0b0001, + // SW1-SW7 active, SW8-SW9 not active + Sw1Sw7 = 0b0010, + // SW1-SW6 active, SW7-SW9 not active + Sw1Sw6 = 0b0011, + // SW1-SW5 active, SW6-SW9 not active + Sw1Sw5 = 0b0100, + // SW1-SW4 active, SW5-SW9 not activee + Sw1Sw4 = 0b0101, + // SW1-SW3 active, SW4-SW9 not active + Sw1Sw3 = 0b0110, + // SW1-SW2 active, SW3-SW9 not active + Sw1Sw2 = 0b0111, + // All CSx pins only act as current sink, no scanning + NoScan = 0b1000, +} From 51188d1392ca1b04e58f17b64048d72d36975636 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Tue, 19 Sep 2023 11:11:31 +0800 Subject: [PATCH 2/2] Make sure sw_enablement doesn't overwrite register Signed-off-by: Daniel Schaefer --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 7143e09..01d6fdb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -127,7 +127,7 @@ where /// How many SW rows to enable pub fn sw_enablement(&mut self, setting: SwSetting) -> Result<(), I2cError> { - let config_register = 1; // Shutdown disable + let config_register = self.read_register(Page::Config, addresses::CONFIG_REGISTER)?; let new_val = (config_register & 0x0F) | (setting as u8) << 4; self.write_register(Page::Config, addresses::CONFIG_REGISTER, new_val)?;