2.5 Threads and programming Lab question

Dependencies:   mbed mbed-rtos C12832_lcd LCD_fonts

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // example to test the mbed Lab Board lcd lib with the mbed rtos
00002 // Pot1 change the contrast
00003 // Pot2 change the speed of the sin wave
00004 
00005 #include "mbed.h"
00006 #include "rtos.h"
00007 #include "Small_6.h"
00008 #include "Small_7.h"
00009 #include "Arial_9.h"
00010 #include "stdio.h"
00011 #include "C12832_lcd.h"
00012 
00013 // LCD object
00014 C12832_LCD LCD;
00015 
00016 AnalogIn Pot1(p19);
00017 AnalogIn Pot2(p20);
00018 BusIn joy(p15,p12,p13,p16);
00019 DigitalIn fire(p14);
00020 
00021 BusOut leds(LED1,LED2,LED3,LED4);
00022 
00023 
00024 // mutex to make the lcd lib thread save
00025 Mutex lcd_mutex;
00026 
00027 // Thread 1
00028 // print counter into first line and wait for 1 s
00029 void thread1(void const *args)
00030 {
00031     int i;
00032     while(true) {       // thread loop
00033         lcd_mutex.lock();
00034         LCD.locate(0,0);
00035         LCD.set_font((unsigned char*) Small_6);
00036         LCD.printf("Thread1 count: %d",i);
00037         lcd_mutex.unlock();
00038         i++;
00039         Thread::wait(1000);
00040     }
00041 }
00042 
00043 // Thread 2
00044 // print counter into third line and wait for 0,5s
00045 void thread2(void const *args)
00046 {
00047     int k;
00048     while(true) {       // thread loop
00049         lcd_mutex.lock();
00050         LCD.locate(0,20);
00051         LCD.set_font((unsigned char*) Arial_9);
00052         LCD.printf("Thread 2 count : %d",k);
00053         lcd_mutex.unlock();
00054         k++;
00055         Thread::wait(500); // wait 0.5s
00056     }
00057 }
00058 
00059 // Thread 3
00060 // print a sin function in a small window
00061 // the value of pot 1 change the speed of the sinwave
00062 void thread3(void const *args)
00063 {
00064     int i,k,v;
00065     double s,a;
00066     k = 1;
00067     lcd_mutex.lock();
00068     LCD.rect(89,0,127,17,1);
00069     lcd_mutex.unlock();
00070     while(true) {       // thread loop
00071         v = Pot1.read_u16();  // get value of pot 1
00072         lcd_mutex.lock();
00073         for (i=90; i<127; i++) {           
00074             s = 8 * sin((long double)(i+k) /5);   // pixel to print  
00075             a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase
00076             LCD.pixel(i,9 + (int)a ,0);           // erase pixel  
00077             LCD.pixel(i,9 + (int)s ,1);           // print pixel  
00078         }
00079         LCD.copy_to_lcd();  // LCD.pixel do not update the lcd 
00080         lcd_mutex.unlock();
00081         k++;
00082         Thread::wait(v/100);   // value of pot1 / 100
00083     }
00084 }
00085 
00086 // Thread 4
00087 // input pot 2 and change the contrast of lcd
00088 void thread4(void const *args)
00089 {
00090     int k;
00091     while(true) {         // thread loop
00092     k = Pot2.read_u16();  // get the value of poti 2
00093     k = k >> 10;          // we need only 6 bit for contrast
00094      lcd_mutex.lock();
00095     LCD.set_contrast(k);
00096      lcd_mutex.unlock();
00097     Thread::wait(500);    // wait 0.5s
00098 }
00099 }
00100 
00101 //Thread 5
00102 // input joystick control for LED
00103 void thread5(void const *args)
00104 {
00105     while(1) {
00106         if (fire) {
00107             leds=0xf;
00108         } else {
00109             leds=joy;
00110         }
00111         wait(0.1);
00112     }
00113 }
00114 // print the actual contrast 
00115 int main()
00116 {
00117     int j;
00118     LCD.cls();
00119   
00120     Thread t1(thread1); //start thread1
00121     Thread t2(thread2); //start thread2
00122     Thread t3(thread3); //start thread3
00123     Thread t4(thread4); //start thread4
00124     Thread t5(thread5); //start thread5
00125 
00126     while(true) {       // main is the next thread
00127         lcd_mutex.lock();
00128         LCD.locate(0,9);
00129         LCD.set_font((unsigned char*) Small_7);
00130         j = LCD.get_contrast();    // read the actual contrast
00131         LCD.printf("contrast : %d",j);
00132         lcd_mutex.unlock();
00133         Thread::wait(500);   // wait 0.5s
00134     }
00135 
00136 }