Thread

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed

Fork of DISCO-F746NG_LCDTS_demo by ST

Committer:
mekai
Date:
Thu Dec 22 05:57:00 2016 +0000
Revision:
2:5ca8733edc78
Parent:
0:9933f7db9a9b
161222_?????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:9933f7db9a9b 1 #include "mbed.h"
bcostm 0:9933f7db9a9b 2 #include "TS_DISCO_F746NG.h"
bcostm 0:9933f7db9a9b 3 #include "LCD_DISCO_F746NG.h"
mekai 2:5ca8733edc78 4 #include "Thread.h"
mekai 2:5ca8733edc78 5 #include "rtos.h"
mekai 2:5ca8733edc78 6
mekai 2:5ca8733edc78 7
bcostm 0:9933f7db9a9b 8
bcostm 0:9933f7db9a9b 9 LCD_DISCO_F746NG lcd;
bcostm 0:9933f7db9a9b 10 TS_DISCO_F746NG ts;
mekai 2:5ca8733edc78 11 Mutex stdio_mutex;
mekai 2:5ca8733edc78 12 uint8_t r=0,g=0,b=0;
mekai 2:5ca8733edc78 13
mekai 2:5ca8733edc78 14
mekai 2:5ca8733edc78 15
mekai 2:5ca8733edc78 16
mekai 2:5ca8733edc78 17
mekai 2:5ca8733edc78 18 class DRAW //クラス宣言
mekai 2:5ca8733edc78 19 {
mekai 2:5ca8733edc78 20 private:
mekai 2:5ca8733edc78 21 uint32_t color;
mekai 2:5ca8733edc78 22 uint16_t Xpos;
mekai 2:5ca8733edc78 23 uint16_t Ypos;
mekai 2:5ca8733edc78 24 uint16_t Rad;
mekai 2:5ca8733edc78 25 public:
mekai 2:5ca8733edc78 26 void fillcircle(uint32_t col,uint16_t x,uint16_t y,uint16_t rad);
mekai 2:5ca8733edc78 27 };
mekai 2:5ca8733edc78 28
mekai 2:5ca8733edc78 29
mekai 2:5ca8733edc78 30 void DRAW::fillcircle(uint32_t col,uint16_t x,uint16_t y,uint16_t rad){
mekai 2:5ca8733edc78 31 lcd.SetTextColor(col|0xFF000000);
mekai 2:5ca8733edc78 32 lcd.FillCircle(x,y,rad);
mekai 2:5ca8733edc78 33 }
mekai 2:5ca8733edc78 34
mekai 2:5ca8733edc78 35
mekai 2:5ca8733edc78 36 DRAW draw;
mekai 2:5ca8733edc78 37
mekai 2:5ca8733edc78 38 void lcd_r_thread(void const *args) {
mekai 2:5ca8733edc78 39 while (true) {
mekai 2:5ca8733edc78 40 stdio_mutex.lock();
mekai 2:5ca8733edc78 41 draw.fillcircle(r*0x00010000,100,150,30);
mekai 2:5ca8733edc78 42 stdio_mutex.unlock();
mekai 2:5ca8733edc78 43 r++;
mekai 2:5ca8733edc78 44 Thread::wait(20);
mekai 2:5ca8733edc78 45 }
mekai 2:5ca8733edc78 46 }
mekai 2:5ca8733edc78 47 void lcd_g_thread(void const *args) {
mekai 2:5ca8733edc78 48 while (true) {
mekai 2:5ca8733edc78 49 stdio_mutex.lock();
mekai 2:5ca8733edc78 50 draw.fillcircle(g*0x00000100,200,150,30);
mekai 2:5ca8733edc78 51 stdio_mutex.unlock();
mekai 2:5ca8733edc78 52 g++;
mekai 2:5ca8733edc78 53 Thread::wait(40);
mekai 2:5ca8733edc78 54 }
mekai 2:5ca8733edc78 55 }
mekai 2:5ca8733edc78 56 void lcd_b_thread(void const *args) {
mekai 2:5ca8733edc78 57 while (true) {
mekai 2:5ca8733edc78 58 stdio_mutex.lock();
mekai 2:5ca8733edc78 59 draw.fillcircle(b,300,150,30);
mekai 2:5ca8733edc78 60 stdio_mutex.unlock();
mekai 2:5ca8733edc78 61 b++;
mekai 2:5ca8733edc78 62 Thread::wait(60);
mekai 2:5ca8733edc78 63 }
mekai 2:5ca8733edc78 64 }
mekai 2:5ca8733edc78 65 void RGBtext(void const *args) {
mekai 2:5ca8733edc78 66 uint8_t text[30];
mekai 2:5ca8733edc78 67 while (true) {
mekai 2:5ca8733edc78 68 stdio_mutex.lock();
mekai 2:5ca8733edc78 69 lcd.SetFont(&Font24);
mekai 2:5ca8733edc78 70 lcd.SetTextColor(LCD_COLOR_WHITE);
mekai 2:5ca8733edc78 71 sprintf((char*)text, "R:0x%x",r);
mekai 2:5ca8733edc78 72 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
mekai 2:5ca8733edc78 73 sprintf((char*)text, "G:0x%x",g);
mekai 2:5ca8733edc78 74 lcd.DisplayStringAt(0, LINE(1), (uint8_t *)&text, LEFT_MODE);
mekai 2:5ca8733edc78 75 sprintf((char*)text, "B:0x%x",b);
mekai 2:5ca8733edc78 76 lcd.DisplayStringAt(0, LINE(2), (uint8_t *)&text, LEFT_MODE);
mekai 2:5ca8733edc78 77 stdio_mutex.unlock();
mekai 2:5ca8733edc78 78 Thread::wait(100);
mekai 2:5ca8733edc78 79 }
mekai 2:5ca8733edc78 80 }
mekai 2:5ca8733edc78 81 void TPtext(void const *args) {
mekai 2:5ca8733edc78 82 TS_StateTypeDef TS_State;
mekai 2:5ca8733edc78 83 uint16_t x, y;
mekai 2:5ca8733edc78 84 uint8_t text[30];
mekai 2:5ca8733edc78 85 uint8_t status;
mekai 2:5ca8733edc78 86 uint8_t idx;
mekai 2:5ca8733edc78 87 uint8_t cleared = 0;
mekai 2:5ca8733edc78 88 uint8_t prev_nb_touches = 0;
mekai 2:5ca8733edc78 89 while (true) {
mekai 2:5ca8733edc78 90 stdio_mutex.lock();
mekai 2:5ca8733edc78 91 lcd.SetFont(&Font12);
mekai 2:5ca8733edc78 92 lcd.SetTextColor(LCD_COLOR_WHITE);
mekai 2:5ca8733edc78 93
mekai 2:5ca8733edc78 94 ts.GetState(&TS_State);
mekai 2:5ca8733edc78 95 if (TS_State.touchDetected) {
mekai 2:5ca8733edc78 96 // Clear lines corresponding to old touches coordinates
mekai 2:5ca8733edc78 97 if (TS_State.touchDetected < prev_nb_touches) {
mekai 2:5ca8733edc78 98 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
mekai 2:5ca8733edc78 99 lcd.ClearStringLine(idx);
mekai 2:5ca8733edc78 100 }
mekai 2:5ca8733edc78 101 }
mekai 2:5ca8733edc78 102 prev_nb_touches = TS_State.touchDetected;
mekai 2:5ca8733edc78 103
mekai 2:5ca8733edc78 104 cleared = 0;
mekai 2:5ca8733edc78 105
mekai 2:5ca8733edc78 106 sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
mekai 2:5ca8733edc78 107 lcd.DisplayStringAt(150, LINE(0), (uint8_t *)&text, LEFT_MODE);
mekai 2:5ca8733edc78 108
mekai 2:5ca8733edc78 109 for (idx = 0; idx < TS_State.touchDetected; idx++) {
mekai 2:5ca8733edc78 110 x = TS_State.touchX[idx];
mekai 2:5ca8733edc78 111 y = TS_State.touchY[idx];
mekai 2:5ca8733edc78 112 sprintf((char*)text, "Touch %d: x=%d y=%d ", idx+1, x, y);
mekai 2:5ca8733edc78 113 lcd.DisplayStringAt(150, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
mekai 2:5ca8733edc78 114 }
mekai 2:5ca8733edc78 115
mekai 2:5ca8733edc78 116 lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
mekai 2:5ca8733edc78 117 } else {
mekai 2:5ca8733edc78 118 if (!cleared) {
mekai 2:5ca8733edc78 119 lcd.Clear(LCD_COLOR_BLACK);
mekai 2:5ca8733edc78 120 sprintf((char*)text, "Touches: 0");
mekai 2:5ca8733edc78 121 lcd.DisplayStringAt(150, LINE(0), (uint8_t *)&text, LEFT_MODE);
mekai 2:5ca8733edc78 122 cleared = 1;
mekai 2:5ca8733edc78 123 }
mekai 2:5ca8733edc78 124 }
mekai 2:5ca8733edc78 125 stdio_mutex.unlock();
mekai 2:5ca8733edc78 126 Thread::wait(1);
mekai 2:5ca8733edc78 127 }
mekai 2:5ca8733edc78 128 }
mekai 2:5ca8733edc78 129
mekai 2:5ca8733edc78 130
mekai 2:5ca8733edc78 131
bcostm 0:9933f7db9a9b 132
bcostm 0:9933f7db9a9b 133 int main()
bcostm 0:9933f7db9a9b 134 {
bcostm 0:9933f7db9a9b 135 TS_StateTypeDef TS_State;
bcostm 0:9933f7db9a9b 136 uint16_t x, y;
bcostm 0:9933f7db9a9b 137 uint8_t text[30];
bcostm 0:9933f7db9a9b 138 uint8_t status;
bcostm 0:9933f7db9a9b 139 uint8_t idx;
bcostm 0:9933f7db9a9b 140 uint8_t cleared = 0;
bcostm 0:9933f7db9a9b 141 uint8_t prev_nb_touches = 0;
mekai 2:5ca8733edc78 142
mekai 2:5ca8733edc78 143 // RtosTimer lcd_r_timer(fillcircle, osTimerPeriodic, (void *)0);
mekai 2:5ca8733edc78 144 // RtosTimer lcd_r_timer(fillcircle(r*0x00010000,100,150,30), osTimerPeriodic);
mekai 2:5ca8733edc78 145
mekai 2:5ca8733edc78 146
mekai 2:5ca8733edc78 147
bcostm 0:9933f7db9a9b 148
bcostm 0:9933f7db9a9b 149 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
bcostm 0:9933f7db9a9b 150 wait(1);
bcostm 0:9933f7db9a9b 151
bcostm 0:9933f7db9a9b 152 status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
bcostm 0:9933f7db9a9b 153 if (status != TS_OK) {
bcostm 0:9933f7db9a9b 154 lcd.Clear(LCD_COLOR_RED);
bcostm 0:9933f7db9a9b 155 lcd.SetBackColor(LCD_COLOR_RED);
bcostm 0:9933f7db9a9b 156 lcd.SetTextColor(LCD_COLOR_WHITE);
bcostm 0:9933f7db9a9b 157 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
bcostm 0:9933f7db9a9b 158 } else {
bcostm 0:9933f7db9a9b 159 lcd.Clear(LCD_COLOR_GREEN);
bcostm 0:9933f7db9a9b 160 lcd.SetBackColor(LCD_COLOR_GREEN);
bcostm 0:9933f7db9a9b 161 lcd.SetTextColor(LCD_COLOR_WHITE);
bcostm 0:9933f7db9a9b 162 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
bcostm 0:9933f7db9a9b 163 }
bcostm 0:9933f7db9a9b 164
bcostm 0:9933f7db9a9b 165 wait(1);
mekai 2:5ca8733edc78 166 lcd.SetFont(&Font24);
mekai 2:5ca8733edc78 167 lcd.SetBackColor(LCD_COLOR_BLACK);
bcostm 0:9933f7db9a9b 168 lcd.SetTextColor(LCD_COLOR_WHITE);
mekai 2:5ca8733edc78 169
mekai 2:5ca8733edc78 170 // thread.start(lcd, color_cng);
mekai 2:5ca8733edc78 171 // Thread::wait(5000);
mekai 2:5ca8733edc78 172 // running = false;
mekai 2:5ca8733edc78 173 // thread.join();
bcostm 0:9933f7db9a9b 174
mekai 2:5ca8733edc78 175 // lcd_r_timer.start(100);
mekai 2:5ca8733edc78 176 Thread thread1(lcd_r_thread);
mekai 2:5ca8733edc78 177 Thread thread2(lcd_g_thread);
mekai 2:5ca8733edc78 178 Thread thread3(lcd_b_thread);
mekai 2:5ca8733edc78 179 Thread thread4(RGBtext);
mekai 2:5ca8733edc78 180 Thread thread5(TPtext);
mekai 2:5ca8733edc78 181
bcostm 0:9933f7db9a9b 182 while(1) {
mekai 2:5ca8733edc78 183
mekai 2:5ca8733edc78 184 /*
mekai 2:5ca8733edc78 185 lcd.SetTextColor(r*0x00010000|0xFF000000);
mekai 2:5ca8733edc78 186 lcd.FillCircle(100,150,100);
mekai 2:5ca8733edc78 187 lcd.SetTextColor(g*0x00000100|0xFF000000);
mekai 2:5ca8733edc78 188 lcd.FillCircle(200,150,100);
mekai 2:5ca8733edc78 189 lcd.SetTextColor(b|0xFF000000);
mekai 2:5ca8733edc78 190 lcd.FillCircle(300,150,100);
mekai 2:5ca8733edc78 191 */
mekai 2:5ca8733edc78 192 // draw.fillcircle(r*0x00010000,100,150,30);
mekai 2:5ca8733edc78 193 // lcd.SetTextColor(r*0x00010000|0xFF000000);
mekai 2:5ca8733edc78 194 // lcd.FillCircle(100,150,30);
mekai 2:5ca8733edc78 195 // lcd.SetTextColor(g*0x00000100|0xFF000000);
mekai 2:5ca8733edc78 196 // lcd.FillCircle(200,150,30);
mekai 2:5ca8733edc78 197 // lcd.SetTextColor(b|0xFF000000);
mekai 2:5ca8733edc78 198 // lcd.FillCircle(300,150,30);
mekai 2:5ca8733edc78 199 /*
mekai 2:5ca8733edc78 200 lcd.SetFont(&Font24);
mekai 2:5ca8733edc78 201 lcd.SetTextColor(LCD_COLOR_WHITE);
mekai 2:5ca8733edc78 202 sprintf((char*)text, "R:0x%x",r);
mekai 2:5ca8733edc78 203 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
mekai 2:5ca8733edc78 204 sprintf((char*)text, "G:0x%x",g);
mekai 2:5ca8733edc78 205 lcd.DisplayStringAt(0, LINE(1), (uint8_t *)&text, LEFT_MODE);
mekai 2:5ca8733edc78 206 sprintf((char*)text, "B:0x%x",b);
mekai 2:5ca8733edc78 207 lcd.DisplayStringAt(0, LINE(2), (uint8_t *)&text, LEFT_MODE);
mekai 2:5ca8733edc78 208 */
mekai 2:5ca8733edc78 209 /* lcd.SetFont(&Font12);
mekai 2:5ca8733edc78 210 // lcd.SetBackColor(~col|0xFF000000);
mekai 2:5ca8733edc78 211 // lcd.SetTextColor(col|0xFF000000);
bcostm 0:9933f7db9a9b 212
mekai 2:5ca8733edc78 213
mekai 2:5ca8733edc78 214
mekai 2:5ca8733edc78 215
mekai 2:5ca8733edc78 216
bcostm 0:9933f7db9a9b 217 ts.GetState(&TS_State);
bcostm 0:9933f7db9a9b 218 if (TS_State.touchDetected) {
bcostm 0:9933f7db9a9b 219 // Clear lines corresponding to old touches coordinates
bcostm 0:9933f7db9a9b 220 if (TS_State.touchDetected < prev_nb_touches) {
bcostm 0:9933f7db9a9b 221 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
bcostm 0:9933f7db9a9b 222 lcd.ClearStringLine(idx);
bcostm 0:9933f7db9a9b 223 }
bcostm 0:9933f7db9a9b 224 }
bcostm 0:9933f7db9a9b 225 prev_nb_touches = TS_State.touchDetected;
bcostm 0:9933f7db9a9b 226
bcostm 0:9933f7db9a9b 227 cleared = 0;
bcostm 0:9933f7db9a9b 228
bcostm 0:9933f7db9a9b 229 sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
mekai 2:5ca8733edc78 230 lcd.DisplayStringAt(150, LINE(0), (uint8_t *)&text, LEFT_MODE);
bcostm 0:9933f7db9a9b 231
bcostm 0:9933f7db9a9b 232 for (idx = 0; idx < TS_State.touchDetected; idx++) {
bcostm 0:9933f7db9a9b 233 x = TS_State.touchX[idx];
bcostm 0:9933f7db9a9b 234 y = TS_State.touchY[idx];
bcostm 0:9933f7db9a9b 235 sprintf((char*)text, "Touch %d: x=%d y=%d ", idx+1, x, y);
mekai 2:5ca8733edc78 236 lcd.DisplayStringAt(150, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
bcostm 0:9933f7db9a9b 237 }
bcostm 0:9933f7db9a9b 238
bcostm 0:9933f7db9a9b 239 lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
bcostm 0:9933f7db9a9b 240 } else {
bcostm 0:9933f7db9a9b 241 if (!cleared) {
mekai 2:5ca8733edc78 242 lcd.Clear(LCD_COLOR_BLACK);
bcostm 0:9933f7db9a9b 243 sprintf((char*)text, "Touches: 0");
mekai 2:5ca8733edc78 244 lcd.DisplayStringAt(150, LINE(0), (uint8_t *)&text, LEFT_MODE);
bcostm 0:9933f7db9a9b 245 cleared = 1;
bcostm 0:9933f7db9a9b 246 }
mekai 2:5ca8733edc78 247 }*/
bcostm 0:9933f7db9a9b 248 }
bcostm 0:9933f7db9a9b 249 }
mekai 2:5ca8733edc78 250
mekai 2:5ca8733edc78 251