1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Alarm support

extern crate embedded_hal as hal;
use super::super::{ Ds323x, Hours, Register, BitFlags, Error };
use interface::{ ReadData, WriteData };
use super::{ decimal_to_packed_bcd, hours_to_register };

/// Parameters for setting Alarm1 on a day of the month
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DayAlarm1 {
    /// Day of the month [1-31]
    pub day: u8,
    /// Hour
    pub hour: Hours,
    /// Minute [0-59]
    pub minute: u8,
    /// Second [0-59]
    pub second: u8
}

/// Parameters for setting Alarm1 on a weekday
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WeekdayAlarm1 {
    /// Weekday [1-7]
    pub weekday: u8,
    /// Hour
    pub hour: Hours,
    /// Minute [0-59]
    pub minute: u8,
    /// Second [0-59]
    pub second: u8
}

/// Alarm1 trigger rate
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Alarm1Matching {
    /// Alarm once per second.
    OncePerSecond,
    /// Alarm when seconds match.
    SecondsMatch,
    /// Alarm when minutes and seconds match.
    MinutesAndSecondsMatch,
    /// Alarm when hours, minutes and seconds match.
    HoursMinutesAndSecondsMatch,
    /// Alarm when date/weekday, hours, minutes and seconds match.
    AllMatch
}


/// Parameters for setting Alarm2 on a day of the month
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DayAlarm2 {
    /// Day of month [1-31]
    pub day: u8,
    /// Hour
    pub hour: Hours,
    /// Minute [0-59]
    pub minute: u8
}

/// Parameters for setting Alarm2 on a weekday
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WeekdayAlarm2 {
    /// Weekday [1-7]
    pub weekday: u8,
    /// Hour
    pub hour: Hours,
    /// Minute [0-59]
    pub minute: u8
}

/// Alarm2 trigger rate
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Alarm2Matching {
    /// Alarm once per minute. (00 seconds of every minute)
    OncePerMinute,
    /// Alarm when minutes match.
    MinutesMatch,
    /// Alarm when hours and minutes match.
    HoursAndMinutesMatch,
    /// Alarm when date/weekday, hours and minutes match.
    AllMatch
}

fn get_matching_mask_alarm1(matching: Alarm1Matching) -> [u8; 4] {
    const AM : u8 = BitFlags::ALARM_MATCH;
    match matching {
        Alarm1Matching::OncePerSecond               => [AM, AM, AM, AM],
        Alarm1Matching::SecondsMatch                => [ 0, AM, AM, AM],
        Alarm1Matching::MinutesAndSecondsMatch      => [ 0,  0, AM, AM],
        Alarm1Matching::HoursMinutesAndSecondsMatch => [ 0,  0,  0, AM],
        Alarm1Matching::AllMatch                    => [ 0,  0,  0,  0],
    }
}

fn get_matching_mask_alarm2(matching: Alarm2Matching) -> [u8; 3] {
    const AM : u8 = BitFlags::ALARM_MATCH;
    match matching {
        Alarm2Matching::OncePerMinute        => [AM, AM, AM],
        Alarm2Matching::MinutesMatch         => [ 0, AM, AM],
        Alarm2Matching::HoursAndMinutesMatch => [ 0,  0, AM],
        Alarm2Matching::AllMatch             => [ 0,  0,  0],
    }
}


impl<DI, IC, E> Ds323x<DI, IC>
where
    DI: ReadData<Error = E> + WriteData<Error = E>
{
    /// Set Alarm1 for day of the month.
    ///
    /// Will return an `Error::InvalidInputData` if any of the parameters is out of range.
    pub fn set_alarm1_day(&mut self, when: DayAlarm1, matching: Alarm1Matching) -> Result<(), Error<E>> {
        if when.day < 1    || when.day > 31 ||
           when.minute > 59 ||
           when.second > 59 {
            return Err(Error::InvalidInputData);
        }
        let match_mask = get_matching_mask_alarm1(matching);
        let mut data = [ Register::ALARM1_SECONDS,
                         decimal_to_packed_bcd(when.second) | match_mask[0],
                         decimal_to_packed_bcd(when.minute) | match_mask[1],
                         hours_to_register(when.hour)?      | match_mask[2],
                         decimal_to_packed_bcd(when.day)    | match_mask[3]];
        self.iface.write_data(&mut data)
    }

    /// Set Alarm1 for weekday.
    ///
    /// Will return an `Error::InvalidInputData` if any of the parameters is out of range.
    pub fn set_alarm1_weekday(&mut self, when: WeekdayAlarm1, matching: Alarm1Matching) -> Result<(), Error<E>> {
        if when.weekday < 1    || when.weekday > 7 ||
           when.minute > 59 ||
           when.second > 59 {
            return Err(Error::InvalidInputData);
        }
        let match_mask = get_matching_mask_alarm1(matching);
        let mut data = [ Register::ALARM1_SECONDS,
                         decimal_to_packed_bcd(when.second)  | match_mask[0],
                         decimal_to_packed_bcd(when.minute)  | match_mask[1],
                         hours_to_register(when.hour)?       | match_mask[2],
                         decimal_to_packed_bcd(when.weekday) | match_mask[3] | BitFlags::WEEKDAY];
        self.iface.write_data(&mut data)
    }

    /// Set Alarm2 for date (day of month).
    ///
    /// Will return an `Error::InvalidInputData` if any of the parameters is out of range.
    pub fn set_alarm2_day(&mut self, when: DayAlarm2, matching: Alarm2Matching) -> Result<(), Error<E>> {
        if when.day < 1    || when.day > 31 ||
           when.minute > 59 {
            return Err(Error::InvalidInputData);
        }
        let match_mask = get_matching_mask_alarm2(matching);
        let mut data = [ Register::ALARM2_MINUTES,
                         decimal_to_packed_bcd(when.minute) | match_mask[0],
                         hours_to_register(when.hour)?      | match_mask[1],
                         decimal_to_packed_bcd(when.day)    | match_mask[2]];
        self.iface.write_data(&mut data)
    }

    /// Set Alarm2 for weekday.
    ///
    /// Will return an `Error::InvalidInputData` if any of the parameters is out of range.
    pub fn set_alarm2_weekday(&mut self, when: WeekdayAlarm2, matching: Alarm2Matching) -> Result<(), Error<E>> {
        if when.weekday < 1    || when.weekday > 7 ||
           when.minute > 59 {
            return Err(Error::InvalidInputData);
        }
        let match_mask = get_matching_mask_alarm2(matching);
        let mut data = [ Register::ALARM2_MINUTES,
                         decimal_to_packed_bcd(when.minute)  | match_mask[0],
                         hours_to_register(when.hour)?       | match_mask[1],
                         decimal_to_packed_bcd(when.weekday) | match_mask[2] | BitFlags::WEEKDAY];
        self.iface.write_data(&mut data)
    }
}