Frederic DUFOURG / Mbed 2 deprecated Ticker_and_PWM

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 #include "mbed.h"
00002 
00003 /**
00004  * @file    main.cpp
00005  * @brief   main file.
00006  *
00007  * @addtogroup test
00008  * @author  fdufnews
00009  * @date    23.08.2014
00010  * @brief   Main module to test led, button and ticker interaction
00011  * @note    required LEDs on pin D9 and D11 in addition to that on the board
00012  * @{
00013  */
00014 
00015 #define F_DELAY1 1.5
00016 #define F_DELAY2 0.25
00017 
00018 void offEnd(void);
00019 
00020 Timeout redOn, redOff; // 2 timeout that call each other to make some sort of programmable oscillator
00021 Ticker flipperPWM; // Ticker that ramp up and down an LED
00022 InterruptIn button1(USER_BUTTON); // button that light an LED and modify timeouts period
00023 DigitalOut led1(LED1); // The green LED onboard
00024 DigitalOut redLED(D9); // a red LED on a breadboard
00025 PwmOut orangeLED(D11); // an orange one on the breadboard
00026 
00027 
00028 /** Function to toggle led1
00029  evry time the button is pushed this function is called
00030  and led1 state toggled*/
00031 void toggleLed1(void){
00032     led1 = !led1;
00033 }
00034 
00035 /** Function called at end of onEnd timeout, sets new timeout defined by led1 state
00036  onEnd calls offEnd which calls onEnd making an oscillator*/
00037 void onEnd(void){
00038     redLED=0; // led off
00039     float delay = led1?F_DELAY1:F_DELAY2; // sets new delay conditionned by led1 state
00040     redOff.attach(&offEnd,delay); // update delay
00041 }
00042 
00043 /** Function called at end of offEnd timeout, sets new timeout defined by led1 state
00044  offEnd calls onEnd which calls offEnd making an oscillator*/
00045 void offEnd(void){
00046     redLED=1; // led on
00047     float delay = led1?F_DELAY1:F_DELAY2; // sets new delay conditionned by led1 state
00048     redOn.attach(&onEnd,delay); // update delay
00049 }
00050 
00051 /** Function to manage PWM for orange LED
00052  at each new tick rampPWM is called changing PWM value ramping up and down the LED*/
00053 void rampPWM(void){
00054     static int rampDir=0;
00055     
00056     float curVal = orangeLED.read(); // read current programmed PWM value
00057     if (rampDir==0){ // if ramping up
00058         curVal+=0.05; // increment current PWM value
00059         orangeLED=curVal; // sets PWM
00060         if (curVal>=1.0){ // if limit reached
00061             rampDir=1; // toggle direction
00062         }
00063     }else if (rampDir==1){ // if ramping down
00064         curVal-=0.1; // decrement current PWM value
00065         orangeLED=curVal; // sets PWM
00066         if (curVal<=0.0){ // if limit reached
00067             rampDir=0; // toggle direction
00068         }
00069     }
00070 }
00071 
00072 /** main function
00073  sets system up by
00074    initializing LEDs state
00075    attaching interrupt
00076    launching ticker and timeout
00077    then enters an infinite while(1) loop
00078 */
00079 int main(void){
00080     // sets LED default state
00081     led1=0;
00082     redLED=1;
00083     orangeLED.write(0.0);
00084     
00085     // attach functions called by ISR
00086     redOn.attach(&onEnd,F_DELAY1); // toggle redLED state
00087     flipperPWM.attach(&rampPWM,0.1); // change PWM value every 100ms
00088     button1.fall(&toggleLed1);
00089    while(1){}; // do nothing
00090 }
00091 
00092 /** @} */