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