Skip to content

Commit

Permalink
create wallpaper.c
Browse files Browse the repository at this point in the history
  • Loading branch information
ligenxxxx committed Jun 21, 2024
1 parent fc59a5c commit e973cad
Show file tree
Hide file tree
Showing 24 changed files with 161 additions and 128 deletions.
4 changes: 3 additions & 1 deletion src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "core/settings.h"
#include "core/sleep_mode.h"
#include "core/thread.h"
#include "core/wallpaper.h"
#include "driver/TP2825.h"
#include "driver/beep.h"
#include "driver/dm5680.h"
Expand Down Expand Up @@ -136,7 +137,8 @@ void lvgl_init() {
lv_disp_set_theme(dispp, theme);
lv_obj_set_style_bg_color(lv_scr_act(), lv_color_make(64, 64, 64), 0);

if (load_wallpaper(WALLPAPER_PATH)) {
load_wallpaper(WALLPAPER_PATH);
if (wallpaper_is_used()) {
lv_obj_t *img_obj = lv_img_create(lv_scr_act());
lv_img_set_src(img_obj, &img_menu_bg);
}
Expand Down
77 changes: 0 additions & 77 deletions src/core/osd.c
Original file line number Diff line number Diff line change
Expand Up @@ -992,83 +992,6 @@ void osd_shadow_clear(void) {
}
}

///////////////////////////////////////////////////////////////////////////////
// load_bmp_file
int load_bmp_image(const char *file, uint32_t *obuf, int width, int height) {
char *buf;
struct stat stFile;
FILE *fd;
int size, rd;
int line_size;

fd = fopen(file, "rb");
if (fd == NULL) {
LOGE("%s cannot open", file);
return 0;
}

// get file size
fseek(fd, 0, SEEK_END);
size = ftell(fd);
fseek(fd, 0, SEEK_SET);

buf = (unsigned char *)malloc(size);
if (buf == NULL) {
LOGE("%s error 1", file);
return 0;
}

rd = fread(buf, 1, size, fd);
if (rd != size) {
LOGE("%s error 2", file);
free(buf);
return 0;
}

fclose(fd);

// check image size
bmpFileHead *bmp = (bmpFileHead *)buf;
char *pb = buf + sizeof(bmpFileHead) + bmp->info.biClrUsed;
if (bmp->info.biWidth != width || bmp->info.biHeight != height) {
LOGE("%s error 3", file);
free(buf);
return 0;
}

line_size = (width * 3 + 3) & 0xFFFC; // 4bytes align

int x, y;
uint32_t addr;

for (y = 0; y < height; y++) {
addr = y * line_size;
for (x = 0; x < width; x++)
obuf[(height - y - 1) * width + x] = (0xff << 24) + ((pb[addr + x * 3] & 0xff)) + ((pb[addr + x * 3 + 1] & 0xff) << 8) + ((pb[addr + x * 3 + 2] & 0xff) << 16);
}

free(buf);
return 1;
}

static uint32_t img_menu_bg_data[1920 * 1080]; // 0x00BBGGRR
lv_img_dsc_t img_menu_bg;
uint8_t wallpaper_is_used = 0;
int load_wallpaper(char *file_path) {
if (load_bmp_image(file_path, img_menu_bg_data, 1920, 1080)) {
img_menu_bg.header.cf = LV_IMG_CF_TRUE_COLOR;
img_menu_bg.header.always_zero = 0;
img_menu_bg.header.reserved = 0;
img_menu_bg.header.w = 1920;
img_menu_bg.header.h = 1080;
img_menu_bg.data_size = 1920 * 1080 * LV_COLOR_SIZE / 8;
img_menu_bg.data = (uint8_t *)img_menu_bg_data;
wallpaper_is_used = 1;
return 1;
} else {
return 0;
}
}
///////////////////////////////////////////////////////////////////////////////
// Threads for updating FC OSD

Expand Down
3 changes: 0 additions & 3 deletions src/core/osd.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ typedef struct {
} osd_font_t;

extern uint8_t channel_osd_mode;
extern lv_img_dsc_t img_menu_bg;
extern uint8_t wallpaper_is_used;

int osd_init(void);
int osd_clear(void);
Expand All @@ -109,7 +107,6 @@ void load_fc_osd_font(uint8_t);
void *thread_osd(void *ptr);
void osd_resource_path(char *buf, const char *fmt, osd_resource_t osd_resource_type, ...);
void osd_toggle();
int load_wallpaper(char *file_path);
#ifdef __cplusplus
}
#endif
90 changes: 90 additions & 0 deletions src/core/wallpaper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

