This is a test program for http://mbed.org/forum/bugs-suggestions/topic/990/

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Test program for a bug. (http://mbed.org/forum/bugs-suggestions/topic/990/)
00003  *
00004  * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
00005  * http://shinta.main.jp/
00006  */
00007 
00008 #include "mbed.h"
00009 
00010 /**
00011  * TEST_MODE 0: Test a Timeout.
00012  * TEST_MODE 1: Test a Ticker.
00013  */
00014 #define TEST_MODE 0
00015 
00016 #if TEST_MODE
00017 
00018 /*
00019  * A test for Ticker.
00020  */
00021 
00022 BusOut myled(LED4, LED3, LED2, LED1);
00023 Ticker tickerForLED;
00024 Ticker tickerForAnother;
00025 
00026 /**
00027  * Tick function for toggle 4 LEDs.
00028  */
00029 void tickfunc_led(void) {
00030     myled = myled + 1;
00031 }
00032 
00033 /**
00034  * Tick function for another ticker.
00035  */
00036 void tickfunc_another(void) {
00037     // Do nothing.
00038 }
00039 
00040 /**
00041  * Entry point for Ticker.
00042  */
00043 int main(void) {
00044     int n = 0;
00045 
00046     /*
00047      * Start my LED ticker.
00048      */
00049     tickerForLED.attach(&tickfunc_led, 0.2);
00050 
00051     /*
00052      * Wait 5 seconds.
00053      */
00054     wait(5);
00055 
00056     /*
00057      * Attach another ticker.
00058      * Then the LED ticker will be stop.
00059      */
00060     while (1) {
00061         /*
00062          * Check for this loop.
00063          */
00064         printf("n=%d\n", n++);
00065 
00066         /*
00067          * Change my another ticker.
00068          */
00069         tickerForAnother.attach_us(&tickfunc_another, 2 * 1000);
00070         wait_ms(100);
00071     }
00072 }
00073 
00074 #else
00075 
00076 /*
00077  * A test for Timeout.
00078  */
00079 
00080 BusOut myled(LED4, LED3, LED2, LED1);
00081 Ticker tickerForLED;
00082 Timeout timeout;
00083 
00084 /**
00085  * Tick function for toggle 4 LEDs.
00086  */
00087 void tickfunc_led(void) {
00088     myled = myled + 1;
00089 }
00090 
00091 /**
00092  * Timeout function.
00093  */
00094 void timeoutfunc(void) {
00095     printf("timeoutfunc.\n");
00096 }
00097 
00098 /**
00099  * Entry point for Timeout.
00100  */
00101 int main(void) {
00102     int n = 0;
00103 
00104     /*
00105      * Start my LED ticker.
00106      */
00107     tickerForLED.attach(&tickfunc_led, 0.2);
00108 
00109     /*
00110      * Wait 5 seconds.
00111      */
00112     wait(5);
00113 
00114     /*
00115      * Attach timeout.
00116      * Then the LED ticker will be stop.
00117      */
00118     while (1) {
00119         /*
00120          * Check for this loop.
00121          */
00122         printf("n=%d\n", n++);
00123 
00124         /*
00125          * Change my timeout.
00126          */
00127         timeout.detach();
00128         timeout.attach_us(&timeoutfunc, 2 * 1000);
00129         wait_ms(100);
00130 
00131         timeout.detach();
00132         timeout.attach_us(&timeoutfunc, 200 * 1000);
00133         wait_ms(100);
00134         
00135         timeout.attach_us(&timeoutfunc, 20 * 1000);
00136         wait_ms(100);
00137 
00138         timeout.attach_us(&timeoutfunc, 200 * 1000);
00139         wait_ms(100);
00140     }
00141 }
00142 
00143 #endif