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

Committer:
lonczy
Date:
Sat Nov 30 12:51:15 2013 +0000
Revision:
7:ec6f541a0636
Parent:
5:03a4211d593a
Child:
8:84597c4309a9
A Volume controller for touch slider, first try...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lonczy 7:ec6f541a0636 1 /**
lonczy 7:ec6f541a0636 2 * A simple app to make Volume Control with the KL25Z onboard Touch Slider
lonczy 7:ec6f541a0636 3 *
lonczy 7:ec6f541a0636 4 * Simply use the top and bottom part of the slider as buttons.
lonczy 7:ec6f541a0636 5 *
lonczy 7:ec6f541a0636 6 * Green led's intensity is controlled by the slider.
lonczy 7:ec6f541a0636 7 * 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.
lonczy 7:ec6f541a0636 8 */
lonczy 7:ec6f541a0636 9
samux 1:291a88a2c151 10 #include "mbed.h"
lonczy 7:ec6f541a0636 11 #include "MMA8451Q.h"
samux 1:291a88a2c151 12 #include "USBKeyboard.h"
lonczy 7:ec6f541a0636 13 #include "TSISensor.h"
lonczy 7:ec6f541a0636 14
lonczy 7:ec6f541a0636 15 #define MMA8451_I2C_ADDRESS (0x1d<<1)
lonczy 7:ec6f541a0636 16
lonczy 7:ec6f541a0636 17 //USB Keyboard interface:
samux 3:8b56768ceca2 18 USBKeyboard keyboard;
lonczy 7:ec6f541a0636 19 //Touch Slider:
lonczy 7:ec6f541a0636 20 TSISensor tsi;
lonczy 7:ec6f541a0636 21
samux 1:291a88a2c151 22 int main(void) {
lonczy 7:ec6f541a0636 23 ///initializing the accelerometer
lonczy 7:ec6f541a0636 24 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
lonczy 7:ec6f541a0636 25 ///get leds
lonczy 7:ec6f541a0636 26 PwmOut rled(LED_RED);
lonczy 7:ec6f541a0636 27 PwmOut gled(LED_GREEN);
lonczy 7:ec6f541a0636 28 PwmOut bled(LED_BLUE);
lonczy 7:ec6f541a0636 29
lonczy 7:ec6f541a0636 30 ///switch on blue led and switch off others
lonczy 7:ec6f541a0636 31 bled=0;
lonczy 7:ec6f541a0636 32 rled=1;
lonczy 7:ec6f541a0636 33 gled=1;
lonczy 7:ec6f541a0636 34 wait(2);
lonczy 7:ec6f541a0636 35 ///Wait until keyboard is configured
lonczy 7:ec6f541a0636 36 while (!keyboard.configured()) {
samux 1:291a88a2c151 37 }
lonczy 7:ec6f541a0636 38 bled=1;
lonczy 7:ec6f541a0636 39
lonczy 7:ec6f541a0636 40 float current=-1; //< Current state of Slider
lonczy 7:ec6f541a0636 41 float newRead; //< New readings of the slider
lonczy 7:ec6f541a0636 42
lonczy 7:ec6f541a0636 43
lonczy 7:ec6f541a0636 44 while (true) {
lonczy 7:ec6f541a0636 45 newRead=tsi.readPercentage();
lonczy 7:ec6f541a0636 46 gled=1.0 - newRead;
lonczy 7:ec6f541a0636 47 /// only if the slider is touched
lonczy 7:ec6f541a0636 48 if (newRead>0)
lonczy 7:ec6f541a0636 49 if (current!=newRead) {
lonczy 7:ec6f541a0636 50 ///and you have a different reading.
lonczy 7:ec6f541a0636 51 ///this is basically for not letting the volume be changed in every .1 seconds, and keeping the code responsive
lonczy 7:ec6f541a0636 52 ///you could have a counter on every 10th reading or so.
lonczy 7:ec6f541a0636 53 current=newRead;
lonczy 7:ec6f541a0636 54 if (current<0.5)
lonczy 7:ec6f541a0636 55 //if touched in the bottom half lower the volume
lonczy 7:ec6f541a0636 56 keyboard.mediaControl(KEY_VOLUME_DOWN);
lonczy 7:ec6f541a0636 57 else
lonczy 7:ec6f541a0636 58 //if touched in the top half rise the volume
lonczy 7:ec6f541a0636 59 keyboard.mediaControl(KEY_VOLUME_UP);
lonczy 7:ec6f541a0636 60 }
lonczy 7:ec6f541a0636 61 rled = 1.0 - (abs(acc.getAccX())/2);
lonczy 7:ec6f541a0636 62 wait(0.1);
lonczy 7:ec6f541a0636 63 }
lonczy 7:ec6f541a0636 64 }