Fix tetris

This commit is contained in:
Mitchell Marino 2024-08-10 00:26:24 -05:00
parent 65203c309f
commit 005976cfb1
4 changed files with 84 additions and 45 deletions

View File

@ -22,7 +22,7 @@ esp_err_t play_raw(const char *fp) {
bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh);
for (int i = 0; i < bytes_read; i++) {
write_buf[i] = read_buf[i];
write_buf[i] = read_buf[i] << 4;
}
// i2s_channel_enable(tx_handle);
@ -39,7 +39,7 @@ esp_err_t play_raw(const char *fp) {
for (int i = 0; i < bytes_read; i++) {
write_buf[i] = read_buf[i] << 4;
}
ESP_LOGV(TAG, "Bytes read: %d", bytes_read);
vTaskDelay(pdMS_TO_TICKS(10));
}
i2s_channel_disable(tx_chan);

View File

@ -42,7 +42,6 @@ extern "C" void app_main(void) {
// create_demo_ui();
clean_bomb();
step0();
set_game_time(30000);
start_game_timer();

View File

@ -15,16 +15,22 @@ static const int width = 10;
static int board[height][width] = {0};
static lv_obj_t* visual_board[height][width] = {0};
static const char* BACKGROUND_FILE_NAME = "A:/sdcard/bg.bin";
static lv_obj_t* line_clear_img;
static const void* LINE_CLEAR_SRC = (void*)"A:/sdcard/clear.bin";
static const void* BACKGROUND_SRC = (void*)"A:/sdcard/bg.bin";
// LV_IMG_DECLARE(background);
// static const void* BACKGROUND_SRC = (void*)&background;
static const char* PIECE_IMG_SRC[] = {
"A:/sdcard/red.bin",
NULL,
"A:/sdcard/lb.bin",
"A:/sdcard/db.bin",
"A:/sdcard/orange.bin",
"A:/sdcard/yellow.bin",
"A:/sdcard/green.bin",
"A:/sdcard/purple.bin",
"A:/sdcard/red.bin",
};
static bool game = true;
@ -44,13 +50,13 @@ lv_obj_t* piece_imgs[4] = {};
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> piece_dist(2, 6);
std::uniform_int_distribution<> piece_dist(2, 7);
static void generate_block(void);
static void show_board(void);
static void get_node_locations(void);
static void line_clear(int height);
static void line_clear(int hi);
static void check_line_clears(void);
static void place_piece(void);
@ -111,25 +117,40 @@ static int bdca(int i) { // [0,1,2,3] -> [ 0,+2,+1,-1]
return map[i];
}
static void music_task(void* arg) {
while (1) {
play_raw(MOUNT_POINT "/tetris.pcm");
}
}
static void init_screen(void) {
// create new screen
// scr = lv_obj_create(NULL);
// lv_style_init(&scr_style);
// lv_style_set_bg_color(&scr_style, lv_color_black());
// lv_obj_add_style(scr, &scr_style, LV_STATE_DEFAULT);
// lv_scr_load(scr);
while (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdFALSE) vTaskDelay(pdMS_TO_TICKS(10));
img = lv_img_create(lv_scr_act());
lv_img_set_src(img, BACKGROUND_FILE_NAME);
lv_img_set_src(img, BACKGROUND_SRC);
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
line_clear_img = lv_img_create(lv_scr_act());
lv_obj_add_flag(line_clear_img, LV_OBJ_FLAG_HIDDEN);
lv_img_set_src(line_clear_img, LINE_CLEAR_SRC);
lv_obj_align(line_clear_img, LV_ALIGN_BOTTOM_LEFT, 159, -(height*16));
// for (int h = 0; h < height; h++) {
// for (int w = 0; w < width; w++) {
// visual_board[h][w] = lv_img_create(lv_scr_act());
// lv_obj_align(visual_board[h][w], LV_ALIGN_BOTTOM_LEFT, 159 + w*16, -(h*16));
// lv_img_set_src(visual_board[h][w], PIECE_IMG_SRC[((w+h)%7)+1]);
// }
// }
for (int i = 0; i < 4; i++) {
piece_imgs[i] = lv_img_create(lv_scr_act());
lv_obj_align(piece_imgs[i], LV_ALIGN_BOTTOM_LEFT, 159, -320);
}
// play_raw(MOUNT_POINT "/tetris.pcm");
xSemaphoreGive(xGuiSemaphore);
xTaskCreate(music_task, "music", 4096, NULL, 5, NULL);
}
static void deinit_screen(void) {
@ -172,7 +193,7 @@ void step2(void) {
int* p = piece_nodes[i];
printf("PieceLocation: %d, %d\n", p[0], p[1]);
}
printf("PieceRotaition: %d\n", piece_rotation);
printf("PieceRotation: %d\n", piece_rotation);
}
vTaskDelay(pdMS_TO_TICKS(10));
@ -208,13 +229,14 @@ static void show_board(void) {
printf("\n");
}
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
for (int i = 0; i < sizeof(piece_nodes)/sizeof(piece_nodes[0]); i++) {
int* p = piece_nodes[i];
lv_obj_t* piece_img = piece_imgs[i];
// lv_img_set_src(piece_img, FILE_NAME[piece]);
lv_obj_align(piece_img, LV_ALIGN_BOTTOM_LEFT, 159 + p[1]*16, -(p[0]*16));
}
xSemaphoreGive(xGuiSemaphore);
}
}
static void generate_block(void) {
@ -243,10 +265,13 @@ static void generate_block(void) {
}
}
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
for (int i = 0; i < 4; i++) {
lv_obj_t* piece_img = piece_imgs[i];
lv_img_set_src(piece_img, PIECE_IMG_SRC[piece]);
}
xSemaphoreGive(xGuiSemaphore);
}
}
static void rotate_block(void) {
@ -349,6 +374,13 @@ static void place_piece(void) {
}
}
}
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
for (int i = 0; i < sizeof(piece_nodes)/sizeof(piece_nodes[0]); i++) {
int* p = piece_nodes[i];
lv_obj_align(piece_imgs[i], LV_ALIGN_BOTTOM_LEFT, 159, -(height*16));
}
xSemaphoreGive(xGuiSemaphore);
}
ESP_LOGI(TAG, "Placed Piece: %d", piece);
}
@ -439,12 +471,32 @@ static void check_line_clears(void) {
}
static void line_clear(int height) {
for (int h = height; h < height-1; h++) {
static void line_clear(int hi) {
for (int h = hi; h < height; h++) {
for (int w = 0; w < width; w++) {
board[h][w] = board[h+1][w];
}
}
for (int w = 0; w < width; w++) {
board[height-1][w] = 0;
}
score++;
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
lv_obj_align(line_clear_img, LV_ALIGN_BOTTOM_LEFT, 159, -(hi*16));
xSemaphoreGive(xGuiSemaphore);
}
for (int i = 0; i < 3; i++) {
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
lv_obj_clear_flag(line_clear_img, LV_OBJ_FLAG_HIDDEN);
xSemaphoreGive(xGuiSemaphore);
}
vTaskDelay(pdMS_TO_TICKS(300));
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
lv_obj_add_flag(line_clear_img, LV_OBJ_FLAG_HIDDEN);
xSemaphoreGive(xGuiSemaphore);
}
vTaskDelay(pdMS_TO_TICKS(300));
}
}

