RTT下移植LVGL到W601_文件系统移植

RTT下移植LVGL到W601_显示驱动移植

声明

个人学习笔记,不保证正确

参考资料

移植参考

DFS参考

图片

图片在线转换

环境

win10

keil5

硬件

正点原子w601开发板

步骤流程

1.在显示驱动移植完成的基础上添加tf卡驱动,文件系统

RTT下移植LVGL到W601_文件系统移植_第1张图片

RTT下移植LVGL到W601_文件系统移植_第2张图片

RTT下移植LVGL到W601_文件系统移植_第3张图片

更新软件包
 pkgs --update
生成keil工程
scons --target=mdk5

2.移植文件系统到lvgl

1.lvgl官方提供了参考移植文档

RTT下移植LVGL到W601_文件系统移植_第4张图片

2.lv_port_fs.h


#ifndef LV_PORT_FS_H
#define LV_PORT_FS_H

#ifdef __cplusplus
extern "C" {
#endif


#include "lvgl.h"

void lv_port_fs_init(void);

#ifdef __cplusplus
} /*extern "C"*/
#endif

#endif /*LV_PORT_FS_TEMPL_H*/

3.lv_port_fs.c

/**
 * @file lv_port_fs_templ.c
 *
 */

/*Copy this file as "lv_port_fs.c" and set this value to "1" to enable content*/
#if 1

/*********************
 *      INCLUDES
 *********************/
#include "lv_port_fs.h"
#include "lvgl.h"
#include 
/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void *fs_open(lv_fs_drv_t *drv, const char *path, lv_fs_mode_t mode);
static lv_fs_res_t fs_close(lv_fs_drv_t *drv, void *file_p);
static lv_fs_res_t fs_read(lv_fs_drv_t *drv, void *file_p, void *buf, uint32_t btr, uint32_t *br);
static lv_fs_res_t fs_write(lv_fs_drv_t *drv, void *file_p, const void *buf, uint32_t btw, uint32_t *bw);
static lv_fs_res_t fs_seek(lv_fs_drv_t *drv, void *file_p, uint32_t pos, lv_fs_whence_t whence);
//static lv_fs_res_t fs_size(lv_fs_drv_t *drv, void *file_p, uint32_t *size_p);
static lv_fs_res_t fs_tell(lv_fs_drv_t *drv, void *file_p, uint32_t *pos_p);

static void *fs_dir_open(lv_fs_drv_t *drv, const char *path);
static lv_fs_res_t fs_dir_read(lv_fs_drv_t *drv, void *rddir_p, char *fn);
static lv_fs_res_t fs_dir_close(lv_fs_drv_t *drv, void *rddir_p);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 * GLOBAL PROTOTYPES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_fs_init(void)
{

	 
    /*Add a simple drive to open images*/
    static lv_fs_drv_t fs_drv;
    lv_fs_drv_init(&fs_drv);

    /*Set up fields...*/
    fs_drv.letter = 'S';
    fs_drv.open_cb = fs_open;
    fs_drv.close_cb = fs_close;
    fs_drv.read_cb = fs_read;
    fs_drv.write_cb = fs_write;
    fs_drv.seek_cb = fs_seek;
    fs_drv.tell_cb = fs_tell;

    fs_drv.dir_close_cb = fs_dir_close;
    fs_drv.dir_open_cb = fs_dir_open;
    fs_drv.dir_read_cb = fs_dir_read;

    lv_fs_drv_register(&fs_drv);
}

/**
 * Open a file
 * @param drv       pointer to a driver where this function belongs
 * @param path      path to the file beginning with the driver letter (e.g. S:/folder/file.txt)
 * @param mode      read: FS_MODE_RD, write: FS_MODE_WR, both: FS_MODE_RD | FS_MODE_WR
 * @return          a file descriptor or NULL on error
 */
static void *fs_open(lv_fs_drv_t *drv, const char *path, lv_fs_mode_t mode)
{
    int fp;
    int flag;

    switch (mode)
    {
    case LV_FS_MODE_WR:
        flag = O_WRONLY | O_CREAT;
        break;
    case LV_FS_MODE_RD:
        flag = O_WRONLY | O_CREAT;
        break;
    case LV_FS_MODE_RD | LV_FS_MODE_WR:
        flag = O_RDWR | O_CREAT;
        break;
    default:
        return NULL;
        break;
    }

    fp = open(path, flag);
    if (fp < 0)
    {
        return NULL;
    }
    else
    {
        return (void *)fp;
    }
}

