Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 2:61fcde3ffe6e, committed 2014-05-31
- Comitter:
- jewirth
- Date:
- Sat May 31 12:01:24 2014 +0000
- Parent:
- 1:609d9df3ffdc
- Commit message:
- UART output moved to ticker function with fixed interval
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat May 31 10:31:44 2014 +0000
+++ b/main.cpp Sat May 31 12:01:24 2014 +0000
@@ -7,6 +7,23 @@
#define RGB_COLORS ((int) pow(2 *1.0, RGB_COMPONENTS *1.0))
#define BAUDRATE 115200
#define MAXDELAY 0.3
+#define UART_INTERVAL 0.2
+
+float sliderNewValue; // temp data of touch slider
+float mainLoopDuration; // main loop measurement
+float delay = MAXDELAY; // blink delay
+Serial pc(USBTX, USBRX); // UART
+
+void debugOutput()
+{
+ if (sliderNewValue > 0)
+ {
+ // UART output
+ pc.printf("slider input = %3.0f%% --> ", 100.0 * sliderNewValue);
+ pc.printf("blink delay = %3.0f ms", 1000 * delay);
+ pc.printf(" (last main loop took %4.0f ms)\n", mainLoopDuration);
+ }
+}
int main()
{
@@ -15,37 +32,41 @@
DigitalOut led_rgb_grn(LED2); // green
DigitalOut led_rgb_blu(LED3); // blue
DigitalOut led_rgb[RGB_COMPONENTS] = {led_rgb_red, led_rgb_grn, led_rgb_blu};
- float delay = MAXDELAY; // blink delay
-
+
// Touch slider
TSISensor tsi;
- float sliderNewValue;
-
+
// UART
- RawSerial pc(USBTX, USBRX);
- char tmpString[64]; // text buffer
pc.baud(BAUDRATE);
+
+ // ticker for debugOutput()
+ Ticker ticker1;
+ ticker1.attach(&debugOutput, UART_INTERVAL);
+ // timer for main loop measurement
+ Timer timer1;
+
+ // main loop
while (true)
{
+ timer1.start();
+
// show all colors
for (int i=1; i<RGB_COLORS; i++, wait(delay)) // for each possible color
{
for (int j=0; j<RGB_COMPONENTS; j++) // for each LED component
led_rgb[j] = ( i & 1<<j ) ? LED_ON : LED_OFF; // set LED component according to active color
-
- // check for slider input
+
+ // check for active slider input
if ( (sliderNewValue = tsi.readPercentage()) > 0)
{
// set delay to slider input
delay = MAXDELAY * sliderNewValue;
-
- // UART output (NOTE: practically, this will increase the blink delay)
- sprintf(tmpString, "slider input = %3.0f%% --> ", 100.0 * sliderNewValue);
- pc.puts(tmpString);
- sprintf(tmpString, "blink delay = %3.0f ms\n", 1000 * delay);
- pc.puts(tmpString);
}
}
+
+ timer1.stop();
+ mainLoopDuration = timer1.read_ms();
+ timer1.reset();
}
}