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.

Revision:
7:0e426e81c566
Parent:
6:d2aa47c92687
--- a/main.cpp	Sat Mar 18 23:53:12 2017 +0000
+++ b/main.cpp	Sat Aug 18 22:20:38 2018 +0000
@@ -10,18 +10,70 @@
 #include <ctime>
 #include "playGround.h"
 
-Serial pc(USBTX, USBRX);    // Not required for display
+LocalFileSystem local("local");
+RawSerial pc(USBTX, USBRX);    // Not required for display
+Timer tSinceTouch;
+
+// Calibrate the resistive touch screen, and store the data on the
+// local file system.
+//
+void CalibrateTS(void)
+{
+    FILE * fh;
+    tpMatrix_t matrix;
+    RetCode_t r;
+    Timer testperiod;
+ 
+    r = TFT.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
+    if (r == noerror) {
+        fh = fopen("/local/tpcal.cfg", "wb");
+        if (fh) {
+            fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
+            fclose(fh);
+            printf("  tp cal written.\r\n");
+            TFT.cls();
+        } else {
+            printf("  couldn't open tpcal file.\r\n");
+        }
+    } else {
+        printf("error return: %d\r\n", r);
+    }
+    TFT.cls();
+}
+
+// Try to load a previous resistive touch screen calibration from storage. If it
+// doesn't exist, activate the touch screen calibration process.
+//
+void InitTS(void)
+{
+    FILE * fh;
+    tpMatrix_t matrix;
+
+    fh = fopen("/local/tpcal.cfg", "rb");
+    if (fh) {
+        fread(&matrix, sizeof(tpMatrix_t), 1, fh);
+        fclose(fh);
+        TFT.TouchPanelSetMatrix(&matrix);
+        printf("  tp cal loaded.\r\n");
+    } else {
+        CalibrateTS();
+    }
+}
+
 
 int main()
 {
     int score = 0;
     int period = SPEED;
-    bool flag;
+    bool bottomedOut;
     clock_t start_s;
     pc.baud(460800);    //I like a snappy terminal, so crank it up!
     pc.printf("\r\nTetris - Build " __DATE__ " " __TIME__ "\r\n");
     int playState = 0;  // 0=init, 1=newBlock, 2=game over
     TFTInit();
+    #ifndef CAP_TOUCH
+    InitTS();               // resistive touch calibration
+    #endif
 
     while (1) {
         switch (playState) {
@@ -29,31 +81,60 @@
                 score = 0;
                 period = SPEED;
                 ReInitGame();
-                drawFrame();
-                drawMap();
-                playState++;
+                playState = 10;
                 // break;   // fall thru
             }
-            case 1: {
+            case 10: {
                 Block NewBlock;
-                flag = false;
+                bottomedOut = false;
                 drawScore(score);
+                drawPeriod(period);
                 drawNextBlock(NewBlock);
-                while( !flag ) {
+                while( !bottomedOut ) {
                     drawMap();
                     drawBlock(NewBlock);
                     start_s = clock();
-                    while( start_s + period > clock() ) {
-                        if ( TouchStatus() )    {
-                            clrBlock(NewBlock);
-                            NewBlock = doGest(NewBlock);
-                            drawBlock(NewBlock);
-                            wait_ms(80);
+                    while( (clock() - start_s) < period ) {
+                        point_t p;
+                        TouchCode_t panel = TFT.TouchPanelReadable(&p);
+                        switch (panel) {
+                            case touch:
+                                //printf("Touch %4d (%d,%d)\r\n", panel, p.x, p.y);
+                                tSinceTouch.start();
+                                clrBlock(NewBlock);
+                                NewBlock = doGest(NewBlock, p);
+                                drawBlock(NewBlock);
+                                break;
+                            case held:
+                                if (tSinceTouch.read_ms() > 300) {
+                                    tSinceTouch.reset();
+                                    tSinceTouch.start();
+                                    clrBlock(NewBlock);
+                                    NewBlock = doGest(NewBlock, p);
+                                    drawBlock(NewBlock);
+                                    //printf("Held  %4d (%d,%d)\r\n", tSinceTouch.read_ms(), p.x,p.y);
+                                } else {
+                                    p.x = 0; p.y = 0;
+                                    //printf("held  %4d (%d,%d)\r", tSinceTouch.read_ms(), p.x,p.y);
+                                }
+                                break;
+                            case release:
+                                //printf("\r\nstop - release\r\n");
+                                tSinceTouch.stop();
+                                break;
+                            case no_touch:
+                                //printf("stop - no_touch\r\n");
+                                tSinceTouch.stop();
+                                break;
+                            default:
+                                //printf("\r\nstop - dunno\r\n");
+                                tSinceTouch.stop();
+                                break;
                         }
                     }
                     if ( NewBlock.CheckBottom() ) {
                         saveToField(NewBlock);
-                        flag = true;
+                        bottomedOut = true;
                     } else {
                         clrBlock(NewBlock);
                         NewBlock.y += 1;
@@ -65,12 +146,12 @@
                     period = SPEED - score / 50;
                 clrNextBlock(NewBlock);
                 if ( checkGameOver() ) {
-                    playState++;
+                    playState = 20;
                     gameOver(score);
                 }
                 break;
             }
-            case 2: {
+            case 20: {
                 if (ReplayTouched()) {
                     playState = 0;     // restart
                     break;