/**
 * Close an opened file
 * @param drv       pointer to a driver where this function belongs
 * @param file_p    pointer to a file_t variable. (opened with fs_open)
 * @return          LV_FS_RES_OK: no error or  any error from @lv_fs_res_t enum
 */
static lv_fs_res_t fs_close(lv_fs_drv_t *drv, void *file_p)
{
 
    lv_fs_res_t res = LV_FS_RES_FS_ERR;
    if (close((int)file_p) == 0)
    {
        res = LV_FS_RES_OK;
    }
    return res;
}

/**
 * Read data from an opened file
 * @param drv       pointer to a driver where this function belongs
 * @param file_p    pointer to a file_t variable.
 * @param buf       pointer to a memory block where to store the read data
 * @param btr       number of Bytes To Read
 * @param br        the real number of read bytes (Byte Read)
 * @return          LV_FS_RES_OK: no error or  any error from @lv_fs_res_t enum
 */
static lv_fs_res_t fs_read(lv_fs_drv_t *drv, void *file_p, void *buf, uint32_t btr, uint32_t *br)
{
 
    lv_fs_res_t res = LV_FS_RES_FS_ERR;
    int ret = read((int)file_p, buf, btr);
    if (ret >= 0)
    {
        *br = ret;
        res = LV_FS_RES_OK;
    }
    return res;
}

/**
 * Write into a file
 * @param drv       pointer to a driver where this function belongs
 * @param file_p    pointer to a file_t variable
 * @param buf       pointer to a buffer with the bytes to write
 * @param btr       Bytes To Write
 * @param br        the number of real written bytes (Bytes Written). NULL if unused.
 * @return          LV_FS_RES_OK: no error or  any error from @lv_fs_res_t enum
 */
static lv_fs_res_t fs_write(lv_fs_drv_t *drv, void *file_p, const void *buf, uint32_t btw, uint32_t *bw)
{
 
    lv_fs_res_t res = LV_FS_RES_FS_ERR;
    int ret = read((int)file_p, (void *)buf, btw);
    if (ret >= 0)
    {
        *bw = ret;
        res = LV_FS_RES_OK;
    }

    return res;
}

/**
 * Set the read write pointer. Also expand the file size if necessary.
 * @param drv       pointer to a driver where this function belongs
 * @param file_p    pointer to a file_t variable. (opened with fs_open )
 * @param pos       the new position of read write pointer
 * @param whence    tells from where to interpret the `pos`. See @lv_fs_whence_t
 * @return          LV_FS_RES_OK: no error or  any error from @lv_fs_res_t enum
 */
static lv_fs_res_t fs_seek(lv_fs_drv_t *drv, void *file_p, uint32_t pos, lv_fs_whence_t whence)
{
 
    lv_fs_res_t res = LV_FS_RES_UNKNOWN;

    int fd = (int)file_p;
    if (lseek(fd, pos, SEEK_SET) >= 0)
        res = LV_FS_RES_OK;

    return res;
}
/**
 * Give the position of the read write pointer
 * @param drv       pointer to a driver where this function belongs
 * @param file_p    pointer to a file_t variable.
 * @param pos_p     pointer to to store the result
 * @return          LV_FS_RES_OK: no error or  any error from @lv_fs_res_t enum
 */
static lv_fs_res_t fs_tell(lv_fs_drv_t *drv, void *file_p, uint32_t *pos_p)
{
 
    lv_fs_res_t res = LV_FS_RES_UNKNOWN;

    int fd = (int)file_p;
    off_t pos = lseek(fd, 0, SEEK_CUR);
    if (pos >= 0)
    {
        *pos_p = pos;
        res = LV_FS_RES_OK;
    }

    return res;
}

/**
 * Initialize a 'lv_fs_dir_t' variable for directory reading
 * @param drv       pointer to a driver where this function belongs
 * @param path      path to a directory
 * @return          pointer to the directory read descriptor or NULL on error
 */
static void *fs_dir_open(lv_fs_drv_t *drv, const char *path)
{
 
    void *dir = NULL;
    /*Add your code here*/
    // dir = ... /*Add your code here*/
    return dir;
}

/**
 * Read the next filename form a directory.
 * The name of the directories will begin with '/'
 * @param drv       pointer to a driver where this function belongs
 * @param rddir_p   pointer to an initialized 'lv_fs_dir_t' variable
 * @param fn        pointer to a buffer to store the filename
 * @return          LV_FS_RES_OK: no error or  any error from @lv_fs_res_t enum
 */
