This demo uses the mbed RTOS to run eight threads (including main). The threads are using different I/O devices on the application board. Several of the threads output to the LCD and an OS mutex lock is used to control access to the LCD and make the LCD thread safe.
Dependencies: C12832_lcd LCD_fonts mbed-rtos mbed
Fork of lab1 by
main.cpp@1:1c6a9eaf55b5, 2012-10-16 (annotated)
- Committer:
- dreschpe
- Date:
- Tue Oct 16 17:51:10 2012 +0000
- Revision:
- 1:1c6a9eaf55b5
- Parent:
- 0:f6a57b843f79
- Child:
- 2:a69c8c5f5b03
ver 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 0:f6a57b843f79 | 1 | // example to test the mbed Lab Board lcd |
dreschpe | 1:1c6a9eaf55b5 | 2 | // Pot1 change the contrast |
dreschpe | 1:1c6a9eaf55b5 | 3 | // Pot2 change the speed of the sin wave |
dreschpe | 1:1c6a9eaf55b5 | 4 | |
dreschpe | 0:f6a57b843f79 | 5 | |
dreschpe | 0:f6a57b843f79 | 6 | #include "mbed.h" |
dreschpe | 0:f6a57b843f79 | 7 | #include "rtos.h" |
dreschpe | 0:f6a57b843f79 | 8 | #include "Small_6.h" |
dreschpe | 0:f6a57b843f79 | 9 | #include "Small_7.h" |
dreschpe | 0:f6a57b843f79 | 10 | #include "Arial_9.h" |
dreschpe | 0:f6a57b843f79 | 11 | #include "stdio.h" |
dreschpe | 1:1c6a9eaf55b5 | 12 | #include "C12832_lcd.h" |
dreschpe | 0:f6a57b843f79 | 13 | |
dreschpe | 0:f6a57b843f79 | 14 | Serial pc(USBTX, USBRX); |
dreschpe | 0:f6a57b843f79 | 15 | |
dreschpe | 0:f6a57b843f79 | 16 | |
dreschpe | 0:f6a57b843f79 | 17 | // LCD object |
dreschpe | 0:f6a57b843f79 | 18 | C12832_LCD LCD("LCD"); |
dreschpe | 0:f6a57b843f79 | 19 | |
dreschpe | 1:1c6a9eaf55b5 | 20 | AnalogIn Pot1(p19); |
dreschpe | 1:1c6a9eaf55b5 | 21 | AnalogIn Pot2(p20); |
dreschpe | 0:f6a57b843f79 | 22 | |
dreschpe | 1:1c6a9eaf55b5 | 23 | // defines to make lib thread save |
dreschpe | 1:1c6a9eaf55b5 | 24 | #define _lock lcd_mutex.lock(); |
dreschpe | 1:1c6a9eaf55b5 | 25 | #define _unlock lcd_mutex.unlock(); |
dreschpe | 1:1c6a9eaf55b5 | 26 | Mutex lcd_mutex; |
dreschpe | 0:f6a57b843f79 | 27 | |
dreschpe | 0:f6a57b843f79 | 28 | // print data into first line and wait for 1s |
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 |
dreschpe | 0:f6a57b843f79 | 33 | _lock |
dreschpe | 0:f6a57b843f79 | 34 | LCD.locate(0,0); |
dreschpe | 0:f6a57b843f79 | 35 | LCD.set_font((unsigned char*) Small_6); |
dreschpe | 1:1c6a9eaf55b5 | 36 | printf("Thread1 count: %d",i); |
dreschpe | 0:f6a57b843f79 | 37 | LCD.copy_to_lcd(); |
dreschpe | 0:f6a57b843f79 | 38 | _unlock |
dreschpe | 0:f6a57b843f79 | 39 | i++; |
dreschpe | 0:f6a57b843f79 | 40 | Thread::wait(1000); |
dreschpe | 0:f6a57b843f79 | 41 | } |
dreschpe | 0:f6a57b843f79 | 42 | } |
dreschpe | 0:f6a57b843f79 | 43 | |
dreschpe | 0:f6a57b843f79 | 44 | // print data into third line and wait for 0,5s |
dreschpe | 0:f6a57b843f79 | 45 | void thread2(void const *args) |
dreschpe | 0:f6a57b843f79 | 46 | { |
dreschpe | 1:1c6a9eaf55b5 | 47 | int k; |
dreschpe | 0:f6a57b843f79 | 48 | while(true) { // thread loop |
dreschpe | 0:f6a57b843f79 | 49 | _lock |
dreschpe | 0:f6a57b843f79 | 50 | LCD.locate(0,20); |
dreschpe | 0:f6a57b843f79 | 51 | LCD.set_font((unsigned char*) Arial_9); |
dreschpe | 1:1c6a9eaf55b5 | 52 | printf("Thread 2 count : %d",k); |
dreschpe | 0:f6a57b843f79 | 53 | LCD.copy_to_lcd(); |
dreschpe | 0:f6a57b843f79 | 54 | _unlock |
dreschpe | 1:1c6a9eaf55b5 | 55 | k++; |
dreschpe | 0:f6a57b843f79 | 56 | Thread::wait(500); |
dreschpe | 0:f6a57b843f79 | 57 | } |
dreschpe | 0:f6a57b843f79 | 58 | } |
dreschpe | 0:f6a57b843f79 | 59 | |
dreschpe | 0:f6a57b843f79 | 60 | |
dreschpe | 1:1c6a9eaf55b5 | 61 | void thread4(void const *args) |
dreschpe | 1:1c6a9eaf55b5 | 62 | { |
dreschpe | 1:1c6a9eaf55b5 | 63 | int i,k,v; |
dreschpe | 1:1c6a9eaf55b5 | 64 | double s,a; |
dreschpe | 1:1c6a9eaf55b5 | 65 | k = 1; |
dreschpe | 1:1c6a9eaf55b5 | 66 | _lock |
dreschpe | 1:1c6a9eaf55b5 | 67 | LCD.rect(89,0,127,17,1); |
dreschpe | 1:1c6a9eaf55b5 | 68 | _unlock |
dreschpe | 1:1c6a9eaf55b5 | 69 | while(true) { // thread loop |
dreschpe | 1:1c6a9eaf55b5 | 70 | _lock |
dreschpe | 1:1c6a9eaf55b5 | 71 | v = Pot1.read_u16(); // get value of pot 1 |
dreschpe | 1:1c6a9eaf55b5 | 72 | for (i=90; i<127; i++) { |
dreschpe | 1:1c6a9eaf55b5 | 73 | s = 8 * sin((long double)(i+k) /5); |
dreschpe | 1:1c6a9eaf55b5 | 74 | a = 8 * sin((long double)(i+k-1) /5); |
dreschpe | 1:1c6a9eaf55b5 | 75 | LCD.pixel(i,9 + (int)a ,0); |
dreschpe | 1:1c6a9eaf55b5 | 76 | LCD.pixel(i,9 + (int)s ,1); |
dreschpe | 1:1c6a9eaf55b5 | 77 | } |
dreschpe | 1:1c6a9eaf55b5 | 78 | LCD.copy_to_lcd(); |
dreschpe | 1:1c6a9eaf55b5 | 79 | _unlock |
dreschpe | 1:1c6a9eaf55b5 | 80 | k++; |
dreschpe | 1:1c6a9eaf55b5 | 81 | Thread::wait(v/100); |
dreschpe | 1:1c6a9eaf55b5 | 82 | } |
dreschpe | 1:1c6a9eaf55b5 | 83 | } |
dreschpe | 0:f6a57b843f79 | 84 | |
dreschpe | 0:f6a57b843f79 | 85 | |
dreschpe | 1:1c6a9eaf55b5 | 86 | void thread5(void const *args) |
dreschpe | 1:1c6a9eaf55b5 | 87 | { |
dreschpe | 1:1c6a9eaf55b5 | 88 | int k; |
dreschpe | 1:1c6a9eaf55b5 | 89 | while(true) { // thread loop |
dreschpe | 1:1c6a9eaf55b5 | 90 | k = Pot2.read_u16(); // get the value of poti 2 |
dreschpe | 1:1c6a9eaf55b5 | 91 | k = k >> 10; // we need only 6 bit |
dreschpe | 1:1c6a9eaf55b5 | 92 | _lock |
dreschpe | 1:1c6a9eaf55b5 | 93 | LCD.set_contrast(k); |
dreschpe | 1:1c6a9eaf55b5 | 94 | _unlock |
dreschpe | 1:1c6a9eaf55b5 | 95 | Thread::wait(500); |
dreschpe | 1:1c6a9eaf55b5 | 96 | } |
dreschpe | 1:1c6a9eaf55b5 | 97 | } |
dreschpe | 1:1c6a9eaf55b5 | 98 | |
dreschpe | 0:f6a57b843f79 | 99 | int main() |
dreschpe | 0:f6a57b843f79 | 100 | { |
dreschpe | 0:f6a57b843f79 | 101 | int j; |
dreschpe | 0:f6a57b843f79 | 102 | |
dreschpe | 0:f6a57b843f79 | 103 | LCD.claim(stdout); // send stdout to the LCD display |
dreschpe | 1:1c6a9eaf55b5 | 104 | LCD.cls(); |
dreschpe | 1:1c6a9eaf55b5 | 105 | //start thread1 |
dreschpe | 0:f6a57b843f79 | 106 | Thread t1(thread1); |
dreschpe | 1:1c6a9eaf55b5 | 107 | //start thread2 |
dreschpe | 0:f6a57b843f79 | 108 | Thread t2(thread2); |
dreschpe | 1:1c6a9eaf55b5 | 109 | //start thread4 |
dreschpe | 1:1c6a9eaf55b5 | 110 | Thread t4(thread4); |
dreschpe | 1:1c6a9eaf55b5 | 111 | Thread t5(thread5); |
dreschpe | 0:f6a57b843f79 | 112 | |
dreschpe | 0:f6a57b843f79 | 113 | while(true) { // this is the third thread |
dreschpe | 0:f6a57b843f79 | 114 | _lock |
dreschpe | 1:1c6a9eaf55b5 | 115 | LCD.locate(0,9); |
dreschpe | 0:f6a57b843f79 | 116 | LCD.set_font((unsigned char*) Small_7); |
dreschpe | 1:1c6a9eaf55b5 | 117 | j = LCD.get_contrast(); |
dreschpe | 1:1c6a9eaf55b5 | 118 | printf("contrast : %d",j); |
dreschpe | 0:f6a57b843f79 | 119 | LCD.copy_to_lcd(); |
dreschpe | 0:f6a57b843f79 | 120 | _unlock |
dreschpe | 1:1c6a9eaf55b5 | 121 | Thread::wait(500); |
dreschpe | 0:f6a57b843f79 | 122 | } |
dreschpe | 1:1c6a9eaf55b5 | 123 | |
dreschpe | 0:f6a57b843f79 | 124 | } |