class of Buttons -isPressed -wasPressed -notPressed -LongPress (Timer)

Revision:
0:8625b4741933
Child:
1:e9d0d9b29acc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/button.cpp	Fri Nov 30 15:46:00 2018 +0000
@@ -0,0 +1,108 @@
+#include "button.h"
+#include "mbed.h"
+
+
+/******************************************************************************/
+// constructor
+/******************************************************************************/
+button::button(PinName pBtn, PinMode  pBtnMode)
+{
+    pin = new DigitalIn(pBtn, pBtnMode);
+    init();
+}
+
+
+/******************************************************************************/
+// initialisation
+/******************************************************************************/
+void button::init()
+{
+    bIsPressed   = false;
+    bWasPressed = false;
+    bNewState   = false;
+    bOldState   = false;
+
+    bBtnNewAction = false;
+
+    iNumCycle = 0;
+
+    return;
+}
+
+
+/******************************************************************************/
+// query the button with debouncing
+//      - bNewState:        current state of button
+//      - bOldState:        previous state of button
+//      - bBtnNewAction:    flag, if an action is occur
+//      - iNumCycle:        cycle counter of same state
+//      - NUNCYCLES:        max. value of cycle counter
+/******************************************************************************/
+void button::polling()
+{
+
+  
+    bOldState   = bNewState;
+    bNewState   = (bool)pin->read();
+    bWasPressed = false;     
+        
+    if( !bBtnNewAction and (bOldState != bNewState) ) {                         // if there is a action (changing signal) on the button (btn), then rise the flag bBtnNewAction
+        bBtnNewAction = true;       
+    }
+
+
+
+    if(bBtnNewAction) {                                                         // if flag ist true (new action on the button)
+
+        if(bOldState == bNewState) {                                            // if current state equals previous state
+            if(iNumCycle <= NUMCYCLE) {                                         // count the number of cycle unless the max. value is reached
+                iNumCycle++;
+
+            } else if(iNumCycle > NUMCYCLE) {                                   // else if the max. value is reached
+                iNumCycle = 0;                                                  // reset the counter, there is no action any more expected
+                bBtnNewAction = false;
+
+                if(bNewState == PRESSED) {
+                    bIsPressed = true;
+
+                } else if(bNewState == NOT_PRESSED) {
+                
+                    if(bIsPressed == true)
+                        bWasPressed = true;
+                    
+                    bIsPressed = false;
+                }
+
+            }
+        } else {
+            iNumCycle = 0;
+        }
+
+    }
+
+    return;
+}
+
+
+
+/******************************************************************************/
+//
+/******************************************************************************/
+bool button::isPressed()
+{
+    if(bIsPressed)  return true;
+    else            return false;
+}
+
+
+/******************************************************************************/
+//
+/******************************************************************************/
+bool button::wasPressed()
+{   
+    if(bWasPressed) return true;
+    else            return false;
+    
+}
+
+