This demo uses the mbed RTOS to run eight threads (including main). The threads are using different I/O devices on the application board. Several of the threads output to the LCD and an OS mutex lock is used to control access to the LCD and make the LCD thread safe.
Dependencies: C12832_lcd LCD_fonts mbed-rtos mbed
Fork of lab1 by
main.cpp
00001 // example to test the mbed Lab Board lcd lib with the mbed rtos 00002 // Pot1 changes the contrast 00003 // Pot2 changes 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 C12832_LCD LCD; 00015 AnalogIn Pot1(p19); 00016 AnalogIn Pot2(p20); 00017 PwmOut Speaker(p26); 00018 PwmOut RGBLED_r(p23); 00019 PwmOut RGBLED_g(p24); 00020 PwmOut RGBLED_b(p25); 00021 DigitalIn joyfire(p14); 00022 BusIn joy(p15,p12,p13,p16); 00023 BusOut leds(LED1,LED2,LED3,LED4); 00024 00025 // mutex to make the lcd lib thread safe 00026 Mutex lcd_mutex; 00027 00028 // Thread 1 00029 // print counter into first line and wait for 1 s 00030 void thread1(void const *args) 00031 { 00032 int i; 00033 while(true) { // thread loop 00034 lcd_mutex.lock(); 00035 LCD.locate(0,0); 00036 LCD.set_font((unsigned char*) Small_6); 00037 LCD.printf("Thread1 count: %d",i); 00038 lcd_mutex.unlock(); 00039 i++; 00040 Thread::wait(1000); 00041 } 00042 } 00043 00044 // Thread 2 00045 // print counter into third line and wait for 0,5s 00046 void thread2(void const *args) 00047 { 00048 int k; 00049 while(true) { // thread loop 00050 lcd_mutex.lock(); 00051 LCD.locate(0,20); 00052 LCD.set_font((unsigned char*) Arial_9); 00053 LCD.printf("Thread 2 count : %d",k); 00054 lcd_mutex.unlock(); 00055 k++; 00056 Thread::wait(500); // wait 0.5s 00057 } 00058 } 00059 00060 // Thread 3 00061 // print a sin function in a small window 00062 // the value of pot 1 changes the speed of the sine wave 00063 void thread3(void const *args) 00064 { 00065 int i,k,v; 00066 double s,a; 00067 k = 1; 00068 lcd_mutex.lock(); 00069 LCD.rect(89,0,127,17,1); 00070 lcd_mutex.unlock(); 00071 while(true) { // thread loop 00072 v = Pot1.read_u16(); // get value of pot 1 00073 lcd_mutex.lock(); 00074 for (i=90; i<127; i++) { 00075 s = 8 * sin((long double)(i+k) /5); // pixel to print 00076 a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase 00077 LCD.pixel(i,9 + (int)a ,0); // erase pixel 00078 LCD.pixel(i,9 + (int)s ,1); // print pixel 00079 } 00080 LCD.copy_to_lcd(); // LCD.pixel does not update the lcd 00081 lcd_mutex.unlock(); 00082 k++; 00083 Thread::wait(v/100); // value of pot1 / 100 00084 } 00085 } 00086 00087 // Thread 4 00088 // input pot 2 and change the contrast of LCD 00089 void thread4(void const *args) 00090 { 00091 int k; 00092 while(true) { // thread loop 00093 k = Pot2.read_u16(); // get the value of poti 2 00094 k = k >> 10; // need only 6 bits for contrast 00095 lcd_mutex.lock(); 00096 LCD.set_contrast(k); 00097 lcd_mutex.unlock(); 00098 Thread::wait(500); // wait 0.5s 00099 } 00100 } 00101 // Thread 5 00102 // RGB LED 00103 void thread5(void const *args) 00104 { 00105 while(true) { // thread loop 00106 RGBLED_r = 0.5 + (rand() % 11)/20.0; 00107 RGBLED_g = 0.5 + (rand() % 11)/20.0; 00108 RGBLED_b = 0.5 + (rand() % 11)/20.0; 00109 Thread::wait(1667); // wait 1.5s 00110 } 00111 } 00112 // Thread 6 00113 // Speaker 00114 void thread6(void const *args) 00115 { 00116 while(true) { // thread loop 00117 Speaker.period(1.0/800.0); 00118 Speaker = 0.01; 00119 Thread::wait(1000); // wait 1.0s 00120 Speaker.period(1.0/969.0); 00121 Speaker = 0.01; 00122 Thread::wait(1000); // wait 1.0s 00123 } 00124 } 00125 00126 // Thread 7 00127 // Joystick controls onboard mbed LEDs 00128 void thread7(void const *args) 00129 { 00130 while(true) { // thread loop 00131 if (joyfire) { 00132 leds = 0xf; 00133 } else { 00134 leds = joy; 00135 } 00136 Thread::wait(200); // wait 0.25s 00137 } 00138 } 00139 00140 00141 00142 int main() 00143 { 00144 int j; 00145 LCD.cls(); 00146 00147 Thread t1(thread1); //start thread1 00148 Thread t2(thread2); //start thread2 00149 Thread t3(thread3); //start thread3 00150 Thread t4(thread4); //start thread4 00151 Thread t5(thread5); //start thread5 00152 Thread t6(thread6); //start thread6 00153 Thread t7(thread7); //start thread7 00154 00155 while(true) { // main is the next thread 00156 lcd_mutex.lock(); 00157 LCD.locate(0,9); 00158 LCD.set_font((unsigned char*) Small_7); 00159 j = LCD.get_contrast(); // read the actual contrast 00160 LCD.printf("contrast : %d",j); 00161 lcd_mutex.unlock(); 00162 Thread::wait(500); // wait 0.5s 00163 } 00164 }
Generated on Thu Jul 14 2022 00:32:43 by 1.7.2