A very simple example of an RTOS executing on an LPC1768 Platform
Fork of app-board-RTOS-Threads by
main.cpp@5:d6da74ac1cc3, 2016-08-08 (annotated)
- Committer:
- TheFella
- Date:
- Mon Aug 08 02:42:42 2016 +0000
- Revision:
- 5:d6da74ac1cc3
- Parent:
- 4:79863d2ea5a0
- Child:
- 6:7f833d7ef195
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 2:a69c8c5f5b03 | 1 | // example to test the mbed Lab Board lcd lib with the mbed rtos |
4180_1 | 4:79863d2ea5a0 | 2 | // Pot1 changes the contrast |
4180_1 | 4:79863d2ea5a0 | 3 | // Pot2 changes the speed of the sin wave |
dreschpe | 1:1c6a9eaf55b5 | 4 | |
dreschpe | 0:f6a57b843f79 | 5 | #include "mbed.h" |
dreschpe | 0:f6a57b843f79 | 6 | #include "rtos.h" |
dreschpe | 0:f6a57b843f79 | 7 | #include "Small_6.h" |
dreschpe | 0:f6a57b843f79 | 8 | #include "Small_7.h" |
dreschpe | 0:f6a57b843f79 | 9 | #include "Arial_9.h" |
dreschpe | 0:f6a57b843f79 | 10 | #include "stdio.h" |
dreschpe | 1:1c6a9eaf55b5 | 11 | #include "C12832_lcd.h" |
dreschpe | 0:f6a57b843f79 | 12 | |
TheFella | 5:d6da74ac1cc3 | 13 | #define LEDON 1 |
TheFella | 5:d6da74ac1cc3 | 14 | #define LEDOFF 0 |
4180_1 | 4:79863d2ea5a0 | 15 | |
TheFella | 5:d6da74ac1cc3 | 16 | |
TheFella | 5:d6da74ac1cc3 | 17 | Serial pc(USBTX, USBRX); |
dreschpe | 1:1c6a9eaf55b5 | 18 | AnalogIn Pot1(p19); |
dreschpe | 1:1c6a9eaf55b5 | 19 | AnalogIn Pot2(p20); |
TheFella | 5:d6da74ac1cc3 | 20 | DigitalOut led1(LED1); |
TheFella | 5:d6da74ac1cc3 | 21 | DigitalOut led2(LED2); |
dreschpe | 2:a69c8c5f5b03 | 22 | |
TheFella | 5:d6da74ac1cc3 | 23 | |
TheFella | 5:d6da74ac1cc3 | 24 | // mutex to make the serial thread safe |
TheFella | 5:d6da74ac1cc3 | 25 | Mutex pc_mutex; |
dreschpe | 0:f6a57b843f79 | 26 | |
dreschpe | 2:a69c8c5f5b03 | 27 | // Thread 1 |
dreschpe | 2:a69c8c5f5b03 | 28 | // print counter into first line and wait for 1 s |
dreschpe | 0:f6a57b843f79 | 29 | void thread1(void const *args) |
dreschpe | 0:f6a57b843f79 | 30 | { |
dreschpe | 0:f6a57b843f79 | 31 | int i; |
dreschpe | 0:f6a57b843f79 | 32 | while(true) { // thread loop |
TheFella | 5:d6da74ac1cc3 | 33 | pc_mutex.lock(); |
TheFella | 5:d6da74ac1cc3 | 34 | pc.printf("Thread1 count: %d\n\r",i); |
TheFella | 5:d6da74ac1cc3 | 35 | pc_mutex.unlock(); |
dreschpe | 0:f6a57b843f79 | 36 | i++; |
dreschpe | 0:f6a57b843f79 | 37 | Thread::wait(1000); |
dreschpe | 0:f6a57b843f79 | 38 | } |
dreschpe | 0:f6a57b843f79 | 39 | } |
dreschpe | 0:f6a57b843f79 | 40 | |
dreschpe | 2:a69c8c5f5b03 | 41 | // Thread 2 |
dreschpe | 2:a69c8c5f5b03 | 42 | // print counter into third line and wait for 0,5s |
dreschpe | 0:f6a57b843f79 | 43 | void thread2(void const *args) |
dreschpe | 0:f6a57b843f79 | 44 | { |
dreschpe | 1:1c6a9eaf55b5 | 45 | int k; |
dreschpe | 0:f6a57b843f79 | 46 | while(true) { // thread loop |
TheFella | 5:d6da74ac1cc3 | 47 | pc_mutex.lock(); |
TheFella | 5:d6da74ac1cc3 | 48 | pc.printf("Thread 2 count : %d\n\r",k); |
TheFella | 5:d6da74ac1cc3 | 49 | pc_mutex.unlock(); |
dreschpe | 1:1c6a9eaf55b5 | 50 | k++; |
dreschpe | 2:a69c8c5f5b03 | 51 | Thread::wait(500); // wait 0.5s |
dreschpe | 0:f6a57b843f79 | 52 | } |
dreschpe | 0:f6a57b843f79 | 53 | } |
dreschpe | 0:f6a57b843f79 | 54 | |
dreschpe | 2:a69c8c5f5b03 | 55 | // Thread 4 |
TheFella | 5:d6da74ac1cc3 | 56 | // the value of pot 1 changes the DC of LED1 |
dreschpe | 2:a69c8c5f5b03 | 57 | void thread4(void const *args) |
dreschpe | 1:1c6a9eaf55b5 | 58 | { |
TheFella | 5:d6da74ac1cc3 | 59 | while(true) { // thread loop |
TheFella | 5:d6da74ac1cc3 | 60 | pc_mutex.lock(); |
TheFella | 5:d6da74ac1cc3 | 61 | if( Pot1.read_u16() > 0xEFFF) |
TheFella | 5:d6da74ac1cc3 | 62 | { |
TheFella | 5:d6da74ac1cc3 | 63 | led1 = LEDON; |
TheFella | 5:d6da74ac1cc3 | 64 | } |
TheFella | 5:d6da74ac1cc3 | 65 | else |
TheFella | 5:d6da74ac1cc3 | 66 | { |
TheFella | 5:d6da74ac1cc3 | 67 | led1 = LEDOFF; |
TheFella | 5:d6da74ac1cc3 | 68 | } |
TheFella | 5:d6da74ac1cc3 | 69 | |
TheFella | 5:d6da74ac1cc3 | 70 | pc_mutex.unlock(); |
4180_1 | 4:79863d2ea5a0 | 71 | } |
TheFella | 5:d6da74ac1cc3 | 72 | Thread::wait(500); // value of pot1 / 100 |
4180_1 | 4:79863d2ea5a0 | 73 | } |
TheFella | 5:d6da74ac1cc3 | 74 | |
4180_1 | 4:79863d2ea5a0 | 75 | // Thread 5 |
TheFella | 5:d6da74ac1cc3 | 76 | // the value of pot 2 changes the DC of LED2 |
4180_1 | 4:79863d2ea5a0 | 77 | void thread5(void const *args) |
4180_1 | 4:79863d2ea5a0 | 78 | { |
TheFella | 5:d6da74ac1cc3 | 79 | while(true) { // thread loop |
TheFella | 5:d6da74ac1cc3 | 80 | pc_mutex.lock(); |
TheFella | 5:d6da74ac1cc3 | 81 | if( Pot2.read_u16() > 0xEFFF) |
TheFella | 5:d6da74ac1cc3 | 82 | { |
TheFella | 5:d6da74ac1cc3 | 83 | led2 = LEDON; |
TheFella | 5:d6da74ac1cc3 | 84 | } |
TheFella | 5:d6da74ac1cc3 | 85 | else |
TheFella | 5:d6da74ac1cc3 | 86 | { |
TheFella | 5:d6da74ac1cc3 | 87 | led2 = LEDOFF; |
TheFella | 5:d6da74ac1cc3 | 88 | } |
TheFella | 5:d6da74ac1cc3 | 89 | |
TheFella | 5:d6da74ac1cc3 | 90 | pc_mutex.unlock(); |
4180_1 | 4:79863d2ea5a0 | 91 | } |
TheFella | 5:d6da74ac1cc3 | 92 | Thread::wait(500); // value of pot1 / 100 |
dreschpe | 1:1c6a9eaf55b5 | 93 | } |
4180_1 | 4:79863d2ea5a0 | 94 | |
dreschpe | 0:f6a57b843f79 | 95 | int main() |
dreschpe | 0:f6a57b843f79 | 96 | { |
dreschpe | 2:a69c8c5f5b03 | 97 | Thread t1(thread1); //start thread1 |
dreschpe | 2:a69c8c5f5b03 | 98 | Thread t2(thread2); //start thread2 |
dreschpe | 2:a69c8c5f5b03 | 99 | Thread t4(thread4); //start thread4 |
4180_1 | 4:79863d2ea5a0 | 100 | Thread t5(thread5); //start thread5 |
TheFella | 5:d6da74ac1cc3 | 101 | |
dreschpe | 2:a69c8c5f5b03 | 102 | while(true) { // main is the next thread |
TheFella | 5:d6da74ac1cc3 | 103 | pc_mutex.lock(); |
TheFella | 5:d6da74ac1cc3 | 104 | pc_mutex.unlock(); |
dreschpe | 2:a69c8c5f5b03 | 105 | Thread::wait(500); // wait 0.5s |
dreschpe | 0:f6a57b843f79 | 106 | } |
dreschpe | 0:f6a57b843f79 | 107 | } |