Add method to create a Datetime since the RTC instance

main
kirbylife 2023-03-30 22:25:02 -06:00
parent 432a770887
commit 5098e7b453
2 changed files with 24 additions and 20 deletions

View File

@ -30,6 +30,28 @@ impl Datetime {
t += self.seconds as i64; t += self.seconds as i64;
t as u64 t as u64
} }
pub fn from_ds3231<T: ds323x::Rtcc>(rtc: &mut T) -> Self {
let year = rtc.year().map_err(|_| ()).unwrap();
let month = rtc.month().map_err(|_| ()).unwrap();
let day = rtc.day().map_err(|_| ()).unwrap();
let hour = match rtc.hours().map_err(|_| ()).unwrap() {
ds323x::Hours::AM(val) => val,
ds323x::Hours::PM(val) => val + 12,
ds323x::Hours::H24(val) => val,
};
let minute = rtc.minutes().map_err(|_| ()).unwrap();
let second = rtc.seconds().map_err(|_| ()).unwrap();
Datetime {
year: year,
month: month,
day: day,
hours: hour,
minutes: minute,
seconds: second,
}
}
} }
fn year_to_secs(year: i64) -> (i64, bool) { fn year_to_secs(year: i64) -> (i64, bool) {

View File

@ -7,7 +7,6 @@ mod datetime;
use arduino_hal::default_serial; use arduino_hal::default_serial;
use arduino_hal::delay_ms; use arduino_hal::delay_ms;
use crypto::hmac_sha1; use crypto::hmac_sha1;
use ds323x::Rtcc;
use panic_halt as _; use panic_halt as _;
@ -73,27 +72,10 @@ fn main() -> ! {
); );
let mut rtc = ds323x::Ds323x::new_ds3231(i2c_clock); let mut rtc = ds323x::Ds323x::new_ds3231(i2c_clock);
let year = rtc.year().unwrap(); let datetime = datetime::Datetime::from_ds3231(&mut rtc);
let month = rtc.month().unwrap();
let day = rtc.day().unwrap();
let hour = match rtc.hours().unwrap() {
ds323x::Hours::AM(val) => val,
ds323x::Hours::PM(val) => val + 12,
ds323x::Hours::H24(val) => val,
};
let minute = rtc.minutes().unwrap();
let second = rtc.seconds().unwrap();
let datetime = datetime::Datetime {
year: year,
month: month,
day: day,
hours: hour,
minutes: minute,
seconds: second,
};
let timestamp = datetime.unix_epoch(); let timestamp = datetime.unix_epoch();
ufmt::uwriteln!(&mut serial, "Timestamp: {}", timestamp); ufmt::uwriteln!(&mut serial, "Timestamp: {}", timestamp).unwrap();
let token = generate_otp_token("FS7J22EHLLSOGKUVJKV2XE7FTIX24JAJ", 1680155315); let token = generate_otp_token("FS7J22EHLLSOGKUVJKV2XE7FTIX24JAJ", 1680155315);
ufmt::uwriteln!(&mut serial, "token: {}", token).unwrap(); ufmt::uwriteln!(&mut serial, "token: {}", token).unwrap();