Ciaran O'Malley / Mbed 2 deprecated Lab_2_5_Threads

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