Thread

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed

Fork of DISCO-F746NG_LCDTS_demo by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TS_DISCO_F746NG.h"
00003 #include "LCD_DISCO_F746NG.h"
00004 #include "Thread.h"
00005 #include "rtos.h"
00006 
00007 
00008 
00009 LCD_DISCO_F746NG lcd;
00010 TS_DISCO_F746NG ts;
00011 Mutex stdio_mutex;
00012 uint8_t r=0,g=0,b=0;
00013 
00014 
00015 
00016 
00017 
00018 class DRAW            //クラス宣言
00019 {
00020 private:
00021     uint32_t color;
00022     uint16_t Xpos;
00023     uint16_t Ypos;
00024     uint16_t Rad;
00025 public:
00026     void fillcircle(uint32_t col,uint16_t x,uint16_t y,uint16_t rad);
00027 };
00028 
00029 
00030 void DRAW::fillcircle(uint32_t col,uint16_t x,uint16_t y,uint16_t rad){
00031     lcd.SetTextColor(col|0xFF000000);
00032     lcd.FillCircle(x,y,rad);
00033 }
00034 
00035 
00036 DRAW draw;
00037 
00038 void lcd_r_thread(void const *args) {
00039     while (true) {
00040         stdio_mutex.lock();
00041         draw.fillcircle(r*0x00010000,100,150,30);
00042         stdio_mutex.unlock();
00043         r++;
00044         Thread::wait(20);
00045     }
00046 }
00047 void lcd_g_thread(void const *args) {
00048     while (true) {
00049         stdio_mutex.lock();
00050         draw.fillcircle(g*0x00000100,200,150,30);
00051         stdio_mutex.unlock();
00052         g++;
00053         Thread::wait(40);
00054     }
00055 }
00056 void lcd_b_thread(void const *args) {
00057     while (true) {
00058         stdio_mutex.lock();
00059         draw.fillcircle(b,300,150,30);
00060         stdio_mutex.unlock();
00061         b++;
00062         Thread::wait(60);
00063     }
00064 }
00065 void RGBtext(void const *args) {
00066     uint8_t text[30];
00067     while (true) {
00068         stdio_mutex.lock();
00069         lcd.SetFont(&Font24);
00070         lcd.SetTextColor(LCD_COLOR_WHITE);
00071         sprintf((char*)text, "R:0x%x",r);
00072         lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
00073         sprintf((char*)text, "G:0x%x",g);
00074         lcd.DisplayStringAt(0, LINE(1), (uint8_t *)&text, LEFT_MODE);
00075         sprintf((char*)text, "B:0x%x",b);
00076         lcd.DisplayStringAt(0, LINE(2), (uint8_t *)&text, LEFT_MODE);
00077         stdio_mutex.unlock();
00078         Thread::wait(100);
00079     }
00080 }
00081 void TPtext(void const *args) {
00082     TS_StateTypeDef TS_State;
00083     uint16_t x, y;
00084     uint8_t text[30];
00085     uint8_t status;
00086     uint8_t idx;
00087     uint8_t cleared = 0;
00088     uint8_t prev_nb_touches = 0;
00089     while (true) {
00090         stdio_mutex.lock();
00091         lcd.SetFont(&Font12);
00092         lcd.SetTextColor(LCD_COLOR_WHITE);
00093 
00094         ts.GetState(&TS_State);
00095         if (TS_State.touchDetected) {
00096             // Clear lines corresponding to old touches coordinates
00097             if (TS_State.touchDetected < prev_nb_touches) {
00098                 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
00099                     lcd.ClearStringLine(idx);
00100                 }
00101             }
00102             prev_nb_touches = TS_State.touchDetected;
00103 
00104             cleared = 0;
00105 
00106             sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
00107             lcd.DisplayStringAt(150, LINE(0), (uint8_t *)&text, LEFT_MODE);
00108 
00109             for (idx = 0; idx < TS_State.touchDetected; idx++) {
00110                 x = TS_State.touchX[idx];
00111                 y = TS_State.touchY[idx];
00112                 sprintf((char*)text, "Touch %d: x=%d y=%d    ", idx+1, x, y);
00113                 lcd.DisplayStringAt(150, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
00114             }
00115 
00116             lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
00117         } else {
00118             if (!cleared) {
00119                 lcd.Clear(LCD_COLOR_BLACK);
00120                 sprintf((char*)text, "Touches: 0");
00121                 lcd.DisplayStringAt(150, LINE(0), (uint8_t *)&text, LEFT_MODE);
00122                 cleared = 1;
00123             }
00124         }        
00125         stdio_mutex.unlock();
00126         Thread::wait(1);
00127     }
00128 }
00129 
00130 
00131 
00132 
00133 int main()
00134 {
00135     TS_StateTypeDef TS_State;
00136     uint16_t x, y;
00137     uint8_t text[30];
00138     uint8_t status;
00139     uint8_t idx;
00140     uint8_t cleared = 0;
00141     uint8_t prev_nb_touches = 0;
00142     
00143 //    RtosTimer lcd_r_timer(fillcircle, osTimerPeriodic, (void *)0);
00144 //    RtosTimer lcd_r_timer(fillcircle(r*0x00010000,100,150,30), osTimerPeriodic);
00145 
00146 
00147     
00148 
00149     lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
00150     wait(1);
00151 
00152     status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
00153     if (status != TS_OK) {
00154         lcd.Clear(LCD_COLOR_RED);
00155         lcd.SetBackColor(LCD_COLOR_RED);
00156         lcd.SetTextColor(LCD_COLOR_WHITE);
00157         lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
00158     } else {
00159         lcd.Clear(LCD_COLOR_GREEN);
00160         lcd.SetBackColor(LCD_COLOR_GREEN);
00161         lcd.SetTextColor(LCD_COLOR_WHITE);
00162         lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
00163     }
00164 
00165     wait(1);
00166     lcd.SetFont(&Font24);
00167     lcd.SetBackColor(LCD_COLOR_BLACK);
00168     lcd.SetTextColor(LCD_COLOR_WHITE);
00169     
00170 //    thread.start(lcd, color_cng);
00171 //    Thread::wait(5000);
00172 //    running = false;
00173 //    thread.join();
00174 
00175 //    lcd_r_timer.start(100);
00176     Thread thread1(lcd_r_thread);
00177     Thread thread2(lcd_g_thread);
00178     Thread thread3(lcd_b_thread);
00179     Thread thread4(RGBtext);
00180     Thread thread5(TPtext);
00181     
00182     while(1) {
00183         
00184 /*        
00185         lcd.SetTextColor(r*0x00010000|0xFF000000);
00186         lcd.FillCircle(100,150,100);
00187         lcd.SetTextColor(g*0x00000100|0xFF000000);
00188         lcd.FillCircle(200,150,100);
00189         lcd.SetTextColor(b|0xFF000000);
00190         lcd.FillCircle(300,150,100);
00191 */
00192 //        draw.fillcircle(r*0x00010000,100,150,30);
00193 //        lcd.SetTextColor(r*0x00010000|0xFF000000);
00194 //        lcd.FillCircle(100,150,30);
00195 //        lcd.SetTextColor(g*0x00000100|0xFF000000);
00196 //        lcd.FillCircle(200,150,30);
00197 //        lcd.SetTextColor(b|0xFF000000);
00198 //        lcd.FillCircle(300,150,30);
00199 /*
00200         lcd.SetFont(&Font24);
00201         lcd.SetTextColor(LCD_COLOR_WHITE);
00202         sprintf((char*)text, "R:0x%x",r);
00203         lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
00204         sprintf((char*)text, "G:0x%x",g);
00205         lcd.DisplayStringAt(0, LINE(1), (uint8_t *)&text, LEFT_MODE);
00206         sprintf((char*)text, "B:0x%x",b);
00207         lcd.DisplayStringAt(0, LINE(2), (uint8_t *)&text, LEFT_MODE);
00208 */
00209 /*        lcd.SetFont(&Font12);
00210 //        lcd.SetBackColor(~col|0xFF000000);
00211 //        lcd.SetTextColor(col|0xFF000000);
00212 
00213         
00214         
00215         
00216         
00217         ts.GetState(&TS_State);
00218         if (TS_State.touchDetected) {
00219             // Clear lines corresponding to old touches coordinates
00220             if (TS_State.touchDetected < prev_nb_touches) {
00221                 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
00222                     lcd.ClearStringLine(idx);
00223                 }
00224             }
00225             prev_nb_touches = TS_State.touchDetected;
00226 
00227             cleared = 0;
00228 
00229             sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
00230             lcd.DisplayStringAt(150, LINE(0), (uint8_t *)&text, LEFT_MODE);
00231 
00232             for (idx = 0; idx < TS_State.touchDetected; idx++) {
00233                 x = TS_State.touchX[idx];
00234                 y = TS_State.touchY[idx];
00235                 sprintf((char*)text, "Touch %d: x=%d y=%d    ", idx+1, x, y);
00236                 lcd.DisplayStringAt(150, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
00237             }
00238 
00239             lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
00240         } else {
00241             if (!cleared) {
00242                 lcd.Clear(LCD_COLOR_BLACK);
00243                 sprintf((char*)text, "Touches: 0");
00244                 lcd.DisplayStringAt(150, LINE(0), (uint8_t *)&text, LEFT_MODE);
00245                 cleared = 1;
00246             }
00247         }*/
00248     }
00249 }
00250 
00251