赞
踩
(1)设计一个初始界面,并且设置电子相册,音乐播放器,视频播放器三个触摸按键。
(2)电子相册——能够实现相册的幻灯片功能,实现相册左右滑动切换相片。
(3)音乐播放器实现——切歌,播放和暂停功能。
(4)视频播放器实现——播放、暂停、音量大小、快进倒退等功能。
(5)对代码进行集成化
(1)头文件、设置变量和素材引入
- #include <stdio.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/mman.h>
- #include <linux/input.h>
- #include <pthread.h>
- #include <stdlib.h>
-
- int get_touch(int *th_x,int *th_y);//x轴和y轴的位置。
- int show_color(int color,int x,int y,int w,int h);//颜色的显示x和y设定位置,w和h设定显示大小
- int mmap_bmp(char *namebuf,int x,int y,int w,int h);//
-
- int lcd,touch;
- int *map;
-
- int th_x,th_y,th_falg = 0,falg_2 = 0,i = -1;
- pthread_t pid;
-
- int fd_fifo;
-
- char music_path[4][32] = {"faded.mp3","faded1.mp3","faded2.mp3"};
-
- char name_buf[4][20] = {"01.bmp","02.bmp","1.bmp"};

(2)对设备的初始化和停止
- int __INIT__()
- {
- touch = open("/dev/input/event0",O_RDWR);
- if(touch == -1)
- {
- perror("open touch failed");
- return -1;
- }
- lcd = open("/dev/fb0",O_RDWR);
- if(lcd == -1)
- {
- perror("open lcd failed");
- return -1;
- }
- map = (int *)mmap(NULL,800*480*4,PROT_READ | PROT_WRITE,MAP_SHARED,lcd,0);
- if(map == MAP_FAILED)
- {
- perror("mmap failed");
- return -1;
- }
-
-
- if(access("/fifo",F_OK))// 默认管道文件创建在根目录 F_OK:判断是否存在
- {
- //如果没有创建
- mkfifo("/fifo",777); //创建管道文件函数
-
- }
- fd_fifo = open("/fifo",O_RDWR);
- if(fd_fifo == -1)
- {
- printf("创建管道文件失败!\n");
- return -1;
-
- }
- return 0;
- }
-
- int __FrEe()
- {
- close(touch);
- close(lcd);
- munmap(map,800*400*4);
- system("killall -9 mplayer");
- }
-
- int Send_Cmd(char *cmd)//写入管道文件
- {
- write(fd_fifo,cmd,strlen(cmd));
-
- return 0;
- }

(3)限制图片的显示大小
-
- void *cyc_showbmp(void *arg)
- {
- for(int i = 0;i<3;i++)
- {
- mmap_bmp(name_buf[i],0,0,800,400);
- sleep(3);
- if(i == 2) i=-1;
- }
- }
(4)获取坐标
- //获取坐标
- int get_touch(int *th_x,int *th_y)
- {
- int old_x,old_y;
- struct input_event buf;
- while(1)
- {
- read(touch,&buf,sizeof(buf));
-
- if(buf.type == EV_ABS && buf.code == ABS_X)
- *th_x = buf.value;
- if(buf.type == EV_ABS && buf.code == ABS_Y)
- *th_y = buf.value;
-
- if(buf.type == EV_KEY && buf.code == BTN_TOUCH && buf.value == 1)
- {
- old_x = *th_x;
- old_y = *th_y;
- }
-
- if(buf.type == EV_KEY && buf.code == BTN_TOUCH && buf.value == 0)
- {
- if(old_x < *th_x && (*th_x - old_x >= 50)) return 1;//向右移动
- if(old_x > *th_x && (old_x - *th_x >= 50)) return 2;//向左移动
- break;
- }
-
- }
-
- return 0;
- }

(5)颜色的显示
- int show_color(int color,int x,int y,int w,int h)
- {
- int * new_map = map + (800*y) + x;
-
- for(int i = 0 ;i < h;i++)
- {
- for(int j = 0;j < w;j++)
- {
- *(new_map+(i*800+j)) = color;//buf[((h -1 -i)*w)+j];
- }
- }
- return 0;
- }
(6)在任意位置显示图片
- //任意位置,任意大小刷图
- int mmap_bmp(char *namebuf,int x,int y,int w,int h)
- {
- int bmp = open(namebuf,O_RDWR);
- if(bmp == -1)
- {
- perror("open failed");
- return -1;
- }
-
- char buf[w*h*3];
- int new_buf[w*h];
-
- lseek(bmp,54,SEEK_SET);
-
- read(bmp,buf,w*h*3);
-
- for(int i = 0 ;i < h;i++)
- {
- for(int j = 0;j < w;j++)
- {
- new_buf[(i*w)+j] = buf[3*((i*w)+j)] << 0 | buf[3*((i*w)+j)+1] << 8 | buf[3*((i*w)+j)+2] << 16;
- }
- }
-
- int * new_map = map + (800*y) + x;
-
- for(int i = 0 ;i < h;i++)
- {
- for(int j = 0;j < w;j++)
- {
- *(new_map+(i*800+j)) = new_buf[((h -1 -i)*w)+j];
- }
- }
- close(bmp);
- return 0;
- }

