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