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

Files at this revision

API Documentation at this revision

Comitter:
lonczy
Date:
Sat Nov 30 12:51:15 2013 +0000
Parent:
6:17871da2a7e4
Child:
8:84597c4309a9
Commit message:
A Volume controller for touch slider, first try...

Changed in this revision

MMA8451Q.lib Show annotated file Show diff for this revision Revisions of this file
TSI.lib Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8451Q.lib	Sat Nov 30 12:51:15 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/emilmont/code/MMA8451Q/#c4d879a39775
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TSI.lib	Sat Nov 30 12:51:15 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/TSI/#1a60ef257879
--- a/USBDevice.lib	Fri Mar 01 13:23:58 2013 +0000
+++ b/USBDevice.lib	Sat Nov 30 12:51:15 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/USBDevice/#335f2506f422
+http://mbed.org/users/mbed_official/code/USBDevice/#849c0c0f2769
--- 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);
+    }
+}