Nicholas Outram / Mbed OS Task522Solution
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //Global objects
00004 DigitalOut onboardLed(LED1);
00005 DigitalOut redLED(D7);
00006 DigitalOut yellowLED(D6);
00007 DigitalOut greenLED(D5);
00008 
00009 //Function prototypes (ISR)
00010 void doRedPWM();
00011 void doYellowPWM();
00012 void doGreenPWM();
00013 
00014 //One-shot timers
00015 Timeout tRed;
00016 Timeout tYellow;
00017 Timeout tGreen;
00018 
00019 //Interrupt Service Routine Flags
00020 volatile int redISRFlag = 0;
00021 volatile int yellowISRFlag = 0;
00022 volatile int greenISRFlag = 0;
00023 
00024 //ON and OFF times
00025 // RED 9:1
00026 float TRedON =  0.009;
00027 float TRedOFF = 0.001;
00028 // YELLOW 1:1
00029 float TYellowON =  0.001; 
00030 float TYellowOFF = 0.001;
00031 // GREEN 1:9 - nearly off
00032 float TGreenON =  0.001;
00033 float TGreenOFF = 0.009;
00034 
00035 int main() {
00036     //Initialise the LEDs
00037     redLED = 0;
00038     yellowLED = 0;
00039     greenLED = 0;
00040 
00041     
00042     //Initialise timers (oneshot)
00043     tRed.attach(doRedPWM, TRedOFF);
00044     tYellow.attach(doYellowPWM, TYellowOFF);
00045     tGreen.attach(doGreenPWM, TGreenOFF);
00046     
00047     while (1) {
00048         //Sleep and wait for an interrupt
00049         sleep();
00050         
00051         //Chech which timer(s) went off
00052         if (redISRFlag == 1) {
00053             redISRFlag = 0; //Reset ISR flag
00054             float t = (redLED==0) ? TRedOFF : TRedON;
00055             tRed.attach(doRedPWM, t);  //Reset timer
00056         }
00057         if (yellowISRFlag == 1) {
00058             yellowISRFlag = 0;
00059             float t = (yellowLED==0) ? TYellowOFF : TYellowON;
00060             tYellow.attach(doYellowPWM, t);  
00061         }
00062         if (greenISRFlag == 1) {
00063             greenISRFlag = 0;
00064             float t = (greenLED==0) ? TGreenOFF : TGreenON;
00065             tGreen.attach(doGreenPWM, t);  
00066         }                
00067 
00068     }
00069 }
00070 
00071 void doRedPWM()
00072 {
00073     //Toggle LED
00074     redLED = !redLED;
00075     //Flag that interrupt has fired
00076     redISRFlag = 1;
00077     
00078 }
00079 void doYellowPWM()
00080 {
00081     yellowLED = !yellowLED;
00082     yellowISRFlag = 1;
00083 }
00084 void doGreenPWM()
00085 {
00086     greenLED = !greenLED;
00087     greenISRFlag = 1; 
00088 }