先叠几层甲:

该项目中很多功能和函数的实现方法肯定有更简单更优化的,本文章里的方法属于比较笨的那种,仅供新手初学者以及博主自己参考,文章中提供的代码及源文件难免会有疏忽、bug,仅供参考、学习、交流;除了正常的讨论问题、建议外,还请各位大佬键盘之下给我留点面子qaq

关于屏幕的显示和触控、图片的显示,可以参考之前的文章:关于GEC6818屏幕的驱动方法(显示屏、触摸屏) 本文章默认已经成功驱动屏幕。

2048源码:小项目:2048
播放器源码:小项目:音视频播放器
粤嵌实验箱为开发板系统自带的自启动的程序,这里作为桌面的附属应用(这下夺舍力)
桌面并没有添加应用的功能,写着玩的(

源码和图片素材会放到文章末尾,仅供参考。

思路其实很简单,将桌面绘制成图片,点击图标的地方启动对应的程序,退出后回到桌面
之前我写的2048和播放器,都设置了点击屏幕左上角退出,因此直接等待线程结束后回到桌面即可;如果没有的话,则需要在桌面里加入点击左上角(或者你自己设置的关闭按钮)杀掉应用进程线程

绘制界面:

桌面
当然,我是不打算也没能力写添加应用的功能的(

工程文件结构:

desktop_img(用于存放图片素材)、main.c(主函数)、lcd.c(屏幕控制相关函数)、ts.c(触摸屏相关函数)、func.c(桌面相关函数)、chardata.c(用于存放各图片文件的路径等字符串,以及位置坐标等)
build.sh里面是编译命令,也可以写Makefile,不过我觉得小工程用不上所以就没写,看自己习惯就好了~

路径、坐标参考:

//图片路径
char *desktop_img="desktop_img/desktop.bmp";
char *reboot_img="desktop_img/reboot.bmp";
char *warn_img="desktop_img/warn_wind.bmp";
//触控坐标
int t_2048_x=67*1.28;
int t_2048_y=58*1.28;
int t_mplay_x=224*1.28;
int t_mplay_y=58*1.28;
int t_gecapp_x=381*1.28;
int t_gecapp_y=58*1.28;
int t_addapp_x=538*1.28;
int t_addapp_y=58*1.28;

int t_reboot_x=0*1.28;
int t_reboot_y=440*1.28;
int t_yes_x=332*1.28;
int t_yes_y=283*1.28;
//弹窗左上角坐标
int warn_wind_x=200;
int warn_wind_y=140;

应用使用新开的线程运行,因此写成线程任务函数的形式

线程任务函数:

void *run_gec_app()
{
    chdir("/IOT");//改变程序工作路径
    system("./iot");//运行程序
    chdir("/chino");//将工作目录更改回去
    pthread_exit(NULL);//应用退出后结束线程
    return 0;
}

void *run_2048_app()
{
    chdir("/chino/2048");
    system("./2048");
    chdir("/chino");
    pthread_exit(NULL);
    return 0;
}

void *run_mplay_app()
{
    chdir("/chino/mplay");
    system("./mplay");
    chdir("/chino");
    pthread_exit(NULL);
    return 0;
}

触摸控制函数:

void ts_ctrl()
{
    int x,y,z;//触摸坐标状态
    int st_warn=0;//有弹窗出现吗?
    int st_isrun=0;//正在运行应用吗?
    pthread_t tid;//定义线程id变量
    while(1)
    {
        int z=get_touchscreen_index(&x,&y);
        if(st_isrun==0 && st_warn==0 && x>t_2048_x && x<=t_2048_x+90*1.28 && y>t_2048_y && y<=t_2048_y+90*1.28)
        {
            st_isrun=1;//运行应用时桌面不再接受触摸操作,归应用管
            pthread_create(&tid,NULL,run_2048_app,NULL);  //创建新线程运行应用
            pthread_join(tid,NULL);//等待线程结束
            load_img(desktop_img,0,0);//线程结束后显示桌面
            st_isrun=0;
        }
        if(st_isrun==0 && st_warn==0 && x>t_mplay_x && x<=t_mplay_x+90*1.28 && y>t_mplay_y && y<=t_mplay_y+90*1.28)
        {
            st_isrun=1;
            pthread_create(&tid,NULL,run_mplay_app,NULL);
            pthread_join(tid,NULL);
            load_img(desktop_img,0,0);
            st_isrun=0;
        }
        if(st_isrun==0 && st_warn==0 && x>t_gecapp_x && x<=t_gecapp_x+90*1.28 && y>t_gecapp_y && y<=t_gecapp_y+90*1.28)
        {
            st_isrun=1;
            pthread_create(&tid,NULL,run_gec_app,NULL);
            pthread_join(tid,NULL);
            load_img(desktop_img,0,0);
            st_isrun=0;
        }
        if(st_isrun==0 && st_warn==0 && x>t_addapp_x && x<=t_addapp_x+90*1.28 && y>t_addapp_y && y<=t_addapp_y+90*1.28)
        {
            load_img(warn_img,warn_wind_x,warn_wind_y);//显示弹窗
            st_warn=1;//弹窗出现时只能点确定
        }
        if(st_isrun==0 && st_warn==1 && x>t_yes_x && x<=t_yes_x+136*1.28 && y>t_yes_y && y<=t_yes_y+33*1.28)
        {
            load_img(desktop_img,0,0);//点击确定后回到桌面
            st_warn=0;//弹窗消失了
        }
        if(st_isrun==0 && st_warn==0 && x>t_reboot_x && x<=t_reboot_x+150*1.28 && y>t_reboot_y && y<=t_reboot_y+40*1.28)
        {//点击重启
            load_img(reboot_img,warn_wind_x,warn_wind_y);//显示重启中
            system("reboot");
        }
    }
}

主函数:

int main()
{
    int fd=lcd_init();
    lcd_clear(0x00ffffff);
    load_img(desktop_img,0,0);
    ts_ctrl();
    return 0;
}

编译运行看看效果:

进入桌面
进入应用
点击添加应用
点击重启
正在重启

这里提一句,开发板默认开机启动粤嵌实验箱程序,如下图所示:

因此为了能够让我们的桌面夺舍代替这个实验箱,奴役他让他成为附属让桌面开机自启,我们需要修改下/etc/profile:

设置开机自启:

将cd /IOT、./iot注释掉,换成我们的桌面,保存重启就好啦~

那么这个简易桌面到这里就完成了,下面放出源码:

点击下载


逸一时,误一世