#include <log/log.h>
#include <lvgl/lvgl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#include "core/common.hh"
#include "osd.h"
#include "wallpaper.h"

static uint32_t img_menu_bg_data[1920 * 1080]; // 0x00BBGGRR
lv_img_dsc_t img_menu_bg;
static uint8_t wallpaper_state = 0;

int load_bmp_image(const char *file, uint32_t *obuf, int width, int height) {
char *buf;
struct stat stFile;
FILE *fd;
int size, rd;
int line_size;

fd = fopen(file, "rb");
if (fd == NULL) {
LOGE("%s cannot open", file);
return 0;
}

// get file size
fseek(fd, 0, SEEK_END);
size = ftell(fd);
fseek(fd, 0, SEEK_SET);

buf = (unsigned char *)malloc(size);
if (buf == NULL) {
LOGE("%s error 1", file);
return 0;
}

rd = fread(buf, 1, size, fd);
if (rd != size) {
LOGE("%s error 2", file);
free(buf);
return 0;
}

fclose(fd);

// check image size
bmpFileHead *bmp = (bmpFileHead *)buf;
char *pb = buf + sizeof(bmpFileHead) + bmp->info.biClrUsed;
if (bmp->info.biWidth != width || bmp->info.biHeight != height) {
LOGE("%s error 3", file);
free(buf);
return 0;
}

line_size = (width * 3 + 3) & 0xFFFC; // 4bytes align

int x, y;
uint32_t addr;

for (y = 0; y < height; y++) {
addr = y * line_size;
for (x = 0; x < width; x++)
obuf[(height - y - 1) * width + x] = (0xff << 24) + ((pb[addr + x * 3] & 0xff)) + ((pb[addr + x * 3 + 1] & 0xff) << 8) + ((pb[addr + x * 3 + 2] & 0xff) << 16);
}

free(buf);
return 1;
}

void load_wallpaper(char *file_path) {
if (load_bmp_image(file_path, img_menu_bg_data, 1920, 1080)) {
img_menu_bg.header.cf = LV_IMG_CF_TRUE_COLOR;
img_menu_bg.header.always_zero = 0;
img_menu_bg.header.reserved = 0;
img_menu_bg.header.w = 1920;
img_menu_bg.header.h = 1080;
img_menu_bg.data_size = 1920 * 1080 * LV_COLOR_SIZE / 8;
img_menu_bg.data = (uint8_t *)img_menu_bg_data;
wallpaper_state = 1;
}
}

uint8_t wallpaper_is_used() {
return wallpaper_state;
}
14 changes: 14 additions & 0 deletions src/core/wallpaper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

void load_wallpaper(char *file_path);
uint8_t wallpaper_is_used();

extern lv_img_dsc_t img_menu_bg;

#ifdef __cplusplus
}
#endif
6 changes: 3 additions & 3 deletions src/ui/page_autoscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <minIni.h>

#include "core/osd.h"
#include "core/settings.h"
#include "core/wallpaper.h"
#include "ui/ui_style.h"

static lv_coord_t col_dsc[] = {160, 150, 180, 220, 180, 160, LV_GRID_TEMPLATE_LAST};
Expand All @@ -22,7 +22,7 @@ static lv_obj_t *page_autoscan_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_t *section = lv_menu_section_create(page);
lv_obj_add_style(section, &style_submenu, LV_PART_MAIN);
lv_obj_set_size(section, 1053, 894);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(section, LV_OPA_TRANSP, 0);

create_text(NULL, section, false, "Auto Scan:", LV_MENU_ITEM_BUILDER_VARIANT_2);
Expand All @@ -33,7 +33,7 @@ static lv_obj_t *page_autoscan_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_set_layout(cont, LV_LAYOUT_GRID);
lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_add_style(cont, &style_context, LV_PART_MAIN);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(cont, LV_OPA_50, 0);

lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);
Expand Down
6 changes: 3 additions & 3 deletions src/ui/page_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <minIni.h>

#include "core/common.hh"
#include "core/osd.h"
#include "core/settings.h"
#include "core/wallpaper.h"
#include "driver/rtc.h"
#include "ui/page_common.h"
#include "ui/ui_attribute.h"
Expand Down Expand Up @@ -354,7 +354,7 @@ static lv_obj_t *page_clock_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_t *section = lv_menu_section_create(page);
lv_obj_add_style(section, &style_submenu, LV_PART_MAIN);
lv_obj_set_size(section, 1053, 894);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(section, LV_OPA_TRANSP, 0);

