This FRDM-KL25Z demo will blink the RGB LED with all colors. The touch slider input controls the blinking frequency. The UART output shows the touch slider input.

Dependencies:   TSI mbed

Committer:
jewirth
Date:
Sat May 31 12:01:24 2014 +0000
Revision:
2:61fcde3ffe6e
Parent:
1:609d9df3ffdc
UART output moved to ticker function with fixed interval

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jewirth 0:6f33cc0cd2ac 1 #include "mbed.h"
jewirth 1:609d9df3ffdc 2 #include "TSISensor.h"
jewirth 0:6f33cc0cd2ac 3
jewirth 0:6f33cc0cd2ac 4 #define LED_ON 0
jewirth 0:6f33cc0cd2ac 5 #define LED_OFF 1
jewirth 0:6f33cc0cd2ac 6 #define RGB_COMPONENTS 3
jewirth 0:6f33cc0cd2ac 7 #define RGB_COLORS ((int) pow(2 *1.0, RGB_COMPONENTS *1.0))
jewirth 1:609d9df3ffdc 8 #define BAUDRATE 115200
jewirth 1:609d9df3ffdc 9 #define MAXDELAY 0.3
jewirth 2:61fcde3ffe6e 10 #define UART_INTERVAL 0.2
jewirth 2:61fcde3ffe6e 11
jewirth 2:61fcde3ffe6e 12 float sliderNewValue; // temp data of touch slider
jewirth 2:61fcde3ffe6e 13 float mainLoopDuration; // main loop measurement
jewirth 2:61fcde3ffe6e 14 float delay = MAXDELAY; // blink delay
jewirth 2:61fcde3ffe6e 15 Serial pc(USBTX, USBRX); // UART
jewirth 2:61fcde3ffe6e 16
jewirth 2:61fcde3ffe6e 17 void debugOutput()
jewirth 2:61fcde3ffe6e 18 {
jewirth 2:61fcde3ffe6e 19 if (sliderNewValue > 0)
jewirth 2:61fcde3ffe6e 20 {
jewirth 2:61fcde3ffe6e 21 // UART output
jewirth 2:61fcde3ffe6e 22 pc.printf("slider input = %3.0f%% --> ", 100.0 * sliderNewValue);
jewirth 2:61fcde3ffe6e 23 pc.printf("blink delay = %3.0f ms", 1000 * delay);
jewirth 2:61fcde3ffe6e 24 pc.printf(" (last main loop took %4.0f ms)\n", mainLoopDuration);
jewirth 2:61fcde3ffe6e 25 }
jewirth 2:61fcde3ffe6e 26 }
jewirth 0:6f33cc0cd2ac 27
jewirth 0:6f33cc0cd2ac 28 int main()
jewirth 0:6f33cc0cd2ac 29 {
jewirth 1:609d9df3ffdc 30 // RGB LED
jewirth 1:609d9df3ffdc 31 DigitalOut led_rgb_red(LED1); // red
jewirth 1:609d9df3ffdc 32 DigitalOut led_rgb_grn(LED2); // green
jewirth 1:609d9df3ffdc 33 DigitalOut led_rgb_blu(LED3); // blue
jewirth 1:609d9df3ffdc 34 DigitalOut led_rgb[RGB_COMPONENTS] = {led_rgb_red, led_rgb_grn, led_rgb_blu};
jewirth 2:61fcde3ffe6e 35
jewirth 1:609d9df3ffdc 36 // Touch slider
jewirth 1:609d9df3ffdc 37 TSISensor tsi;
jewirth 2:61fcde3ffe6e 38
jewirth 1:609d9df3ffdc 39 // UART
jewirth 1:609d9df3ffdc 40 pc.baud(BAUDRATE);
jewirth 2:61fcde3ffe6e 41
jewirth 2:61fcde3ffe6e 42 // ticker for debugOutput()
jewirth 2:61fcde3ffe6e 43 Ticker ticker1;
jewirth 2:61fcde3ffe6e 44 ticker1.attach(&debugOutput, UART_INTERVAL);
jewirth 1:609d9df3ffdc 45
jewirth 2:61fcde3ffe6e 46 // timer for main loop measurement
jewirth 2:61fcde3ffe6e 47 Timer timer1;
jewirth 2:61fcde3ffe6e 48
jewirth 2:61fcde3ffe6e 49 // main loop
jewirth 1:609d9df3ffdc 50 while (true)
jewirth 0:6f33cc0cd2ac 51 {
jewirth 2:61fcde3ffe6e 52 timer1.start();
jewirth 2:61fcde3ffe6e 53
jewirth 0:6f33cc0cd2ac 54 // show all colors
jewirth 1:609d9df3ffdc 55 for (int i=1; i<RGB_COLORS; i++, wait(delay)) // for each possible color
jewirth 1:609d9df3ffdc 56 {
jewirth 0:6f33cc0cd2ac 57 for (int j=0; j<RGB_COMPONENTS; j++) // for each LED component
jewirth 0:6f33cc0cd2ac 58 led_rgb[j] = ( i & 1<<j ) ? LED_ON : LED_OFF; // set LED component according to active color
jewirth 2:61fcde3ffe6e 59
jewirth 2:61fcde3ffe6e 60 // check for active slider input
jewirth 1:609d9df3ffdc 61 if ( (sliderNewValue = tsi.readPercentage()) > 0)
jewirth 1:609d9df3ffdc 62 {
jewirth 1:609d9df3ffdc 63 // set delay to slider input
jewirth 1:609d9df3ffdc 64 delay = MAXDELAY * sliderNewValue;
jewirth 1:609d9df3ffdc 65 }
jewirth 1:609d9df3ffdc 66 }
jewirth 2:61fcde3ffe6e 67
jewirth 2:61fcde3ffe6e 68 timer1.stop();
jewirth 2:61fcde3ffe6e 69 mainLoopDuration = timer1.read_ms();
jewirth 2:61fcde3ffe6e 70 timer1.reset();
jewirth 0:6f33cc0cd2ac 71 }
jewirth 0:6f33cc0cd2ac 72 }