fix LVGL screen cleaning code

This commit is contained in:
Mitchell Marino 2025-03-26 19:28:33 -05:00 committed by Mitchell M
parent 43b01f0b2b
commit 12eedf9722
3 changed files with 48 additions and 33 deletions

View File

@ -30,6 +30,7 @@ static std::uniform_int_distribution<> zero_to_one(0, 1);
static std::uniform_int_distribution<> zero_to_two(0, 2);
static std::uniform_int_distribution<> zero_to_three(0, 3);
static lv_obj_t *old_scr;
static lv_obj_t *scr;
static lv_obj_t *text = NULL;
@ -58,13 +59,13 @@ static int part = 0;
static void init_step(void) {
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
scr = lv_disp_get_scr_act(NULL);
scr = lv_obj_create(NULL);
// Set the background color of the display to black.
lv_style_init(&style_screen);
lv_style_set_bg_color(&style_screen, lv_color_black());
lv_style_set_text_font(&style_screen, &lv_font_montserrat_32);
lv_obj_add_style(lv_scr_act(), &style_screen, LV_STATE_DEFAULT);
lv_obj_add_style(scr, &style_screen, LV_STATE_DEFAULT);
// rgb565
lv_color_t green = { .full = 0x0560 };
@ -85,15 +86,18 @@ static void init_step(void) {
lv_style_set_text_color(&blue_text, blue);
text = lv_label_create(scr);
old_scr = lv_scr_act();
lv_scr_load(scr);
xSemaphoreGive(xGuiSemaphore);
}
}
static void clean_up_step(void) {
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
lv_obj_clean(scr);
xSemaphoreGive(xGuiSemaphore);
}
while (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdFALSE) vTaskDelay(pdMS_TO_TICKS(10));
lv_scr_load(old_scr);
lv_obj_del(scr);
xSemaphoreGive(xGuiSemaphore);
}
static void generate_switch_leds(void) {
@ -199,7 +203,6 @@ static void update_lcd_count(int times) {
}
static bool play_part(uint32_t time) {
stop_module_timer();
set_module_time(time);
lcd_clear();
@ -252,12 +255,14 @@ static bool play_part(uint32_t time) {
}
if (get_module_time() <= 0) {
strike("Out of time");
vTaskDelay(pdMS_TO_TICKS(2000));
return false;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
stop_module_timer();
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
lv_label_set_text(text, "");
xSemaphoreGive(xGuiSemaphore);

View File

@ -3,11 +3,10 @@
__attribute__((unused))
static const char *TAG = "step4";
static lv_obj_t *old_scr;
static lv_obj_t* scr;
static lv_obj_t* img;
static bool invisible_blocks = false;
static bool text_output = false;
static const int height = 22;
static const int width = 10;
@ -81,7 +80,7 @@ static int piece_nodes[4][2] = {
lv_obj_t* piece_imgs[4] = {};
TaskHandle_t music_handle;
static bool music_playing;
static std::random_device rd;
static std::mt19937 gen(rd());
@ -153,12 +152,13 @@ static int bdca(int i) { // [0,1,2,3] -> [ 0,+2,+1,-1]
static void init_screen() {
while (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdFALSE) vTaskDelay(pdMS_TO_TICKS(10));
scr = lv_obj_create(NULL);
img = lv_img_create(lv_scr_act());
img = lv_img_create(scr);
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());
line_clear_img = lv_img_create(scr);
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));
@ -169,37 +169,44 @@ static void init_screen() {
lv_style_set_bg_opa(&game_over_style, LV_OPA_100);
lv_style_set_text_align(&game_over_style, LV_TEXT_ALIGN_CENTER);
game_over_label = lv_label_create(lv_scr_act());
game_over_label = lv_label_create(scr);
lv_obj_add_flag(game_over_label, LV_OBJ_FLAG_HIDDEN);
lv_obj_align(game_over_label, LV_ALIGN_CENTER, 0, 0);
lv_obj_align(game_over_label, LV_ALIGN_LEFT_MID, 0, 0);
lv_obj_set_width(game_over_label, 160);
lv_obj_add_style(game_over_label, &game_over_style, LV_STATE_DEFAULT);
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
visual_board[h][w] = lv_img_create(lv_scr_act());
visual_board[h][w] = lv_img_create(scr);
lv_obj_align(visual_board[h][w], LV_ALIGN_BOTTOM_LEFT, 159 + w*16, -(h*16));
lv_obj_add_flag(visual_board[h][w], LV_OBJ_FLAG_HIDDEN);
}
}
for (int i = 0; i < 4; i++) {
piece_imgs[i] = lv_img_create(lv_scr_act());
piece_imgs[i] = lv_img_create(scr);
lv_obj_align(piece_imgs[i], LV_ALIGN_BOTTOM_LEFT, 159, -320);
}
old_scr = lv_scr_act();
lv_scr_load(scr);
xSemaphoreGive(xGuiSemaphore);
// play_clip_wav(MUSIC_FILE, true, true, 4, 0);
play_clip_wav(MUSIC_FILE, true, true, 4, 0);
music_playing = true;
}
static void deinit_screen() {
while (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdFALSE) vTaskDelay(pdMS_TO_TICKS(10));
lv_obj_clean(lv_scr_act());
lv_scr_load(old_scr);
lv_obj_del(scr);
xSemaphoreGive(xGuiSemaphore);
if (!stop_clip(MUSIC_FILE, pdMS_TO_TICKS(1000))) {
ESP_LOGW(TAG, "Can't stop, addicted to the shindig.");
}
music_playing = false;
}
static void reveal_board() {
@ -275,6 +282,20 @@ bool play_game(int time, int required_score) {
}
}
// Toggle music with switch4
SwitchKey switch_;
while (get_switch_flipped(&switch_)) {
if (switch_ == SwitchKey::s4) {
if (music_playing) {
stop_clip(MUSIC_FILE, 0);
music_playing = false;
} else {
play_clip_wav(MUSIC_FILE, true, true, 4, 0);
music_playing = true;
}
}
}
if (down_held != 0) {
// check repeat
TickType_t now = xTaskGetTickCount();
@ -377,7 +398,6 @@ void step4() {
while (!play_game(7*60*1000, 8)) fail();
play_clip_wav(MOUNT_POINT "/stepdone.wav", true, false, 1, 0);
complete();
// vTaskDelay(pdMS_TO_TICKS(3000));
deinit_screen();
}
@ -396,16 +416,6 @@ static void show_board() {
board[p[0]][p[1]] = 9;
}
if (text_output) {
for (int h = height-1; h >= 0; h--) {
for (int w = 0; w < width; w++) {
printf("|%c", board[h][w] == 9 ? 'X' : (invisible_blocks ? '0' : ('0' + board[h][w])));
}
printf("|\n");
}
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];

View File

@ -10,8 +10,8 @@
#include "../drivers/char_lcd.h"
#include "../helper.h"
// #define TETRIS_USE_FLASH_IMG
// #define TETRIS_USE_FLASH_BG_IMG
#define TETRIS_USE_FLASH_IMG
#define TETRIS_USE_FLASH_BG_IMG
void step4(void);