add sound task
authorkyz <kongyuzhong@google.com>
Fri, 19 Dec 2025 08:53:49 +0000 (16:53 +0800)
committerkyz <kongyuzhong@google.com>
Fri, 19 Dec 2025 08:53:49 +0000 (16:53 +0800)
20240801完整版/扳手下位机软件/cpbs-202103-20240423/game/game.c
20240801完整版/扳手下位机软件/cpbs-202103-20240423/game/tetris.c

index 084e9c9eefc9278b263d1f49b5e323e4cbc5ab94..dc92b537ecafc7c73689ce61fd31e25124eca2ca 100644 (file)
@@ -14,9 +14,6 @@
 #include "key.h"
 #include "tetris.h"
 
-// Ä£ÄâÖ¡»º³åÇø
-uint16_t framebuffer[SCREEN_WIDTH * SCREEN_HEIGHT];
-
 
 // 5ÃëÒôЧÊý¾Ý£ºÆµÂÊ(Hz), Õ¼¿Õ±È(%), ³ÖÐøÊ±¼ä(ms)
 typedef struct {
@@ -25,6 +22,13 @@ typedef struct {
     uint16_t dur;
 } SoundNote_t;
 
+// ±³¾°ÒôÀÖ£¨¼òµ¥Ñ­»·ÐýÂÉ£©
+SoundNote_t backgroundMusic[] = {
+    {330, 30, 250}, {392, 30, 250}, {440, 30, 250}, {392, 30, 250},
+    {330, 30, 250}, {392, 30, 250}, {440, 30, 250}, {523, 30, 250},
+    {587, 30, 500}, {523, 30, 250}, {440, 30, 250}, {392, 30, 250},
+    {330, 30, 750}
+};
 // 5ÃëÑÝʾÒôЧ£¨¼òµ¥ÐýÂÉ£©
 SoundNote_t demoSound[] = {
     {523, 50, 200}, {587, 50, 200}, {659, 50, 200}, {698, 50, 200},
@@ -37,33 +41,87 @@ SoundNote_t demoSound[] = {
     {523, 50, 800}, {0, 0, 200}, {523, 50, 1000}
 };
 
-#define DEMO_SOUND_LENGTH (sizeof(demoSound) / sizeof(SoundNote_t))
+typedef struct{
+    SoundNote_t *sound;
+    uint8_t len; // ÒôЧÊý×鳤¶È
+    uint8_t offset; // ÒôЧÊý×éÆ«ÒÆÁ¿
+    uint16_t du; // ³ÖÐøÊ±¼ä
+    uint8_t repeat; // Öظ´
+    uint8_t start; // ¿ªÊ¼²¥·Å
+}play_cmd_t;
 
-// ²¥·Å5ÃëÑÝʾÒôЧ
-void playDemoSound(void) {
-    uint8_t i;
-    for (i = 0; i < DEMO_SOUND_LENGTH; i++) {
-        if (demoSound[i].freq > 0) {
-            startBuzzer(demoSound[i].freq, demoSound[i].dc);
-        }
-        delay_ms(demoSound[i].dur);
-        stopBuzzer();
-    }
+play_cmd_t play_cmd;
+
+void sound_play( SoundNote_t *sound, uint8_t len, uint32_t repeat )
+{
+    //lock
+    play_cmd.sound = sound;
+    play_cmd.len = len;
+    play_cmd.offset = 0;
+    play_cmd.du = 0;
+    play_cmd.repeat = repeat;
+    play_cmd.start = 1;
+    //unlock
+}
+void sound_stop( SoundNote_t *sound, uint8_t len, uint32_t repeat )
+{
+    //lock
+    play_cmd.sound = NULL;
+    play_cmd.len = 0;
+    play_cmd.offset = 0;
+    play_cmd.du = 0;
+    play_cmd.repeat = 0;
+    play_cmd.start = 1;
+    //unlock
 }
 
 void sound_task(void *pvParameters)
 {
+    SoundNote_t *note;
+    uint32_t last_tick;
     vTaskDelay(3000);
-    playDemoSound();
+       sound_play( backgroundMusic, sizeof(backgroundMusic)/sizeof(backgroundMusic[0]), 0 );
+    last_tick = xTaskGetTickCount();
     while(1){
-        vTaskDelay(10);
+        vTaskDelayUntil(&last_tick, 50);
+        if( !play_cmd.sound && play_cmd.start ){
+            play_cmd.start = 0;
+            stopBuzzer();
+            continue;
+        }
+        if( play_cmd.sound && play_cmd.len && play_cmd.start ){
+            if( play_cmd.offset >= play_cmd.len ){
+
+                if( play_cmd.repeat ){
+                    play_cmd.offset = 0;
+                    play_cmd.du = 0;
+                    play_cmd.repeat--;
+                }else{
+                    play_cmd.start = 0;
+                    stopBuzzer();
+                    continue;
+                }
+            }
+
+            note = &play_cmd.sound[play_cmd.offset];
+            if( play_cmd.du >= note->dur ){
+                play_cmd.du = 0;
+                play_cmd.offset++;
+                               stopBuzzer();
+            }else{
+                if( play_cmd.du == 0 ){
+                    startBuzzer(note->freq, note->dc);
+                                       //printf( "-----------[%d] start [%d/%d] freq:%d, dc:%d\n", xTaskGetTickCount(), play_cmd.offset, play_cmd.len, note->freq, note->dc );
+                               }
+                play_cmd.du += 50;
+            }
+        }
     }
 }
 
 void game_task(void *pvParameters)
 {
     uint32_t ms = 0;
-    uint32_t update = 0;
     uint8_t key;
     static int k1,k2,k3,k4;
 
@@ -79,13 +137,8 @@ void game_task(void *pvParameters)
     // ³õʼ»¯ÓÎÏ·
     tetris_init();
 
-    k1 = KEY1;
-    k2 = KEY2;
-    k3 = KEY3;
-    k4 = KEY4;
-
-    while(1)
-    {
+    k1 = KEY1; k2 = KEY2; k3 = KEY3; k4 = KEY4;
+    while(1){
         vTaskDelay(10);
         ms += 10;
         if(k1 != KEY1 && k1 )
@@ -99,54 +152,26 @@ void game_task(void *pvParameters)
         else
                 key = KEY_NOPRES;
 
-        k1 = KEY1;
-        k3 = KEY3;
-        k2 = KEY2;
-        k4 = KEY4;
-        update = 0;
-        switch (key) {
-            case KEY1_PRES:
-                printf( "--KEY1 PRESSED\n");
-                tetris_move_piece(1, 0);
-                update = 1;
-
-                break;
-            case KEY2_PRES:
-                printf( "--KEY2 PRESSED\n");
-                tetris_move_piece(-1, 0);
-                update = 1;
-                break;
-            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 KEY4_Long_PRES:
-                printf( "--KEY4 Long PRESSED\n");
-                tetris_hard_drop();
-                update = 1;
-                break;
-            default:
-                // ¿ØÖÆÓÎÏ·ËÙ¶È
-                // ¸ù¾Ýµ±Ç°µÈ¼¶µ÷ÕûÑÓ³Ùʱ¼ä
-                if( ms >= 1000 )
-                    update = 1;
-                break;
+        k1 = KEY1; k2 = KEY2; k3 = KEY3; k4 = KEY4;
+        if( key == KEY1_PRES ){
+            tetris_move_piece(1, 0);
+        }else if( key == KEY2_PRES ){
+            tetris_move_piece(-1, 0);
+        }else if( key == KEY3_PRES ){
+            tetris_rotate_piece();
+        }else if( key == KEY4_PRES ){
+            tetris_drop_piece();
+        }else if( key == KEY4_Long_PRES ){
+            tetris_hard_drop();
         }
-        if( update ){
+
+        if( ms * game.level > 1000 ){
             // ¸üÐÂÓÎϷ״̬
             tetris_update();
-
-            // »æÖÆÓÎÏ·
-            tetris_draw();
             ms = 0;
         }
-
+        // »æÖÆÓÎÏ·
+        tetris_draw();
     }
 }
 
@@ -158,5 +183,5 @@ void game_start(void)
 {
     // ´´½¨ÓÎÏ·ÈÎÎñ
     xTaskCreate(game_task, "Game Task", 2048/4, NULL, 1, &game_task_handle);
-    xTaskCreate(sound_task, "Sound Task", 256/4, NULL, 1, &sound_task_handle);
+    //xTaskCreate(sound_task, "Sound Task", 1024/4, NULL, 1, &sound_task_handle);
 }
index bd0bfe89113ef2e41d905360b8c5398f36867663..32f37a81abd29e2cc9fbbd5a558e459c758f924a 100644 (file)
@@ -72,6 +72,8 @@ const uint16_t colors[8] = {
     0x780F   // ×ÏÉ« (ZÐÍ)
 };
 
+void draw_screen( void );
+
 // ³õʼ»¯ÓÎÏ·
 void tetris_init( void ) {
     extern u32 motor_current_avg_1;
@@ -87,23 +89,16 @@ void tetris_init( void ) {
     game.game_over = false;
 
     // Éú³ÉµÚÒ»¸ö·½¿é
+    game.next_piece.type = rand() % 7;
     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);
 
+    draw_screen();
 }
 
 // Éú³Éз½¿é
-void tetris_spawn_piece( void ) {
-    // Ëæ»úÑ¡Ôñ·½¿éÀàÐÍ
-    static int flag = 0;
-    if( !flag ){
-        game.next_piece.type = rand() % 7;
-        flag = 1;
-    }
+void tetris_spawn_piece( void )
+{
     game.current_piece.type = game.next_piece.type;
     game.current_piece.rotation = 0;
     game.current_piece.x = BOARD_WIDTH / 2 - 2;
@@ -115,7 +110,7 @@ void tetris_spawn_piece( void ) {
         return;
     }
 
-    // Éú³ÉÏÂÒ»¸ö·½¿é
+    // Éú³ÉÏÂÒ»¸ö·½¿é  Ëæ»úÑ¡Ôñ·½¿éÀàÐÍ
     game.next_piece.type = rand() % 7;
     game.next_piece.rotation = 0;
 }
@@ -271,7 +266,7 @@ void draw_board_block( int row, int col, int color )
 {
     int x,y;
     int cell_size = 12;
-    x = 0 + col * cell_size + 1;
+    x = 10 + col * cell_size + 1;
     y = 0 + row * cell_size + 1;
     tetris_draw_cell(x, y, cell_size - 1, cell_size - 1, color);
 }
@@ -343,6 +338,24 @@ void tetris_draw( void )
     to_disp.score = game.score;
     to_disp.level = game.level;
     to_disp.game_over = game.game_over;
+    if( to_disp.score != disp_info.score ){
+        tetris_draw_number(to_disp.score, 140 + 60, 80, WHITE);
+        disp_info.score;
+    }
+    if( to_disp.level != disp_info.level ){
+        tetris_draw_number(to_disp.level, 140 + 60, 120, WHITE);
+        disp_info.level = to_disp.level;
+    }
+    if( to_disp.game_over != disp_info.game_over ){
+        if( to_disp.game_over ){
+            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(to_disp.score, SCREEN_WIDTH / 2 + 10, SCREEN_HEIGHT / 2 + 10, WHITE);
+        }
+        disp_info.game_over = to_disp.game_over;
+    }
 #endif
 
     if( memcmp( &to_disp, &disp_info, sizeof(disp_info_t)) != 0)
@@ -403,3 +416,20 @@ void tetris_draw_number(int number, int x, int y, uint16_t color) {
     sprintf(buffer, "%d", number);
     tetris_draw_text(buffer, x, y, color);
 }
+
+void draw_screen( void )
+{
+    //ÇåÆÁ
+       LCD_Fill(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0x0000);
+
+    // »æÖÆÓÎÏ·ÇøÓò±ß¿ò
+    LCD_DrawLine(10, 0, 10, 239, YELLOW);
+    LCD_DrawLine(130, 0, 130, 239, YELLOW);
+
+    tetris_draw_text("Score:", 140, 80, WHITE);
+    tetris_draw_number(game.score, 140 + 60, 80, WHITE);
+
+    tetris_draw_text("Level:", 140, 120, WHITE);
+    tetris_draw_number(game.level, 140 + 60, 120, WHITE);
+
+}