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

Revision:
1:e9d0d9b29acc
Parent:
0:8625b4741933
Child:
2:cf58f3863c07
diff -r 8625b4741933 -r e9d0d9b29acc button.cpp
--- a/button.cpp	Fri Nov 30 15:46:00 2018 +0000
+++ b/button.cpp	Tue Dec 04 10:40:33 2018 +0000
@@ -5,10 +5,16 @@
 /******************************************************************************/
 // constructor
 /******************************************************************************/
-button::button(PinName pBtn, PinMode  pBtnMode)
+button::button(PinName pBtn, PinMode  pBtnMode, float time)
 {
+    fTime = time;
     pin = new DigitalIn(pBtn, pBtnMode);
+
+    if(fTime > 0)
+        timer = new Timeout;
+
     init();
+
 }
 
 
@@ -17,7 +23,8 @@
 /******************************************************************************/
 void button::init()
 {
-    bIsPressed   = false;
+    bIsPressed  = false;
+    bLongPress  = false;
     bWasPressed = false;
     bNewState   = false;
     bOldState   = false;
@@ -25,6 +32,9 @@
     bBtnNewAction = false;
 
     iNumCycle = 0;
+    if(fTime > 0)
+        timer->detach();
+
 
     return;
 }
@@ -40,16 +50,14 @@
 /******************************************************************************/
 void button::polling()
 {
+    bOldState   = bNewState;                                                    // bNewState gets the becomes the bOldstate
+    bNewState   = (bool)pin->read();                                            // update bNewState
+    bWasPressed = false;                                                        // reset the flag was pressed
 
-  
-    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 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)
@@ -60,24 +68,30 @@
 
             } 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;
+                bBtnNewAction = false;                                          // there is nomore new action.
 
-                if(bNewState == PRESSED) {
-                    bIsPressed = true;
+                if(bNewState == PRESSED) {                                      // btn is pressed, then
+                    bIsPressed = true;                                          // set this flag true
+                    
+                    if(fTime > 0)                                                // if btn has a timer, 
+                        timer->attach(this, &button::setLongPressFlag, fTime);
+                        
 
-                } else if(bNewState == NOT_PRESSED) {
-                
-                    if(bIsPressed == true)
-                        bWasPressed = true;
+                } else if(bNewState == NOT_PRESSED) {                           // if btn is not pressed, then
+                    if(bIsPressed == true)                                      // if btn was pressed, then
+                        bWasPressed = true;                                     // set this flag true
                     
-                    bIsPressed = false;
+                    bIsPressed = false;                                         // set this flag false
+                    
+                    if(fTime > 0)                                               // if btn has a timer, 
+                        timer->detach();
+                        bLongPress = false;
                 }
 
             }
         } else {
             iNumCycle = 0;
         }
-
     }
 
     return;
@@ -98,11 +112,31 @@
 /******************************************************************************/
 //
 /******************************************************************************/
-bool button::wasPressed()
-{   
-    if(bWasPressed) return true;
+bool button::isLongPress()
+{
+    if(bLongPress)  return true;
     else            return false;
-    
+
 }
 
 
+/******************************************************************************/
+//
+/******************************************************************************/
+bool button::wasPressed()
+{
+    if(bWasPressed) return true;
+    else            return false;
+
+}
+
+
+/******************************************************************************/
+//
+/******************************************************************************/
+void button::setLongPressFlag(){
+    bLongPress = true;
+    timer->detach();
+}
+
+