(7)电子相册
- int pic()
- {
- while(1)
- {
- //获取坐标
-
- //判断坐标位置
- mmap_bmp("12.bmp",0,400,800,80);
- get_touch(&th_x,&th_y);
- if(th_x >= 0 && th_x < 200 && th_y >= 400 &&th_y <= 480)
- {
- //结束自动循环
-
- pthread_cancel(pid);
- falg_2 = 0;
- mmap_bmp(name_buf[i],0,0,800,400);
-
- }
- if(th_x >= 201 && th_x < 400 && th_y >= 400 &&th_y <= 480)
- {
- //开启自动循环
-
- pthread_create(&pid,NULL,cyc_showbmp,NULL);
- }
- if(th_x >= 401 && th_x < 600 && th_y >= 400 &&th_y <= 480)
- {
- //上一张 数组 --
- i--;
- if(i < 0) i = 3;
- mmap_bmp(name_buf[i],0,0,800,400);
- }//上一张
- if(th_x >= 601 && th_x < 799 && th_y >= 400 &&th_y <= 480)
- {
- //下一张 数组 ++
- i++;
- if(i > 3) i = 0;
- printf("1111\n");
- mmap_bmp(name_buf[i],0,0,800,400);
- } //下一张
- if(th_x >= 0 && th_x < 100 && th_y >= 0 &&th_y <= 100)
- break;
-
- //启动自动循环:开启线程
-
- //停止自动循环:结束线程
-
- //退出
-
- }
- }

(8)音乐播放器
- int music()
- {
- //execl("/bin/madplay", "madplay", music_path[i], NULL);
-
-
- system("madplay faded.mp3 &");
- sleep(1);
- while(1)
- {
- //获取坐标
- mmap_bmp("14.bmp",0,0,800,400);
- mmap_bmp("16.bmp",0,400,800,80);
- get_touch(&th_x,&th_y);
- //判断坐标位置
- /*if(th_x >= 441 && th_x <= 560 && th_y >= 400 && th_y <= 480)//结束
- {
- system("killall -9 madplay");
- }*/
- if(th_x >= 0 && th_x < 200 && th_y >= 400 &&th_y <= 480)//暂停
- {
- printf("继续!\n");
- system("killall -18 madplay");
- //Send_Cmd("pause\n");
- }
- if(th_x >= 201 && th_x < 400 && th_y >= 400 &&th_y <= 480)
- {
- -
- i--;
- if(i < 0) i = 3;
- system(name_buf[i]);
- }//
- if(th_x >= 401 && th_x < 600 && th_y >= 400 &&th_y <= 480)
- {
- i++;
- if(i > 3) i = 0;
- system( name_buf[i] );
- } //下一首
- if(th_x >= 551 && th_x <= 800 && th_y >= 400 && th_y <= 480)//退出
- {
- system("killall -19 madplay");
- break;
- }
- if(th_x >= 0 && th_x < 100 && th_y >= 0 &&th_y <= 100)
- {
- system("killall -9 madplay");
- break;
- }
-
- }
- }

(9)视频播放器
- int video()
- {
- system("mplayer -slave -quiet -input file=/fifo -geometry 0:0 -zoom -x 800 -y 400 Faded3.avi &");
- sleep(1);
- mmap_bmp("13.bmp",0,0,800,480);
- while(1)
- {
- //获取坐标
- /*show_color(0x00ffff00,0,400,120,80);
- show_color(0x00ff0000,120,400,120,80);
- show_color(0x0000ff00,240,400,200,80);
- show_color(0x000000ff,440,400,120,80);
- show_color(0x00ff00ff,560,400,120,80);
- show_color(0x00ffffff,680,400,119,80);*/
-
- get_touch(&th_x,&th_y);
- //判断坐标位置
- if(th_x>241 && th_x<440 && th_y>400 && th_y<480)
- {
- printf("暂停 继续!\n");
- Send_Cmd("pause\n");
- }
- if(th_x>0 && th_x<120 && th_y>400 && th_y<480)
- {
- printf("音量+\n");
- Send_Cmd("volume +10\n");
- }
- if(th_x>121 && th_x<240 && th_y>400 && th_y<480)
- {
- printf("音量-\n");
- Send_Cmd("volume -10\n");
- }
- if(th_x>441 && th_x<560 && th_y>400 && th_y<480)
- {
- printf("快进!\n");
- Send_Cmd("seek +10\n");
- }
- if(th_x>561 && th_x<680 && th_y>400 && th_y<480)
- {
- printf("快退!\n");
- Send_Cmd("seek -10\n");
- }
- if(th_x>681 && th_x<799 && th_y>400 && th_y<480)
- {
- system("killall -9 mplayer");
- break;
- }
-
-
- //启动视频播放器
- //退出
- //上一个
- //下一个
- //暂停
- //继续
- //音量+-
- //快进快退
-
- }
- }

(10)主函数
- int main(int argc, char const *argv[])
- {
- //初始化
- int ret = __INIT__();
- if(ret == -1) return -1;
-
-
- while(1)
- {//主界面
- mmap_bmp("11.bmp",0,0,800,480);
- //获取坐标
- get_touch(&th_x,&th_y);
- //判断坐标位置
- if(th_x >= 100 && th_x < 200 && th_y >= 300 && th_y < 400)
- {
- //图片
- pic();
- }
-
- if(th_x >= 300 && th_x < 500 && th_y >= 300 && th_y < 400)
- {
- //音乐
- music();
- }
-
- if(th_x >= 600 && th_x < 700 && th_y >= 300 && th_y < 400)
- {
- //视频
- video();
- }
-
- }
-
-
- //结束初始化
- __FrEe();
- return 0;
- }

(11)项目成果
(12)程序编译指令 arm-linux-gcc xxx.c -o xxx -lphread
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。