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