Tetris for the RA8875, derived from another users implementation.

Dependencies:   RA8875

Fork of Tetris by Sergejs Popovs

Tetris, adapted to the RA8875 graphics library and display.

As currently implemented, this version is defined for the 800x480 display. A number of macros can adapt it for other screen resolutions.

Further, while presently configured for landscape mode, it should be fairly easy to reconfigure it for portrait mode.

Committer:
WiredHome
Date:
Sun Mar 29 18:21:14 2020 +0000
Revision:
11:2bdc83648d7f
Parent:
7:0e426e81c566
Pick up a bug-fix on jpeg rendering

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sergun2311 4:107d1d5a642e 1 /* Tetris game for touchscreen MicroTFT 320x240 and microcontorller LPC1768
sergun2311 4:107d1d5a642e 2 * Uses SeeedStudioTFTv2 library
sergun2311 4:107d1d5a642e 3 * Copyright (c) 2017 Sergejs Popovs sergun2311
WiredHome 6:d2aa47c92687 4 *
WiredHome 6:d2aa47c92687 5 * Derivation for RA8875 and to clean up a lot of magic numbers by D.Smart
WiredHome 6:d2aa47c92687 6 *
sergun2311 4:107d1d5a642e 7 */
sergun2311 4:107d1d5a642e 8
sergun2311 0:645509d95b8d 9 #include "mbed.h"
sergun2311 0:645509d95b8d 10 #include <ctime>
sergun2311 0:645509d95b8d 11 #include "playGround.h"
sergun2311 0:645509d95b8d 12
WiredHome 7:0e426e81c566 13 LocalFileSystem local("local");
WiredHome 7:0e426e81c566 14 RawSerial pc(USBTX, USBRX); // Not required for display
WiredHome 7:0e426e81c566 15 Timer tSinceTouch;
WiredHome 7:0e426e81c566 16
WiredHome 7:0e426e81c566 17 // Calibrate the resistive touch screen, and store the data on the
WiredHome 7:0e426e81c566 18 // local file system.
WiredHome 7:0e426e81c566 19 //
WiredHome 7:0e426e81c566 20 void CalibrateTS(void)
WiredHome 7:0e426e81c566 21 {
WiredHome 7:0e426e81c566 22 FILE * fh;
WiredHome 7:0e426e81c566 23 tpMatrix_t matrix;
WiredHome 7:0e426e81c566 24 RetCode_t r;
WiredHome 7:0e426e81c566 25 Timer testperiod;
WiredHome 7:0e426e81c566 26
WiredHome 7:0e426e81c566 27 r = TFT.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
WiredHome 7:0e426e81c566 28 if (r == noerror) {
WiredHome 7:0e426e81c566 29 fh = fopen("/local/tpcal.cfg", "wb");
WiredHome 7:0e426e81c566 30 if (fh) {
WiredHome 7:0e426e81c566 31 fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 7:0e426e81c566 32 fclose(fh);
WiredHome 7:0e426e81c566 33 printf(" tp cal written.\r\n");
WiredHome 7:0e426e81c566 34 TFT.cls();
WiredHome 7:0e426e81c566 35 } else {
WiredHome 7:0e426e81c566 36 printf(" couldn't open tpcal file.\r\n");
WiredHome 7:0e426e81c566 37 }
WiredHome 7:0e426e81c566 38 } else {
WiredHome 7:0e426e81c566 39 printf("error return: %d\r\n", r);
WiredHome 7:0e426e81c566 40 }
WiredHome 7:0e426e81c566 41 TFT.cls();
WiredHome 7:0e426e81c566 42 }
WiredHome 7:0e426e81c566 43
WiredHome 7:0e426e81c566 44 // Try to load a previous resistive touch screen calibration from storage. If it
WiredHome 7:0e426e81c566 45 // doesn't exist, activate the touch screen calibration process.
WiredHome 7:0e426e81c566 46 //
WiredHome 7:0e426e81c566 47 void InitTS(void)
WiredHome 7:0e426e81c566 48 {
WiredHome 7:0e426e81c566 49 FILE * fh;
WiredHome 7:0e426e81c566 50 tpMatrix_t matrix;
WiredHome 7:0e426e81c566 51
WiredHome 7:0e426e81c566 52 fh = fopen("/local/tpcal.cfg", "rb");
WiredHome 7:0e426e81c566 53 if (fh) {
WiredHome 7:0e426e81c566 54 fread(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 7:0e426e81c566 55 fclose(fh);
WiredHome 7:0e426e81c566 56 TFT.TouchPanelSetMatrix(&matrix);
WiredHome 7:0e426e81c566 57 printf(" tp cal loaded.\r\n");
WiredHome 7:0e426e81c566 58 } else {
WiredHome 7:0e426e81c566 59 CalibrateTS();
WiredHome 7:0e426e81c566 60 }
WiredHome 7:0e426e81c566 61 }
WiredHome 7:0e426e81c566 62
WiredHome 5:5ce8976cd303 63
sergun2311 0:645509d95b8d 64 int main()
sergun2311 0:645509d95b8d 65 {
sergun2311 1:b4aa36ae11ac 66 int score = 0;
sergun2311 4:107d1d5a642e 67 int period = SPEED;
WiredHome 7:0e426e81c566 68 bool bottomedOut;
sergun2311 0:645509d95b8d 69 clock_t start_s;
WiredHome 5:5ce8976cd303 70 pc.baud(460800); //I like a snappy terminal, so crank it up!
WiredHome 5:5ce8976cd303 71 pc.printf("\r\nTetris - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 5:5ce8976cd303 72 int playState = 0; // 0=init, 1=newBlock, 2=game over
sergun2311 0:645509d95b8d 73 TFTInit();
WiredHome 7:0e426e81c566 74 #ifndef CAP_TOUCH
WiredHome 7:0e426e81c566 75 InitTS(); // resistive touch calibration
WiredHome 7:0e426e81c566 76 #endif
WiredHome 5:5ce8976cd303 77
sergun2311 0:645509d95b8d 78 while (1) {
WiredHome 5:5ce8976cd303 79 switch (playState) {
WiredHome 5:5ce8976cd303 80 case 0: { // init
WiredHome 5:5ce8976cd303 81 score = 0;
WiredHome 5:5ce8976cd303 82 period = SPEED;
WiredHome 5:5ce8976cd303 83 ReInitGame();
WiredHome 7:0e426e81c566 84 playState = 10;
WiredHome 5:5ce8976cd303 85 // break; // fall thru
WiredHome 5:5ce8976cd303 86 }
WiredHome 7:0e426e81c566 87 case 10: {
WiredHome 5:5ce8976cd303 88 Block NewBlock;
WiredHome 7:0e426e81c566 89 bottomedOut = false;
WiredHome 5:5ce8976cd303 90 drawScore(score);
WiredHome 7:0e426e81c566 91 drawPeriod(period);
WiredHome 5:5ce8976cd303 92 drawNextBlock(NewBlock);
WiredHome 7:0e426e81c566 93 while( !bottomedOut ) {
WiredHome 5:5ce8976cd303 94 drawMap();
sergun2311 1:b4aa36ae11ac 95 drawBlock(NewBlock);
WiredHome 5:5ce8976cd303 96 start_s = clock();
WiredHome 7:0e426e81c566 97 while( (clock() - start_s) < period ) {
WiredHome 7:0e426e81c566 98 point_t p;
WiredHome 7:0e426e81c566 99 TouchCode_t panel = TFT.TouchPanelReadable(&p);
WiredHome 7:0e426e81c566 100 switch (panel) {
WiredHome 7:0e426e81c566 101 case touch:
WiredHome 7:0e426e81c566 102 //printf("Touch %4d (%d,%d)\r\n", panel, p.x, p.y);
WiredHome 7:0e426e81c566 103 tSinceTouch.start();
WiredHome 7:0e426e81c566 104 clrBlock(NewBlock);
WiredHome 7:0e426e81c566 105 NewBlock = doGest(NewBlock, p);
WiredHome 7:0e426e81c566 106 drawBlock(NewBlock);
WiredHome 7:0e426e81c566 107 break;
WiredHome 7:0e426e81c566 108 case held:
WiredHome 7:0e426e81c566 109 if (tSinceTouch.read_ms() > 300) {
WiredHome 7:0e426e81c566 110 tSinceTouch.reset();
WiredHome 7:0e426e81c566 111 tSinceTouch.start();
WiredHome 7:0e426e81c566 112 clrBlock(NewBlock);
WiredHome 7:0e426e81c566 113 NewBlock = doGest(NewBlock, p);
WiredHome 7:0e426e81c566 114 drawBlock(NewBlock);
WiredHome 7:0e426e81c566 115 //printf("Held %4d (%d,%d)\r\n", tSinceTouch.read_ms(), p.x,p.y);
WiredHome 7:0e426e81c566 116 } else {
WiredHome 7:0e426e81c566 117 p.x = 0; p.y = 0;
WiredHome 7:0e426e81c566 118 //printf("held %4d (%d,%d)\r", tSinceTouch.read_ms(), p.x,p.y);
WiredHome 7:0e426e81c566 119 }
WiredHome 7:0e426e81c566 120 break;
WiredHome 7:0e426e81c566 121 case release:
WiredHome 7:0e426e81c566 122 //printf("\r\nstop - release\r\n");
WiredHome 7:0e426e81c566 123 tSinceTouch.stop();
WiredHome 7:0e426e81c566 124 break;
WiredHome 7:0e426e81c566 125 case no_touch:
WiredHome 7:0e426e81c566 126 //printf("stop - no_touch\r\n");
WiredHome 7:0e426e81c566 127 tSinceTouch.stop();
WiredHome 7:0e426e81c566 128 break;
WiredHome 7:0e426e81c566 129 default:
WiredHome 7:0e426e81c566 130 //printf("\r\nstop - dunno\r\n");
WiredHome 7:0e426e81c566 131 tSinceTouch.stop();
WiredHome 7:0e426e81c566 132 break;
WiredHome 5:5ce8976cd303 133 }
WiredHome 5:5ce8976cd303 134 }
WiredHome 5:5ce8976cd303 135 if ( NewBlock.CheckBottom() ) {
WiredHome 5:5ce8976cd303 136 saveToField(NewBlock);
WiredHome 7:0e426e81c566 137 bottomedOut = true;
WiredHome 5:5ce8976cd303 138 } else {
WiredHome 5:5ce8976cd303 139 clrBlock(NewBlock);
WiredHome 5:5ce8976cd303 140 NewBlock.y += 1;
WiredHome 5:5ce8976cd303 141 drawBlock(NewBlock);
WiredHome 5:5ce8976cd303 142 }
WiredHome 5:5ce8976cd303 143 }
WiredHome 5:5ce8976cd303 144 score += checkLine();
WiredHome 5:5ce8976cd303 145 if ( score < 3200 )
WiredHome 5:5ce8976cd303 146 period = SPEED - score / 50;
WiredHome 5:5ce8976cd303 147 clrNextBlock(NewBlock);
WiredHome 5:5ce8976cd303 148 if ( checkGameOver() ) {
WiredHome 7:0e426e81c566 149 playState = 20;
WiredHome 5:5ce8976cd303 150 gameOver(score);
WiredHome 5:5ce8976cd303 151 }
WiredHome 5:5ce8976cd303 152 break;
WiredHome 5:5ce8976cd303 153 }
WiredHome 7:0e426e81c566 154 case 20: {
WiredHome 5:5ce8976cd303 155 if (ReplayTouched()) {
WiredHome 5:5ce8976cd303 156 playState = 0; // restart
WiredHome 5:5ce8976cd303 157 break;
sergun2311 1:b4aa36ae11ac 158 }
sergun2311 0:645509d95b8d 159 }
sergun2311 0:645509d95b8d 160 }
sergun2311 0:645509d95b8d 161 }
sergun2311 0:645509d95b8d 162 }