Frederic DUFOURG
/
Ticker_and_PWM
A first try to test how interrupt, timeout and ticker interact.
main.cpp@2:87c848ac42d9, 2014-08-30 (annotated)
- Committer:
- fdufnews
- Date:
- Sat Aug 30 14:33:18 2014 +0000
- Revision:
- 2:87c848ac42d9
- Parent:
- 1:d07b9b4d4264
updated comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fdufnews | 0:3789d7ce5639 | 1 | #include "mbed.h" |
fdufnews | 0:3789d7ce5639 | 2 | |
fdufnews | 0:3789d7ce5639 | 3 | /** |
fdufnews | 0:3789d7ce5639 | 4 | * @file main.cpp |
fdufnews | 0:3789d7ce5639 | 5 | * @brief main file. |
fdufnews | 0:3789d7ce5639 | 6 | * |
fdufnews | 0:3789d7ce5639 | 7 | * @addtogroup test |
fdufnews | 0:3789d7ce5639 | 8 | * @author fdufnews |
fdufnews | 0:3789d7ce5639 | 9 | * @date 23.08.2014 |
fdufnews | 1:d07b9b4d4264 | 10 | * @brief Main module to test led, button and ticker interaction |
fdufnews | 0:3789d7ce5639 | 11 | * @note required LEDs on pin D9 and D11 in addition to that on the board |
fdufnews | 0:3789d7ce5639 | 12 | * @{ |
fdufnews | 0:3789d7ce5639 | 13 | */ |
fdufnews | 0:3789d7ce5639 | 14 | |
fdufnews | 0:3789d7ce5639 | 15 | #define F_DELAY1 1.5 |
fdufnews | 0:3789d7ce5639 | 16 | #define F_DELAY2 0.25 |
fdufnews | 0:3789d7ce5639 | 17 | |
fdufnews | 0:3789d7ce5639 | 18 | void offEnd(void); |
fdufnews | 0:3789d7ce5639 | 19 | |
fdufnews | 0:3789d7ce5639 | 20 | Timeout redOn, redOff; // 2 timeout that call each other to make some sort of programmable oscillator |
fdufnews | 0:3789d7ce5639 | 21 | Ticker flipperPWM; // Ticker that ramp up and down an LED |
fdufnews | 0:3789d7ce5639 | 22 | InterruptIn button1(USER_BUTTON); // button that light an LED and modify timeouts period |
fdufnews | 0:3789d7ce5639 | 23 | DigitalOut led1(LED1); // The green LED onboard |
fdufnews | 0:3789d7ce5639 | 24 | DigitalOut redLED(D9); // a red LED on a breadboard |
fdufnews | 0:3789d7ce5639 | 25 | PwmOut orangeLED(D11); // an orange one on the breadboard |
fdufnews | 0:3789d7ce5639 | 26 | |
fdufnews | 0:3789d7ce5639 | 27 | |
fdufnews | 0:3789d7ce5639 | 28 | /** Function to toggle led1 |
fdufnews | 0:3789d7ce5639 | 29 | evry time the button is pushed this function is called |
fdufnews | 0:3789d7ce5639 | 30 | and led1 state toggled*/ |
fdufnews | 0:3789d7ce5639 | 31 | void toggleLed1(void){ |
fdufnews | 0:3789d7ce5639 | 32 | led1 = !led1; |
fdufnews | 0:3789d7ce5639 | 33 | } |
fdufnews | 0:3789d7ce5639 | 34 | |
fdufnews | 0:3789d7ce5639 | 35 | /** Function called at end of onEnd timeout, sets new timeout defined by led1 state |
fdufnews | 2:87c848ac42d9 | 36 | onEnd calls offEnd which calls onEnd making an oscillator*/ |
fdufnews | 0:3789d7ce5639 | 37 | void onEnd(void){ |
fdufnews | 0:3789d7ce5639 | 38 | redLED=0; // led off |
fdufnews | 0:3789d7ce5639 | 39 | float delay = led1?F_DELAY1:F_DELAY2; // sets new delay conditionned by led1 state |
fdufnews | 0:3789d7ce5639 | 40 | redOff.attach(&offEnd,delay); // update delay |
fdufnews | 0:3789d7ce5639 | 41 | } |
fdufnews | 0:3789d7ce5639 | 42 | |
fdufnews | 2:87c848ac42d9 | 43 | /** Function called at end of offEnd timeout, sets new timeout defined by led1 state |
fdufnews | 2:87c848ac42d9 | 44 | offEnd calls onEnd which calls offEnd making an oscillator*/ |
fdufnews | 0:3789d7ce5639 | 45 | void offEnd(void){ |
fdufnews | 0:3789d7ce5639 | 46 | redLED=1; // led on |
fdufnews | 0:3789d7ce5639 | 47 | float delay = led1?F_DELAY1:F_DELAY2; // sets new delay conditionned by led1 state |
fdufnews | 0:3789d7ce5639 | 48 | redOn.attach(&onEnd,delay); // update delay |
fdufnews | 0:3789d7ce5639 | 49 | } |
fdufnews | 0:3789d7ce5639 | 50 | |
fdufnews | 0:3789d7ce5639 | 51 | /** Function to manage PWM for orange LED |
fdufnews | 2:87c848ac42d9 | 52 | at each new tick rampPWM is called changing PWM value ramping up and down the LED*/ |
fdufnews | 0:3789d7ce5639 | 53 | void rampPWM(void){ |
fdufnews | 0:3789d7ce5639 | 54 | static int rampDir=0; |
fdufnews | 0:3789d7ce5639 | 55 | |
fdufnews | 0:3789d7ce5639 | 56 | float curVal = orangeLED.read(); // read current programmed PWM value |
fdufnews | 0:3789d7ce5639 | 57 | if (rampDir==0){ // if ramping up |
fdufnews | 0:3789d7ce5639 | 58 | curVal+=0.05; // increment current PWM value |
fdufnews | 0:3789d7ce5639 | 59 | orangeLED=curVal; // sets PWM |
fdufnews | 0:3789d7ce5639 | 60 | if (curVal>=1.0){ // if limit reached |
fdufnews | 0:3789d7ce5639 | 61 | rampDir=1; // toggle direction |
fdufnews | 0:3789d7ce5639 | 62 | } |
fdufnews | 0:3789d7ce5639 | 63 | }else if (rampDir==1){ // if ramping down |
fdufnews | 0:3789d7ce5639 | 64 | curVal-=0.1; // decrement current PWM value |
fdufnews | 0:3789d7ce5639 | 65 | orangeLED=curVal; // sets PWM |
fdufnews | 0:3789d7ce5639 | 66 | if (curVal<=0.0){ // if limit reached |
fdufnews | 0:3789d7ce5639 | 67 | rampDir=0; // toggle direction |
fdufnews | 0:3789d7ce5639 | 68 | } |
fdufnews | 0:3789d7ce5639 | 69 | } |
fdufnews | 0:3789d7ce5639 | 70 | } |
fdufnews | 0:3789d7ce5639 | 71 | |
fdufnews | 0:3789d7ce5639 | 72 | /** main function |
fdufnews | 0:3789d7ce5639 | 73 | sets system up by |
fdufnews | 0:3789d7ce5639 | 74 | initializing LEDs state |
fdufnews | 0:3789d7ce5639 | 75 | attaching interrupt |
fdufnews | 0:3789d7ce5639 | 76 | launching ticker and timeout |
fdufnews | 0:3789d7ce5639 | 77 | then enters an infinite while(1) loop |
fdufnews | 0:3789d7ce5639 | 78 | */ |
fdufnews | 0:3789d7ce5639 | 79 | int main(void){ |
fdufnews | 0:3789d7ce5639 | 80 | // sets LED default state |
fdufnews | 0:3789d7ce5639 | 81 | led1=0; |
fdufnews | 0:3789d7ce5639 | 82 | redLED=1; |
fdufnews | 0:3789d7ce5639 | 83 | orangeLED.write(0.0); |
fdufnews | 0:3789d7ce5639 | 84 | |
fdufnews | 0:3789d7ce5639 | 85 | // attach functions called by ISR |
fdufnews | 0:3789d7ce5639 | 86 | redOn.attach(&onEnd,F_DELAY1); // toggle redLED state |
fdufnews | 0:3789d7ce5639 | 87 | flipperPWM.attach(&rampPWM,0.1); // change PWM value every 100ms |
fdufnews | 0:3789d7ce5639 | 88 | button1.fall(&toggleLed1); |
fdufnews | 0:3789d7ce5639 | 89 | while(1){}; // do nothing |
fdufnews | 0:3789d7ce5639 | 90 | } |
fdufnews | 0:3789d7ce5639 | 91 | |
fdufnews | 0:3789d7ce5639 | 92 | /** @} */ |