KL46Z, slider to serial demo

Dependencies:   SLCD TSI mbed

Fork of kl46z_btn_slider_toSerial by Simone Seagle

Files at this revision

API Documentation at this revision

Comitter:
sim1sgl
Date:
Mon Oct 12 03:19:22 2015 +0000
Parent:
6:a109f45fae66
Commit message:
Simple button and slider command to PC program, adapted from the KL46Z button with interrupt made by Stan

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Sep 27 23:28:00 2015 +0000
+++ b/main.cpp	Mon Oct 12 03:19:22 2015 +0000
@@ -10,10 +10,9 @@
 #define LBUTINDEX 1
 #define LBUT PTC12  // port addresses for buttons
 #define RBUT PTC3
-#define BLINKTIME 0.3 // in seconds
-#define DATATIME    50 //milli seccnds
+#define DATATIME    200 //milliseconds
 #define LCDLEN      10
-#define PROGNAME "blink_kl46z_buttton interrupt slider\r\n"
+#define PROGNAME "kl46z_btn_slider_toSerial\r\n"
 
 // Cap slider interface
 SLCD slcd; //define LCD display
@@ -21,46 +20,22 @@
 TSISensor tsiScaling; // Capacitive sensor/slider
 Timer dataTimer;
 
-// Timer to elliminate wait() function
-// Timer LEDTimer; // for blinking LEDs
-Ticker Blinker;
-
-//Timer ButtonTimer; // no longer needed
 // Button interrrupts
-InterruptIn rtButton(RBUT); // Big change.
+InterruptIn rtButton(RBUT);
 InterruptIn lfButton(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
-
-void allLEDsOff(){
-    int i;
-    for (i=0; i<NUMBUTS; i++){ 
-        LEDs[i] = LEDOFF;    
-    }
-}
+Serial pc(USBTX, USBRX);// set up USB as communications to Host PC via USB connectons
  
 // Interrupt service routines
-
 void rtButtonPressed(){
-    allLEDsOff();  
-    currentLED = RBUTINDEX;
+    pc.printf("button: right\r\n");
 }
 
 void lfButtonPressed(){
-    allLEDsOff();  
-    currentLED = LBUTINDEX;
+    pc.printf("button: left \r\n");
 }
-
-void LEDBlinker(){           
-            ledState = !ledState; // Flip the general state
-            LEDs[currentLED] = ledState;
-        }
         
 // End interrupt routines
 
@@ -71,41 +46,35 @@
         slcd.printf(lMess);
 } 
 
-
-void initialize_global_vars(){
-    pc.printf(PROGNAME);
-    // set up DAQ timers
-    allLEDsOff();
-} 
 // --------------------------------
  int main() {
-    float sliderValue=0.0;
-    // int currentLED = 0;  needs to be gobal for interrupt routine
+    float sliderValue = 0.0;
     
-    initialize_global_vars(); //keep things organized
-    LEDs[currentLED] = LEDON;
+    pc.printf(PROGNAME);
     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) {
-    // All in the interrupts
+    // All 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
+            // Read the slider value
+            float newSliderValue = tsiScaling.readPercentage();
+            // Only do stuff with it if it's a new value or if it's not zero
+            if(newSliderValue > 0.0 && newSliderValue != sliderValue) {
+                sliderValue = newSliderValue;
+                sprintf (lcdData,"%4.3f", sliderValue);  // Just to make things user readable
+                
                 LCDMessNoDwell(lcdData);
+                pc.printf("slider: %4.3f\r\n", sliderValue);
             }
             dataTimer.reset(); 
         }