init
authorkyz <kongyuzhong@google.com>
Thu, 18 Dec 2025 03:25:07 +0000 (11:25 +0800)
committerkyz <kongyuzhong@google.com>
Thu, 18 Dec 2025 09:39:33 +0000 (17:39 +0800)
20240801完整版/扳手下位机软件/cpbs-202103-20240423/APP/wifi.c
20240801完整版/扳手下位机软件/cpbs-202103-20240423/HARDWARE/lcdspi.c
20240801完整版/扳手下位机软件/cpbs-202103-20240423/USER/FreeRTOS.uvprojx
20240801完整版/扳手下位机软件/cpbs-202103-20240423/game/game.c
20240801完整版/扳手下位机软件/cpbs-202103-20240423/game/tetris.c
20240801完整版/扳手下位机软件/cpbs-202103-20240423/game/tetris.h

index 6f663eda7684c3ff6565c3c91a900b230ce1bd23..56c7a86ac693736f279667ea5daf3ee44c0df06c 100644 (file)
@@ -257,6 +257,15 @@ static char* generate_response(tcp_command_t* cmd)
         len = snprintf(response, sizeof(response),
             "ASSP;%s;%s;%s;0;\r\n",
             cmd->ver, SN, cmd->tg);
+            {
+                static int flag = 0;
+                if( flag == 0 )
+                {
+                    extern void game_start(void);
+                    flag = 1;
+                    game_start();
+                }
+            }
     } else if (strcmp(cmd->mid, "RSEN") == 0) {
         // Ê¹Äܹ¤¾ßÃüÁîÏìÓ¦
         len = snprintf(response, sizeof(response),
index ba1b43379b8e013a45d26112f80710f31636224d..7c4e91c91a40a56a5af6749df4d3c10d2ba40c28 100644 (file)
@@ -23,7 +23,7 @@ void SPI2_Init(void)
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;//´®ÐÐͬ²½Ê±ÖÓ¿ÕÏÐʱSCLKλ¸ßµçƽ
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//´®ÐÐͬ²½Ê±ÖÓ¿ÕµÚ¶þ¸öʱÖÓÑØ²¶»ñ
        SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//NSSÐźÅÓÉÓ²¼þ¹ÜÀí
-       SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;//²¨ÌØÂÊÔ¤·ÖƵֵ£º²¨ÌØÂÊÔ¤·ÖƵֵΪ4
+       SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//²¨ÌØÂÊÔ¤·ÖƵֵ£º²¨ÌØÂÊÔ¤·ÖƵֵΪ4
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//Êý¾Ý´«Êä¸ßλÏÈÐÐ
        SPI_InitStructure.SPI_CRCPolynomial = 7;//CRCÖµ¼ÆËãµÄ¶àÏîʽ
        SPI_Init(SPI2,&SPI_InitStructure);//³õʼ»¯SPI
index ca72eef5f6f663e493eb85997edd3ba375a04e72..93bd328782d95a050d2c00f741b8ece1cb0dc00f 100644 (file)
             </File>
           </Files>
         </Group>
+        <Group>
+          <GroupName>tetris</GroupName>
+          <Files>
+            <File>
+              <FileName>game.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\game\game.c</FilePath>
+            </File>
+            <File>
+              <FileName>tetris.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\game\tetris.c</FilePath>
+            </File>
+          </Files>
+        </Group>
       </Groups>
     </Target>
   </Targets>
index d8c6fdedc970317b8e77a0716f2dfc39d85ff6f4..a42443861eb899d410bd1330267aa4158f7b5e83 100644 (file)
 #include "eventmonitoring.h"
 #include "batterytesting.h"
 
+#include "key.h"
 #include "tetris.h"
 
 // Ä£ÄâÖ¡»º³åÇø
 uint16_t framebuffer[SCREEN_WIDTH * SCREEN_HEIGHT];
 
-// Ä£Äâ°´¼üÊäÈë
-typedef enum {
-    KEY_NONE,
-    KEY_LEFT,
-    KEY_RIGHT,
-    KEY_DOWN,
-    KEY_UP,
-    KEY_SPACE
-} Key;
 
-Key get_key() {
-    // ÕâÀïÐèÒª¸ù¾Ýʵ¼ÊÓ²¼þʵÏÖ°´¼ü¼ì²â
-    // ÏÖÔÚ·µ»ØËæ»ú°´¼üÓÃÓÚ²âÊÔ
-    return rand() % 6;
-}
-
-// Ä£ÄâÏÔʾ¸üÐÂ
-void update_display(uint16_t *framebuffer) {
-    // ÕâÀïÐèÒª¸ù¾Ýʵ¼ÊÓ²¼þʵÏÖÏÔʾ¸üÐÂ
-    // ÀýÈ磺½«framebuffer·¢Ë͵½LCDÏÔʾÆÁ
-    printf("Display updated\n");
-}
-
-game_task(void *pvParameters)
+void game_task(void *pvParameters)
 {
+    uint32_t ms = 0;
+    uint32_t update = 0;
+    uint8_t key;
+    static int k1,k2,k3,k4;
+
     //delete other tasks
     vTaskDelete(ButtonMenuTask_Handler);
     vTaskDelete(UiShowTask_Handler);
@@ -53,50 +37,80 @@ game_task(void *pvParameters)
     // ³õʼ»¯ÓÎÏ·
     tetris_init();
 
+    k1 = KEY1;
+    k2 = KEY2;
+    k3 = KEY3;
+    k4 = KEY4;
+
     while(1)
     {
-        // ´¦ÀíÊäÈë
-        Key key = get_key();
+        vTaskDelay(10);
+        ms += 10;
+        if(k1 != KEY1 && k1 )
+                key = KEY1_PRES;
+        else if(k2 != KEY2 && k2 )
+                key = KEY2_PRES;
+        else if(k3 != KEY3 && k3 )
+                key = KEY3_PRES;
+        else if(k4 != KEY4 && k4 )
+                key = KEY4_PRES;
+        else
+                key = KEY_NOPRES;
+
+        k1 = KEY1;
+        k3 = KEY3;
+        k2 = KEY2;
+        k4 = KEY4;
+        update = 0;
         switch (key) {
-            case KEY_LEFT:
-                tetris_move_piece(-1, 0);
-                break;
-            case KEY_RIGHT:
+            case KEY1_PRES:
+                printf( "--KEY1 PRESSED\n");
                 tetris_move_piece(1, 0);
+                update = 1;
+
                 break;
-            case KEY_DOWN:
-                tetris_drop_piece();
+            case KEY2_PRES:
+                printf( "--KEY2 PRESSED\n");
+                tetris_move_piece(-1, 0);
+                update = 1;
                 break;
-            case KEY_UP:
+            case KEY3_PRES:
+                printf( "--KEY3 PRESSED\n");
                 tetris_rotate_piece();
+                update = 1;
+                break;
+            case KEY4_PRES:
+                printf( "--KEY4 PRESSED\n");
+                tetris_drop_piece();
+                update = 1;
                 break;
-            case KEY_SPACE:
+            case KEY4_Long_PRES:
+                printf( "--KEY4 Long PRESSED\n");
                 tetris_hard_drop();
+                update = 1;
                 break;
             default:
+                // ¿ØÖÆÓÎÏ·ËÙ¶È
+                // ¸ù¾Ýµ±Ç°µÈ¼¶µ÷ÕûÑÓ³Ùʱ¼ä
+                if( ms >= 1000 )
+                    update = 1;
                 break;
         }
-        
-        // ¸üÐÂÓÎϷ״̬
-        tetris_update();
-        
-        // »æÖÆÓÎÏ·
-        tetris_draw(framebuffer);
-        
-        // ¸üÐÂÏÔʾ
-        update_display(framebuffer);
-        
-        // ¿ØÖÆÓÎÏ·ËÙ¶È
-        // ¸ù¾Ýµ±Ç°µÈ¼¶µ÷ÕûÑÓ³Ùʱ¼ä
-        int delay = 1000 / game.level;
-        delay_ms(delay); // ÐèÒª¸ù¾Ýʵ¼ÊÓ²¼þʵÏÖÑÓ³Ùº¯Êý
+        if( update ){
+            // ¸üÐÂÓÎϷ״̬
+            tetris_update();
+
+            // »æÖÆÓÎÏ·
+            tetris_draw();
+            ms = 0;
+        }
+
     }
 }
 
-game_task_handle_t game_task_handle = NULL;
+TaskHandle_t game_task_handle = NULL;
 void game_start(void)
 {
-    
     // ´´½¨ÓÎÏ·ÈÎÎñ
-    xTaskCreate(game_task, "Game Task", 2048, NULL, 1, &game_task_handle);
+    xTaskCreate(game_task, "Game Task", 2048/4, NULL, 1, &game_task_handle);
 }
index 7faed2b85e091a761a508eed87915ae0d7089366..bd0bfe89113ef2e41d905360b8c5398f36867663 100644 (file)
@@ -2,11 +2,12 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include "lcdshow.h"
 
 // ÓÎϷ״̬
 TetrisGame game;
 
-// ·½¿éÐÎ×´¶¨Òå (7ÖÖÐÎ×´£¬Ã¿ÖÖ4¸öÐýת״̬)
+// ·½¿éÐÎ×´Êý¾Ý (7ÖÖ·½¿é£¬Ã¿ÖÖ4ÖÖÐýת״̬)
 const uint8_t tetrominoes[7][4][4][4] = {
     // IÐÍ
     {
@@ -63,63 +64,78 @@ const uint8_t tetrominoes[7][4][4][4] = {
 const uint16_t colors[8] = {
     0x0000,  // ºÚÉ« (±³¾°)
     0x07E0,  // ÂÌÉ« (IÐÍ)
-    0xFFFF,  // »ÆÉ« (OÐÍ)
-    0x7F00,  // ×ØÉ« (TÐÍ)
-    0xFFA5,  // ³ÈÉ« (LÐÍ)
-    0x1F00,  // À¶É« (JÐÍ)
-    0x001F,  // ºìÉ« (SÐÍ)
+    0xFFFF,  // °×É« (OÐÍ)
+    0x7F00,  // ºìÉ« (TÐÍ)
+    0xFFA5,  // »ÆÉ« (LÐÍ)
+    0x1F00,  // ÉîÀ¶É« (JÐÍ)
+    0x001F,  // À¶É« (SÐÍ)
     0x780F   // ×ÏÉ« (ZÐÍ)
 };
 
 // ³õʼ»¯ÓÎÏ·
-void tetris_init() {
-    // ³õʼ»¯Ëæ»úÊýÖÖ×Ó
-    srand(time(NULL));
-    
-    // Çå¿ÕÓÎÏ·ÇøÓò
+void tetris_init( void ) {
+    extern u32 motor_current_avg_1;
+    // ³õʼ»¯Ëæ»úÊýÉú³ÉÆ÷
+    srand( motor_current_avg_1 );
+
+    // Çå¿ÕÓÎÏ·°å
     memset(game.board, 0, sizeof(game.board));
-    
+
     // ³õʼ»¯ÓÎϷ״̬
     game.score = 0;
     game.level = 1;
     game.game_over = false;
-    
+
     // Éú³ÉµÚÒ»¸ö·½¿é
     tetris_spawn_piece();
+
+    //ÇåÆÁ
+       LCD_Fill(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0x0000);
+    // »æÖÆÓÎÏ·ÇøÓò±ß¿ò
+    //tetris_draw_rect(10, 10, BOARD_WIDTH * cell_size + 10, BOARD_HEIGHT * cell_size + 10, WHITE);
+
 }
 
 // Éú³Éз½¿é
-void tetris_spawn_piece() {
+void tetris_spawn_piece( void ) {
     // Ëæ»úÑ¡Ôñ·½¿éÀàÐÍ
-    game.current_piece.type = rand() % 7;
+    static int flag = 0;
+    if( !flag ){
+        game.next_piece.type = rand() % 7;
+        flag = 1;
+    }
+    game.current_piece.type = game.next_piece.type;
     game.current_piece.rotation = 0;
     game.current_piece.x = BOARD_WIDTH / 2 - 2;
     game.current_piece.y = 0;
-    
+
     // ¼ì²éÊÇ·ñÓÎÏ·½áÊø
     if (!tetris_is_valid_position(game.current_piece.x, game.current_piece.y, game.current_piece.type, game.current_piece.rotation)) {
         game.game_over = true;
         return;
     }
-    
+
     // Éú³ÉÏÂÒ»¸ö·½¿é
     game.next_piece.type = rand() % 7;
     game.next_piece.rotation = 0;
 }
 
 // ¼ì²éλÖÃÊÇ·ñÓÐЧ
-bool tetris_is_valid_position(int x, int y, int type, int rotation) {
-    for (int i = 0; i < 4; i++) {
-        for (int j = 0; j < 4; j++) {
+bool tetris_is_valid_position(int x, int y, int type, int rotation)
+{
+    int i,j;
+
+    for ( i = 0; i < 4; i++) {
+        for ( j = 0; j < 4; j++) {
             if (tetrominoes[type][rotation][i][j] != 0) {
                 int board_x = x + j;
                 int board_y = y + i;
-                
+
                 // ¼ì²é±ß½ç
                 if (board_x < 0 || board_x >= BOARD_WIDTH || board_y >= BOARD_HEIGHT) {
                     return false;
                 }
-                
+
                 // ¼ì²éÊÇ·ñÓëÒÑÓз½¿éÅöײ
                 if (board_y >= 0 && game.board[board_y][board_x] != 0) {
                     return false;
@@ -131,10 +147,11 @@ bool tetris_is_valid_position(int x, int y, int type, int rotation) {
 }
 
 // Òƶ¯·½¿é
-bool tetris_move_piece(int dx, int dy) {
+bool tetris_move_piece(int dx, int dy)
+{
     int new_x = game.current_piece.x + dx;
     int new_y = game.current_piece.y + dy;
-    
+
     if (tetris_is_valid_position(new_x, new_y, game.current_piece.type, game.current_piece.rotation)) {
         game.current_piece.x = new_x;
         game.current_piece.y = new_y;
@@ -144,9 +161,9 @@ bool tetris_move_piece(int dx, int dy) {
 }
 
 // Ðýת·½¿é
-bool tetris_rotate_piece() {
+bool tetris_rotate_piece( void ) {
     int new_rotation = (game.current_piece.rotation + 1) % 4;
-    
+
     if (tetris_is_valid_position(game.current_piece.x, game.current_piece.y, game.current_piece.type, new_rotation)) {
         game.current_piece.rotation = new_rotation;
         return true;
@@ -155,21 +172,24 @@ bool tetris_rotate_piece() {
 }
 
 // ·½¿éÏÂÂäÒ»¸ñ
-bool tetris_drop_piece() {
+bool tetris_drop_piece( void ) {
     return tetris_move_piece(0, 1);
 }
 
-// ·½¿éÖ±½ÓÂäµ½µ×²¿
-void tetris_hard_drop() {
+// ·½¿éÖ±½ÓÏÂÂäµ½µ×²¿
+void tetris_hard_drop( void ) {
     while (tetris_drop_piece());
     tetris_lock_piece();
 }
 
-// Ëø¶¨·½¿éµ½ÓÎÏ·°å
-void tetris_lock_piece() {
+// ½«·½¿é¹Ì¶¨µ½ÓÎÏ·°å
+void tetris_lock_piece( void )
+{
+    int i,j;
+
     // ½«·½¿é¹Ì¶¨µ½ÓÎÏ·°å
-    for (int i = 0; i < 4; i++) {
-        for (int j = 0; j < 4; j++) {
+    for ( i = 0; i < 4; i++) {
+        for ( j = 0; j < 4; j++) {
             if (tetrominoes[game.current_piece.type][game.current_piece.rotation][i][j] != 0) {
                 int board_x = game.current_piece.x + j;
                 int board_y = game.current_piece.y + i;
@@ -179,47 +199,49 @@ void tetris_lock_piece() {
             }
         }
     }
-    
-    // ¼ì²é²¢Ïû³ýÍêÕûµÄÐÐ
+
+    // ¼ì²é²¢Ïû³ýÍêÕûÐÐ
     tetris_clear_lines();
-    
+
     // Éú³Éз½¿é
     tetris_spawn_piece();
 }
 
-// Ïû³ýÍêÕûµÄÐÐ
-void tetris_clear_lines() {
+// Ïû³ýÍêÕûÐÐ
+void tetris_clear_lines( void )
+{
+    int i,j,k;
     int lines_cleared = 0;
-    
-    for (int i = BOARD_HEIGHT - 1; i >= 0; i--) {
+
+    for ( i = BOARD_HEIGHT - 1; i >= 0; i--) {
         bool line_complete = true;
-        
+
         // ¼ì²éÐÐÊÇ·ñÍêÕû
-        for (int j = 0; j < BOARD_WIDTH; j++) {
+        for ( j = 0; j < BOARD_WIDTH; j++) {
             if (game.board[i][j] == 0) {
                 line_complete = false;
                 break;
             }
         }
-        
-        // Èç¹ûÐÐÍêÕû£¬Ïû³ý²¢ÏÂÒÆÉÏÃæµÄÐÐ
+
+        // Èç¹ûÐÐÍêÕû£¬½«ÉÏÃæµÄÐÐÏòÏÂÒÆ¶¯
         if (line_complete) {
-            for (int k = i; k > 0; k--) {
+            for ( k = i; k > 0; k--) {
                 memcpy(game.board[k], game.board[k-1], sizeof(game.board[0]));
             }
             memset(game.board[0], 0, sizeof(game.board[0]));
             lines_cleared++;
-            i++; // ¼ì²éÐÂÏÂÒÆµÄÐÐ
+            i++; // ÖØÐ¼ì²éµ±Ç°ÐÐ
         }
     }
-    
+
     // ¸üзÖÊýºÍµÈ¼¶
     if (lines_cleared > 0) {
         // ·ÖÊý¼ÆË㣺1ÐÐ100·Ö£¬2ÐÐ300·Ö£¬3ÐÐ600·Ö£¬4ÐÐ1000·Ö
         int line_scores[] = {0, 100, 300, 600, 1000};
         game.score += line_scores[lines_cleared] * game.level;
-        
-        // ÿÏû³ý10ÐÐÉýÒ»¼¶
+
+        // ÿµÃ1000·ÖÉýÒ»¼¶
         game.level = game.score / 1000 + 1;
     }
 }
@@ -229,248 +251,155 @@ void tetris_update() {
     if (game.game_over) {
         return;
     }
-    
-    // ³¢ÊÔÏÂÂä·½¿é
+
+    // ·½¿éÏÂÂäÂß¼­
     if (!tetris_drop_piece()) {
-        // ·½¿éÎÞ·¨ÏÂÂä£¬Ëø¶¨µ½ÓÎÏ·°å
+        // ·½¿éÎÞ·¨ÏÂÂ䣬¹Ì¶¨µ½ÓÎÏ·°å
         tetris_lock_piece();
     }
 }
 
+typedef struct{
+    uint8_t board[BOARD_HEIGHT][BOARD_WIDTH];
+    uint8_t next[4][4];
+    int score;
+    int level;
+    bool game_over;
+} disp_info_t;
+
+void draw_board_block( int row, int col, int color )
+{
+    int x,y;
+    int cell_size = 12;
+    x = 0 + col * cell_size + 1;
+    y = 0 + row * cell_size + 1;
+    tetris_draw_cell(x, y, cell_size - 1, cell_size - 1, color);
+}
+
+void draw_next_block( int row, int col, int color )
+{
+    int x,y;
+    int cell_size = 12;
+    x = 144 + col * cell_size + 1;
+    y = 0 + row * cell_size + 1;
+    tetris_draw_cell(x, y, cell_size - 1, cell_size - 1, color);
+}
+
 // »æÖÆÓÎÏ·
-void tetris_draw(uint16_t *framebuffer) {
+void tetris_draw( void )
+{
+    int i,j;
+    int cell_size;
+    int preview_x;
+    int preview_y;
+    disp_info_t to_disp;
+    static disp_info_t disp_info = {0};
+
     // Çå¿ÕÆÁÄ»
-    memset(framebuffer, 0, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(uint16_t));
-    
+    //LCD_Fill(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0x0000);
+
     // ¼ÆËã·½¿é´óС
-    int cell_size = (SCREEN_WIDTH - 20) / (BOARD_WIDTH + 6); // 6ÁÐÓÃÓÚÏÂÒ»¸ö·½¿éÔ¤ÀÀ
-    
+    cell_size = 10; // (SCREEN_WIDTH - 20) / (BOARD_WIDTH + 6); // 6ΪÓÒ²àÔ¤ÀÀÇøÓòµÄ¿í¶È
+
     // »æÖÆÓÎÏ·ÇøÓò±ß¿ò
-    tetris_draw_rect(framebuffer, 10, 10, BOARD_WIDTH * cell_size + 10, BOARD_HEIGHT * cell_size + 10, 0xFFFF);
-    
+    //tetris_draw_rect(10, 10, BOARD_WIDTH * cell_size + 10, BOARD_HEIGHT * cell_size + 10, WHITE);
+
     // »æÖÆÓÎÏ·°å
-    for (int i = 0; i < BOARD_HEIGHT; i++) {
-        for (int j = 0; j < BOARD_WIDTH; j++) {
-            if (game.board[i][j] != 0) {
-                tetris_draw_cell(framebuffer, 10 + j * cell_size + 1, 10 + i * cell_size + 1, cell_size - 1, cell_size - 1, colors[game.board[i][j]]);
-            }
-        }
-    }
-    
+    for ( i = 0; i < BOARD_HEIGHT; i++)
+        for ( j = 0; j < BOARD_WIDTH; j++)
+            to_disp.board[i][j] = game.board[i][j];
     // »æÖƵ±Ç°·½¿é
-    for (int i = 0; i < 4; i++) {
-        for (int j = 0; j < 4; j++) {
-            if (tetrominoes[game.current_piece.type][game.current_piece.rotation][i][j] != 0) {
-                int x = game.current_piece.x + j;
-                int y = game.current_piece.y + i;
-                if (y >= 0) {
-                    tetris_draw_cell(framebuffer, 10 + x * cell_size + 1, 10 + y * cell_size + 1, cell_size - 1, cell_size - 1, colors[tetrominoes[game.current_piece.type][game.current_piece.rotation][i][j]]);
-                }
-            }
+    for ( i = 0; i < 4; i++){
+        int row = game.current_piece.y + i;
+        for ( j = 0; j < 4; j++){
+            int col = game.current_piece.x + j;
+            int color = tetrominoes[game.current_piece.type][game.current_piece.rotation][i][j];
+            if( color )
+                to_disp.board[row][col] = color;
         }
     }
-    
-    // »æÖÆÏÂÒ»¸ö·½¿éÔ¤ÀÀÇøÓò
-    int preview_x = 10 + BOARD_WIDTH * cell_size + 20;
-    int preview_y = 10;
-    tetris_draw_rect(framebuffer, preview_x, preview_y, preview_x + 6 * cell_size, preview_y + 6 * cell_size, 0xFFFF);
-    
+
     // »æÖÆÏÂÒ»¸ö·½¿é
-    for (int i = 0; i < 4; i++) {
-        for (int j = 0; j < 4; j++) {
-            if (tetrominoes[game.next_piece.type][game.next_piece.rotation][i][j] != 0) {
-                tetris_draw_cell(framebuffer, preview_x + (j + 1) * cell_size + 1, preview_y + (i + 1) * cell_size + 1, cell_size - 1, cell_size - 1, colors[tetrominoes[game.next_piece.type][game.next_piece.rotation][i][j]]);
-            }
-        }
-    }
-    
+    for ( i = 0; i < 4; i++)
+        for ( j = 0; j < 4; j++)
+            to_disp.next[i][j] = tetrominoes[game.next_piece.type][game.next_piece.rotation][i][j];
+#if 0
     // »æÖÆ·ÖÊýºÍµÈ¼¶
-    tetris_draw_text(framebuffer, "Score:", preview_x, preview_y + 7 * cell_size, 0xFFFF);
-    tetris_draw_number(framebuffer, game.score, preview_x + 40, preview_y + 7 * cell_size, 0xFFFF);
-    
-    tetris_draw_text(framebuffer, "Level:", preview_x, preview_y + 8 * cell_size, 0xFFFF);
-    tetris_draw_number(framebuffer, game.level, preview_x + 40, preview_y + 8 * cell_size, 0xFFFF);
-    
-    // Èç¹ûÓÎÏ·½áÊø£¬»æÖÆÓÎÏ·½áÊøÐÅÏ¢
+    tetris_draw_text("Score:", preview_x, preview_y + 7 * cell_size, WHITE);
+    tetris_draw_number(game.score, preview_x + 40, preview_y + 7 * cell_size, WHITE);
+
+    tetris_draw_text("Level:", preview_x, preview_y + 8 * cell_size, WHITE);
+    tetris_draw_number(game.level, preview_x + 40, preview_y + 8 * cell_size, WHITE);
+
+    // Èç¹ûÓÎÏ·½áÊø£¬ÏÔʾÓÎÏ·½áÊøÐÅÏ¢
     if (game.game_over) {
-        tetris_draw_rect(framebuffer, SCREEN_WIDTH / 2 - 60, SCREEN_HEIGHT / 2 - 30, SCREEN_WIDTH / 2 + 60, SCREEN_HEIGHT / 2 + 30, 0x0000);
-        tetris_draw_rect(framebuffer, SCREEN_WIDTH / 2 - 62, SCREEN_HEIGHT / 2 - 32, SCREEN_WIDTH / 2 + 62, SCREEN_HEIGHT / 2 + 32, 0xFFFF);
-        tetris_draw_text(framebuffer, "GAME OVER", SCREEN_WIDTH / 2 - 40, SCREEN_HEIGHT / 2 - 10, 0xFFFF);
-        tetris_draw_text(framebuffer, "Score:", SCREEN_WIDTH / 2 - 30, SCREEN_HEIGHT / 2 + 10, 0xFFFF);
-        tetris_draw_number(framebuffer, game.score, SCREEN_WIDTH / 2 + 10, SCREEN_HEIGHT / 2 + 10, 0xFFFF);
+        tetris_draw_rect(SCREEN_WIDTH / 2 - 60, SCREEN_HEIGHT / 2 - 30, SCREEN_WIDTH / 2 + 60, SCREEN_HEIGHT / 2 + 30, BLACK);
+        tetris_draw_rect(SCREEN_WIDTH / 2 - 62, SCREEN_HEIGHT / 2 - 32, SCREEN_WIDTH / 2 + 62, SCREEN_HEIGHT / 2 + 32, WHITE);
+        tetris_draw_text("GAME OVER", SCREEN_WIDTH / 2 - 40, SCREEN_HEIGHT / 2 - 10, WHITE);
+        tetris_draw_text("Score:", SCREEN_WIDTH / 2 - 30, SCREEN_HEIGHT / 2 + 10, WHITE);
+        tetris_draw_number(game.score, SCREEN_WIDTH / 2 + 10, SCREEN_HEIGHT / 2 + 10, WHITE);
     }
-}
+#else
+    to_disp.score = game.score;
+    to_disp.level = game.level;
+    to_disp.game_over = game.game_over;
+#endif
 
-// »æÖƾØÐÎ
-void tetris_draw_rect(uint16_t *framebuffer, int x1, int y1, int x2, int y2, uint16_t color) {
-    for (int y = y1; y < y2; y++) {
-        for (int x = x1; x < x2; x++) {
-            if (x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT) {
-                framebuffer[y * SCREEN_WIDTH + x] = color;
-            }
-        }
+    if( memcmp( &to_disp, &disp_info, sizeof(disp_info_t)) != 0)
+    {
+        int board_x = 10;
+        int board_y = 10;
+        int preview_x = 10 + BOARD_WIDTH * cell_size + 20;
+        int preview_y = 10;
+        int row, col;
+
+        for( row=0; row<20; row++)
+            for( col=0; col<10; col++)
+                if( to_disp.board[row][col] != disp_info.board[row][col])
+                    draw_board_block(row, col, colors[to_disp.board[row][col]]);
+        for( row=0; row<4; row++)
+            for( col=0; col<4; col++)
+                if( to_disp.next[row][col] != disp_info.next[row][col])
+                    draw_next_block(row, col, colors[to_disp.next[row][col]]);
+        memcpy( &disp_info, &to_disp, sizeof(disp_info_t));
     }
 }
 
-// »æÖƵ¥Ôª¸ñ
-void tetris_draw_cell(uint16_t *framebuffer, int x, int y, int width, int height, uint16_t color) {
-    // »æÖƵ¥Ôª¸ñÖ÷Ìå
-    tetris_draw_rect(framebuffer, x, y, x + width, y + height, color);
-    
-    // »æÖƵ¥Ôª¸ñ±ß¿ò
-    uint16_t border_color = color > 0x7FFF ? color - 0x1010 : color + 0x1010;
-    tetris_draw_line(framebuffer, x, y, x + width - 1, y, border_color);
-    tetris_draw_line(framebuffer, x, y, x, y + height - 1, border_color);
-    
-    // »æÖƵ¥Ôª¸ñ¸ß¹â
-    uint16_t highlight_color = color + 0x2020;
-    tetris_draw_line(framebuffer, x + width - 1, y + 1, x + width - 1, y + height - 1, highlight_color);
-    tetris_draw_line(framebuffer, x + 1, y + height - 1, x + width - 1, y + height - 1, highlight_color);
+// »æÖƾØÐÎ
+void tetris_draw_rect(int x1, int y1, int x2, int y2, uint16_t color) {
+    LCD_Fill(x1, y1, x2, y2, color);
 }
 
-// »æÖÆÏß¶Î
-void tetris_draw_line(uint16_t *framebuffer, int x1, int y1, int x2, int y2, uint16_t color) {
-    // BresenhamÖ±ÏßËã·¨
-    int dx = abs(x2 - x1);
-    int dy = abs(y2 - y1);
-    int sx = x1 < x2 ? 1 : -1;
-    int sy = y1 < y2 ? 1 : -1;
-    int err = dx - dy;
-    
-    while (1) {
-        if (x1 >= 0 && x1 < SCREEN_WIDTH && y1 >= 0 && y1 < SCREEN_HEIGHT) {
-            framebuffer[y1 * SCREEN_WIDTH + x1] = color;
-        }
-        
-        if (x1 == x2 && y1 == y2) {
-            break;
-        }
-        
-        int e2 = 2 * err;
-        if (e2 > -dy) {
-            err -= dy;
-            x1 += sx;
-        }
-        if (e2 < dx) {
-            err += dx;
-            y1 += sy;
-        }
-    }
+// »æÖÆ·½¿é
+void tetris_draw_cell(int x, int y, int width, int height, uint16_t color)
+{
+    uint16_t border_color;
+    uint16_t highlight_color;
+
+    // »æÖÆ·½¿éÖ÷Ìå
+    LCD_Fill(x, y, x + width, y + height, color);
+    if( color == 0 )
+        return;
+    // »æÖÆ·½¿é±ß¿ò
+    border_color = color > 0x7FFF ? color - 0x1010 : color + 0x1010;
+    LCD_DrawLine(x, y, x + width - 1, y, border_color);
+    LCD_DrawLine(x, y, x, y + height - 1, border_color);
+
+    // »æÖÆ·½¿é¸ß¹â
+    highlight_color = color + 0x2020;
+    LCD_DrawLine(x + width - 1, y + 1, x + width - 1, y + height - 1, highlight_color);
+    LCD_DrawLine(x + 1, y + height - 1, x + width - 1, y + height - 1, highlight_color);
 }
 
 // »æÖÆÎı¾
-void tetris_draw_text(uint16_t *framebuffer, const char *text, int x, int y, uint16_t color) {
-    // ¼òµ¥µÄÎı¾»æÖƺ¯Êý£¬Ö»Ö§³ÖÊý×ֺʹóд×Öĸ
-    const uint8_t font[] = {
-        // 0
-        0x3E, 0x41, 0x41, 0x41, 0x3E,
-        // 1
-        0x00, 0x21, 0x7F, 0x01, 0x00,
-        // 2
-        0x21, 0x43, 0x45, 0x49, 0x31,
-        // 3
-        0x22, 0x41, 0x49, 0x49, 0x36,
-        // 4
-        0x0C, 0x14, 0x24, 0x7F, 0x04,
-        // 5
-        0x72, 0x51, 0x51, 0x51, 0x4E,
-        // 6
-        0x1E, 0x29, 0x49, 0x49, 0x06,
-        // 7
-        0x40, 0x47, 0x48, 0x50, 0x60,
-        // 8
-        0x36, 0x49, 0x49, 0x49, 0x36,
-        // 9
-        0x30, 0x49, 0x49, 0x4A, 0x3C,
-        // A
-        0x1E, 0x21, 0x21, 0x21, 0x1E,
-        // B
-        0x3F, 0x49, 0x49, 0x49, 0x26,
-        // C
-        0x1E, 0x21, 0x21, 0x21, 0x21,
-        // D
-        0x3F, 0x21, 0x21, 0x21, 0x1E,
-        // E
-        0x3F, 0x49, 0x49, 0x49, 0x41,
-        // F
-        0x3F, 0x48, 0x48, 0x48, 0x40,
-        // G
-        0x1E, 0x21, 0x29, 0x29, 0x3E,
-        // H
-        0x3F, 0x08, 0x08, 0x08, 0x3F,
-        // I
-        0x00, 0x41, 0x3F, 0x41, 0x00,
-        // J
-        0x06, 0x01, 0x41, 0x3F, 0x00,
-        // K
-        0x3F, 0x08, 0x14, 0x22, 0x41,
-        // L
-        0x3F, 0x01, 0x01, 0x01, 0x01,
-        // M
-        0x3F, 0x20, 0x10, 0x20, 0x3F,
-        // N
-        0x3F, 0x20, 0x10, 0x08, 0x3F,
-        // O
-        0x1E, 0x21, 0x21, 0x21, 0x1E,
-        // P
-        0x3F, 0x48, 0x48, 0x48, 0x30,
-        // Q
-        0x1E, 0x21, 0x25, 0x22, 0x1D,
-        // R
-        0x3F, 0x48, 0x4C, 0x4A, 0x31,
-        // S
-        0x26, 0x49, 0x49, 0x49, 0x32,
-        // T
-        0x40, 0x40, 0x7F, 0x40, 0x40,
-        // U
-        0x3F, 0x01, 0x01, 0x01, 0x3F,
-        // V
-        0x3F, 0x02, 0x04, 0x02, 0x3F,
-        // W
-        0x3F, 0x10, 0x0C, 0x10, 0x3F,
-        // X
-        0x21, 0x12, 0x0C, 0x12, 0x21,
-        // Y
-        0x21, 0x12, 0x0C, 0x04, 0x04,
-        // Z
-        0x41, 0x43, 0x45, 0x49, 0x41
-    };
-    
-    int char_width = 6;
-    int char_height = 5;
-    
-    while (*text) {
-        char c = *text++;
-        int index = -1;
-        
-        if (c >= '0' && c <= '9') {
-            index = (c - '0') * 5;
-        } else if (c >= 'A' && c <= 'Z') {
-            index = (c - 'A' + 10) * 5;
-        } else if (c == ':') {
-            index = 35 * 5; // Ã°ºÅµÄλÖÃ
-        }
-        
-        if (index >= 0) {
-            for (int i = 0; i < char_height; i++) {
-                uint8_t row = font[index + i];
-                for (int j = 0; j < 5; j++) {
-                    if (row & (1 << (4 - j))) {
-                        if (x + j < SCREEN_WIDTH && y + i < SCREEN_HEIGHT) {
-                            framebuffer[(y + i) * SCREEN_WIDTH + (x + j)] = color;
-                        }
-                    }
-                }
-            }
-        }
-        
-        x += char_width;
-    }
+void tetris_draw_text(const char *text, int x, int y, uint16_t color) {
+    // Ê¹ÓÃLCD_ShowStringº¯Êý»æÖÆÎı¾
+    LCD_ShowString(x, y, (u8*)text, color, BLACK, 16, 0);
 }
 
 // »æÖÆÊý×Ö
-void tetris_draw_number(uint16_t *framebuffer, int number, int x, int y, uint16_t color) {
+void tetris_draw_number(int number, int x, int y, uint16_t color) {
     char buffer[10];
     sprintf(buffer, "%d", number);
-    tetris_draw_text(framebuffer, buffer, x, y, color);
+    tetris_draw_text(buffer, x, y, color);
 }
index 4fee1c8bc3359c453c4fde1174cbbfb933188208..41bef69a2a2ad2257518a026624b1916ffffedd3 100644 (file)
@@ -3,14 +3,15 @@
 
 #include <stdbool.h>
 #include <stdint.h>
+#include "lcdshow.h"
 
-// ÆÁÄ»²ÎÊý
+// ÆÁÄ»³ß´ç
 #define SCREEN_WIDTH  240
 #define SCREEN_HEIGHT 240
 
-// ÓÎÏ·ÇøÓò²ÎÊý
-#define BOARD_WIDTH   10
-#define BOARD_HEIGHT  20
+// ÓÎÏ·ÇøÓò³ß´ç
+#define BOARD_WIDTH   10  //240 -±ß¿ò10x2£¬ Ã¿¸öµ¥Ôª¸ñ12x12  10¸öµ¥Ôª¸ñ +¿Õ4¸öµ¥Ôª¸ñ + Ô¤ÀÀ 6¸öµ¥Ôª¸ñ
+#define BOARD_HEIGHT  20  //240 -±ß¿ò10x2£¬ Ã¿¸öµ¥Ôª¸ñ12x12  x 20¸öµ¥Ôª¸ñ
 
 // ·½¿é½á¹¹
 typedef struct {
@@ -22,6 +23,8 @@ typedef struct {
 
 // ÓÎϷ״̬
 typedef struct {
+    //µ±Ç°ÏÔʾ״̬£¬ÓÃÓÚÅжϱ仯²¿Î»Ë¢ÐÂ
+
     uint8_t board[BOARD_HEIGHT][BOARD_WIDTH];
     TetrisPiece current_piece;
     TetrisPiece next_piece;
@@ -33,22 +36,21 @@ typedef struct {
 // ÓÎϷ״̬ÍⲿÉùÃ÷
 extern TetrisGame game;
 
-// º¯ÊýÉùÃ÷
-void tetris_init();
-void tetris_spawn_piece();
+// ÓÎÏ·º¯Êý
+void tetris_init( void );
+void tetris_spawn_piece( void );
 bool tetris_is_valid_position(int x, int y, int type, int rotation);
 bool tetris_move_piece(int dx, int dy);
-bool tetris_rotate_piece();
-bool tetris_drop_piece();
-void tetris_hard_drop();
-void tetris_lock_piece();
-void tetris_clear_lines();
-void tetris_update();
-void tetris_draw(uint16_t *framebuffer);
-void tetris_draw_rect(uint16_t *framebuffer, int x1, int y1, int x2, int y2, uint16_t color);
-void tetris_draw_cell(uint16_t *framebuffer, int x, int y, int width, int height, uint16_t color);
-void tetris_draw_line(uint16_t *framebuffer, int x1, int y1, int x2, int y2, uint16_t color);
-void tetris_draw_text(uint16_t *framebuffer, const char *text, int x, int y, uint16_t color);
-void tetris_draw_number(uint16_t *framebuffer, int number, int x, int y, uint16_t color);
+bool tetris_rotate_piece( void );
+bool tetris_drop_piece( void );
+void tetris_hard_drop( void );
+void tetris_lock_piece( void );
+void tetris_clear_lines( void );
+void tetris_update( void );
+void tetris_draw( void );
+void tetris_draw_rect(int x1, int y1, int x2, int y2, uint16_t color);
+void tetris_draw_cell(int x, int y, int width, int height, uint16_t color);
+void tetris_draw_text(const char *text, int x, int y, uint16_t color);
+void tetris_draw_number(int number, int x, int y, uint16_t color);
 
 #endif // TETRIS_H