Chris Talbot / Mbed 2 deprecated SimpleRTOSExample

Dependencies:   mbed-rtos mbed

Fork of app-board-RTOS-Threads by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Example using mbedRTOS on the LPC1768
00002 // Introduction to RTOS
00003 // Pot1 ADC limit for LED1
00004 // Pot2 ADC limit for LED2
00005 
00006 #include "mbed.h"
00007 #include "rtos.h"
00008 #include "stdio.h"
00009 
00010 #define LEDON   1
00011 #define LEDOFF  0
00012 
00013 
00014 Serial pc(USBTX, USBRX);
00015 AnalogIn Pot1(p19);
00016 AnalogIn Pot2(p20);
00017 DigitalOut led1(LED1);
00018 DigitalOut led2(LED2);
00019 
00020 
00021 // mutex to make the serial thread safe
00022 Mutex pc_mutex;
00023 
00024 // Thread 1
00025 // print counter into first line and wait for 1 s
00026 void thread1(void const *args)
00027 {
00028     int i;
00029     while(true) 
00030     {       // thread loop
00031         pc_mutex.lock();
00032         pc.printf("Thread1 count: %d\n\r",i);
00033         pc_mutex.unlock();
00034         i++;
00035         Thread::wait(1000);
00036     }
00037 }
00038 
00039 // Thread 2
00040 // print counter into third line and wait for 0,5s
00041 void thread2(void const *args)
00042 {
00043     int k;
00044     while(true) 
00045     {       // thread loop
00046         pc_mutex.lock();
00047         pc.printf("Thread 2 count : %d\n\r",k);
00048         pc_mutex.unlock();
00049         k++;
00050         Thread::wait(500); // wait 0.5s
00051     }
00052 }
00053 
00054 // Thread 4
00055 // Pot1's value is compared to a limit and LED1 is set or not
00056 void thread4(void const *args)
00057 {
00058     while(true) 
00059     {       // thread loop
00060         pc_mutex.lock();
00061         if( Pot1.read_u16() > 0xEFFF)
00062         {
00063             led1 = LEDON;
00064         }
00065         else
00066         {
00067             led1 = LEDOFF;
00068         }
00069             
00070         pc_mutex.unlock();
00071     }
00072     Thread::wait(500);   // value of pot1 / 100
00073 }
00074 
00075 // Thread 5
00076 // Pot2's value is compared to a limit and LED2 is set or not
00077 void thread5(void const *args)
00078 {
00079     while(true) 
00080     {       // thread loop
00081         pc_mutex.lock();
00082         if( Pot2.read_u16() > 0xEFFF)
00083         {
00084             led2 = LEDON;
00085         }
00086         else
00087         {
00088             led2 = LEDOFF;
00089         }
00090          
00091         pc_mutex.unlock();
00092     }
00093     Thread::wait(500);   // value of pot1 / 100
00094 }
00095 
00096 int main()
00097 {
00098     Thread t1(thread1); //start thread1
00099     Thread t2(thread2); //start thread2
00100     Thread t4(thread4); //start thread4
00101     Thread t5(thread5); //start thread5
00102     
00103     while(true) 
00104     {       // main is the next thread
00105         pc_mutex.lock();
00106         pc.printf("main\n\r");
00107         pc_mutex.unlock();
00108         Thread::wait(500);   // wait 0.5s
00109     }
00110 }