Ondelay OneShot timer example

Dependencies:   mbed

Revision:
2:2e40a6f0c892
Parent:
0:d31d989a13fc
--- a/main.cpp	Wed Nov 21 05:48:43 2018 +0000
+++ b/main.cpp	Thu Nov 22 00:03:53 2018 +0000
@@ -2,7 +2,7 @@
 #include "mbed.h"
 
 // IO instance
-//#define F746ZG
+#define F746ZG
 #undef F746ZG
 #ifdef F746ZG    
 DigitalOut myled(PB_0);     // Green
@@ -22,49 +22,58 @@
 #define MYSWLOGIC 0
 #endif
 
-
-// Timer instance
-Timeout delay1;
-Timeout oneShot1;
+class OND{
+    public:
+        // Timer instance
+        static Timeout delay1;
+        static Timeout oneShot1;
+    
+        // Timer flag
+        static bool swOn;          // Switch status. Set true when press. Set false when Time up and release. 
+        static bool tmon;          // Timer status.  Set to the true when the OneShot timer starts. Set to the false when time up. 
 
-// Timer flag
-bool swon = false;          // Switch status. Set true when press. Set false when Time up and release. 
-bool tmon = false;          // Timer status.  Set to the true when the OneShot timer starts. Set to the false when time up. 
-
-// Function prototype
-void upDelay1();
-void upOneShot1();
+        // Function prototype
+        static void upDelay1(void);
+        static void upOneShot1(void);
+    
+        OND(void);
+};
 
 // Functions
+OND::OND(void){
+    swOn = false;
+    tmon = false;    
+}
 // Up time from delay timer. 
-void upDelay1(){
+void OND::upDelay1(void){
     myled2 = 1;                                 // LED on
     oneShot1.attach_us(&upOneShot1, 50000);   // Set oneShot
     tmon = true;                                // tmon flag set.
 }
 
 // Up time from oneShot timeer.
-void upOneShot1(){
+void OND::upOneShot1(void){
     myled2 = 0;                                 // LED off
     delay1.detach();                            // Timeout::detach
     oneShot1.detach();                          // Timeout::detach
     tmon = false;                               // tmon flag reset.
 }
 
-int main() {
+int main(void) {
+    OND ond;
     while(1) {
         myled3 = !myled3;
         if (mySw == MYSWLOGIC){                         // If sw on,
             myled = 1;                          // Monitor LED ON
-            if(myled2 == 0 && swon == false){   // If timer is not started,
-                swon = true;                    //  flag on
-                delay1.attach_us(&upDelay1, 50000);    // Start delay timer.
+            if(myled2 == 0 && ond.swOn == false){   // If timer is not started,
+                ond.swOn = true;                    //  flag on
+                ond.delay1.attach_us(&ond.upDelay1, 50000);    // Start delay timer.
             }            
         }else{                                  // If sw off,
             myled = 0; // LED is OFF            //  Monitor LED off
-            if(tmon == false){                  //  If timer is completed,
-                swon = false;                   //      frag reset.
-                delay1.detach();                //      Cansel the on delay timer.
+            if(ond.tmon == false){                  //  If timer is completed,
+                ond.swOn = false;                   //      frag reset.
+                ond.delay1.detach();                //      Cansel the on delay timer.
             }
          }
     }