Pulse measurement to know signal occupancy

Dependents:   PwmReaderTest

Revision:
1:ebc39fb22351
Parent:
0:15aa9d3aeb2e
--- a/PwmReader.cpp	Wed Jan 25 08:52:00 2017 +0000
+++ b/PwmReader.cpp	Thu Jan 26 08:26:22 2017 +0000
@@ -23,51 +23,89 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
- 
+
 #include "PwmReader.h"
 
+/// #define TRACE to enable TRACEs using printf #undef to disable
+#undef TRACE
+
 
 void PwmReader::pressedInt()
 {
+    long now = _timer.read_us();
+    // signal moved from high to low, record the time spent in this state
     _high += _timer.read_us() - _last_toggle;
+    // record the new timestamp
     _last_toggle = _timer.read_us();
-//    _timer.reset();
-//    printf("p %ld ", _high);
+#ifdef TRACE
+    printf("p %ld ", _high);
+#endif
 //    sleep();
 }
 
 void PwmReader::releasedInt()
 {
-    _down += _timer.read_us() - _last_toggle;
-    _last_toggle = _timer.read_us();
-//    _timer.reset();
-//    printf("r %ld", _down);
+    long now = _timer.read_us();
+    // signal moved from down to high, record the time spent in this state
+    _down += now - _last_toggle;
+    // record the new timestamp
+    _last_toggle = now;
+#ifdef TRACE
+    printf("r %ld", _down);
+#endif
 //    sleep();
 }
 
 void PwmReader::init()
 {
+    // register the interupt attached with the levels changes
     _pin.fall(callback(this, &PwmReader::pressedInt));
     _pin.rise(callback(this, &PwmReader::releasedInt));
 }
 
 void PwmReader::start()
 {
+    // reset the storing variable
     _high = 0;
     _down = 0;
+    _last_toggle = 0;
+
+    // store the inital state
     _start_state = _pin.read();
+
+    // start the timer at 0
+    _timer.reset();
+    _timer.start();
+
+    // enable the IRQ requests
     _pin.enable_irq();
-    _timer.start();
+
+#ifdef TRACE
+    if (_pin.read() != _start_state) {
+        printf("toggle ");
+    }
+    if (_pin.read() == 0) {
+        printf("low ");
+    }
+    if (_pin.read() == 1) {
+        printf("high ");
+    }
+#endif
 }
 
 void PwmReader::stop()
 {
+    // disable the IRQ request, to avoid code call
     _pin.disable_irq();
 
+    // no time has been recorded. The signal stayed at the same level from the start of measurment
     if (_high == 0 && _down == 0)
-        if (_start_state == 0)
+        // retrieve the duration of the measurment to assign it to the right state
+        if (_start_state == 1)
             _high = _timer.read_us();
-        else if (_start_state == 1)
+        else if (_start_state == 0)
             _down = _timer.read_us();
+
+    // stop the running timer
     _timer.stop();
 }
\ No newline at end of file