mirror of
https://github.com/lvgl/lvgl.git
synced 2025-01-21 06:53:01 +08:00
first step to simplify FS API
This commit is contained in:
parent
f30dc5d186
commit
686483be89
@ -277,13 +277,12 @@ lv_res_t lv_img_decoder_built_in_info(lv_img_decoder_t * decoder, const void * s
|
|||||||
}
|
}
|
||||||
#if LV_USE_FILESYSTEM
|
#if LV_USE_FILESYSTEM
|
||||||
else if(src_type == LV_IMG_SRC_FILE) {
|
else if(src_type == LV_IMG_SRC_FILE) {
|
||||||
lv_fs_file_t file;
|
lv_fs_file_t * f;
|
||||||
lv_fs_res_t res;
|
|
||||||
uint32_t rn;
|
uint32_t rn;
|
||||||
res = lv_fs_open(&file, src, LV_FS_MODE_RD);
|
f = lv_fs_open(src, LV_FS_MODE_RD);
|
||||||
if(res == LV_FS_RES_OK) {
|
if(f) {
|
||||||
res = lv_fs_read(&file, header, sizeof(lv_img_header_t), &rn);
|
lv_fs_res_t res = lv_fs_read(f, header, sizeof(lv_img_header_t), &rn);
|
||||||
lv_fs_close(&file);
|
lv_fs_close(f);
|
||||||
if(res != LV_FS_RES_OK || rn != sizeof(lv_img_header_t)) {
|
if(res != LV_FS_RES_OK || rn != sizeof(lv_img_header_t)) {
|
||||||
LV_LOG_WARN("Image get info get read file header");
|
LV_LOG_WARN("Image get info get read file header");
|
||||||
return LV_RES_INV;
|
return LV_RES_INV;
|
||||||
@ -325,9 +324,8 @@ lv_res_t lv_img_decoder_built_in_open(lv_img_decoder_t * decoder, lv_img_decoder
|
|||||||
/*Support only "*.bin" files*/
|
/*Support only "*.bin" files*/
|
||||||
if(strcmp(lv_fs_get_ext(dsc->src), "bin")) return LV_RES_INV;
|
if(strcmp(lv_fs_get_ext(dsc->src), "bin")) return LV_RES_INV;
|
||||||
|
|
||||||
lv_fs_file_t f;
|
lv_fs_file_t * f = lv_fs_open(dsc->src, LV_FS_MODE_RD);
|
||||||
lv_fs_res_t res = lv_fs_open(&f, dsc->src, LV_FS_MODE_RD);
|
if(f == NULL) {
|
||||||
if(res != LV_FS_RES_OK) {
|
|
||||||
LV_LOG_WARN("Built-in image decoder can't open the file");
|
LV_LOG_WARN("Built-in image decoder can't open the file");
|
||||||
return LV_RES_INV;
|
return LV_RES_INV;
|
||||||
}
|
}
|
||||||
@ -415,7 +413,7 @@ lv_res_t lv_img_decoder_built_in_open(lv_img_decoder_t * decoder, lv_img_decoder
|
|||||||
if(dsc->src_type == LV_IMG_SRC_FILE) {
|
if(dsc->src_type == LV_IMG_SRC_FILE) {
|
||||||
/*Read the palette from file*/
|
/*Read the palette from file*/
|
||||||
#if LV_USE_FILESYSTEM
|
#if LV_USE_FILESYSTEM
|
||||||
lv_fs_seek(user_data->f, 4); /*Skip the header*/
|
lv_fs_seek(user_data->f, 4, LV_FS_SEEK_SET); /*Skip the header*/
|
||||||
lv_color32_t cur_color;
|
lv_color32_t cur_color;
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < palette_size; i++) {
|
for(i = 0; i < palette_size; i++) {
|
||||||
@ -552,7 +550,7 @@ static lv_res_t lv_img_decoder_built_in_line_true_color(lv_img_decoder_dsc_t * d
|
|||||||
|
|
||||||
uint32_t pos = ((y * dsc->header.w + x) * px_size) >> 3;
|
uint32_t pos = ((y * dsc->header.w + x) * px_size) >> 3;
|
||||||
pos += 4; /*Skip the header*/
|
pos += 4; /*Skip the header*/
|
||||||
res = lv_fs_seek(user_data->f, pos);
|
res = lv_fs_seek(user_data->f, pos, LV_FS_SEEK_SET);
|
||||||
if(res != LV_FS_RES_OK) {
|
if(res != LV_FS_RES_OK) {
|
||||||
LV_LOG_WARN("Built-in image decoder seek failed");
|
LV_LOG_WARN("Built-in image decoder seek failed");
|
||||||
return LV_RES_INV;
|
return LV_RES_INV;
|
||||||
@ -654,7 +652,7 @@ static lv_res_t lv_img_decoder_built_in_line_alpha(lv_img_decoder_dsc_t * dsc, l
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
#if LV_USE_FILESYSTEM
|
#if LV_USE_FILESYSTEM
|
||||||
lv_fs_seek(user_data->f, ofs + 4); /*+4 to skip the header*/
|
lv_fs_seek(user_data->f, ofs + 4, LV_FS_SEEK_SET); /*+4 to skip the header*/
|
||||||
lv_fs_read(user_data->f, fs_buf, w, NULL);
|
lv_fs_read(user_data->f, fs_buf, w, NULL);
|
||||||
data_tmp = fs_buf;
|
data_tmp = fs_buf;
|
||||||
#else
|
#else
|
||||||
@ -740,7 +738,7 @@ static lv_res_t lv_img_decoder_built_in_line_indexed(lv_img_decoder_dsc_t * dsc,
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
#if LV_USE_FILESYSTEM
|
#if LV_USE_FILESYSTEM
|
||||||
lv_fs_seek(user_data->f, ofs + 4); /*+4 to skip the header*/
|
lv_fs_seek(user_data->f, ofs + 4, LV_FS_SEEK_SET); /*+4 to skip the header*/
|
||||||
lv_fs_read(user_data->f, fs_buf, w, NULL);
|
lv_fs_read(user_data->f, fs_buf, w, NULL);
|
||||||
data_tmp = fs_buf;
|
data_tmp = fs_buf;
|
||||||
#else
|
#else
|
||||||
|
@ -83,51 +83,52 @@ bool lv_fs_is_ready(char letter)
|
|||||||
* @param mode read: FS_MODE_RD, write: FS_MODE_WR, both: FS_MODE_RD | FS_MODE_WR
|
* @param mode read: FS_MODE_RD, write: FS_MODE_WR, both: FS_MODE_RD | FS_MODE_WR
|
||||||
* @return LV_FS_RES_OK or any error from lv_fs_res_t enum
|
* @return LV_FS_RES_OK or any error from lv_fs_res_t enum
|
||||||
*/
|
*/
|
||||||
lv_fs_res_t lv_fs_open(lv_fs_file_t * file_p, const char * path, lv_fs_mode_t mode)
|
void * lv_fs_open(const char * path, lv_fs_mode_t mode)
|
||||||
{
|
{
|
||||||
file_p->drv = NULL;
|
if(path == NULL) {
|
||||||
file_p->file_d = NULL;
|
LV_LOG_WARN("Can't open file: path is NULL");
|
||||||
|
return NULL;
|
||||||
if(path == NULL) return LV_FS_RES_INV_PARAM;
|
}
|
||||||
|
|
||||||
char letter = path[0];
|
char letter = path[0];
|
||||||
|
lv_fs_drv_t * drv = lv_fs_get_drv(letter);
|
||||||
|
|
||||||
file_p->drv = lv_fs_get_drv(letter);
|
|
||||||
|
|
||||||
if(file_p->drv == NULL) {
|
if(drv == NULL) {
|
||||||
|
LV_LOG_WARN("Can't open file (%s): unknown driver letter", path);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(drv->ready_cb) {
|
||||||
|
if(drv->ready_cb(drv) == false) {
|
||||||
|
LV_LOG_WARN("Can't open file (%s): driver not ready", path);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(drv->open_cb == NULL) {
|
||||||
|
LV_LOG_WARN("Can't open file (%s): open function not exists", path);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
lv_fs_file_t * file_p = lv_mem_alloc(sizeof(lv_fs_file_t));
|
||||||
|
if(file_p == NULL) {
|
||||||
|
LV_LOG_WARN("Can't open file (%s): out of memory", path);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
file_p->drv = drv;
|
||||||
file_p->file_d = NULL;
|
file_p->file_d = NULL;
|
||||||
return LV_FS_RES_NOT_EX;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(file_p->drv->ready_cb != NULL) {
|
|
||||||
if(file_p->drv->ready_cb(file_p->drv) == false) {
|
|
||||||
file_p->drv = NULL;
|
|
||||||
file_p->file_d = NULL;
|
|
||||||
return LV_FS_RES_HW_ERR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
file_p->file_d = lv_mem_alloc(file_p->drv->file_size);
|
|
||||||
LV_ASSERT_MEM(file_p->file_d);
|
|
||||||
if(file_p->file_d == NULL) {
|
|
||||||
file_p->drv = NULL;
|
|
||||||
return LV_FS_RES_OUT_OF_MEM; /* Out of memory */
|
|
||||||
}
|
|
||||||
|
|
||||||
if(file_p->drv->open_cb == NULL) {
|
|
||||||
return LV_FS_RES_NOT_IMP;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char * real_path = lv_fs_get_real_path(path);
|
const char * real_path = lv_fs_get_real_path(path);
|
||||||
lv_fs_res_t res = file_p->drv->open_cb(file_p->drv, file_p->file_d, real_path, mode);
|
file_p->file_d = drv->open_cb(drv, real_path, mode);
|
||||||
|
|
||||||
if(res != LV_FS_RES_OK) {
|
if(file_p->file_d == NULL || file_p->file_d == (void*)(-1)) {
|
||||||
lv_mem_free(file_p->file_d);
|
lv_mem_free(file_p);
|
||||||
file_p->file_d = NULL;
|
return NULL;
|
||||||
file_p->drv = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return file_p;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -147,10 +148,7 @@ lv_fs_res_t lv_fs_close(lv_fs_file_t * file_p)
|
|||||||
|
|
||||||
lv_fs_res_t res = file_p->drv->close_cb(file_p->drv, file_p->file_d);
|
lv_fs_res_t res = file_p->drv->close_cb(file_p->drv, file_p->file_d);
|
||||||
|
|
||||||
lv_mem_free(file_p->file_d); /*Clean up*/
|
lv_mem_free(file_p); /*Clean up*/
|
||||||
file_p->file_d = NULL;
|
|
||||||
file_p->drv = NULL;
|
|
||||||
file_p->file_d = NULL;
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -234,7 +232,7 @@ lv_fs_res_t lv_fs_write(lv_fs_file_t * file_p, const void * buf, uint32_t btw, u
|
|||||||
* @param pos the new position expressed in bytes index (0: start of file)
|
* @param pos the new position expressed in bytes index (0: start of file)
|
||||||
* @return LV_FS_RES_OK or any error from lv_fs_res_t enum
|
* @return LV_FS_RES_OK or any error from lv_fs_res_t enum
|
||||||
*/
|
*/
|
||||||
lv_fs_res_t lv_fs_seek(lv_fs_file_t * file_p, uint32_t pos)
|
lv_fs_res_t lv_fs_seek(lv_fs_file_t * file_p, uint32_t pos, lv_fs_whence_t whence)
|
||||||
{
|
{
|
||||||
if(file_p->drv == NULL) {
|
if(file_p->drv == NULL) {
|
||||||
return LV_FS_RES_INV_PARAM;
|
return LV_FS_RES_INV_PARAM;
|
||||||
@ -244,7 +242,7 @@ lv_fs_res_t lv_fs_seek(lv_fs_file_t * file_p, uint32_t pos)
|
|||||||
return LV_FS_RES_NOT_IMP;
|
return LV_FS_RES_NOT_IMP;
|
||||||
}
|
}
|
||||||
|
|
||||||
lv_fs_res_t res = file_p->drv->seek_cb(file_p->drv, file_p->file_d, pos);
|
lv_fs_res_t res = file_p->drv->seek_cb(file_p->drv, file_p->file_d, pos, whence);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ enum {
|
|||||||
typedef uint8_t lv_fs_res_t;
|
typedef uint8_t lv_fs_res_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filesystem mode.
|
* File open mode.
|
||||||
*/
|
*/
|
||||||
enum {
|
enum {
|
||||||
LV_FS_MODE_WR = 0x01,
|
LV_FS_MODE_WR = 0x01,
|
||||||
@ -60,18 +60,28 @@ enum {
|
|||||||
};
|
};
|
||||||
typedef uint8_t lv_fs_mode_t;
|
typedef uint8_t lv_fs_mode_t;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seek modes.
|
||||||
|
*/
|
||||||
|
enum {
|
||||||
|
LV_FS_SEEK_SET = 0x01,
|
||||||
|
LV_FS_SEEK_CUR = 0x02,
|
||||||
|
LV_FS_SEEK_END = 0x03,
|
||||||
|
};
|
||||||
|
typedef uint8_t lv_fs_whence_t;
|
||||||
|
|
||||||
typedef struct _lv_fs_drv_t {
|
typedef struct _lv_fs_drv_t {
|
||||||
char letter;
|
char letter;
|
||||||
uint16_t file_size;
|
|
||||||
uint16_t rddir_size;
|
uint16_t rddir_size;
|
||||||
bool (*ready_cb)(struct _lv_fs_drv_t * drv);
|
bool (*ready_cb)(struct _lv_fs_drv_t * drv);
|
||||||
|
|
||||||
lv_fs_res_t (*open_cb)(struct _lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode);
|
void * (*open_cb)(struct _lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode);
|
||||||
lv_fs_res_t (*close_cb)(struct _lv_fs_drv_t * drv, void * file_p);
|
lv_fs_res_t (*close_cb)(struct _lv_fs_drv_t * drv, void * file_p);
|
||||||
lv_fs_res_t (*remove_cb)(struct _lv_fs_drv_t * drv, const char * fn);
|
lv_fs_res_t (*remove_cb)(struct _lv_fs_drv_t * drv, const char * fn);
|
||||||
lv_fs_res_t (*read_cb)(struct _lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br);
|
lv_fs_res_t (*read_cb)(struct _lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br);
|
||||||
lv_fs_res_t (*write_cb)(struct _lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw);
|
lv_fs_res_t (*write_cb)(struct _lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw);
|
||||||
lv_fs_res_t (*seek_cb)(struct _lv_fs_drv_t * drv, void * file_p, uint32_t pos);
|
lv_fs_res_t (*seek_cb)(struct _lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence);
|
||||||
lv_fs_res_t (*tell_cb)(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p);
|
lv_fs_res_t (*tell_cb)(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p);
|
||||||
lv_fs_res_t (*trunc_cb)(struct _lv_fs_drv_t * drv, void * file_p);
|
lv_fs_res_t (*trunc_cb)(struct _lv_fs_drv_t * drv, void * file_p);
|
||||||
lv_fs_res_t (*size_cb)(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * size_p);
|
lv_fs_res_t (*size_cb)(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * size_p);
|
||||||
@ -143,7 +153,7 @@ bool lv_fs_is_ready(char letter);
|
|||||||
* @param mode read: FS_MODE_RD, write: FS_MODE_WR, both: FS_MODE_RD | FS_MODE_WR
|
* @param mode read: FS_MODE_RD, write: FS_MODE_WR, both: FS_MODE_RD | FS_MODE_WR
|
||||||
* @return LV_FS_RES_OK or any error from lv_fs_res_t enum
|
* @return LV_FS_RES_OK or any error from lv_fs_res_t enum
|
||||||
*/
|
*/
|
||||||
lv_fs_res_t lv_fs_open(lv_fs_file_t * file_p, const char * path, lv_fs_mode_t mode);
|
void * lv_fs_open(const char * path, lv_fs_mode_t mode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close an already opened file
|
* Close an already opened file
|
||||||
@ -185,7 +195,7 @@ lv_fs_res_t lv_fs_write(lv_fs_file_t * file_p, const void * buf, uint32_t btw, u
|
|||||||
* @param pos the new position expressed in bytes index (0: start of file)
|
* @param pos the new position expressed in bytes index (0: start of file)
|
||||||
* @return LV_FS_RES_OK or any error from lv_fs_res_t enum
|
* @return LV_FS_RES_OK or any error from lv_fs_res_t enum
|
||||||
*/
|
*/
|
||||||
lv_fs_res_t lv_fs_seek(lv_fs_file_t * file_p, uint32_t pos);
|
lv_fs_res_t lv_fs_seek(lv_fs_file_t * file_p, uint32_t pos, lv_fs_whence_t whence);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Give the position of the read write pointer
|
* Give the position of the read write pointer
|
||||||
|
Loading…
x
Reference in New Issue
Block a user