From 8bea87081319c208ba36dde8927b46f9b74dc35b Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 31 Oct 2018 10:01:47 +0100 Subject: [PATCH] Extract function to write the status register --- src/ds323x/configuration.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ds323x/configuration.rs b/src/ds323x/configuration.rs index 7973f9e..2a8f1a8 100644 --- a/src/ds323x/configuration.rs +++ b/src/ds323x/configuration.rs @@ -34,20 +34,14 @@ where /// Enable the 32kHz output. pub fn enable_32khz_output(&mut self) -> Result<(), Error> { - // avoid clearing alarm flags - let status = self.status | BitFlags::EN32KHZ | BitFlags::ALARM2F | BitFlags::ALARM1F; - self.iface.write_register(Register::STATUS, status)?; - self.status = status; - Ok(()) + let status = self.status | BitFlags::EN32KHZ; + self.write_status_without_clearing_alarm(status) } /// Disable the 32kHz output. pub fn disable_32khz_output(&mut self) -> Result<(), Error> { - // avoid clearing alarm flags - let status = self.status & !BitFlags::EN32KHZ | BitFlags::ALARM2F | BitFlags::ALARM1F; - self.iface.write_register(Register::STATUS, status)?; - self.status = status; - Ok(()) + let status = self.status & !BitFlags::EN32KHZ; + self.write_status_without_clearing_alarm(status) } /// Set the aging offset. @@ -102,4 +96,12 @@ where self.control = control; Ok(()) } + + pub(crate) fn write_status_without_clearing_alarm(&mut self, status: u8) -> Result<(), Error> { + // avoid clearing alarm flags + let new_status = status | BitFlags::ALARM2F | BitFlags::ALARM1F; + self.iface.write_register(Register::STATUS, new_status)?; + self.status = status; + Ok(()) + } }