From 8e0b5333e3d275d20b2d7e668749263940d39d60 Mon Sep 17 00:00:00 2001
From: Jorge Aparicio <jorge@japaric.io>
Date: Tue, 13 Jun 2017 17:20:23 -0500
Subject: [PATCH] add listen/unlisten to Capture

---
 src/capture.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/src/capture.rs b/src/capture.rs
index 6156680..89c111e 100644
--- a/src/capture.rs
+++ b/src/capture.rs
@@ -52,6 +52,18 @@ pub enum Error {
     _Extensible,
 }
 
+/// Interrupt event
+pub enum Event {
+    /// Capture on channel 1
+    Capture1,
+    /// Capture on channel 2
+    Capture2,
+    /// Capture on channel 3
+    Capture3,
+    /// Capture on channel 4
+    Capture4,
+}
+
 /// Input capture interface
 pub struct Capture<'a, T>(pub &'a T)
 where
@@ -149,6 +161,30 @@ impl<'a> Capture<'a, TIM1> {
         );
     }
 
+    /// Starts listening for an interrupt `event`
+    pub fn listen(&self, event: Event) {
+        let tim1 = self.0;
+
+        match event {
+            Event::Capture1 => tim1.dier.modify(|_, w| w.cc1ie().set()),
+            Event::Capture2 => tim1.dier.modify(|_, w| w.cc2ie().set()),
+            Event::Capture3 => tim1.dier.modify(|_, w| w.cc3ie().set()),
+            Event::Capture4 => tim1.dier.modify(|_, w| w.cc4ie().set()),
+        }
+    }
+
+    /// Stops listening for an interrupt `event`
+    pub fn unlisten(&self, event: Event) {
+        let tim1 = self.0;
+
+        match event {
+            Event::Capture1 => tim1.dier.modify(|_, w| w.cc1ie().clear()),
+            Event::Capture2 => tim1.dier.modify(|_, w| w.cc2ie().clear()),
+            Event::Capture3 => tim1.dier.modify(|_, w| w.cc3ie().clear()),
+            Event::Capture4 => tim1.dier.modify(|_, w| w.cc4ie().clear()),
+        }
+    }
+
     fn _set_resolution(&self, resolution: ::apb2::Ticks) {
         let psc = u16(
             resolution.0.checked_sub(1).expect("impossible resolution"),
@@ -420,6 +456,30 @@ where
         );
     }
 
+    /// Starts listening for an interrupt `event`
+    pub fn listen(&self, event: Event) {
+        let tim = self.0;
+
+        match event {
+            Event::Capture1 => tim.dier.modify(|_, w| w.cc1ie().set()),
+            Event::Capture2 => tim.dier.modify(|_, w| w.cc2ie().set()),
+            Event::Capture3 => tim.dier.modify(|_, w| w.cc3ie().set()),
+            Event::Capture4 => tim.dier.modify(|_, w| w.cc4ie().set()),
+        }
+    }
+
+    /// Stops listening for an interrupt `event`
+    pub fn unlisten(&self, event: Event) {
+        let tim = self.0;
+
+        match event {
+            Event::Capture1 => tim.dier.modify(|_, w| w.cc1ie().clear()),
+            Event::Capture2 => tim.dier.modify(|_, w| w.cc2ie().clear()),
+            Event::Capture3 => tim.dier.modify(|_, w| w.cc3ie().clear()),
+            Event::Capture4 => tim.dier.modify(|_, w| w.cc4ie().clear()),
+        }
+    }
+
     fn _set_resolution(&self, resolution: ::apb1::Ticks) {
         let psc = u16(
             resolution.0.checked_sub(1).expect("impossible resolution"),
-- 
GitLab