Threads
Dependencies: mbed mbed-rtos C12832_lcd LCD_fonts
main.cpp@5:9f4b49b1adbf, 2020-07-28 (annotated)
- Committer:
- jforde
- Date:
- Tue Jul 28 11:59:30 2020 +0000
- Revision:
- 5:9f4b49b1adbf
- Parent:
- 4:e1e874f4c973
Threads
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 |
dreschpe | 1:1c6a9eaf55b5 | 2 | // Pot1 change the contrast |
dreschpe | 1:1c6a9eaf55b5 | 3 | // Pot2 change the speed of the sin wave |
dreschpe | 1:1c6a9eaf55b5 | 4 | |
jforde | 5:9f4b49b1adbf | 5 | #include "mbed.h" //Preprocessor Directives |
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 | |
jforde | 5:9f4b49b1adbf | 13 | BusIn joy(p15,p12,p13,p16); // Declarations |
mariana_batalim | 4:e1e874f4c973 | 14 | DigitalIn fire(p14); |
mariana_batalim | 4:e1e874f4c973 | 15 | BusOut leds(LED1,LED2,LED3,LED4); |
dreschpe | 3:3ec443c0842a | 16 | C12832_LCD LCD; |
dreschpe | 1:1c6a9eaf55b5 | 17 | AnalogIn Pot1(p19); |
dreschpe | 1:1c6a9eaf55b5 | 18 | AnalogIn Pot2(p20); |
mariana_batalim | 4:e1e874f4c973 | 19 | Mutex lcd_mutex; |
dreschpe | 0:f6a57b843f79 | 20 | |
dreschpe | 2:a69c8c5f5b03 | 21 | |
mariana_batalim | 4:e1e874f4c973 | 22 | // Thread 0 |
mariana_batalim | 4:e1e874f4c973 | 23 | // LEDs controlled by JOYSTICK |
mariana_batalim | 4:e1e874f4c973 | 24 | void thread0(void const *args) |
mariana_batalim | 4:e1e874f4c973 | 25 | { |
mariana_batalim | 4:e1e874f4c973 | 26 | int j; |
mariana_batalim | 4:e1e874f4c973 | 27 | while(true) { // thread loop |
mariana_batalim | 4:e1e874f4c973 | 28 | if (fire) { |
mariana_batalim | 4:e1e874f4c973 | 29 | leds=0xf; |
mariana_batalim | 4:e1e874f4c973 | 30 | } else { |
mariana_batalim | 4:e1e874f4c973 | 31 | leds=joy; |
mariana_batalim | 4:e1e874f4c973 | 32 | } |
mariana_batalim | 4:e1e874f4c973 | 33 | wait(0.1); |
mariana_batalim | 4:e1e874f4c973 | 34 | } |
mariana_batalim | 4:e1e874f4c973 | 35 | } |
mariana_batalim | 4:e1e874f4c973 | 36 | |
mariana_batalim | 4:e1e874f4c973 | 37 | |
dreschpe | 0:f6a57b843f79 | 38 | |
dreschpe | 2:a69c8c5f5b03 | 39 | // Thread 1 |
dreschpe | 2:a69c8c5f5b03 | 40 | // print counter into first line and wait for 1 s |
dreschpe | 0:f6a57b843f79 | 41 | void thread1(void const *args) |
dreschpe | 0:f6a57b843f79 | 42 | { |
dreschpe | 0:f6a57b843f79 | 43 | int i; |
dreschpe | 0:f6a57b843f79 | 44 | while(true) { // thread loop |
dreschpe | 2:a69c8c5f5b03 | 45 | lcd_mutex.lock(); |
dreschpe | 0:f6a57b843f79 | 46 | LCD.locate(0,0); |
dreschpe | 0:f6a57b843f79 | 47 | LCD.set_font((unsigned char*) Small_6); |
dreschpe | 3:3ec443c0842a | 48 | LCD.printf("Thread1 count: %d",i); |
dreschpe | 2:a69c8c5f5b03 | 49 | lcd_mutex.unlock(); |
dreschpe | 0:f6a57b843f79 | 50 | i++; |
dreschpe | 0:f6a57b843f79 | 51 | Thread::wait(1000); |
dreschpe | 0:f6a57b843f79 | 52 | } |
dreschpe | 0:f6a57b843f79 | 53 | } |
dreschpe | 0:f6a57b843f79 | 54 | |
dreschpe | 2:a69c8c5f5b03 | 55 | // Thread 2 |
dreschpe | 2:a69c8c5f5b03 | 56 | // print counter into third line and wait for 0,5s |
dreschpe | 0:f6a57b843f79 | 57 | void thread2(void const *args) |
dreschpe | 0:f6a57b843f79 | 58 | { |
dreschpe | 1:1c6a9eaf55b5 | 59 | int k; |
dreschpe | 0:f6a57b843f79 | 60 | while(true) { // thread loop |
dreschpe | 2:a69c8c5f5b03 | 61 | lcd_mutex.lock(); |
dreschpe | 0:f6a57b843f79 | 62 | LCD.locate(0,20); |
dreschpe | 0:f6a57b843f79 | 63 | LCD.set_font((unsigned char*) Arial_9); |
dreschpe | 3:3ec443c0842a | 64 | LCD.printf("Thread 2 count : %d",k); |
dreschpe | 2:a69c8c5f5b03 | 65 | lcd_mutex.unlock(); |
dreschpe | 1:1c6a9eaf55b5 | 66 | k++; |
dreschpe | 2:a69c8c5f5b03 | 67 | Thread::wait(500); // wait 0.5s |
dreschpe | 0:f6a57b843f79 | 68 | } |
dreschpe | 0:f6a57b843f79 | 69 | } |
dreschpe | 0:f6a57b843f79 | 70 | |
dreschpe | 2:a69c8c5f5b03 | 71 | // Thread 3 |
dreschpe | 2:a69c8c5f5b03 | 72 | // print a sin function in a small window |
dreschpe | 2:a69c8c5f5b03 | 73 | // the value of pot 1 change the speed of the sinwave |
dreschpe | 2:a69c8c5f5b03 | 74 | void thread3(void const *args) |
dreschpe | 1:1c6a9eaf55b5 | 75 | { |
dreschpe | 1:1c6a9eaf55b5 | 76 | int i,k,v; |
dreschpe | 1:1c6a9eaf55b5 | 77 | double s,a; |
dreschpe | 1:1c6a9eaf55b5 | 78 | k = 1; |
dreschpe | 2:a69c8c5f5b03 | 79 | lcd_mutex.lock(); |
dreschpe | 1:1c6a9eaf55b5 | 80 | LCD.rect(89,0,127,17,1); |
dreschpe | 2:a69c8c5f5b03 | 81 | lcd_mutex.unlock(); |
dreschpe | 1:1c6a9eaf55b5 | 82 | while(true) { // thread loop |
dreschpe | 1:1c6a9eaf55b5 | 83 | v = Pot1.read_u16(); // get value of pot 1 |
dreschpe | 2:a69c8c5f5b03 | 84 | lcd_mutex.lock(); |
dreschpe | 1:1c6a9eaf55b5 | 85 | for (i=90; i<127; i++) { |
dreschpe | 2:a69c8c5f5b03 | 86 | s = 8 * sin((long double)(i+k) /5); // pixel to print |
dreschpe | 2:a69c8c5f5b03 | 87 | a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase |
dreschpe | 2:a69c8c5f5b03 | 88 | LCD.pixel(i,9 + (int)a ,0); // erase pixel |
dreschpe | 2:a69c8c5f5b03 | 89 | LCD.pixel(i,9 + (int)s ,1); // print pixel |
dreschpe | 1:1c6a9eaf55b5 | 90 | } |
dreschpe | 2:a69c8c5f5b03 | 91 | LCD.copy_to_lcd(); // LCD.pixel do not update the lcd |
dreschpe | 2:a69c8c5f5b03 | 92 | lcd_mutex.unlock(); |
dreschpe | 1:1c6a9eaf55b5 | 93 | k++; |
dreschpe | 2:a69c8c5f5b03 | 94 | Thread::wait(v/100); // value of pot1 / 100 |
dreschpe | 1:1c6a9eaf55b5 | 95 | } |
dreschpe | 1:1c6a9eaf55b5 | 96 | } |
dreschpe | 0:f6a57b843f79 | 97 | |
dreschpe | 2:a69c8c5f5b03 | 98 | // Thread 4 |
dreschpe | 2:a69c8c5f5b03 | 99 | // input pot 2 and change the contrast of lcd |
dreschpe | 2:a69c8c5f5b03 | 100 | void thread4(void const *args) |
dreschpe | 1:1c6a9eaf55b5 | 101 | { |
dreschpe | 1:1c6a9eaf55b5 | 102 | int k; |
dreschpe | 1:1c6a9eaf55b5 | 103 | while(true) { // thread loop |
dreschpe | 1:1c6a9eaf55b5 | 104 | k = Pot2.read_u16(); // get the value of poti 2 |
dreschpe | 2:a69c8c5f5b03 | 105 | k = k >> 10; // we need only 6 bit for contrast |
dreschpe | 2:a69c8c5f5b03 | 106 | lcd_mutex.lock(); |
dreschpe | 1:1c6a9eaf55b5 | 107 | LCD.set_contrast(k); |
dreschpe | 2:a69c8c5f5b03 | 108 | lcd_mutex.unlock(); |
dreschpe | 2:a69c8c5f5b03 | 109 | Thread::wait(500); // wait 0.5s |
dreschpe | 1:1c6a9eaf55b5 | 110 | } |
dreschpe | 1:1c6a9eaf55b5 | 111 | } |
dreschpe | 1:1c6a9eaf55b5 | 112 | |
dreschpe | 2:a69c8c5f5b03 | 113 | |
dreschpe | 2:a69c8c5f5b03 | 114 | // print the actual contrast |
dreschpe | 0:f6a57b843f79 | 115 | int main() |
dreschpe | 0:f6a57b843f79 | 116 | { |
dreschpe | 0:f6a57b843f79 | 117 | int j; |
dreschpe | 2:a69c8c5f5b03 | 118 | LCD.cls(); |
dreschpe | 2:a69c8c5f5b03 | 119 | |
mariana_batalim | 4:e1e874f4c973 | 120 | Thread t0(thread0); //start thread0 |
dreschpe | 2:a69c8c5f5b03 | 121 | Thread t1(thread1); //start thread1 |
dreschpe | 2:a69c8c5f5b03 | 122 | Thread t2(thread2); //start thread2 |
dreschpe | 2:a69c8c5f5b03 | 123 | Thread t3(thread3); //start thread3 |
dreschpe | 2:a69c8c5f5b03 | 124 | Thread t4(thread4); //start thread4 |
dreschpe | 0:f6a57b843f79 | 125 | |
dreschpe | 2:a69c8c5f5b03 | 126 | while(true) { // main is the next thread |
dreschpe | 2:a69c8c5f5b03 | 127 | lcd_mutex.lock(); |
dreschpe | 1:1c6a9eaf55b5 | 128 | LCD.locate(0,9); |
dreschpe | 0:f6a57b843f79 | 129 | LCD.set_font((unsigned char*) Small_7); |
dreschpe | 2:a69c8c5f5b03 | 130 | j = LCD.get_contrast(); // read the actual contrast |
dreschpe | 3:3ec443c0842a | 131 | LCD.printf("contrast : %d",j); |
dreschpe | 2:a69c8c5f5b03 | 132 | lcd_mutex.unlock(); |
dreschpe | 2:a69c8c5f5b03 | 133 | Thread::wait(500); // wait 0.5s |
dreschpe | 0:f6a57b843f79 | 134 | } |
dreschpe | 1:1c6a9eaf55b5 | 135 | |
dreschpe | 0:f6a57b843f79 | 136 | } |