Nicholas Outram / Mbed OS Task614-mbedos54

Fork of Task614-mbedos54 by Stage-1 Students SoCEM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define RED_DONE 1
00004 #define YELLOW_DONE 2
00005 
00006 
00007 //Function declarations
00008 void countUP();
00009 void countDOWN();
00010 
00011 //Digital outputs
00012 DigitalOut onBoardLED(LED1);
00013 DigitalOut redLED(D7);
00014 DigitalOut yellowLED(D6);
00015 DigitalOut greenLED(D5);
00016 
00017 //Digital inputs
00018 DigitalIn  onBoardSwitch(USER_BUTTON);
00019 DigitalIn  SW1(D4);
00020 DigitalIn  SW2(D3);
00021 
00022 //Thread ID for the Main function (CMSIS API)
00023 osThreadId tidMain;
00024 
00025 //Stared mutable state
00026 volatile long long count = 0;
00027 
00028 //Threads
00029 void countUP()
00030 {
00031     redLED = 1;
00032     
00033     for (unsigned int n=0; n<10000; n++) {
00034         count++;
00035         count++;        
00036         count++;
00037         count++;    
00038         count++;
00039         count++; 
00040         count++;
00041         count++;
00042         count++;
00043         count++;   
00044     }
00045     
00046     redLED = 0;
00047     osSignalSet(tidMain, RED_DONE);  //Signal main thread we are done
00048 }
00049 
00050 void countDOWN()
00051 {
00052     yellowLED = 1;
00053     
00054     for (unsigned int n=0; n<10000; n++) {
00055         count--;
00056         count--;        
00057         count--;
00058         count--;    
00059         count--;
00060         count--; 
00061         count--;
00062         count--;
00063         count--;
00064         count--;               
00065     }   
00066     
00067     osSignalSet(tidMain, YELLOW_DONE); //Signal main thread we are done
00068     yellowLED = 0;
00069 }
00070 
00071 
00072 //Main thread
00073 int main() {
00074     Thread t1, t2;
00075     
00076     redLED    = 0;
00077     yellowLED = 0;
00078     greenLED  = 1;
00079     
00080     //Main thread ID
00081     tidMain = Thread::gettid();  
00082     
00083     //Press the switch to run concurrently
00084     if (onBoardSwitch == 1) {
00085         printf("Running sequntially\n");
00086         countUP();
00087         countDOWN();        
00088     } else {
00089         printf("Running concurrently\n");
00090         t1.start(countUP);           
00091         t2.start(countDOWN);    
00092   
00093         //Wait for the ALL_ON signal
00094         Thread::signal_wait(RED_DONE,osWaitForever);
00095         Thread::signal_wait(YELLOW_DONE,osWaitForever);        
00096     }
00097 
00098     printf("Final result = %lld\n", count);
00099     if (count == 0) {
00100         greenLED = 0;
00101     }
00102     while(true);
00103 }