Draw Poker
Draw Poker for the Arduino Mega with a 800x480 IPS Touchscreen. Includes full Graphical cards which are stored on the Micro SD card.
Components and supplies
1
4 Inch IPS Touchscreen for the Arduino Mega
1
Micro SD card
1
Arduino Mega 2560
Apps and platforms
1
Arduino IDE
Project description
Code
Deck of cards in 16 Bit 565 Colour.
snippets
Unzip the file and copy the 53 BMP's to a Micro SD card
1inary file (no preview
5 Card Draw Poker
arduino
Use Arduino IDE to compile it.
1// Only Runs on Mega. Uses 6k Ram 2// BMP Must be 154x220 3// BMP Must be 16 Bit 565 Color 4// 5 6 7#include <SPI.h> // SPI Library 8#include <SdFat.h> // SD Card Library 9 10#include <LCDWIKI_GUI.h> // Core graphics library 11#include <LCDWIKI_KBV.h> // Hardware-specific library 12#include <XPT2046_Touchscreen.h> // Hardware SPI touch library 13 14 15LCDWIKI_KBV my_lcd(NT35510, 40, 38, 39, 43, 41); // model,cs,cd,wr,rd,reset 4" 800x480 16 Bit Mega 16XPT2046_Touchscreen ts(53); // Touch Library CS Pin 17SdFat SD; 18 19#define BLACK 0x0000 20#define BLUE 0x001F 21#define RED 0xF800 22#define GREEN 0x07E0 23#define CYAN 0x07FF 24#define MAGENTA 0xF81F 25#define YELLOW 0xFFE0 26#define WHITE 0xFFFF 27 28#define FILE_NUMBER 53 29#define FILE_NAME_SIZE_MAX 20 30 31 32 33typedef struct button_push 34{ 35 int16_t posx1; 36 int16_t posy1; 37 int16_t posx2; 38 int16_t posy2; 39}; 40 41button_push button[11]={ // Button Calibration Data 42 43{141, 1769, 863, 3146}, // Button1 44{896, 1724, 1667, 3148}, // Button2 45{1690, 1730, 2432, 3101}, // Button3 46{2458, 1727, 3197, 3094}, // Button4 47{3239, 1725, 3957, 3103}, // Button5 48{146, 1387, 877, 1743}, // Button6 49{889, 1362, 1666, 1730}, // Button7 50{1702, 1359, 2431, 1724}, // Button8 51{2468, 1375, 3214, 1688}, // Button9 52{3239, 1338, 3942, 1691}, // Button10 53{2770, 3421, 3958, 3811}, // Button11 54 55}; 56 57 58 59char file_name[FILE_NUMBER][FILE_NAME_SIZE_MAX]; 60uint32_t bmp_offset; 61uint8_t pushed; 62uint16_t start = 5; 63uint8_t cards[53]; 64uint8_t card_face[5]; 65char card_suit[5]; 66uint8_t hold[5]; 67String winner; 68uint8_t bet = 5; // Starting bet 69uint8_t demo = 0; // Demo mode 70int16_t credit = 100; 71uint16_t won; 72uint16_t x1; 73uint16_t y1; 74uint16_t x2; 75uint16_t y2; 76uint8_t z; 77 78 79void setup() 80{ 81 Serial.begin(9600); 82 83 my_lcd.reset(); 84 my_lcd.Init_LCD(); 85 my_lcd.Set_Rotation(1); 86 ts.begin(); 87 ts.setRotation(3); 88 89// calib(); // Calibrate Touch Screen 90// test_buttons(); // Test Button Calibration 91 92 my_lcd.Fill_Screen(RED); 93 my_lcd.Fill_Rect(5, 5, 790, 190, BLUE); 94 my_lcd.Fill_Rect(318, 0, 5, 195, WHITE); 95 my_lcd.Fill_Rect(636, 0, 5, 195, WHITE); 96 my_lcd.Fill_Rect(0, 425, 800, 55, BLUE); 97 my_lcd.Fill_Rect(0, 150, 800, 5, WHITE); 98 my_lcd.Fill_Rect(0, 0, 800, 5, WHITE); 99 my_lcd.Fill_Rect(0, 0, 5, 195, WHITE); 100 my_lcd.Fill_Rect(795, 0, 800, 195, WHITE); 101 my_lcd.Fill_Rect(0, 155, 800, 40, BLACK); 102 103 my_lcd.Set_Text_colour(YELLOW); 104 my_lcd.Set_Text_Back_colour(BLUE); 105 my_lcd.Set_Text_Size(2); 106 my_lcd.Print_String("ROYAL FLUSH........", 10, 10); 107 my_lcd.Print_String("STRAIGHT FLUSH.....", 10, 40); 108 my_lcd.Print_String("FOUR OF A KIND.....", 10, 70); 109 my_lcd.Print_String("FULL HOUSE.........", 10, 100); 110 my_lcd.Print_String("FLUSH..............", 10, 130); 111 my_lcd.Print_String("STRAIGHT...........", 328, 10); 112 my_lcd.Print_String("THREE OF A KIND....", 328, 40); 113 my_lcd.Print_String("TWO PAIR...........", 328, 70); 114 my_lcd.Print_String("JACKS OR BETTER....", 328, 100); 115 my_lcd.Set_Text_Size(3); 116 my_lcd.Print_String("BET 1", 685, 15); 117 my_lcd.Print_String("CREDIT", 670, 70); 118 my_lcd.Print_Number_Int(credit, 680, 110, 6, 32, 10); 119 my_lcd.Print_Number_Int(bet, 760, 15, 2, 32, 10); 120 121 showbet(); 122 123 randomSeed(analogRead(0)); 124 for ( int a = 1; a < 53; a++) { cards[a] = a; } 125 126 { 127 strcpy(file_name[0], "back.bmp"); 128 strcpy(file_name[1], "14H.bmp"); 129 strcpy(file_name[2], "14D.bmp"); 130 strcpy(file_name[3], "14C.bmp"); 131 strcpy(file_name[4], "14S.bmp"); 132 strcpy(file_name[5], "13H.bmp"); 133 strcpy(file_name[6], "13D.bmp"); 134 strcpy(file_name[7], "13C.bmp"); 135 strcpy(file_name[8], "13S.bmp"); 136 strcpy(file_name[9], "12H.bmp"); 137 strcpy(file_name[10], "12D.bmp"); 138 strcpy(file_name[11], "12C.bmp"); 139 strcpy(file_name[12], "12S.bmp"); 140 strcpy(file_name[13], "11H.bmp"); 141 strcpy(file_name[14], "11D.bmp"); 142 strcpy(file_name[15], "11C.bmp"); 143 strcpy(file_name[16], "11S.bmp"); 144 strcpy(file_name[17], "10H.bmp"); 145 strcpy(file_name[18], "10D.bmp"); 146 strcpy(file_name[19], "10C.bmp"); 147 strcpy(file_name[20], "10S.bmp"); 148 strcpy(file_name[21], "09H.bmp"); 149 strcpy(file_name[22], "09D.bmp"); 150 strcpy(file_name[23], "09C.bmp"); 151 strcpy(file_name[24], "09S.bmp"); 152 strcpy(file_name[25], "08H.bmp"); 153 strcpy(file_name[26], "08D.bmp"); 154 strcpy(file_name[27], "08C.bmp"); 155 strcpy(file_name[28], "08S.bmp"); 156 strcpy(file_name[29], "07H.bmp"); 157 strcpy(file_name[30], "07D.bmp"); 158 strcpy(file_name[31], "07C.bmp"); 159 strcpy(file_name[32], "07S.bmp"); 160 strcpy(file_name[33], "06H.bmp"); 161 strcpy(file_name[34], "06D.bmp"); 162 strcpy(file_name[35], "06C.bmp"); 163 strcpy(file_name[36], "06S.bmp"); 164 strcpy(file_name[37], "05H.bmp"); 165 strcpy(file_name[38], "05D.bmp"); 166 strcpy(file_name[39], "05C.bmp"); 167 strcpy(file_name[40], "05S.bmp"); 168 strcpy(file_name[41], "04H.bmp"); 169 strcpy(file_name[42], "04D.bmp"); 170 strcpy(file_name[43], "04C.bmp"); 171 strcpy(file_name[44], "04S.bmp"); 172 strcpy(file_name[45], "03H.bmp"); 173 strcpy(file_name[46], "03D.bmp"); 174 strcpy(file_name[47], "03C.bmp"); 175 strcpy(file_name[48], "03S.bmp"); 176 strcpy(file_name[49], "02H.bmp"); 177 strcpy(file_name[50], "02D.bmp"); 178 strcpy(file_name[51], "02C.bmp"); 179 strcpy(file_name[52], "02S.bmp"); 180 } 181 182 if (!SD.begin(48)) { 183 my_lcd.Set_Text_Back_colour(BLUE); 184 my_lcd.Set_Text_colour(WHITE); 185 my_lcd.Set_Text_Size(2); 186 my_lcd.Print_String("SD Card Init fail!", 0, 300); 187 } 188} 189 190 191void loop() 192{ 193 uint8_t c; 194 shuffle(); 195 196 showcard(0, 5); 197 showcard(0, 164); 198 showcard(0, 323); 199 showcard(0, 482); 200 showcard(0, 641); 201 dodeal(); 202 203 showcard(cards[1], 5); card_face[0] = atoi(file_name[cards[1]]); card_suit[0] = file_name[cards[1]][2]; 204 showcard(cards[2], 164); card_face[1] = atoi(file_name[cards[2]]); card_suit[1] = file_name[cards[2]][2]; 205 showcard(cards[3], 323); card_face[2] = atoi(file_name[cards[3]]); card_suit[2] = file_name[cards[3]][2]; 206 showcard(cards[4], 482); card_face[3] = atoi(file_name[cards[4]]); card_suit[3] = file_name[cards[4]][2]; 207 showcard(cards[5], 641); card_face[4] = atoi(file_name[cards[5]]); card_suit[4] = file_name[cards[5]][2]; 208 c = 6; 209 dohold(); 210 211 if (hold[0] == 0) { showcard(0, 5); } 212 if (hold[1] == 0) { showcard(0, 164); } 213 if (hold[2] == 0) { showcard(0, 323); } 214 if (hold[3] == 0) { showcard(0, 482); } 215 if (hold[4] == 0) { showcard(0, 641); } 216 217 if (hold[0] == 0) { showcard(cards[c], 5); 218 card_face[0] = atoi(file_name[cards[c]]); 219 card_suit[0] = file_name[cards[c]][2]; 220 c = c + 1; 221 } 222 if (hold[1] == 0) { showcard(cards[c], 164); 223 card_face[1] = atoi(file_name[cards[c]]); 224 card_suit[1] = file_name[cards[c]][2]; 225 c = c + 1; 226 } 227 if (hold[2] == 0) { showcard(cards[c], 323); 228 card_face[2] = atoi(file_name[cards[c]]); 229 card_suit[2] = file_name[cards[c]][2]; 230 c = c + 1; 231 } 232 if (hold[3] == 0) { showcard(cards[c], 482); 233 card_face[3] = atoi(file_name[cards[c]]); 234 card_suit[3] = file_name[cards[c]][2]; 235 c = c + 1; 236 } 237 if (hold[4] == 0) { showcard(cards[c], 641); 238 card_face[4] = atoi(file_name[cards[c]]); 239 card_suit[4] = file_name[cards[c]][2]; 240 c = c + 1; 241 } 242 243 dosort(); 244 checkcards(); 245 dowin(); 246 247} 248 249 250void showbet() 251{ 252 my_lcd.Set_Text_Size(2); 253 if (bet < 5) { my_lcd.Print_Number_Int(250 * bet, 250, 10, 5, 32, 10); } 254 if (bet == 5) { my_lcd.Print_Number_Int(4000, 250, 10, 5, 32, 10); } 255 my_lcd.Print_Number_Int(50 * bet, 250, 40, 5, 32, 10); 256 my_lcd.Print_Number_Int(25 * bet, 250, 70, 5, 32, 10); 257 my_lcd.Print_Number_Int(9 * bet, 250, 100, 5, 32, 10); 258 my_lcd.Print_Number_Int(6 * bet, 250, 130, 5, 32, 10); 259 my_lcd.Print_Number_Int(4 * bet, 550, 10, 5, 32, 10); 260 my_lcd.Print_Number_Int(3 * bet, 550, 40, 5, 32, 10); 261 my_lcd.Print_Number_Int(2 * bet, 550, 70, 5, 32, 10); 262 my_lcd.Print_Number_Int(1 * bet, 550, 100, 5, 32, 10); 263 my_lcd.Set_Text_Size(3); 264} 265 266 267void checkcards() 268{ 269 if ( card_face[4] == 14 && card_face[3] == 13 && card_face[2] == 12 && card_face[1] == 11 && card_face[0] == 10 270 && card_suit[4] == card_suit[3] && card_suit[3] == card_suit[2] && card_suit[2] == card_suit[1] && card_suit[1] == card_suit[0]) { 271 winner = "ROYAL FLUSH"; 272 won = 250 * bet; 273 if (bet == 5) { won = 4000; } 274 credit += won; 275 return (1); } 276 277 if (card_face[4] == card_face[3] + 1 && card_face[3] + 1 == card_face[2] + 2 && card_face[2] + 2 == card_face[1] + 3 && card_face[1] + 3 == card_face[0] + 4 && card_suit[4] == card_suit[3] && card_suit[3] == card_suit[2] && card_suit[2] == card_suit[1] && card_suit[1] == card_suit[0]) { 278 winner = "STRAIGHT FLUSH"; 279 won = 50 * bet; 280 credit += won; 281 return (1); 282 } 283 284 if (card_face[4] == 14 && card_face[0] == 2 && card_face[1] == 3 && card_face[2] == 4 && card_face[3] == 5 && card_suit[4] == card_suit[3] && card_suit[3] == card_suit[2] && card_suit[2] == card_suit[1] && card_suit[1] == card_suit[0]) { 285 winner = "STRAIGHT FLUSH"; 286 won = 50 * bet; 287 credit += won; 288 return (1); 289 } 290 291 if ((card_face[0] == card_face[1] && card_face[1] == card_face[2] && card_face[2] == card_face[3]) || 292 (card_face[1] == card_face[2] && card_face[2] == card_face[3] && card_face[3] == card_face[4])) { 293 winner = "FOUR OF A KIND"; 294 won = 25 * bet; 295 credit += won; 296 return (1); 297 } 298 299 if ((card_face[0] == card_face[1] && card_face[1] == card_face[2] && card_face[3] == card_face[4]) || 300 (card_face[0] == card_face[1] && card_face[2] == card_face[3] && card_face[3] == card_face[4])) { 301 winner = "FULL HOUSE"; 302 won = 9 * bet; 303 credit += won; 304 return (1); 305 } 306 307 if (card_suit[0] == card_suit[1] && card_suit[1] == card_suit[2] && card_suit[2] == card_suit[3] && card_suit[3] == card_suit[4]) { 308 winner = "FLUSH"; 309 won = 6 * bet; 310 credit += won; 311 return (1); 312 } 313 314 if (card_face[4] == card_face[3] + 1 && card_face[3] + 1 == card_face[2] + 2 && card_face[2] + 2 == card_face[1] + 3 && card_face[1] + 3 == card_face[0] + 4) { 315 winner = "STRAIGHT"; 316 won = 4 * bet; 317 credit += won; 318 return (1); 319 } 320 321 if (card_face[4] == 14 && card_face[0] == 2 && card_face[1] == 3 && card_face[2] == 4 && card_face[3]) { 322 winner = "STRAIGHT"; 323 won = 4 * bet; 324 credit += won; 325 return (1); 326 } 327 328 for (int i = 0; i < 3; ++i) 329 { 330 if ((card_face[i] == card_face[i + 1]) && (card_face[i + 1] == card_face[i + 2])) { 331 winner = "THREE OF A KIND"; 332 won = 3 * bet; 333 credit += won; 334 return (1); 335 } 336 } 337 338 if ((card_face[0] == card_face[1] && card_face[2] == card_face[3]) || 339 (card_face[1] == card_face[2] && card_face[3] == card_face[4]) || 340 (card_face[0] == card_face[1] && card_face[3] == card_face[4])) { 341 winner = "TWO PAIR"; 342 won = 2 * bet; 343 credit += won; 344 return (1); 345 } 346 347 for (int i = 0; i < 4; i++) 348 { 349 if (card_face[i] == card_face[i + 1] && card_face[i] > 10) { 350 winner = "JACKS OR BETTER"; 351 won = 1 * bet; 352 credit += won; 353 return (1); 354 } 355 } 356 won = 0; 357 winner = ""; 358} 359 360 361void dosort() 362{ 363 uint8_t c = 0; 364 uint8_t a; 365 char b; 366 367 while (c == 0) { 368 c = 1; 369 if (card_face[0] > card_face[1]) { 370 a = card_face[1]; card_face[1] = card_face[0]; card_face[0] = a; 371 b = card_suit[1]; card_suit[1] = card_suit[0]; card_suit[0] = b; 372 c = 0; 373 } 374 375 if (card_face[1] > card_face[2]) { 376 a = card_face[2]; card_face[2] = card_face[1]; card_face[1] = a; 377 b = card_suit[2]; card_suit[2] = card_suit[1]; card_suit[1] = b; 378 c = 0; 379 } 380 381 if (card_face[2] > card_face[3]) { 382 a = card_face[3]; card_face[3] = card_face[2]; card_face[2] = a; 383 b = card_suit[3]; card_suit[3] = card_suit[2]; card_suit[2] = b; 384 c = 0; 385 } 386 387 if (card_face[3] > card_face[4]) { 388 a = card_face[4]; card_face[4] = card_face[3]; card_face[3] = a; 389 b = card_suit[4]; card_suit[4] = card_suit[3]; card_suit[3] = b; 390 c = 0; 391 } 392 } 393} 394 395 396void dohold() 397{ 398 uint16_t x; 399 uint16_t y; 400 hold[0] = 0; 401 hold[1] = 0; 402 hold[2] = 0; 403 hold[3] = 0; 404 hold[4] = 0; 405 406 my_lcd.Set_Text_Size(3); 407 my_lcd.Set_Text_colour(YELLOW); 408 my_lcd.Set_Text_Back_colour(BLACK); 409 my_lcd.Fill_Rect(0, 425, 800, 75, BLACK); 410 my_lcd.Print_String("SELECT CARDS TO HOLD THEN PUSH", 5, 450); 411 my_lcd.Set_Text_colour(RED); 412 my_lcd.Set_Text_Size(5); 413 my_lcd.Print_String(" DEAL ", 565, 439); 414 my_lcd.Set_Text_colour(YELLOW); 415 my_lcd.Set_Text_Size(3); 416 417 if (demo == 1) { return (1); } 418 419 while (1) { 420 button_wait(); 421 422 if (pushed==0) { 423 if (hold[0] == 0) { 424 hold[0] = 1; 425 my_lcd.Print_String("HELD ", 50, 165); } 426 else { 427 hold[0] = 0; 428 my_lcd.Print_String(" ", 50, 165); } 429 } 430 if (pushed==1) { 431 if (hold[1] == 0) { 432 hold[1] = 1; 433 my_lcd.Print_String("HELD ", 210, 165); } 434 else { 435 hold[1] = 0; 436 my_lcd.Print_String(" ", 210, 165); } 437 } 438 if (pushed==2) { 439 if (hold[2] == 0) { 440 hold[2] = 1; 441 my_lcd.Print_String("HELD ", 370, 165); } 442 else { 443 hold[2] = 0; 444 my_lcd.Print_String(" ", 370, 165); } 445 } 446 if (pushed==3) { 447 if (hold[3] == 0) { 448 hold[3] = 1; 449 my_lcd.Print_String("HELD ", 530, 165); } 450 else { 451 hold[3] = 0; 452 my_lcd.Print_String(" ", 530, 165); } 453 } 454 if (pushed==4) { 455 if (hold[4] == 0) { 456 hold[4] = 1; 457 my_lcd.Print_String("HELD ", 680, 165); } 458 else { 459 hold[4] = 0; 460 my_lcd.Print_String(" ", 680, 165); } 461 } 462 463 if (pushed==10) { 464 my_lcd.Fill_Rect(0, 425, 800, 55, BLACK); 465 my_lcd.Fill_Rect(0, 155, 800, 40, BLACK); 466 return (1); } 467 delay(300); 468 } 469} 470 471 472void dodeal() 473{ 474 uint16_t x; 475 uint16_t y; 476 477 my_lcd.Set_Text_Size(3); 478 my_lcd.Set_Text_colour(YELLOW); 479 my_lcd.Set_Text_Back_colour(BLACK); 480 my_lcd.Fill_Rect(0, 425, 800, 75, BLACK); 481 my_lcd.Print_String("SELECT BET THEN PUSH DEAL", 5, 450); 482 my_lcd.Set_Text_colour(RED); 483 my_lcd.Set_Text_Size(5); 484 my_lcd.Print_String(" DEAL ", 565, 439); 485 my_lcd.Set_Text_Back_colour(BLACK); 486 my_lcd.Set_Text_Size(3); 487 my_lcd.Set_Text_colour(RED); 488 my_lcd.Print_String("BET 1", 40, 165); 489 my_lcd.Print_String("BET 2", 200, 165); 490 my_lcd.Print_String("BET 3", 360, 165); 491 my_lcd.Print_String("BET 4", 510, 165); 492 my_lcd.Print_String("BET 5", 670, 165); 493 my_lcd.Set_Text_Back_colour(BLUE); 494 my_lcd.Set_Text_colour(YELLOW); 495 496 if (demo == 1) { 497 credit -= bet; 498 my_lcd.Print_Number_Int(credit, 680, 110, 6, 32, 10); 499 return (1); 500 } 501 502 while (1) { 503 button_wait(); 504 505 if (pushed==5) { 506 my_lcd.Print_String("BET 1", 685, 15); 507 bet = 1; 508 showbet(); } 509 510 if (pushed==6) { 511 my_lcd.Print_String("BET 2", 685, 15); 512 bet = 2; 513 showbet(); } 514 515 if (pushed==7) { 516 my_lcd.Print_String("BET 3", 685, 15); 517 bet = 3; 518 showbet(); } 519 520 if (pushed==8) { 521 my_lcd.Print_String("BET 4", 685, 15); 522 bet = 4; 523 showbet(); } 524 525 if (pushed==9) { 526 my_lcd.Print_String("BET 5", 685, 15); 527 bet = 5; 528 showbet(); } 529 530 if (pushed==10) { 531 my_lcd.Fill_Rect(0, 425, 800, 55, BLACK); 532 my_lcd.Fill_Rect(0, 155, 800, 40, BLACK); 533 credit -= bet; 534 my_lcd.Print_Number_Int(credit, 680, 110, 6, 32, 10); 535 return (1); } 536 } 537} 538 539 540void dowin() 541{ 542 uint16_t x; 543 uint16_t y; 544 545 my_lcd.Set_Text_Size(3); 546 my_lcd.Set_Text_colour(YELLOW); 547 my_lcd.Set_Text_Back_colour(BLACK); 548 my_lcd.Fill_Rect(0, 425, 800, 75, BLACK); 549 my_lcd.Print_String(winner, 200, 450); 550 my_lcd.Print_String("WIN", 5, 450); 551 my_lcd.Print_Number_Int(won, 60, 450, 5, 32, 10); 552 my_lcd.Set_Text_Back_colour(BLUE); 553 my_lcd.Print_Number_Int(credit, 680, 110, 6, 32, 10); 554 my_lcd.Set_Text_Back_colour(BLACK); 555 my_lcd.Set_Text_colour(RED); 556 my_lcd.Set_Text_Size(5); 557 my_lcd.Print_String(" SHUFFLE ", 550, 439); 558 my_lcd.Set_Text_colour(YELLOW); 559 my_lcd.Set_Text_Back_colour(BLUE); 560 my_lcd.Set_Text_Size(3); 561 562 if (demo == 1) { return (1); } 563 564 while (1) { 565 button_wait(); 566 567 if (pushed==10) { 568 my_lcd.Fill_Rect(0, 425, 800, 55, BLACK); 569 my_lcd.Fill_Rect(0, 155, 800, 40, BLACK); 570 return (1); } 571 delay(300); 572 } 573} 574 575 576void showcard(int a, int b) 577{ 578 File bmp_file; 579 start = b; 580 581 bmp_file = SD.open(file_name[a]); 582 if (bmp_file) 583 { 584 if (analysis_bpm_header(bmp_file)) 585 { 586 draw_bmp_picture(bmp_file); 587 bmp_file.close(); 588 } 589 else 590 { 591 my_lcd.Set_Text_Back_colour(BLUE); 592 my_lcd.Set_Text_colour(WHITE); 593 my_lcd.Set_Text_Size(2); 594 my_lcd.Print_String("Invalid BMPimage!", 0, 330); 595 } 596 } 597 else { 598 my_lcd.Set_Text_Back_colour(BLUE); 599 my_lcd.Set_Text_colour(WHITE); 600 my_lcd.Set_Text_Size(2); 601 my_lcd.Print_String("Cant find BMPimage!", 0, 360); 602 } 603} 604 605 606void shuffle() 607{ 608 int a = 0; 609 int b = 0; 610 int c = 0; 611 int d = 0; 612 613 for (a = 0; a < 500; a++) { 614 b = (random(52)) + 1; 615 c = (random(52)) + 1; 616 d = cards[b]; 617 cards[b] = cards[c]; 618 cards[c] = d; 619 } 620} 621 622 623bool analysis_bpm_header(File fp) 624{ 625 uint8_t dat8[2]; 626 uint16_t dat16[2]; 627 uint32_t dat32; 628 uint32_t bpm_heigh; 629 uint32_t bpm_width; 630 631 fp.read(dat8, 2); // Read BMP identifier 632 dat16[0] = (dat8[1] << 8 | dat8[0]); 633 if (dat16[0] != 0x4D42) { 634 return false; 635 } 636 637 fp.read(dat8, 4); // Read BMP Size 638 639 fp.read(dat8, 4); // Read Creator Information 640 641 fp.read(dat8, 4); // Read Data offset 642 dat16[1] = (dat8[1] << 8 | dat8[0]); 643 dat16[0] = (dat8[3] << 8 | dat8[2]); 644 bmp_offset = (dat16[0] << 16 | dat16[1]); 645 646 fp.read(dat8, 4); // Read DIB Information 647 648 fp.read(dat8, 4); // Read BMP Width 649 dat16[1] = (dat8[1] << 8 | dat8[0]); 650 dat16[0] = (dat8[3] << 8 | dat8[2]); 651 bpm_width = (dat16[0] << 16 | dat16[1]); 652 if (bpm_width != 154) { 653 return false; 654 } 655 656 fp.read(dat8, 4); // Read BMP Height 657 dat16[1] = (dat8[1] << 8 | dat8[0]); 658 dat16[0] = (dat8[3] << 8 | dat8[2]); 659 bpm_heigh = (dat16[0] << 16 | dat16[1]); 660 if (bpm_heigh != 220) { 661 return false; 662 } 663 664 fp.read(dat8, 2); // Read Colour Planes 665 dat16[0] = (dat8[1] << 8 | dat8[0]); 666 if (dat16[0] != 1) { 667 return false; 668 } 669 670 fp.read(dat8, 2); // Read Bits per Pixel 671 dat16[0] = (dat8[1] << 8 | dat8[0]); 672 if (dat16[0] != 16) { 673 return false; 674 } 675 676 fp.read(dat8, 4); // Read Compression 677 dat16[1] = (dat8[1] << 8 | dat8[0]); 678 dat16[0] = (dat8[3] << 8 | dat8[2]); 679 dat32 = (dat16[0] << 16 | dat16[1]); 680 if (dat32 != 0) { 681 return false; 682 } 683 684 return true; 685} 686 687 688void draw_bmp_picture(File fp) 689{ 690 uint16_t i; 691 uint16_t bpm_color[616]; 692 693 fp.seek(bmp_offset); 694 for (i = 0; i < 220; i += 4) { 695 fp.read(bpm_color, 1232); 696 my_lcd.Draw_Bit_Map((start), (i + 200), (154), 4, bpm_color, 1); 697 } 698} 699 700 701void calib() 702{ 703 Serial.println(" "); 704 Serial.println("Touch the Red dots untill all of them are done"); 705 Serial.println("There are 22 Points to calibrate"); 706 Serial.println(" "); 707 708 my_lcd.Fill_Screen(BLACK); 709 my_lcd.Set_Draw_color (RED); 710 my_lcd.Fill_Circle(5, 200, 5); get_touch(); 711 my_lcd.Fill_Circle(159, 380, 5); get_touch1(); 712 my_lcd.Fill_Circle(164, 200, 5); get_touch(); 713 my_lcd.Fill_Circle(319, 380, 5); get_touch1(); 714 my_lcd.Fill_Circle(323, 200, 5); get_touch(); 715 my_lcd.Fill_Circle(477, 380, 5); get_touch1(); 716 my_lcd.Fill_Circle(482, 200, 5); get_touch(); 717 my_lcd.Fill_Circle(636, 380, 5); get_touch1(); 718 my_lcd.Fill_Circle(641, 200, 5); get_touch(); 719 my_lcd.Fill_Circle(795, 380, 5); get_touch1(); 720 my_lcd.Fill_Circle(5, 151, 5); get_touch(); 721 my_lcd.Fill_Circle(159, 199, 5); get_touch1(); 722 my_lcd.Fill_Circle(164, 151, 5); get_touch(); 723 my_lcd.Fill_Circle(319, 199, 5); get_touch1(); 724 my_lcd.Fill_Circle(323, 151, 5); get_touch(); 725 my_lcd.Fill_Circle(477, 199, 5); get_touch1(); 726 my_lcd.Fill_Circle(482, 151, 5); get_touch(); 727 my_lcd.Fill_Circle(636, 199, 5); get_touch1(); 728 my_lcd.Fill_Circle(641, 151, 5); get_touch(); 729 my_lcd.Fill_Circle(795, 199, 5); get_touch1(); 730 my_lcd.Fill_Circle(550, 421, 5); get_touch(); 731 my_lcd.Fill_Circle(795, 475, 5); get_touch1(); 732 733 Serial.println(" "); 734 Serial.println("Enter those values into the Sketch or Copy and Paste the whole lines"); 735 Serial.println("Then run the Test Calibration to confirm calibration is correct"); 736 737 while(1) { 738 delay(100);} 739 740} 741 742 743void test_buttons() 744{ 745 z=1; 746 my_lcd.Fill_Screen(BLACK); 747 draw_box(); 748 while(1) { 749 button_wait(); 750 my_lcd.Set_Draw_color (RED); 751 if (pushed==0) { my_lcd.Draw_Rectangle(5,200,159,380); } 752 if (pushed==1) { my_lcd.Draw_Rectangle(164,200,319,380); } 753 if (pushed==2) { my_lcd.Draw_Rectangle(323,200,477,380); } 754 if (pushed==3) { my_lcd.Draw_Rectangle(482,200,636,380); } 755 if (pushed==4) { my_lcd.Draw_Rectangle(641,200,795,380); } 756 if (pushed==5) { my_lcd.Draw_Rectangle(5,151,159,199); } 757 if (pushed==6) { my_lcd.Draw_Rectangle(164,151,319,199); } 758 if (pushed==7) { my_lcd.Draw_Rectangle(323,151,477,199); } 759 if (pushed==8) { my_lcd.Draw_Rectangle(482,151,636,199); } 760 if (pushed==9) { my_lcd.Draw_Rectangle(641,151,795,199); } 761 if (pushed==10) { my_lcd.Draw_Rectangle(550,421,795,475); } 762 delay(100); 763 draw_box(); } 764} 765 766 767void draw_box() 768{ 769 my_lcd.Set_Draw_color (GREEN); 770 my_lcd.Draw_Rectangle(5,200,159,380); 771 my_lcd.Draw_Rectangle(164,200,319,380); 772 my_lcd.Draw_Rectangle(323,200,477,380); 773 my_lcd.Draw_Rectangle(482,200,636,380); 774 my_lcd.Draw_Rectangle(641,200,795,380); 775 my_lcd.Draw_Rectangle(5,151,159,199); 776 my_lcd.Draw_Rectangle(164,151,319,199); 777 my_lcd.Draw_Rectangle(323,151,477,199); 778 my_lcd.Draw_Rectangle(482,151,636,199); 779 my_lcd.Draw_Rectangle(641,151,795,199); 780 my_lcd.Draw_Rectangle(550,421,795,475); 781} 782 783 784void get_touch() 785{ 786TS_Point p; 787 788 while (!ts.touched()) { delay(100); } 789 p = ts.getPoint(); delay(50); 790 p = ts.getPoint(); delay(50); 791 p = ts.getPoint(); 792 x1 = p.x; 793 y1 = p.y; 794 delay(300); 795 my_lcd.Fill_Screen(BLACK); 796} 797 798 799void get_touch1() 800{ 801TS_Point p; 802 803 while (!ts.touched()) { delay(100); } 804 p = ts.getPoint(); delay(50); 805 p = ts.getPoint(); delay(50); 806 p = ts.getPoint(); 807 x2 = p.x; 808 y2 = p.y; 809 810 Serial.print("{ "); Serial.print(x1); 811 Serial.print(", "); Serial.print(y1); 812 Serial.print(", "); Serial.print(x2); 813 Serial.print(", "); Serial.print(y2); 814 Serial.print(" }, //Button"); Serial.println(z+1); 815 816 z = z + 1; 817 delay(300); 818 my_lcd.Fill_Screen(BLACK); 819} 820 821 822void button_wait() 823{ 824int8_t a; 825TS_Point p; 826 827 while (1) { 828 if (ts.touched()) { 829 p = ts.getPoint(); delay(50); 830 p = ts.getPoint(); delay(50); 831 p = ts.getPoint(); 832 833 for (a = 0; a < 11; a += 1) { 834 x1=button[a].posx1; 835 y1=button[a].posy1; 836 x2=button[a].posx2; 837 y2=button[a].posy2; 838 if (p.x>x1 && p.x<x2 && p.y>y1 && p.y<y2) { 839 pushed=a; 840 return(1); 841 } 842 } 843 } 844 } 845} 846 847 848
Read_Me
asciidoc
Instructions on Calibrating the Touch Screen
1Instructions to get the Touch Screen Calibrated and Confirm the calibration is accurate. 2 3 4 5The first Step is to Calibrate the Touch Screen Button Positions. 6 7Under the void setup() section of the code you will find two calls to Sub routenes. 8 9 10// calib(); 11// test_buttons(); 12 13 14calib() is used to calibrate the Touch Screen Button positions. 15 16test_buttons() is used to test the Calibration is correct. 17 18 19First, Uncomment the calib() then compile the sketch and program your Mega. 20Open the Serial Port Monitor and you will get instructions on what to do. 21 22After all 22 points are done you can either edit the data points in the sketch 23or copy and paste the whole data table. The Data Table format is laid out in 24the Serial Port Monitor screen the way the Sketch needs it. 25 26 27Next, Comment the calib() agan and uncomment the test_buttons() call. Compile 28the sketch and program your Mega. When you touch inside the boxes they should 29change to Red colour to show where you are touching. 30 31 32Now Comment the test_buttons() again and compile and program your Mega. The Game is now 33ready to run. 34 35 36Note: There is sample calibration data in the sketch that is accurate for my Touch 37Screen. It may or maynot be accurate for yours. 38 39 40This is a sample of what the Button Calibration Data Table looks like in the Sketch. 41 42{141, 1769, 863, 3146}, // Button1 43{896, 1724, 1667, 3148}, // Button2 44{1690, 1730, 2432, 3101}, // Button3 45{2458, 1727, 3197, 3094}, // Button4 46{3239, 1725, 3957, 3103}, // Button5 47{146, 1387, 877, 1743}, // Button6 48{889, 1362, 1666, 1730}, // Button7 49{1702, 1359, 2431, 1724}, // Button8 50{2468, 1375, 3214, 1688}, // Button9 51{3239, 1338, 3942, 1691}, // Button10 52{2770, 3421, 3958, 3811}, // Button11 53 54 55 56Their is a Demo mode in the code. Just change the Demo declaration to a 1. Compile 57and program. The Game will play Automatically. To go back to normal mode change the 58demo value back to 0. 59 60uint8_t demo = 0; // Normal Mode 61uint8_t demo = 1; // Demo Mode 62 63 64 65Enjoy. 66
Comments
Only logged in users can leave comments