Bob Merrison-Hort / Mbed 2 deprecated elec350

Dependencies:   mbed

Fork of elec350 by Bob Merrison-Hort

Revision:
14:b1f94dd4a47f
Parent:
12:ae626e46b996
diff -r 95223a7ce05b -r b1f94dd4a47f microphone.cpp
--- a/microphone.cpp	Wed Oct 21 22:43:49 2015 +0000
+++ b/microphone.cpp	Thu Oct 22 08:40:54 2015 +0000
@@ -35,7 +35,7 @@
 
 void Microphone::start()
 {
-    if (this->isStarted == false) {
+    if (!this->isStarted) {
         // Two pins: clock to microphone and data from microphone.
         _mic_d = new DigitalIn(PC_3);
         _mic_clk = new DigitalOut(PB_10);
@@ -63,30 +63,37 @@
         NVIC_SetVector(TIM3_IRQn, (uint32_t)_mic_timer_int);
         NVIC_EnableIRQ(TIM3_IRQn);
         TIM3->DIER |= TIM_DIER_UIE;
+        this->isStarted = true;
     }
 }
 
 void Microphone::stop() // Not tested!
 {
-    // Disable interrupts.
-    NVIC_SetVector(TIM3_IRQn, (uint32_t)_mic_timer_int);
-    NVIC_DisableIRQ(TIM3_IRQn);
-    TIM3->DIER &= ~TIM_DIER_UIE;
-        
-    // Disable counter
-    TIM3->CR1 &= ~TIM_CR1_CEN;    
+    if (this->isStarted) {
+        // Disable interrupts.
+        NVIC_SetVector(TIM3_IRQn, (uint32_t)_mic_timer_int);
+        NVIC_DisableIRQ(TIM3_IRQn);
+        TIM3->DIER &= ~TIM_DIER_UIE;
+            
+        // Disable counter
+        TIM3->CR1 &= ~TIM_CR1_CEN;    
+            
+        // Disable timer.
+        RCC->APB1ENR &= ~RCC_APB1ENR_TIM3EN;
+            
+        // Clear interrupt
+        TIM3->SR &= ~TIM_SR_UIF;
         
-    // Disable timer.
-    RCC->APB1ENR &= ~RCC_APB1ENR_TIM3EN;
-        
-    // Clear interrupt
-    TIM3->SR &= ~TIM_SR_UIF;
-    
-    delete _mic_d; _mic_d = NULL;
-    delete _mic_clk; _mic_clk = NULL;
+        delete _mic_d; _mic_d = NULL;
+        delete _mic_clk; _mic_clk = NULL;
+    }
 }
 
 int8_t Microphone::read()
 {
-    return (int8_t)((uint16_t)_mic_pulses_buffered - 128);
+    if (this->isStarted) {
+        return (int8_t)((uint16_t)_mic_pulses_buffered - 128);
+    } else {
+        return 0;
+    }
 }