This is my first program on mbed.org. A Small program for KL25Z that'll act as a Volume Control, thanks to samux for his code with USB keyboard support (https://mbed.org/users/samux/code/USBKeyboard_HelloWorld/) Just added functionality to let the user use the Touch Slider on the KL25Z board to control system volume.

Dependencies:   mbed TSI MMA8451Q USBDevice

Fork of USBKeyboard_HelloWorld by Samuel Mokrani

Revision:
7:ec6f541a0636
Parent:
5:03a4211d593a
Child:
8:84597c4309a9
--- a/main.cpp	Fri Mar 01 13:23:58 2013 +0000
+++ b/main.cpp	Sat Nov 30 12:51:15 2013 +0000
@@ -1,21 +1,64 @@
+/**
+* A simple app to make Volume Control with the KL25Z onboard Touch Slider
+* 
+* Simply use the top and bottom part of the slider as buttons.
+*
+* Green led's intensity is controlled by the slider.
+* The Red component of the RGB led is changing intensity as you tilt the board on the X coordinate to have multiple actions at the same time.
+*/
+
 #include "mbed.h"
+#include "MMA8451Q.h"
 #include "USBKeyboard.h"
- 
-//LED1: NUM_LOCK
-//LED2: CAPS_LOCK
-//LED3: SCROLL_LOCK
-BusOut leds(LED1, LED2, LED3);
- 
-//USBKeyboard
+#include "TSISensor.h"
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+
+//USB Keyboard interface:
 USBKeyboard keyboard;
- 
+//Touch Slider:
+TSISensor tsi;
+
 int main(void) {
-    while (1) {
-        keyboard.mediaControl(KEY_VOLUME_DOWN);
-        keyboard.printf("Hello World from Mbed\r\n");
-        keyboard.keyCode('s', KEY_CTRL);
-        keyboard.keyCode(KEY_CAPS_LOCK);
-        wait(1);
-        leds = keyboard.lockStatus();
+    ///initializing the accelerometer
+    MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
+    ///get leds
+    PwmOut rled(LED_RED);
+    PwmOut gled(LED_GREEN);
+    PwmOut bled(LED_BLUE);
+
+    ///switch on blue led and switch off others
+    bled=0;
+    rled=1;
+    gled=1;
+    wait(2);
+    ///Wait until keyboard is configured
+    while (!keyboard.configured()) {
     }
-}
\ No newline at end of file
+    bled=1;
+
+    float current=-1; //< Current state of Slider
+    float newRead; //< New readings of the slider
+
+
+    while (true) {
+        newRead=tsi.readPercentage();
+        gled=1.0 - newRead;
+        /// only if the slider is touched
+        if (newRead>0)
+            if (current!=newRead) {
+                ///and you have a different reading.
+                ///this is basically for not letting the volume be changed in every .1 seconds, and keeping the code responsive
+                ///you could have a counter on every 10th reading or so.
+                current=newRead;
+                if (current<0.5)
+                    //if touched in the bottom half lower the volume
+                    keyboard.mediaControl(KEY_VOLUME_DOWN);
+                else
+                    //if touched in the top half rise the volume
+                    keyboard.mediaControl(KEY_VOLUME_UP);
+            }
+        rled = 1.0 - (abs(acc.getAccX())/2);
+        wait(0.1);
+    }
+}