当前位置:   article > 正文

学习笔记(1)——粤嵌gec6818实现电子相册,音乐播放器,视频播放器。_gec6818开发板上音乐播放器

gec6818开发板上音乐播放器

项目要求:

(1)设计一个初始界面,并且设置电子相册,音乐播放器,视频播放器三个触摸按键。

(2)电子相册——能够实现相册的幻灯片功能,实现相册左右滑动切换相片。

(3)音乐播放器实现——切歌,播放和暂停功能。

(4)视频播放器实现——播放、暂停、音量大小、快进倒退等功能。

(5)对代码进行集成化

项目设计:

(1)头文件、设置变量和素材引入

  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/mman.h>
  8. #include <linux/input.h>
  9. #include <pthread.h>
  10. #include <stdlib.h>
  11. int get_touch(int *th_x,int *th_y);//x轴和y轴的位置。
  12. int show_color(int color,int x,int y,int w,int h);//颜色的显示x和y设定位置,w和h设定显示大小
  13. int mmap_bmp(char *namebuf,int x,int y,int w,int h);//
  14. int lcd,touch;
  15. int *map;
  16. int th_x,th_y,th_falg = 0,falg_2 = 0,i = -1;
  17. pthread_t pid;
  18. int fd_fifo;
  19. char music_path[4][32] = {"faded.mp3","faded1.mp3","faded2.mp3"};
  20. char name_buf[4][20] = {"01.bmp","02.bmp","1.bmp"};

(2)对设备的初始化和停止

  1. int __INIT__()
  2. {
  3. touch = open("/dev/input/event0",O_RDWR);
  4. if(touch == -1)
  5. {
  6. perror("open touch failed");
  7. return -1;
  8. }
  9. lcd = open("/dev/fb0",O_RDWR);
  10. if(lcd == -1)
  11. {
  12. perror("open lcd failed");
  13. return -1;
  14. }
  15. map = (int *)mmap(NULL,800*480*4,PROT_READ | PROT_WRITE,MAP_SHARED,lcd,0);
  16. if(map == MAP_FAILED)
  17. {
  18. perror("mmap failed");
  19. return -1;
  20. }
  21. if(access("/fifo",F_OK))// 默认管道文件创建在根目录 F_OK:判断是否存在
  22. {
  23. //如果没有创建
  24. mkfifo("/fifo",777); //创建管道文件函数
  25. }
  26. fd_fifo = open("/fifo",O_RDWR);
  27. if(fd_fifo == -1)
  28. {
  29. printf("创建管道文件失败!\n");
  30. return -1;
  31. }
  32. return 0;
  33. }
  34. int __FrEe()
  35. {
  36. close(touch);
  37. close(lcd);
  38. munmap(map,800*400*4);
  39. system("killall -9 mplayer");
  40. }
  41. int Send_Cmd(char *cmd)//写入管道文件
  42. {
  43. write(fd_fifo,cmd,strlen(cmd));
  44. return 0;
  45. }

(3)限制图片的显示大小

  1. void *cyc_showbmp(void *arg)
  2. {
  3. for(int i = 0;i<3;i++)
  4. {
  5. mmap_bmp(name_buf[i],0,0,800,400);
  6. sleep(3);
  7. if(i == 2) i=-1;
  8. }
  9. }

(4)获取坐标

  1. //获取坐标
  2. int get_touch(int *th_x,int *th_y)
  3. {
  4. int old_x,old_y;
  5. struct input_event buf;
  6. while(1)
  7. {
  8. read(touch,&buf,sizeof(buf));
  9. if(buf.type == EV_ABS && buf.code == ABS_X)
  10. *th_x = buf.value;
  11. if(buf.type == EV_ABS && buf.code == ABS_Y)
  12. *th_y = buf.value;
  13. if(buf.type == EV_KEY && buf.code == BTN_TOUCH && buf.value == 1)
  14. {
  15. old_x = *th_x;
  16. old_y = *th_y;
  17. }
  18. if(buf.type == EV_KEY && buf.code == BTN_TOUCH && buf.value == 0)
  19. {
  20. if(old_x < *th_x && (*th_x - old_x >= 50)) return 1;//向右移动
  21. if(old_x > *th_x && (old_x - *th_x >= 50)) return 2;//向左移动
  22. break;
  23. }
  24. }
  25. return 0;
  26. }

