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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use crate::{
    drivers::pins::Pin,
    traits::Gint,
    typestates::pin::{
        self,
        PinId,
    }
};

pub trait Mode {}
pub trait NotAnd {}
pub trait NotOr {}
pub trait Set {}
pub struct NotSet;
impl Mode for NotSet {}
impl NotAnd for NotSet {}
impl NotOr for NotSet {}
pub struct And;
impl Mode for And {}
impl NotOr for And {}
impl Set for And {}
pub struct Or;
impl Mode for Or {}
impl NotAnd for Or {}
impl Set for Or {}

// pub enum Mode {
//     And,
//     Or,
// }

pub enum Active {
    High,
    Low,
}

pub enum Trigger {
    Edge,
    Level,
}

// This is a kind of build, but on itself.
// To accommodate the typestate changes, it's a consuming builder.

pub struct GroupInterrupt<GINT, MODE = NotSet>
where
    GINT: Gint,
    MODE: Mode,
{
    gint: GINT,
    #[allow(dead_code)]
    mode: MODE,
    trigger: Trigger,
}

impl<GINT> GroupInterrupt<GINT>
where
    GINT: Gint,
{
    pub fn new_edge_triggered(gint: GINT) -> GroupInterrupt<GINT, NotSet> {
        GroupInterrupt::new(gint, Trigger::Edge)
    }

    pub fn new_level_triggered(gint: GINT) -> GroupInterrupt<GINT, NotSet> {
        GroupInterrupt::new(gint, Trigger::Level)
    }

    pub fn new(gint: GINT, trigger: Trigger) -> GroupInterrupt<GINT, NotSet> {
        match trigger {
            Trigger::Edge => {
                gint.ctrl.modify(|_, w| w.trig().edge_triggered());
            },
            Trigger::Level => {
                gint.ctrl.modify(|_, w| w.trig().level_triggered());
            },
        };

        Self {
            gint,
            mode: NotSet,
            trigger,
        }
    }
}


impl<GINT, MODE> GroupInterrupt<GINT, MODE>
where
    GINT: Gint,
    MODE: Mode,
    MODE: NotAnd,
{
    pub fn or(
        self,
    ) -> GroupInterrupt<GINT, Or> {

        self.gint.ctrl.modify(|_, w| w.comb().or());

        GroupInterrupt {
            gint: self.gint,
            mode: Or,
            trigger: self.trigger,
        }
    }
}

impl<GINT, MODE> GroupInterrupt<GINT, MODE>
where
    GINT: Gint,
    MODE: Mode,
    MODE: NotOr,
{
    pub fn and(
        self,
    ) -> GroupInterrupt<GINT, And> {

        self.gint.ctrl.modify(|_, w| w.comb().and());

        GroupInterrupt {
            gint: self.gint,
            mode: And,
            trigger: self.trigger,
        }
    }
}

impl<GINT, MODE> GroupInterrupt<GINT, MODE>
where
    GINT: Gint,
    MODE: Mode,
    MODE: Set,
{

    pub fn on<PIO: PinId>(
        self,
        _pin: &Pin<PIO, pin::state::Gpio<pin::gpio::direction::Input>>,
        active: Active,
    ) -> GroupInterrupt<GINT, MODE> {

        match active {
            Active::Low =>  {
                self.gint.port_pol[PIO::PORT].modify(|r, w| unsafe {
                    w.pol().bits(r.pol().bits() & !PIO::MASK)
                });
            },
            Active::High =>  {
                self.gint.port_pol[PIO::PORT].modify(|r, w| unsafe {
                    w.pol().bits(r.pol().bits() | PIO::MASK)
                });
            },
        };

        self.gint.port_ena[PIO::PORT].modify(|r, w| unsafe {
            w.ena().bits(r.ena().bits() | PIO::MASK)
        });

        GroupInterrupt {
            gint: self.gint,
            mode: self.mode,
            trigger: self.trigger,
        }
    }

    pub fn on_high<PIO: PinId>(
        self,
        pin: &Pin<PIO, pin::state::Gpio<pin::gpio::direction::Input>>,
    ) -> GroupInterrupt<GINT, MODE> {
        self.on(pin, Active::High)
    }

    pub fn on_low<PIO: PinId>(
        self,
        pin: &Pin<PIO, pin::state::Gpio<pin::gpio::direction::Input>>,
    ) -> GroupInterrupt<GINT, MODE> {
        self.on(pin, Active::Low)
    }

    pub fn clear_interrupt(&self) {
        self.gint.ctrl.modify(|_, w| w.int().set_bit());
    }

}

impl<GINT, MODE> GroupInterrupt<GINT, MODE>
where
    GINT: Gint,
    MODE: Mode,
    MODE: NotAnd,
{
    pub fn or_on<PIO: PinId>(
        self,
        pin: &Pin<PIO, pin::state::Gpio<pin::gpio::direction::Input>>,
        active: Active,
    ) -> GroupInterrupt<GINT, Or> {

        self.or().on(pin, active)
    }

    pub fn or_on_high<PIO: PinId>(
        self,
        pin: &Pin<PIO, pin::state::Gpio<pin::gpio::direction::Input>>,
    ) -> GroupInterrupt<GINT, Or> {
        self.or_on(pin, Active::High)
    }

    pub fn or_on_low<PIO: PinId>(
        self,
        pin: &Pin<PIO, pin::state::Gpio<pin::gpio::direction::Input>>,
    ) -> GroupInterrupt<GINT, Or> {
        self.or_on(pin, Active::Low)
    }

}

impl<GINT, MODE> GroupInterrupt<GINT, MODE>
where
    GINT: Gint,
    MODE: Mode,
    MODE: NotOr,
{
    pub fn and_on<PIO: PinId>(
        self,
        pin: &Pin<PIO, pin::state::Gpio<pin::gpio::direction::Input>>,
        active: Active,
    ) -> GroupInterrupt<GINT, And> {

        self.and().on(pin, active)
    }

    pub fn and_on_high<PIO: PinId>(
        self,
        pin: &Pin<PIO, pin::state::Gpio<pin::gpio::direction::Input>>,
    ) -> GroupInterrupt<GINT, And> {
        self.and_on(pin, Active::High)
    }

    pub fn and_on_low<PIO: PinId>(
        self,
        pin: &Pin<PIO, pin::state::Gpio<pin::gpio::direction::Input>>,
    ) -> GroupInterrupt<GINT, And> {
        self.and_on(pin, Active::Low)
    }

}