test program for mbed Lab Board

Dependencies:   C12832_lcd LCD_fonts mbed-rtos mbed

Dependents:   Experiment_5

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