View File

@ -1,6 +1,6 @@
#
# Automatically generated file. DO NOT EDIT.
# Espressif IoT Development Framework (ESP-IDF) 5.2.2 Project Configuration
# Espressif IoT Development Framework (ESP-IDF) 5.2.1 Project Configuration
#
CONFIG_SOC_MPU_MIN_REGION_SIZE=0x20000000
CONFIG_SOC_MPU_REGIONS_MAX_NUM=8
@ -331,7 +331,6 @@ CONFIG_SOC_WIFI_WAPI_SUPPORT=y
CONFIG_SOC_WIFI_CSI_SUPPORT=y
CONFIG_SOC_WIFI_MESH_SUPPORT=y
CONFIG_SOC_WIFI_SUPPORT_VARIABLE_BEACON_WINDOW=y
CONFIG_SOC_WIFI_PHY_NEEDS_USB_WORKAROUND=y
CONFIG_SOC_BLE_SUPPORTED=y
CONFIG_SOC_BLE_MESH_SUPPORTED=y
CONFIG_SOC_BLE_50_SUPPORTED=y
@ -436,7 +435,6 @@ CONFIG_ESP_ROM_HAS_RETARGETABLE_LOCKING=y
CONFIG_ESP_ROM_USB_OTG_NUM=3
CONFIG_ESP_ROM_USB_SERIAL_DEVICE_NUM=4
CONFIG_ESP_ROM_HAS_ERASE_0_REGION_BUG=y
CONFIG_ESP_ROM_HAS_ENCRYPTED_WRITES_USING_LEGACY_DRV=y
CONFIG_ESP_ROM_GET_CLK_FREQ=y
CONFIG_ESP_ROM_HAS_HAL_WDT=y
CONFIG_ESP_ROM_NEEDS_SWSETUP_WORKAROUND=y
@ -500,12 +498,12 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200
#
# Partition Table
#
CONFIG_PARTITION_TABLE_SINGLE_APP=y
# CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set
# CONFIG_PARTITION_TABLE_SINGLE_APP is not set
CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y
# CONFIG_PARTITION_TABLE_TWO_OTA is not set
# CONFIG_PARTITION_TABLE_CUSTOM is not set
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp.csv"
CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp_large.csv"
CONFIG_PARTITION_TABLE_OFFSET=0x8000
CONFIG_PARTITION_TABLE_MD5=y
# end of Partition Table
@ -559,7 +557,6 @@ CONFIG_APPTRACE_LOCK_ENABLE=y
# Bluetooth
#
# CONFIG_BT_ENABLED is not set
CONFIG_BT_ALARM_MAX_NUM=50
# end of Bluetooth
#
@ -747,10 +744,7 @@ CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR=y
#
# GDB Stub
#
CONFIG_ESP_GDBSTUB_ENABLED=y
# CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME is not set
CONFIG_ESP_GDBSTUB_SUPPORT_TASKS=y
CONFIG_ESP_GDBSTUB_MAX_TASKS=32
# end of GDB Stub
#
@ -915,7 +909,6 @@ CONFIG_ESP_PHY_RF_CAL_PARTIAL=y
# CONFIG_ESP_PHY_RF_CAL_NONE is not set
# CONFIG_ESP_PHY_RF_CAL_FULL is not set
CONFIG_ESP_PHY_CALIBRATION_MODE=0
# CONFIG_ESP_PHY_PLL_TRACK_DEBUG is not set
# end of PHY
#
@ -1213,7 +1206,6 @@ CONFIG_FREERTOS_QUEUE_REGISTRY_SIZE=0
CONFIG_FREERTOS_TASK_NOTIFICATION_ARRAY_ENTRIES=1
# CONFIG_FREERTOS_USE_TRACE_FACILITY is not set
# CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS is not set
# CONFIG_FREERTOS_USE_APPLICATION_TASK_TAG is not set
# end of Kernel
#
@ -1501,9 +1493,7 @@ CONFIG_MBEDTLS_CMAC_C=y
CONFIG_MBEDTLS_HARDWARE_AES=y
CONFIG_MBEDTLS_AES_USE_INTERRUPT=y
CONFIG_MBEDTLS_AES_INTERRUPT_LEVEL=0
# CONFIG_MBEDTLS_GCM_SUPPORT_NON_AES_CIPHER is not set
CONFIG_MBEDTLS_HARDWARE_MPI=y
# CONFIG_MBEDTLS_LARGE_KEY_SOFTWARE_MPI is not set
CONFIG_MBEDTLS_MPI_USE_INTERRUPT=y
CONFIG_MBEDTLS_MPI_INTERRUPT_LEVEL=0
CONFIG_MBEDTLS_HARDWARE_SHA=y
@ -1590,7 +1580,7 @@ CONFIG_MBEDTLS_ECP_FIXED_POINT_OPTIM=y
# CONFIG_MBEDTLS_CHACHA20_C is not set
# CONFIG_MBEDTLS_HKDF_C is not set
# CONFIG_MBEDTLS_THREADING_C is not set
CONFIG_MBEDTLS_ERROR_STRINGS=y
# CONFIG_MBEDTLS_LARGE_KEY_SOFTWARE_MPI is not set
# end of mbedTLS
#
@ -2235,8 +2225,6 @@ CONFIG_ESP32_APPTRACE_LOCK_ENABLE=y
# CONFIG_EVENT_LOOP_PROFILING is not set
CONFIG_POST_EVENTS_FROM_ISR=y
CONFIG_POST_EVENTS_FROM_IRAM_ISR=y
CONFIG_GDBSTUB_SUPPORT_TASKS=y
CONFIG_GDBSTUB_MAX_TASKS=32
# CONFIG_OTA_ALLOW_HTTP is not set
# CONFIG_ESP_SYSTEM_PD_FLASH is not set
CONFIG_ESP32S3_DEEP_SLEEP_WAKEUP_DELAY=2000