Skip to content

Commit 251eae6

Browse files
committed
Added datetime back, this time with a nice type cehcking on Date & time objects, simply use 1.seconds() to convert an into the 'Seconds type etc'
1 parent ee93f53 commit 251eae6

File tree

5 files changed

+130
-98
lines changed

5 files changed

+130
-98
lines changed

examples/rtc.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ use hal::stm32l4::stm32l4x2;
1919

2020
use hal::delay::Delay;
2121
use hal::rtc::Rtc;
22-
use hal::rtc::Time;
23-
use hal::rtc::Date;
2422
use hal::pwr::Pwr;
23+
use hal::datetime::{Date,Time};
2524
use rt::ExceptionFrame;
2625

2726
use core::fmt::Write;
@@ -51,8 +50,8 @@ fn main() -> ! {
5150
let mut pwr = Pwr::pwr(&mut rcc.apb1r1);
5251
let rtc = Rtc::rtc(dp.RTC, &mut rcc.apb1r1, &mut rcc.bdcr, &mut pwr.cr1);
5352

54-
let mut time = Time::new(21, 57, 32, false);
55-
let mut date = Date::new(1, 24, 4, 2018);
53+
let mut time = Time::new(21.hours(), 57.minutes(), 32.seconds(), false);
54+
let mut date = Date::new(1.day(), 24.date(), 4.month(), 2018.year());
5655

5756
rtc.set_time(&time);
5857
rtc.set_date(&date);

src/datetime.rs

Lines changed: 107 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,74 @@
11
/// Date and timer units & helper functions
22
33
/// Seconds
4-
#[derive(Clone, Copy)]
5-
pub struct Seconds(pub u32);
4+
#[derive(Clone, Copy, Debug)]
5+
pub struct Second(pub u32);
66

77
/// Minutes
8-
#[derive(Clone, Copy)]
9-
pub struct Minutes(pub u32);
8+
#[derive(Clone, Copy, Debug)]
9+
pub struct Minute(pub u32);
1010

1111
/// Hours
12-
#[derive(Clone, Copy)]
13-
pub struct Hours(pub u32);
12+
#[derive(Clone, Copy, Debug)]
13+
pub struct Hour(pub u32);
1414

1515
/// Day (1-7)
16-
#[derive(Clone, Copy)]
16+
#[derive(Clone, Copy, Debug)]
1717
pub struct Day(pub u32);
1818

1919
/// Date (1-31)
20-
#[derive(Clone, Copy)]
21-
pub struct Date(pub u32);
20+
#[derive(Clone, Copy, Debug)]
21+
pub struct DateInMonth(pub u32);
2222

2323
/// Week (1-52)
24-
#[derive(Clone, Copy)]
24+
#[derive(Clone, Copy, Debug)]
2525
pub struct Week(pub u32);
2626

2727
/// Month (1-12)
28-
#[derive(Clone, Copy)]
28+
#[derive(Clone, Copy, Debug)]
2929
pub struct Month(pub u32);
3030

3131
/// Year
32-
#[derive(Clone, Copy)]
32+
#[derive(Clone, Copy, Debug)]
3333
pub struct Year(pub u32);
3434

3535
/// Extension trait that adds convenience methods to the `u32` type
3636
pub trait U32Ext {
3737
/// Seconds
38-
fn seconds(self) -> Seconds;
38+
fn seconds(self) -> Second;
3939
/// Minutes
40-
fn minutes(self) -> Minutes;
40+
fn minutes(self) -> Minute;
4141
/// Hours
42-
fn hours(self) -> Hours;
42+
fn hours(self) -> Hour;
4343
/// Day
4444
fn day(self) -> Day;
4545
/// Seconds
46-
fn date(self) -> Date;
46+
fn date(self) -> DateInMonth;
4747
/// Month
4848
fn month(self) -> Month;
4949
/// Year
5050
fn year(self) -> Year;
5151
}
5252

5353
impl U32Ext for u32 {
54-
fn seconds(self) -> Seconds {
55-
Seconds(self)
54+
fn seconds(self) -> Second {
55+
Second(self)
5656
}
5757

58-
fn minutes(self) -> Minutes {
59-
Minutes(self)
58+
fn minutes(self) -> Minute {
59+
Minute(self)
6060
}
6161

62-
fn hours(self) -> Hours {
63-
Hours(self)
62+
fn hours(self) -> Hour {
63+
Hour(self)
6464
}
6565

6666
fn day(self) -> Day {
6767
Day(self)
6868
}
6969

70-
fn date(self) -> Date {
71-
Date(self)
70+
fn date(self) -> DateInMonth {
71+
DateInMonth(self)
7272
}
7373

7474
fn month(self) -> Month {
@@ -80,32 +80,100 @@ impl U32Ext for u32 {
8080
}
8181
}
8282

83-
impl Into<Seconds> for Minutes {
84-
fn into(self) -> Seconds {
85-
Seconds(self.0 * 60)
83+
#[derive(Clone,Copy,Debug)]
84+
pub struct Time {
85+
pub hours: Hour,
86+
pub minutes: Minute,
87+
pub seconds: Second,
88+
pub daylight_savings: bool
89+
}
90+
91+
impl Time {
92+
pub fn new(hours: Hour, minutes: Minute, seconds: Second, daylight_savings: bool) -> Self {
93+
Self {
94+
hours: hours,
95+
minutes: minutes,
96+
seconds: seconds,
97+
daylight_savings: daylight_savings
98+
}
99+
}
100+
}
101+
102+
#[derive(Clone,Copy, Debug)]
103+
pub struct Date {
104+
pub day: Day,
105+
pub date: DateInMonth,
106+
pub month: Month,
107+
pub year: Year,
108+
}
109+
110+
impl Date {
111+
pub fn new(day: Day, date: DateInMonth, month: Month, year: Year) -> Self {
112+
Self {
113+
day: day,
114+
date: date,
115+
month: month,
116+
year: year
117+
}
86118
}
87119
}
88120

89-
impl Into<Seconds> for Hours {
90-
fn into(self) -> Seconds {
91-
Seconds(self.0 * 3600)
121+
impl Into<Second> for Minute {
122+
fn into(self) -> Second {
123+
Second(self.0 * 60)
92124
}
93125
}
94126

95-
impl From <u32> for Seconds {
96-
fn from(inner: u32) -> Seconds {
97-
Seconds(inner)
127+
impl Into<Second> for Hour {
128+
fn into(self) -> Second {
129+
Second(self.0 * 3600)
98130
}
99131
}
100132

101-
impl From <u32> for Minutes {
102-
fn from(inner: u32) -> Minutes {
103-
Minutes(inner)
133+
macro_rules! impl_from_struct {
134+
($(
135+
$type:ident: [ $($to:ident),+ ],
136+
)+) => {
137+
$(
138+
$(
139+
impl From <$type> for $to {
140+
fn from(inner: $type) -> $to {
141+
inner.0 as $to
142+
}
143+
}
144+
)+
145+
)+
104146
}
105147
}
106148

107-
impl From <u32> for Hours {
108-
fn from(inner: u32) -> Hours {
109-
Hours(inner)
149+
macro_rules! impl_to_struct {
150+
($(
151+
$type:ident: [ $($to:ident),+ ],
152+
)+) => {
153+
$(
154+
$(
155+
impl From <$type> for $to {
156+
fn from(inner: $type) -> $to {
157+
$to(inner as u32)
158+
}
159+
}
160+
)+
161+
)+
110162
}
111163
}
164+
165+
impl_from_struct!(
166+
Hour: [u32, u16, u8],
167+
Second: [u32, u16, u8],
168+
Minute: [u32, u16, u8],
169+
Day: [u32, u16, u8],
170+
DateInMonth: [u32, u16, u8],
171+
Month: [u32, u16, u8],
172+
Year: [u32, u16, u8],
173+
);
174+
175+
impl_to_struct!(
176+
u32: [Hour, Minute, Second, Day, DateInMonth, Month, Year],
177+
u16: [Hour, Minute, Second, Day, DateInMonth, Month, Year],
178+
u8: [Hour, Minute, Second, Day, DateInMonth, Month, Year],
179+
);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub mod timer;
2424
pub mod spi;
2525
pub mod rtc;
2626
pub mod pwr;
27-
// pub mod datetime;
27+
pub mod datetime;
2828

2929

3030
#[cfg(test)]

src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ pub use flash::FlashExt as _FlashExtHal;
55
pub use gpio::GpioExt as _GpioExtHal;
66
pub use hal::prelude::*; // embedded hal traits
77
pub use time::U32Ext as _stm32f30x_hal_time_U32Ext;
8-
// pub use datetime::U32Ext as _stm32f30x_hal_datetime;
8+
pub use datetime::U32Ext as _stm32f30x_hal_datetime;
99
pub use dma::DmaExt as _DmaExtHal;

src/rtc.rs

Lines changed: 18 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,10 @@
11
/// RTC peripheral abstraction
22
3-
// use datetime::*;
3+
use datetime::*;
44
use rcc::{BDCR, APB1R1};
55
use pwr;
66
use stm32l4::stm32l4x2::{RTC};
77

8-
#[derive(Clone,Copy,Debug)]
9-
pub struct Time {
10-
pub hours: u8,
11-
pub minutes: u8,
12-
pub seconds: u8,
13-
pub daylight_savings: bool
14-
}
15-
16-
impl Time {
17-
pub fn new(hours: u8, minutes: u8, seconds: u8, daylight_savings: bool) -> Self {
18-
Self {
19-
hours: hours,
20-
minutes: minutes,
21-
seconds: seconds,
22-
daylight_savings: daylight_savings
23-
}
24-
}
25-
}
26-
27-
#[derive(Clone,Copy,Debug)]
28-
pub struct Date {
29-
pub day: u8,
30-
pub date: u8,
31-
pub month: u8,
32-
pub year: u16,
33-
}
34-
35-
impl Date {
36-
pub fn new(day: u8, date: u8, month: u8, year: u16) -> Self {
37-
Self {
38-
day: day,
39-
date: date,
40-
month: month,
41-
year: year
42-
}
43-
}
44-
}
45-
468
/// RTC Abstraction
479
pub struct Rtc {
4810
rtc: RTC
@@ -124,9 +86,10 @@ impl Rtc {
12486
{
12587
init_mode(&self.rtc, true);
12688
{
127-
let (ht, hu) = byte_to_bcd2(time.hours);
128-
let (mnt, mnu) = byte_to_bcd2(time.minutes);
129-
let (st, su) = byte_to_bcd2(time.seconds);
89+
90+
let (ht, hu) = byte_to_bcd2(time.hours.into());
91+
let (mnt, mnu) = byte_to_bcd2(time.minutes.into());
92+
let (st, su) = byte_to_bcd2(time.seconds.into());
13093
self.rtc.tr.write(|w| unsafe {
13194
w.ht().bits(ht)
13295
.hu().bits(hu)
@@ -155,9 +118,9 @@ impl Rtc {
155118

156119
let timer = self.rtc.tr.read();
157120
let cr = self.rtc.cr.read();
158-
time = Time::new(bcd2_to_byte((timer.ht().bits(), timer.hu().bits())),
159-
bcd2_to_byte((timer.mnt().bits(), timer.mnu().bits())),
160-
bcd2_to_byte((timer.st().bits(), timer.su().bits())),
121+
time = Time::new(bcd2_to_byte((timer.ht().bits(), timer.hu().bits())).into(),
122+
bcd2_to_byte((timer.mnt().bits(), timer.mnu().bits())).into(),
123+
bcd2_to_byte((timer.st().bits(), timer.su().bits())).into(),
161124
cr.fmt().bit());
162125

163126
write_protection(&self.rtc, true);
@@ -170,9 +133,11 @@ impl Rtc {
170133
{
171134
init_mode(&self.rtc, true);
172135
{
173-
let (dt, du) = byte_to_bcd2(date.date);
174-
let (mt, mu) = byte_to_bcd2(date.month);
175-
let (yt, yu) = byte_to_bcd2((date.year - 1970_u16) as u8);
136+
let (dt, du) = byte_to_bcd2(date.date.into());
137+
let (mt, mu) = byte_to_bcd2(date.month.into());
138+
let yr: u16 = date.year.into();
139+
let yr_offset = (yr - 1970_u16) as u8;
140+
let (yt, yu) = byte_to_bcd2(yr_offset);
176141

177142
self.rtc.dr.write(|w| unsafe {
178143
w.dt().bits(dt)
@@ -181,7 +146,7 @@ impl Rtc {
181146
.mu().bits(mu)
182147
.yt().bits(yt)
183148
.yu().bits(yu)
184-
.wdu().bits(date.day)
149+
.wdu().bits(date.day.into())
185150
});
186151

187152

@@ -195,10 +160,10 @@ impl Rtc {
195160
let date;
196161

197162
let dater = self.rtc.dr.read();
198-
date = Date::new(dater.wdu().bits(),
199-
bcd2_to_byte((dater.dt().bits(), dater.du().bits())),
200-
bcd2_to_byte((dater.mt().bit() as u8, dater.mu().bits())),
201-
(bcd2_to_byte((dater.yt().bits(), dater.yu().bits())) as u16 + 1970_u16) as u16);
163+
date = Date::new(dater.wdu().bits().into(),
164+
bcd2_to_byte((dater.dt().bits(), dater.du().bits())).into(),
165+
bcd2_to_byte((dater.mt().bit() as u8, dater.mu().bits())).into(),
166+
(bcd2_to_byte((dater.yt().bits(), dater.yu().bits())) as u16 + 1970_u16).into());
202167
date
203168
}
204169

0 commit comments

Comments
 (0)