Mbed OS 5

Fork of PinDetect by Arturo Alvarado

Rewritten to Mbed OS 5 and their callback mechanism

Files at this revision

API Documentation at this revision

Comitter:
pilotak
Date:
Thu Jun 28 14:02:22 2018 +0200
Parent:
4:bacc386fe2ad
Child:
6:09458c0b5cad
Commit message:
Callback fix for Mbed OS 5.9

Changed in this revision

ChangeLog.h Show annotated file Show diff for this revision Revisions of this file
PinDetect.cpp Show annotated file Show diff for this revision Revisions of this file
PinDetect.h Show annotated file Show diff for this revision Revisions of this file
--- a/ChangeLog.h	Wed Nov 16 23:20:29 2016 +0000
+++ b/ChangeLog.h	Thu Jun 28 14:02:22 2018 +0200
@@ -19,6 +19,8 @@
     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     THE SOFTWARE.
     
+    1.7     28 Jun 2018 - Callback fix for Mbed OS 5.9
+
     1.6     17 Nov 2016 - Rewritten to Mbed OS 5.2
 
     1.5     13 Jan 2011 - Made the isr() protected and made the class a friend of Ticker.
--- 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;
         }
     }
--- a/PinDetect.h	Wed Nov 16 23:20:29 2016 +0000
+++ b/PinDetect.h	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,127 +24,98 @@
 #define AJK_PIN_DETECT_H
 
 #ifndef MBED_H
-#include "mbed.h"
+    #include "mbed.h"
 #endif
 
-#ifndef PINDETECT_PIN_ASSTERED
-#define PINDETECT_PIN_ASSTERED   1
+#ifndef PINDETECT_PIN_ASSERTED
+    #define PINDETECT_PIN_ASSERTED   1
 #endif
 
 #ifndef PINDETECT_SAMPLE_PERIOD
-#define PINDETECT_SAMPLE_PERIOD 20000
+    #define PINDETECT_SAMPLE_PERIOD 20000
 #endif
 
-#ifndef PINDETECT_ASSERT_COUNT  
-#define PINDETECT_ASSERT_COUNT  1
+#ifndef PINDETECT_ASSERT_COUNT
+    #define PINDETECT_ASSERT_COUNT  1
 #endif
 
 #ifndef PINDETECT_HOLD_COUNT
-#define PINDETECT_HOLD_COUNT    50
+    #define PINDETECT_HOLD_COUNT    50
 #endif
 
 namespace AjK {
 
 class PinDetect {
-
-public:
+  public:
     friend class Ticker;
-    
-    
+
+
     /** PinDetect constructor
      *
      * By default the PinMode is set to PullDown.
      *
      * @see http://mbed.org/handbook/DigitalIn
      * @param p PinName is a valid pin that supports DigitalIn
-     */   
+     */
     PinDetect(PinName p);
-    
+
     /** PinDetect constructor
      *
      * @see http://mbed.org/handbook/DigitalIn
      * @param PinName p is a valid pin that supports DigitalIn
      * @param PinMode m The mode the DigitalIn should use.
-     */  
+     */
     PinDetect(PinName p, PinMode m);
-    
+
     /** PinDetect destructor
-     */ 
+     */
     virtual ~PinDetect(void);
-    
+
     /** Set the sampling time in microseconds.
      *
      * @param int The time between pin samples in microseconds.
      */
     void setSampleFrequency(int i = PINDETECT_SAMPLE_PERIOD);
-    
+
     /** Set the value used as assert.
      *
      * Defaults to 1 (ie if pin == 1 then pin asserted).
      *
      * @param int New assert value (1 or 0)
      */
-    void setAssertValue (int i = PINDETECT_PIN_ASSTERED);
-    
+    void setAssertValue(int i = PINDETECT_PIN_ASSTERED);
+
     /** Set the number of continuous samples until assert assumed.
      *
      * Defaults to 1 (1 * sample frequency).
      *
      * @param int The number of continuous samples until assert assumed.
-     */    
+     */
     void setSamplesTillAssert(int i);
-    
+
     /** Set the number of continuous samples until held assumed.
      *
      * Defaults to 50 * sample frequency.
      *
      * @param int The number of continuous samples until held assumed.
-     */    
+     */
     void setSamplesTillHeld(int i);
-    
+
     /** Set the pin mode.
      *
      * @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode
      * @param PinMode m The mode to pass on to the DigitalIn
      */
     void mode(PinMode m);
-    
+
     void attach_asserted(Callback<void()> function);
-    
-    template<typename T, typename M>
-    void attach_asserted(T *obj, M method){
-        core_util_critical_section_enter();
-        attach_asserted(callback(obj, method));
-        core_util_critical_section_exit();
-    }
-    
+
     void attach_deasserted(Callback<void()> function);
-    
-    template<typename T, typename M>
-    void attach_deasserted(T *obj, M method){
-        core_util_critical_section_enter();
-        attach_deasserted(callback(obj, method));
-        core_util_critical_section_exit();
-    }
-    
+
     void attach_asserted_held(Callback<void()> function);
-    
-    template<typename T, typename M>
-    void attach_asserted_held(T *obj, M method){
-        core_util_critical_section_enter();
-        attach_asserted_held(callback(obj, method));
-        core_util_critical_section_exit();
-    }
-    
+
     void attach_deasserted_held(Callback<void()> function);
-    
-    template<typename T, typename M>
-    void attach_deasserted_held(T *obj, M method){
-        core_util_critical_section_enter();
-        attach_deasserted_held(callback(obj, method));
-        core_util_critical_section_exit();
-    }
-    
+
     /** operator int()
      *
      * Read the value of the pin being sampled.
@@ -153,7 +124,7 @@
         return _in->read();
     }
 
-protected:    
+  protected:
     DigitalIn   *_in;
     Ticker      *_ticker;
     int         _prevState;
@@ -168,11 +139,11 @@
     Callback<void()> _callbackDeasserted;
     Callback<void()> _callbackAssertedHeld;
     Callback<void()> _callbackDeassertedHeld;
-    
-   /** The Ticker periodic callback function
-     */
+
+    /** The Ticker periodic callback function
+      */
     void isr(void);
-    
+
     /** initialise class
      *
      * @param PinName p is a valid pin that supports DigitalIn