Stop watch to demo mod buttons and timer class.

Dependencies:   SLCD mbed

Fork of kl46z_slider_v1 by Stanley Cohen

Files at this revision

API Documentation at this revision

Comitter:
scohennm
Date:
Wed Oct 12 15:55:44 2016 +0000
Parent:
0:04499bc54bee
Commit message:
Stop watch to demo mod buttons and timer class.

Changed in this revision

TSI.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 04499bc54bee -r 0d13b6d7907f TSI.lib
--- a/TSI.lib	Fri Sep 09 17:51:13 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/TSI/#1a60ef257879
diff -r 04499bc54bee -r 0d13b6d7907f main.cpp
--- a/main.cpp	Fri Sep 09 17:51:13 2016 +0000
+++ b/main.cpp	Wed Oct 12 15:55:44 2016 +0000
@@ -1,41 +1,119 @@
 #include "mbed.h"
-#include "TSISensor.h"
+#include <math.h>  
 #include "SLCD.h"
-#define TSILIMIT 0.99
+
+#define LEDON false
+#define LEDOFF true
+#define NUMBUTS 2
+#define LBUT PTC12  // port addresses for buttons
+#define RBUT PTC3
+#define STOPPEDSTATE 0
+#define TIMINGSTATE  1
+#define RESETTINGSTATE 0
+#define PRINTDELTA 0.01
 #define LCDCHARLEN 10
-#define DATAINTERVAL 0.1
-#define PROGNAME "kl46z_slider_test_v1\n\r"
+#define BUTTONTIME 0.2
+#define FULLMINUTE 60 //seconds
+#define PROGNAME "kl46z_stop_watch_v1\n\r"
+#define LCDTITLE "STPW"
+#define TITLEWAIT 2.0
+
+//#define PRINTDBUG // uncomment if printing to serial port desired
 
 SLCD slcd; //define LCD display
 Serial pc(USBTX, USBRX);
 
-float tsidata;
+Timer ButtonTimer; // for reading button states
+DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
+int displayState = STOPPEDSTATE;
+
+
+
+void initialize_global_vars(){
+    pc.printf(PROGNAME);
+    // set up DAQ timer
+    // set up DAQ timers
+    ButtonTimer.start();
+    ButtonTimer.reset(); 
+} 
 
 void LCDMess(char *lMess){
         slcd.Home();
         slcd.clear();
         slcd.printf(lMess);
 }
-
+void showTitle(){
+    LCDMess(LCDTITLE);
+    wait(TITLEWAIT);
+    return;
+}
 int main(void) {
+    int i;
     char lcdData[LCDCHARLEN];
     PwmOut gled(LED_GREEN);
     PwmOut rled(LED_RED);
     pc.printf(PROGNAME);
-    TSISensor tsi;
+    float secondsCount = 0.0;
+    int minutesCount; // for displaying mininuts
+    int seconds; //
+    int fifthSeconds;
+    bool statetoggle = false; //was in stopped state.
+    
+    initialize_global_vars();
+    showTitle();
 
      while (true) {
-        tsidata = tsi.readPercentage();
-        if (tsidata > TSILIMIT){
-            gled = 0.0;
-            rled = 0.0;
-        }else {
-            pc.printf("\n Position %f\n\r", tsidata);
-            sprintf (lcdData,"%0.4f",tsidata);  
-            LCDMess(lcdData);  
-            gled = tsidata;
-            rled = 1.0 - tsidata;
-        }
-            wait(DATAINTERVAL);
+        if (ButtonTimer > BUTTONTIME){
+            for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 
+                if(!buttons[i]) { // a buttton is pressed
+                    displayState = i; 
+                    switch (displayState){  // this keeps things generic
+                        case TIMINGSTATE: {              
+                         statetoggle = !statetoggle;
+                         break;
+                        }
+                        case RESETTINGSTATE :{
+                        break;
+                        }
+                    }   
+                } // if ! buttons
+            }// for loop to look at buttons
+            ButtonTimer.reset();
+            switch (displayState){
+                    /* this goes away
+                    case STOPPEDSTATE : {                     
+                        rled = 0.0;
+                        gled = 1.0;
+                        break;
+                    }
+                    */
+                    case RESETTINGSTATE:
+                        if (!statetoggle){
+                            secondsCount = 0;
+                            displayState = TIMINGSTATE;
+                        }
+                        rled = 0.0;
+                        gled = 1.0;
+                        break;
+                        
+                        
+                    case TIMINGSTATE : {
+                        if(statetoggle){
+                            secondsCount = secondsCount + BUTTONTIME; 
+                        }                   
+                        rled = 1.0;
+                        gled = 0.0;
+                        break;
+                    }
+            }
+            
+            // Parse the seconds
+            seconds = (int)secondsCount; // make seconds "mask"
+            fifthSeconds = (int)((secondsCount - (float)seconds) * 10); // the 0.2 seconds
+            minutesCount = seconds / FULLMINUTE;
+            seconds = seconds % FULLMINUTE;          
+            sprintf (lcdData,"%1d.%02d.%1d",minutesCount,seconds,fifthSeconds);  
+            LCDMess(lcdData);            
+        }// end Button timer
     }
 }
\ No newline at end of file