From 0f866f8145f5d73cf3f1825f6527c70196d63248 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 31 Oct 2018 10:47:10 +0100 Subject: [PATCH] Move constructors/destructors to the specific modules --- src/ds3231.rs | 30 ++++++++++++++++++++ src/ds3232.rs | 22 ++++++++++++++- src/ds3234.rs | 22 ++++++++++++++- src/lib.rs | 76 ++------------------------------------------------- 4 files changed, 74 insertions(+), 76 deletions(-) create mode 100644 src/ds3231.rs diff --git a/src/ds3231.rs b/src/ds3231.rs new file mode 100644 index 0000000..82ccee6 --- /dev/null +++ b/src/ds3231.rs @@ -0,0 +1,30 @@ +//! Functions exclusive of DS3231 + +extern crate embedded_hal as hal; +use hal::blocking; +use core::marker::PhantomData; +use super::{ Ds323x, BitFlags, ic, CONTROL_POR_VALUE }; +use interface::I2cInterface; + +impl Ds323x, ic::DS3231> +where + I2C: blocking::i2c::Write + blocking::i2c::WriteRead +{ + /// Create a new instance of the DS3231 device. + pub fn new_ds3231(i2c: I2C) -> Self { + const STATUS_POR_VALUE : u8 = BitFlags::OSC_STOP | BitFlags::EN32KHZ; + Ds323x { + iface: I2cInterface { + i2c, + }, + control: CONTROL_POR_VALUE, + status: STATUS_POR_VALUE, + _ic: PhantomData + } + } + + /// Destroy driver instance, return I²C bus instance. + pub fn destroy_ds3231(self) -> I2C { + self.iface.i2c + } +} diff --git a/src/ds3232.rs b/src/ds3232.rs index 22232dc..f7ea383 100644 --- a/src/ds3232.rs +++ b/src/ds3232.rs @@ -2,13 +2,33 @@ extern crate embedded_hal as hal; use hal::blocking; -use super::{ Ds323x, TempConvRate, BitFlags, Error, ic }; +use core::marker::PhantomData; +use super::{ Ds323x, TempConvRate, BitFlags, Error, ic, CONTROL_POR_VALUE }; use interface::I2cInterface; + impl Ds323x, ic::DS3232> where I2C: blocking::i2c::Write + blocking::i2c::WriteRead { + /// Create a new instance of the DS3232 device. + pub fn new_ds3232(i2c: I2C) -> Self { + const STATUS_POR_VALUE : u8 = BitFlags::OSC_STOP | BitFlags::BB32KHZ | BitFlags::EN32KHZ; + Ds323x { + iface: I2cInterface { + i2c, + }, + control: CONTROL_POR_VALUE, + status: STATUS_POR_VALUE, + _ic: PhantomData + } + } + + /// Destroy driver instance, return I²C bus instance. + pub fn destroy_ds3232(self) -> I2C { + self.iface.i2c + } + /// Enable the 32kHz output when battery-powered. /// /// Additionally, the 32kHz output needs to be enabled. See diff --git a/src/ds3234.rs b/src/ds3234.rs index 07b3a96..b7fbd6e 100644 --- a/src/ds3234.rs +++ b/src/ds3234.rs @@ -2,7 +2,8 @@ extern crate embedded_hal as hal; use hal::blocking; -use super::{ Ds323x, TempConvRate, BitFlags, Error, ic }; +use core::marker::PhantomData; +use super::{ Ds323x, TempConvRate, BitFlags, Error, ic, CONTROL_POR_VALUE }; use interface::SpiInterface; impl Ds323x, ic::DS3234> @@ -10,6 +11,25 @@ where SPI: blocking::spi::Transfer + blocking::spi::Write, CS: hal::digital::OutputPin { + /// Create a new instance. + pub fn new_ds3234(spi: SPI, chip_select: CS) -> Self { + const STATUS_POR_VALUE : u8 = BitFlags::OSC_STOP | BitFlags::BB32KHZ | BitFlags::EN32KHZ; + Ds323x { + iface: SpiInterface { + spi, + cs: chip_select + }, + control: CONTROL_POR_VALUE, + status: STATUS_POR_VALUE, + _ic: PhantomData + } + } + + /// Destroy driver instance, return SPI bus instance and CS output pin. + pub fn destroy_ds3234(self) -> (SPI, CS) { + (self.iface.spi, self.iface.cs) + } + /// Enable the 32kHz output when battery-powered. /// /// Additionally, the 32kHz output needs to be enabled. See diff --git a/src/lib.rs b/src/lib.rs index 9a34f89..6793494 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -385,7 +385,6 @@ #![no_std] extern crate embedded_hal as hal; -use hal::blocking; use core::marker::PhantomData; /// All possible errors in this crate @@ -475,8 +474,6 @@ pub mod ic { /// DS3234 IC marker pub struct DS3234; } -pub mod interface; -use interface::{ I2cInterface, SpiInterface }; /// DS3231, DS3232 and DS3234 RTC driver #[derive(Debug, Default)] @@ -487,78 +484,9 @@ pub struct Ds323x { _ic: PhantomData } -impl Ds323x, ic::DS3231> -where - I2C: blocking::i2c::Write + blocking::i2c::WriteRead -{ - /// Create a new instance of the DS3231 device. - pub fn new_ds3231(i2c: I2C) -> Self { - const STATUS_POR_VALUE : u8 = BitFlags::OSC_STOP | BitFlags::EN32KHZ; - Ds323x { - iface: I2cInterface { - i2c, - }, - control: CONTROL_POR_VALUE, - status: STATUS_POR_VALUE, - _ic: PhantomData - } - } - - /// Destroy driver instance, return I²C bus instance. - pub fn destroy_ds3231(self) -> I2C { - self.iface.i2c - } -} - -impl Ds323x, ic::DS3232> -where - I2C: blocking::i2c::Write + blocking::i2c::WriteRead -{ - /// Create a new instance of the DS3232 device. - pub fn new_ds3232(i2c: I2C) -> Self { - const STATUS_POR_VALUE : u8 = BitFlags::OSC_STOP | BitFlags::BB32KHZ | BitFlags::EN32KHZ; - Ds323x { - iface: I2cInterface { - i2c, - }, - control: CONTROL_POR_VALUE, - status: STATUS_POR_VALUE, - _ic: PhantomData - } - } - - /// Destroy driver instance, return I²C bus instance. - pub fn destroy_ds3232(self) -> I2C { - self.iface.i2c - } -} - -impl Ds323x, ic::DS3234> -where - SPI: blocking::spi::Transfer + blocking::spi::Write, - CS: hal::digital::OutputPin -{ - /// Create a new instance. - pub fn new_ds3234(spi: SPI, chip_select: CS) -> Self { - const STATUS_POR_VALUE : u8 = BitFlags::OSC_STOP | BitFlags::BB32KHZ | BitFlags::EN32KHZ; - Ds323x { - iface: SpiInterface { - spi, - cs: chip_select - }, - control: CONTROL_POR_VALUE, - status: STATUS_POR_VALUE, - _ic: PhantomData - } - } - - /// Destroy driver instance, return SPI bus instance and CS output pin. - pub fn destroy_ds3234(self) -> (SPI, CS) { - (self.iface.spi, self.iface.cs) - } -} - +pub mod interface; mod ds323x; pub use ds323x::{ Hours, DateTime }; +mod ds3231; mod ds3232; mod ds3234;