(5)颜色的显示

  1. int show_color(int color,int x,int y,int w,int h)
  2. {
  3. int * new_map = map + (800*y) + x;
  4. for(int i = 0 ;i < h;i++)
  5. {
  6. for(int j = 0;j < w;j++)
  7. {
  8. *(new_map+(i*800+j)) = color;//buf[((h -1 -i)*w)+j];
  9. }
  10. }
  11. return 0;
  12. }

(6)在任意位置显示图片

  1. //任意位置,任意大小刷图
  2. int mmap_bmp(char *namebuf,int x,int y,int w,int h)
  3. {
  4. int bmp = open(namebuf,O_RDWR);
  5. if(bmp == -1)
  6. {
  7. perror("open failed");
  8. return -1;
  9. }
  10. char buf[w*h*3];
  11. int new_buf[w*h];
  12. lseek(bmp,54,SEEK_SET);
  13. read(bmp,buf,w*h*3);
  14. for(int i = 0 ;i < h;i++)
  15. {
  16. for(int j = 0;j < w;j++)
  17. {
  18. 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;
  19. }
  20. }
  21. int * new_map = map + (800*y) + x;
  22. for(int i = 0 ;i < h;i++)
  23. {
  24. for(int j = 0;j < w;j++)
  25. {
  26. *(new_map+(i*800+j)) = new_buf[((h -1 -i)*w)+j];
  27. }
  28. }
  29. close(bmp);
  30. return 0;
  31. }

(7)电子相册

  1. int pic()
  2. {
  3. while(1)
  4. {
  5. //获取坐标
  6. //判断坐标位置
  7. mmap_bmp("12.bmp",0,400,800,80);
  8. get_touch(&th_x,&th_y);
  9. if(th_x >= 0 && th_x < 200 && th_y >= 400 &&th_y <= 480)
  10. {
  11. //结束自动循环
  12. pthread_cancel(pid);
  13. falg_2 = 0;
  14. mmap_bmp(name_buf[i],0,0,800,400);
  15. }
  16. if(th_x >= 201 && th_x < 400 && th_y >= 400 &&th_y <= 480)
  17. {
  18. //开启自动循环
  19. pthread_create(&pid,NULL,cyc_showbmp,NULL);
  20. }
  21. if(th_x >= 401 && th_x < 600 && th_y >= 400 &&th_y <= 480)
  22. {
  23. //上一张 数组 --
  24. i--;
  25. if(i < 0) i = 3;
  26. mmap_bmp(name_buf[i],0,0,800,400);
  27. }//上一张
  28. if(th_x >= 601 && th_x < 799 && th_y >= 400 &&th_y <= 480)
  29. {
  30. //下一张 数组 ++
  31. i++;
  32. if(i > 3) i = 0;
  33. printf("1111\n");
  34. mmap_bmp(name_buf[i],0,0,800,400);
  35. } //下一张
  36. if(th_x >= 0 && th_x < 100 && th_y >= 0 &&th_y <= 100)
  37. break;
  38. //启动自动循环:开启线程
  39. //停止自动循环:结束线程
  40. //退出
  41. }
  42. }

