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:
Tue May 17 13:47:45 2022 +0000
Revision:
10:2c506aad93de
Parent:
9:0b6c84d01c5d
lib versions updated

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
samux 1:291a88a2c151 17 int main(void) {
lonczy 8:84597c4309a9 18 ///get leds, and set def. values
lonczy 8:84597c4309a9 19 PwmOut bled(LED_BLUE);
lonczy 8:84597c4309a9 20 bled=0.8;
lonczy 8:84597c4309a9 21 PwmOut rled(LED_RED);
lonczy 8:84597c4309a9 22 rled=1;
lonczy 8:84597c4309a9 23 PwmOut gled(LED_GREEN);
lonczy 8:84597c4309a9 24 gled=1;
lonczy 7:ec6f541a0636 25 ///initializing the accelerometer
lonczy 7:ec6f541a0636 26 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
lonczy 8:84597c4309a9 27 bled=0.6;
lonczy 8:84597c4309a9 28 //USB Keyboard interface:
lonczy 8:84597c4309a9 29 USBKeyboard keyboard;
lonczy 8:84597c4309a9 30 bled=0.4;
lonczy 8:84597c4309a9 31 //Touch Slider:
lonczy 8:84597c4309a9 32 TSISensor tsi;
lonczy 8:84597c4309a9 33 bled=0.2;
lonczy 7:ec6f541a0636 34
lonczy 8:84597c4309a9 35 ///switch on blue led
lonczy 8:84597c4309a9 36 bled=0.9;
lonczy 7:ec6f541a0636 37 ///Wait until keyboard is configured
lonczy 7:ec6f541a0636 38 while (!keyboard.configured()) {
lonczy 8:84597c4309a9 39 bled=0.1;
lonczy 8:84597c4309a9 40 wait(1);
lonczy 8:84597c4309a9 41 bled=0.9;
lonczy 8:84597c4309a9 42 wait(1);
samux 1:291a88a2c151 43 }
lonczy 7:ec6f541a0636 44 bled=1;
lonczy 7:ec6f541a0636 45
lonczy 7:ec6f541a0636 46 float current=-1; //< Current state of Slider
lonczy 7:ec6f541a0636 47 float newRead; //< New readings of the slider
lonczy 7:ec6f541a0636 48
lonczy 7:ec6f541a0636 49 while (true) {
lonczy 7:ec6f541a0636 50 newRead=tsi.readPercentage();
lonczy 7:ec6f541a0636 51 gled=1.0 - newRead;
lonczy 7:ec6f541a0636 52 /// only if the slider is touched
lonczy 7:ec6f541a0636 53 if (newRead>0)
lonczy 7:ec6f541a0636 54 if (current!=newRead) {
lonczy 7:ec6f541a0636 55 ///and you have a different reading.
lonczy 7:ec6f541a0636 56 ///this is basically for not letting the volume be changed in every .1 seconds, and keeping the code responsive
lonczy 7:ec6f541a0636 57 ///you could have a counter on every 10th reading or so.
lonczy 7:ec6f541a0636 58 current=newRead;
lonczy 9:0b6c84d01c5d 59 if (current<0.4)
lonczy 7:ec6f541a0636 60 //if touched in the bottom half lower the volume
lonczy 7:ec6f541a0636 61 keyboard.mediaControl(KEY_VOLUME_DOWN);
lonczy 7:ec6f541a0636 62 else
lonczy 9:0b6c84d01c5d 63 if (current<0.6)
lonczy 9:0b6c84d01c5d 64 //if touched in the bottom half lower the volume
lonczy 9:0b6c84d01c5d 65 keyboard.mediaControl(KEY_PLAY_PAUSE);
lonczy 9:0b6c84d01c5d 66 else
lonczy 7:ec6f541a0636 67 //if touched in the top half rise the volume
lonczy 7:ec6f541a0636 68 keyboard.mediaControl(KEY_VOLUME_UP);
lonczy 7:ec6f541a0636 69 }
lonczy 7:ec6f541a0636 70 rled = 1.0 - (abs(acc.getAccX())/2);
lonczy 7:ec6f541a0636 71 wait(0.1);
lonczy 7:ec6f541a0636 72 }
lonczy 7:ec6f541a0636 73 }