static lv_fs_res_t fs_dir_read(lv_fs_drv_t *drv, void *rddir_p, char *fn)
{
 
    lv_fs_res_t res = LV_FS_RES_NOT_IMP;

    /*Add your code here*/

    return res;
}

/**
 * Close the directory reading
 * @param drv       pointer to a driver where this function belongs
 * @param rddir_p   pointer to an initialized 'lv_fs_dir_t' variable
 * @return          LV_FS_RES_OK: no error or  any error from @lv_fs_res_t enum
 */
static lv_fs_res_t fs_dir_close(lv_fs_drv_t *drv, void *rddir_p)
{
 
    lv_fs_res_t res = LV_FS_RES_NOT_IMP;

    /*Add your code here*/

    return res;
}

#else /*Enable this file at the top*/

/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif

4.main.c

/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-05-07     yangjie      first implementation
 */

#include 
#include 
#include 
#include 
#include 
#include 
#define DBG_TAG "MIAN"
#define DBG_LVL DBG_LOG

#include 

static rt_thread_t lvgl_tid = RT_NULL;
static rt_timer_t lvgl_timer = RT_NULL;

#include "lv_port_disp.h"
#include "lv_obj.h"
#include "lv_port_fs.h"

void demo(void)
{
    lv_obj_t *label1 = lv_label_create(lv_scr_act());
    lv_label_set_long_mode(label1, LV_LABEL_LONG_WRAP); /*Break the long lines*/
    lv_label_set_recolor(label1, true);                 /*Enable re-coloring by commands in the text*/
    lv_label_set_text(label1, "#0000ff Re-color# #ff00ff words# #ff0000 of a# label, align the lines to the center "
                              "and wrap long text automatically.");
    lv_obj_set_width(label1, 150); /*Set smaller width to make the lines wrap*/
    lv_obj_set_style_text_align(label1, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_align(label1, LV_ALIGN_CENTER, 0, -40);

    lv_obj_t *label2 = lv_label_create(lv_scr_act());
    lv_label_set_long_mode(label2, LV_LABEL_LONG_SCROLL_CIRCULAR); /*Circular scroll*/
    lv_obj_set_width(label2, 150);
    lv_label_set_text(label2, "It is a circularly scrolling text. ");
    lv_obj_align(label2, LV_ALIGN_CENTER, 0, 40);
}
MSH_CMD_EXPORT(demo, demo);

void demo_fs(int argc,char *argv[])
{
	char temp_buf[50];
	
	if(argc==1)
	{
		rt_kprintf("sys para error");
		return ;
	}
	rt_sprintf(temp_buf,"S:/weather/%s.bin",argv[1]);
	rt_kprintf("file path  %s",temp_buf);
	lv_obj_t * icon = lv_img_create(lv_scr_act());
	lv_img_set_src(icon, temp_buf);
	lv_obj_align(icon, LV_ALIGN_CENTER, 0, 0);
}
MSH_CMD_EXPORT(demo_fs, demo_fs);

static void lvgl_thread_entry(void *parameter)
{

    lv_init();
    lv_port_disp_init();
	  lv_port_fs_init();
    //demo();
    while (1)
    {
        lv_task_handler();
        rt_thread_mdelay(5); /* 官方手册是提议至少 5 ms */
    }
}

static void lvgl_timeout(void *parameter)
{
    lv_tick_inc(10); /* 因为一个时钟是 10ms */
}

int main(void)
{
    lcd_clear(WHITE);
		
    lvgl_tid = rt_thread_create("lvgl",
                                lvgl_thread_entry,
                                RT_NULL,
                                4096, /* stack size */
                                12,   /* priority */
                                5);   /* time slice */
    if (lvgl_tid != RT_NULL)
        rt_thread_startup(lvgl_tid);

    lvgl_timer = rt_timer_create("lvgl_timer",
                                 lvgl_timeout,
                                 RT_NULL,
                                 1, /* 超时时间,单位是时钟节拍 */
                                 RT_TIMER_FLAG_PERIODIC);
    if (lvgl_timer != RT_NULL)
        rt_timer_start(lvgl_timer);
		
		if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
    {
        LOG_I("Filesystem initialized!");
    }
    else
    {
        LOG_E("Failed to initialize filesystem!");
    }

    return 0;
}

测试

RTT下移植LVGL到W601_文件系统移植_第5张图片

RTT下移植LVGL到W601_文件系统移植_第6张图片

你可能感兴趣的:(W601,lvgl,单片机,stm32,嵌入式硬件)