Avoid using deprecated methods

master
Diego Barrios Romero 2023-07-16 13:43:46 +02:00
parent b06eb59cd6
commit fe990d7295
5 changed files with 39 additions and 22 deletions

View File

@ -117,7 +117,10 @@ use linux_embedded_hal::I2cdev;
fn main() {
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut rtc = Ds323x::new_ds3231(dev);
let datetime = NaiveDate::from_ymd(2020, 5, 1).and_hms(19, 59, 58);
let datetime = NaiveDate::from_ymd_opt(2020, 5, 1)
.unwrap()
.and_hms_opt(19, 59, 58)
.unwrap();
rtc.set_datetime(&datetime).unwrap();
// do something else...
let time = rtc.time().unwrap();

View File

@ -4,7 +4,10 @@ use linux_embedded_hal::I2cdev;
fn main() {
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut rtc = Ds323x::new_ds3231(dev);
let datetime = NaiveDate::from_ymd(2020, 5, 1).and_hms(19, 59, 58);
let datetime = NaiveDate::from_ymd_opt(2020, 5, 1)
.unwrap()
.and_hms_opt(19, 59, 58)
.unwrap();
rtc.set_datetime(&datetime).unwrap();
// do something else...
let time = rtc.time().unwrap();

View File

@ -84,13 +84,12 @@ where
/// Set the square-wave output frequency.
pub fn set_square_wave_frequency(&mut self, freq: SqWFreq) -> Result<(), Error<CommE, PinE>> {
let new_control;
match freq {
SqWFreq::_1Hz => new_control = self.control & !BitFlags::RS2 & !BitFlags::RS1,
SqWFreq::_1_024Hz => new_control = self.control & !BitFlags::RS2 | BitFlags::RS1,
SqWFreq::_4_096Hz => new_control = self.control | BitFlags::RS2 & !BitFlags::RS1,
SqWFreq::_8_192Hz => new_control = self.control | BitFlags::RS2 | BitFlags::RS1,
}
let new_control = match freq {
SqWFreq::_1Hz => self.control & !BitFlags::RS2 & !BitFlags::RS1,
SqWFreq::_1_024Hz => self.control & !BitFlags::RS2 | BitFlags::RS1,
SqWFreq::_4_096Hz => self.control | BitFlags::RS2 & !BitFlags::RS1,
SqWFreq::_8_192Hz => self.control | BitFlags::RS2 | BitFlags::RS1,
};
self.write_control(new_control)
}

View File

@ -637,7 +637,7 @@ mod alarm1_day {
set_alarm1_hms,
ALARM1_SECONDS,
[4, 3, 2, AM | 1],
NaiveTime::from_hms(2, 3, 4)
NaiveTime::from_hms_opt(2, 3, 4).unwrap()
);
set_alarm_test!(
match_hms,
@ -922,7 +922,7 @@ mod alarm2_day {
set_alarm2_hm,
ALARM2_MINUTES,
[3, 2, AM | 1],
NaiveTime::from_hms(2, 3, 0)
NaiveTime::from_hms_opt(2, 3, 0).unwrap()
);
set_alarm_test!(
match_hm,

View File

@ -1,4 +1,5 @@
use embedded_hal_mock::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
use rtcc::NaiveDateTime;
mod common;
use self::common::{
destroy_ds3231, destroy_ds3232, destroy_ds3234, new_ds3231, new_ds3232, new_ds3234, Register,
@ -8,6 +9,17 @@ use self::common::{
use ds323x::Rtcc;
use ds323x::{DateTimeAccess, Error, Hours, NaiveDate, NaiveTime};
fn new_datetime(y: i32, mo: u32, d: u32, h: u32, min: u32, s: u32) -> NaiveDateTime {
NaiveDate::from_ymd_opt(y, mo, d)
.unwrap()
.and_hms_opt(h, min, s)
.unwrap()
}
fn new_date(y: i32, mo: u32, d: u32) -> NaiveDate {
NaiveDate::from_ymd_opt(y, mo, d).unwrap()
}
macro_rules! read_set_param_write_two_test {
($name:ident, $method:ident, $value:expr, $register:ident, $binary_value1_read:expr, $bin1:expr, $bin2:expr) => {
_set_param_test!(
@ -206,28 +218,28 @@ macro_rules! invalid_dt_test {
use super::*;
#[test]
fn datetime_too_small() {
let dt = NaiveDate::from_ymd(1999, 1, 2).and_hms(3, 4, 5);
let dt = new_datetime(1999, 1, 2, 3, 4, 5);
let mut dev = $create_method(&[]);
assert_invalid_input_data!(dev.set_datetime(&dt));
$destroy_method(dev);
}
#[test]
fn datetime_too_big() {
let dt = NaiveDate::from_ymd(2101, 1, 2).and_hms(3, 4, 5);
let dt = new_datetime(2101, 1, 2, 3, 4, 5);
let mut dev = $create_method(&[]);
assert_invalid_input_data!(dev.set_datetime(&dt));
$destroy_method(dev);
}
#[test]
fn date_too_small() {
let d = NaiveDate::from_ymd(1999, 1, 2);
let d = new_date(1999, 1, 2);
let mut dev = $create_method(&[]);
assert_invalid_input_data!(dev.set_date(&d));
$destroy_method(dev);
}
#[test]
fn date_too_big() {
let d = NaiveDate::from_ymd(2101, 1, 2);
let d = new_date(2101, 1, 2);
let mut dev = $create_method(&[]);
assert_invalid_input_data!(dev.set_date(&d));
$destroy_method(dev);
@ -255,7 +267,7 @@ macro_rules! dt_test {
use super::*;
#[test]
fn get_datetime() {
let dt = NaiveDate::from_ymd(2018, 8, 13).and_hms(23, 59, 58);
let dt = new_datetime(2018, 8, 13, 23, 59, 58);
let mut dev = $create_method(&$mac_trans_read!(
SECONDS,
[
@ -275,7 +287,7 @@ macro_rules! dt_test {
#[test]
fn set_datetime() {
let dt = NaiveDate::from_ymd(2018, 8, 13).and_hms(23, 59, 58);
let dt = new_datetime(2018, 8, 13, 23, 59, 58);
let mut dev = $create_method(&$mac_trans_write!(
SECONDS,
[
@ -294,7 +306,7 @@ macro_rules! dt_test {
#[test]
fn get_date() {
let d = NaiveDate::from_ymd(2018, 8, 13);
let d = new_date(2018, 8, 13);
let mut dev = $create_method(&$mac_trans_read!(
DOM,
[0b0001_0011, 0b0000_1000, 0b0001_1000],
@ -306,7 +318,7 @@ macro_rules! dt_test {
#[test]
fn set_date() {
let d = NaiveDate::from_ymd(2018, 8, 13);
let d = new_date(2018, 8, 13);
let mut dev = $create_method(&$mac_trans_write!(
DOW,
[0b0000_0010, 0b0001_0011, 0b0000_1000, 0b0001_1000]
@ -317,7 +329,7 @@ macro_rules! dt_test {
#[test]
fn set_date_century() {
let d = NaiveDate::from_ymd(2100, 8, 13);
let d = new_date(2100, 8, 13);
let mut dev = $create_method(&$mac_trans_write!(
DOW,
[0b0000_0110, 0b0001_0011, 0b1000_1000, 0]
@ -328,7 +340,7 @@ macro_rules! dt_test {
#[test]
fn get_time() {
let t = NaiveTime::from_hms(23, 59, 58);
let t = NaiveTime::from_hms_opt(23, 59, 58).unwrap();
let mut dev = $create_method(&$mac_trans_read!(
SECONDS,
[0b0101_1000, 0b0101_1001, 0b0010_0011],
@ -340,7 +352,7 @@ macro_rules! dt_test {
#[test]
fn set_time() {
let t = NaiveTime::from_hms(23, 59, 58);
let t = NaiveTime::from_hms_opt(23, 59, 58).unwrap();
let mut dev = $create_method(&$mac_trans_write!(
SECONDS,
[0b0101_1000, 0b0101_1001, 0b0010_0011]