![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Threads
Dependencies: mbed mbed-rtos C12832_lcd LCD_fonts
Diff: main.cpp
- Revision:
- 1:1c6a9eaf55b5
- Parent:
- 0:f6a57b843f79
- Child:
- 2:a69c8c5f5b03
--- a/main.cpp Mon Oct 15 21:48:28 2012 +0000 +++ b/main.cpp Tue Oct 16 17:51:10 2012 +0000 @@ -1,4 +1,7 @@ // example to test the mbed Lab Board lcd +// Pot1 change the contrast +// Pot2 change the speed of the sin wave + #include "mbed.h" #include "rtos.h" @@ -6,16 +9,21 @@ #include "Small_7.h" #include "Arial_9.h" #include "stdio.h" - +#include "C12832_lcd.h" Serial pc(USBTX, USBRX); -#include "C12832_lcd.h" // LCD object C12832_LCD LCD("LCD"); +AnalogIn Pot1(p19); +AnalogIn Pot2(p20); +// defines to make lib thread save +#define _lock lcd_mutex.lock(); +#define _unlock lcd_mutex.unlock(); +Mutex lcd_mutex; // print data into first line and wait for 1s void thread1(void const *args) @@ -25,7 +33,7 @@ _lock LCD.locate(0,0); LCD.set_font((unsigned char*) Small_6); - printf("Thread 1 count : %d",i); + printf("Thread1 count: %d",i); LCD.copy_to_lcd(); _unlock i++; @@ -36,44 +44,81 @@ // print data into third line and wait for 0,5s void thread2(void const *args) { - int i; + int k; while(true) { // thread loop _lock LCD.locate(0,20); LCD.set_font((unsigned char*) Arial_9); - printf("Thread 2 count : %d",i); + printf("Thread 2 count : %d",k); LCD.copy_to_lcd(); _unlock - i++; + k++; Thread::wait(500); } } +void thread4(void const *args) +{ + int i,k,v; + double s,a; + k = 1; + _lock + LCD.rect(89,0,127,17,1); + _unlock + while(true) { // thread loop + _lock + v = Pot1.read_u16(); // get value of pot 1 + for (i=90; i<127; i++) { + s = 8 * sin((long double)(i+k) /5); + a = 8 * sin((long double)(i+k-1) /5); + LCD.pixel(i,9 + (int)a ,0); + LCD.pixel(i,9 + (int)s ,1); + } + LCD.copy_to_lcd(); + _unlock + k++; + Thread::wait(v/100); + } +} +void thread5(void const *args) +{ + int k; + while(true) { // thread loop + k = Pot2.read_u16(); // get the value of poti 2 + k = k >> 10; // we need only 6 bit + _lock + LCD.set_contrast(k); + _unlock + Thread::wait(500); +} +} + int main() { int j; - pc.printf("Test LCD \n\r"); - LCD.set_orientation(1); LCD.claim(stdout); // send stdout to the LCD display - - // start thread1 + LCD.cls(); + //start thread1 Thread t1(thread1); - // start thread2 + //start thread2 Thread t2(thread2); + //start thread4 + Thread t4(thread4); + Thread t5(thread5); while(true) { // this is the third thread _lock - LCD.locate(0,10); + LCD.locate(0,9); LCD.set_font((unsigned char*) Small_7); - printf("Thread3 count : %d",j); + j = LCD.get_contrast(); + printf("contrast : %d",j); LCD.copy_to_lcd(); _unlock - j++; - Thread::wait(1500); + Thread::wait(500); } - + }