Mbed OS 5

Fork of PinDetect by Arturo Alvarado

Rewritten to Mbed OS 5 and their callback mechanism

Revision:
5:2b0afd293d1d
Parent:
4:bacc386fe2ad
diff -r bacc386fe2ad -r 2b0afd293d1d PinDetect.cpp
--- a/PinDetect.cpp	Wed Nov 16 23:20:29 2016 +0000
+++ b/PinDetect.cpp	Thu Jun 28 14:02:22 2018 +0200
@@ -1,16 +1,16 @@
 /*
     Copyright (c) 2010 Andy Kirkham
- 
+
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"), to deal
     in the Software without restriction, including without limitation the rights
     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     copies of the Software, and to permit persons to whom the Software is
     furnished to do so, subject to the following conditions:
- 
+
     The above copyright notice and this permission notice shall be included in
     all copies or substantial portions of the Software.
- 
+
     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -24,33 +24,18 @@
 
 namespace AjK {
 
-static void donothing() {}
-    
-PinDetect::PinDetect(PinName p) : _callbackAsserted(),
-                                  _callbackDeasserted(),
-                                  _callbackAssertedHeld(),
-                                  _callbackDeassertedHeld() {
-    init( p, PullDefault );
-    _callbackAsserted.attach(donothing);
-    _callbackDeasserted.attach(donothing);
-    _callbackAssertedHeld.attach(donothing);
-    _callbackDeassertedHeld.attach(donothing);
+PinDetect::PinDetect(PinName p) {
+    init(p, PullDefault);
 }
 
-PinDetect::PinDetect(PinName p, PinMode m) : _callbackAsserted(),
-                                             _callbackDeasserted(),
-                                             _callbackAssertedHeld(),
-                                             _callbackDeassertedHeld() {
-    init( p, m );
-    _callbackAsserted.attach(donothing);
-    _callbackDeasserted.attach(donothing);
-    _callbackAssertedHeld.attach(donothing);
-    _callbackDeassertedHeld.attach(donothing);
+PinDetect::PinDetect(PinName p, PinMode m) {
+    init(p, m);
 }
 
 
-PinDetect::~PinDetect(void){
+PinDetect::~PinDetect(void) {
     if ( _ticker )  delete( _ticker );
+
     if ( _in )      delete( _in );
 }
 
@@ -60,21 +45,26 @@
     _samplesTillHeld         = 0;
     _samplesTillAssertReload = PINDETECT_ASSERT_COUNT;
     _samplesTillHeldReload   = PINDETECT_HOLD_COUNT;
-    _assertValue             = PINDETECT_PIN_ASSTERED;
-    
-    _in = new DigitalIn( p );
-    _in->mode( m );        
-    _prevState = _in->read();        
+    _assertValue             = PINDETECT_PIN_ASSERTED;
+
+    _in = new DigitalIn(p);
+    _in->mode(m);
+    _prevState = _in->read();
     _ticker = new Ticker;
+
+    _callbackAsserted = NULL;
+    _callbackDeasserted = NULL;
+    _callbackAssertedHeld = NULL;
+    _callbackDeassertedHeld = NULL;
 }
 
-void PinDetect::setSampleFrequency(int i) { 
-    _sampleTime = i; 
-    _prevState  = _in->read();        
-    _ticker->attach_us(callback(this, &PinDetect::isr), _sampleTime );
+void PinDetect::setSampleFrequency(int i) {
+    _sampleTime = i;
+    _prevState  = _in->read();
+    _ticker->attach_us(callback(this, &PinDetect::isr), _sampleTime);
 }
 
-void PinDetect::setAssertValue (int i) {
+void PinDetect::setAssertValue(int i) {
     _assertValue = i & 1;
 }
 
@@ -87,30 +77,30 @@
 }
 
 void PinDetect::mode(PinMode m) {
-    _in->mode( m );
+    _in->mode(m);
 }
 
 void PinDetect::attach_asserted(Callback<void()> function) {
     core_util_critical_section_enter();
-    _callbackAsserted.attach( function );
+    _callbackAsserted = function;
     core_util_critical_section_exit();
 }
 
 void PinDetect::attach_deasserted(Callback<void()> function) {
     core_util_critical_section_enter();
-    _callbackDeasserted.attach( function );
+    _callbackDeasserted = function;
     core_util_critical_section_exit();
 }
 
 void PinDetect::attach_asserted_held(Callback<void()> function) {
     core_util_critical_section_enter();
-    _callbackAssertedHeld.attach( function );
+    _callbackAssertedHeld = function;
     core_util_critical_section_exit();
 }
 
 void PinDetect::attach_deasserted_held(Callback<void()> function) {
     core_util_critical_section_enter();
-    _callbackDeassertedHeld.attach( function );
+    _callbackDeassertedHeld = function;
     core_util_critical_section_exit();
 }
 
@@ -121,30 +111,36 @@
         if ( _samplesTillAssert == 0 ) {
             _prevState = currentState;
             _samplesTillHeld = _samplesTillHeldReload;
-            if ( currentState == _assertValue ) 
-                _callbackAsserted.call();
-            else                              
-                _callbackDeasserted.call();
-        }
-        else {
+
+            if ( currentState == _assertValue ) {
+                if (_callbackAsserted) _callbackAsserted.call();
+
+            } else {
+                if (_callbackDeasserted) _callbackDeasserted.call();
+            }
+
+        } else {
             _samplesTillAssert--;
         }
-    }
-    else {
+
+    } else {
         _samplesTillAssert = _samplesTillAssertReload;
     }
-    
+
     if ( _samplesTillHeld ) {
         if ( _prevState == currentState ) {
             _samplesTillHeld--;
+
             if ( _samplesTillHeld == 0 ) {
-                if ( currentState == _assertValue ) 
-                    _callbackAssertedHeld.call();
-                else                              
-                    _callbackDeassertedHeld.call();
+                if ( currentState == _assertValue ) {
+                    if (_callbackAssertedHeld) _callbackAssertedHeld.call();
+
+                } else {
+                    if (_callbackDeassertedHeld) _callbackDeassertedHeld.call();
+                }
             }
-        }
-        else {
+
+        } else {
             _samplesTillHeld = 0;
         }
     }