(8)音乐播放器

  1. int music()
  2. {
  3. //execl("/bin/madplay", "madplay", music_path[i], NULL);
  4. system("madplay faded.mp3 &");
  5. sleep(1);
  6. while(1)
  7. {
  8. //获取坐标
  9. mmap_bmp("14.bmp",0,0,800,400);
  10. mmap_bmp("16.bmp",0,400,800,80);
  11. get_touch(&th_x,&th_y);
  12. //判断坐标位置
  13. /*if(th_x >= 441 && th_x <= 560 && th_y >= 400 && th_y <= 480)//结束
  14. {
  15. system("killall -9 madplay");
  16. }*/
  17. if(th_x >= 0 && th_x < 200 && th_y >= 400 &&th_y <= 480)//暂停
  18. {
  19. printf("继续!\n");
  20. system("killall -18 madplay");
  21. //Send_Cmd("pause\n");
  22. }
  23. if(th_x >= 201 && th_x < 400 && th_y >= 400 &&th_y <= 480)
  24. {
  25. -
  26. i--;
  27. if(i < 0) i = 3;
  28. system(name_buf[i]);
  29. }//
  30. if(th_x >= 401 && th_x < 600 && th_y >= 400 &&th_y <= 480)
  31. {
  32. i++;
  33. if(i > 3) i = 0;
  34. system( name_buf[i] );
  35. } //下一首
  36. if(th_x >= 551 && th_x <= 800 && th_y >= 400 && th_y <= 480)//退出
  37. {
  38. system("killall -19 madplay");
  39. break;
  40. }
  41. if(th_x >= 0 && th_x < 100 && th_y >= 0 &&th_y <= 100)
  42. {
  43. system("killall -9 madplay");
  44. break;
  45. }
  46. }
  47. }

(9)视频播放器

  1. int video()
  2. {
  3. system("mplayer -slave -quiet -input file=/fifo -geometry 0:0 -zoom -x 800 -y 400 Faded3.avi &");
  4. sleep(1);
  5. mmap_bmp("13.bmp",0,0,800,480);
  6. while(1)
  7. {
  8. //获取坐标
  9. /*show_color(0x00ffff00,0,400,120,80);
  10. show_color(0x00ff0000,120,400,120,80);
  11. show_color(0x0000ff00,240,400,200,80);
  12. show_color(0x000000ff,440,400,120,80);
  13. show_color(0x00ff00ff,560,400,120,80);
  14. show_color(0x00ffffff,680,400,119,80);*/
  15. get_touch(&th_x,&th_y);
  16. //判断坐标位置
  17. if(th_x>241 && th_x<440 && th_y>400 && th_y<480)
  18. {
  19. printf("暂停 继续!\n");
  20. Send_Cmd("pause\n");
  21. }
  22. if(th_x>0 && th_x<120 && th_y>400 && th_y<480)
  23. {
  24. printf("音量+\n");
  25. Send_Cmd("volume +10\n");
  26. }
  27. if(th_x>121 && th_x<240 && th_y>400 && th_y<480)
  28. {
  29. printf("音量-\n");
  30. Send_Cmd("volume -10\n");
  31. }
  32. if(th_x>441 && th_x<560 && th_y>400 && th_y<480)
  33. {
  34. printf("快进!\n");
  35. Send_Cmd("seek +10\n");
  36. }
  37. if(th_x>561 && th_x<680 && th_y>400 && th_y<480)
  38. {
  39. printf("快退!\n");
  40. Send_Cmd("seek -10\n");
  41. }
  42. if(th_x>681 && th_x<799 && th_y>400 && th_y<480)
  43. {
  44. system("killall -9 mplayer");
  45. break;
  46. }
  47. //启动视频播放器
  48. //退出
  49. //上一个
  50. //下一个
  51. //暂停
  52. //继续
  53. //音量+-
  54. //快进快退
  55. }
  56. }

(10)主函数

  1. int main(int argc, char const *argv[])
  2. {
  3. //初始化
  4. int ret = __INIT__();
  5. if(ret == -1) return -1;
  6. while(1)
  7. {//主界面
  8. mmap_bmp("11.bmp",0,0,800,480);
  9. //获取坐标
  10. get_touch(&th_x,&th_y);
  11. //判断坐标位置
  12. if(th_x >= 100 && th_x < 200 && th_y >= 300 && th_y < 400)
  13. {
  14. //图片
  15. pic();
  16. }
  17. if(th_x >= 300 && th_x < 500 && th_y >= 300 && th_y < 400)
  18. {
  19. //音乐
  20. music();
  21. }
  22. if(th_x >= 600 && th_x < 700 && th_y >= 300 && th_y < 400)
  23. {
  24. //视频
  25. video();
  26. }
  27. }
  28. //结束初始化
  29. __FrEe();
  30. return 0;
  31. }

 

 (11)项目成果

 

 

(12)程序编译指令  arm-linux-gcc xxx.c -o xxx -lphread 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/47575
推荐阅读
相关标签
  

闽ICP备14008679号