create_text(NULL, section, false, "Clock:", LV_MENU_ITEM_BUILDER_VARIANT_2);
Expand All @@ -365,7 +365,7 @@ static lv_obj_t *page_clock_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_set_layout(cont, LV_LAYOUT_GRID);
lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_add_style(cont, &style_context, LV_PART_MAIN);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(cont, LV_OPA_50, 0);

lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);
Expand Down
6 changes: 3 additions & 3 deletions src/ui/page_elrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#include "core/common.hh"
#include "core/elrs.h"
#include "core/osd.h"
#include "core/settings.h"
#include "core/wallpaper.h"
#include "driver/esp32.h"
#include "page_version.h"
#include "ui/ui_style.h"
Expand Down Expand Up @@ -80,7 +80,7 @@ static lv_obj_t *page_elrs_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_t *section = lv_menu_section_create(page);
lv_obj_add_style(section, &style_submenu, LV_PART_MAIN);
lv_obj_set_size(section, 1053, 894);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(section, LV_OPA_TRANSP, 0);

create_text(NULL, section, false, "ELRS:", LV_MENU_ITEM_BUILDER_VARIANT_2);
Expand All @@ -91,7 +91,7 @@ static lv_obj_t *page_elrs_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_set_layout(cont, LV_LAYOUT_GRID);
lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_add_style(cont, &style_context, LV_PART_MAIN);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(cont, LV_OPA_50, 0);

lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);
Expand Down
6 changes: 3 additions & 3 deletions src/ui/page_fans.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#include "core/app_state.h"
#include "core/common.hh"
#include "core/osd.h"
#include "core/settings.h"
#include "core/wallpaper.h"
#include "driver/fans.h"
#include "driver/nct75.h"
#include "ui/page_common.h"
Expand Down Expand Up @@ -51,7 +51,7 @@ static lv_obj_t *page_fans_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_t *section = lv_menu_section_create(page);
lv_obj_add_style(section, &style_submenu, LV_PART_MAIN);
lv_obj_set_size(section, 1053, 894);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(section, LV_OPA_TRANSP, 0);

create_text(NULL, section, false, "Fans:", LV_MENU_ITEM_BUILDER_VARIANT_2);
Expand All @@ -62,7 +62,7 @@ static lv_obj_t *page_fans_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_set_layout(cont, LV_LAYOUT_GRID);
lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_add_style(cont, &style_context, LV_PART_MAIN);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(cont, LV_OPA_50, 0);

lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);
Expand Down
3 changes: 2 additions & 1 deletion src/ui/page_focus_chart.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "core/osd.h"
#include "core/settings.h"
#include "core/wallpaper.h"
#include "ui/ui_porting.h"

static lv_obj_t *focus_chart_img;
Expand All @@ -18,7 +19,7 @@ lv_obj_t *page_focus_chart_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_t *section = lv_menu_section_create(page);
lv_obj_add_style(section, &style_submenu, LV_PART_MAIN);
lv_obj_set_size(section, 1053, 894);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(section, LV_OPA_TRANSP, 0);

create_text(NULL, section, false, "Focus Chart:", LV_MENU_ITEM_BUILDER_VARIANT_2);
Expand Down
6 changes: 3 additions & 3 deletions src/ui/page_headtracker.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include "common.hh"
#include "core/app_state.h"
#include "core/osd.h"
#include "core/settings.h"
#include "core/wallpaper.h"
#include "ht.h"
#include "page_common.h"
#include "ui/ui_style.h"
Expand Down Expand Up @@ -141,7 +141,7 @@ static lv_obj_t *page_headtracker_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_t *section = lv_menu_section_create(page);
lv_obj_add_style(section, &style_submenu, LV_PART_MAIN);
lv_obj_set_size(section, 1053, 894);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(section, LV_OPA_TRANSP, 0);

create_text(NULL, section, false, "Head Tracker:", LV_MENU_ITEM_BUILDER_VARIANT_2);
Expand All @@ -152,7 +152,7 @@ static lv_obj_t *page_headtracker_create(lv_obj_t *parent, panel_arr_t *arr) {
lv_obj_set_layout(cont, LV_LAYOUT_GRID);
lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_add_style(cont, &style_context, LV_PART_MAIN);
if (wallpaper_is_used)
if (wallpaper_is_used())
lv_obj_set_style_bg_opa(cont, LV_OPA_50, 0);

lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);
Expand Down
Loading

0 comments on commit e973cad

Please sign in to comment.