button interrupt with LED's and slider/LCD display demo

Dependencies:   SLCD TSI mbed

Fork of blink_kl46z_button_nowait by Stanley Cohen

Revision:
6:a109f45fae66
Parent:
5:dc506f422393
diff -r dc506f422393 -r a109f45fae66 main.cpp
--- a/main.cpp	Thu Sep 24 15:08:25 2015 +0000
+++ b/main.cpp	Sun Sep 27 23:28:00 2015 +0000
@@ -1,24 +1,40 @@
 #include "mbed.h"
+#include "SLCD.h"
+#include "TSISensor.h"
+
 
 #define LEDON false
 #define LEDOFF true
 #define NUMBUTS 2
+#define RBUTINDEX 0
+#define LBUTINDEX 1
 #define LBUT PTC12  // port addresses for buttons
 #define RBUT PTC3
 #define BLINKTIME 0.3 // in seconds
-#define BUTTONTIME 0.2
-#define LCDCHARLEN 10
-#define NUMMESS 2
+#define DATATIME    50 //milli seccnds
+#define LCDLEN      10
+#define PROGNAME "blink_kl46z_buttton interrupt slider\r\n"
 
-#define PROGNAME "blink_kl46z_buttton no wait\r\n"
+// Cap slider interface
+SLCD slcd; //define LCD display
+
+TSISensor tsiScaling; // Capacitive sensor/slider
+Timer dataTimer;
 
 // Timer to elliminate wait() function
-Timer LEDTimer; // for blinking LEDs
-Timer ButtonTimer; // for reading button states
+// Timer LEDTimer; // for blinking LEDs
+Ticker Blinker;
 
-bool ledState = LEDON;
+//Timer ButtonTimer; // no longer needed
+// Button interrrupts
+InterruptIn rtButton(RBUT); // Big change.
+InterruptIn lfButton(LBUT); 
 
-DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
+char lcdData[LCDLEN];
+bool ledState = LEDON;
+int currentLED = 0;  // used in LEDBliker interrupt 
+
+// DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};  This array paradigm will not work right now.
 DigitalOut LEDs[NUMBUTS] = {LED_GREEN, LED_RED};
 Serial pc(USBTX, USBRX);// set up USB as communicationis to Host PC via USB connectons
 
@@ -29,40 +45,70 @@
     }
 }
  
+// Interrupt service routines
+
+void rtButtonPressed(){
+    allLEDsOff();  
+    currentLED = RBUTINDEX;
+}
+
+void lfButtonPressed(){
+    allLEDsOff();  
+    currentLED = LBUTINDEX;
+}
+
+void LEDBlinker(){           
+            ledState = !ledState; // Flip the general state
+            LEDs[currentLED] = ledState;
+        }
+        
+// End interrupt routines
+
+// Regular routines
+void LCDMessNoDwell(char *lMess){
+        slcd.Home();
+        slcd.clear();
+        slcd.printf(lMess);
+} 
+
 
 void initialize_global_vars(){
     pc.printf(PROGNAME);
     // set up DAQ timers
-    ButtonTimer.start();
-    ButtonTimer.reset();
-    LEDTimer.start();
-    LEDTimer.reset();  
     allLEDsOff();
 } 
 // --------------------------------
  int main() {
-    int i; 
-    int currentLED = 0;
+    float sliderValue=0.0;
+    // int currentLED = 0;  needs to be gobal for interrupt routine
     
     initialize_global_vars(); //keep things organized
     LEDs[currentLED] = LEDON;
+    dataTimer.start();
+    dataTimer.reset(); 
+    sprintf (lcdData,"%4.3f",0.0);
+    LCDMessNoDwell(lcdData);
+    
+// Interrupt routine setups
+    Blinker.attach(&LEDBlinker, BLINKTIME);
+    rtButton.fall(&rtButtonPressed);
+    lfButton.fall(&lfButtonPressed);
+    
 // End of setup
 
     while(true) {
-        if (ButtonTimer > BUTTONTIME){
-            for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 
-                if(!buttons[i]) { 
-                    allLEDsOff();  
-                    currentLED = i;
-                } // if ! buttons
-            }// for loop to look at buttons
-            ButtonTimer.reset();
-        }
-        if(LEDTimer.read() > BLINKTIME){
-            LEDTimer.reset();               
-            ledState = !ledState; // Flip the general state
-            LEDs[currentLED] = ledState;
-        }
+    // All in the interrupts
+        
     // Do other things here between times of reading and flashing
+    // OK
+    sliderValue = tsiScaling.readPercentage();
+        if (dataTimer.read_ms() > DATATIME){
+            if(sliderValue > 0.0) {
+                sprintf (lcdData,"%4.3f",sliderValue);  // Just to amke things user readable
+                LCDMessNoDwell(lcdData);
+            }
+            dataTimer.reset(); 
+        }   
+      
     }
 }
\ No newline at end of file