Instrumentation Monitoring and Control Winter Lab 1 T00221592

Dependencies:   mbed mbed-rtos C12832_lcd LCD_fonts

Committer:
t00221592
Date:
Mon Dec 20 19:22:25 2021 +0000
Revision:
4:23fbf558b971
Parent:
3:3ec443c0842a
Michael Mc Namara T00221592 Instrumentation Monitoring and control Winter Lab Report

Who changed what in which revision?

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