赞
踩
【基于粤嵌gec6818开发板嵌入式开发电子相册,音乐播放,视频播放,2048游戏】

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/mman.h> #include <linux/input.h> #include <stdlib.h> #include <string.h> #include <termios.h> //打开触摸屏设备文件 void open_ts(); //关闭触摸屏设备文件 void close_ts(); //获取坐标 void get_xy(int *x,int *y); //打开LCD设备文件 void open_lcd(); //关闭LCD设备文件 void close_lcd(); //显示某张大小的图片某个位置 int show_bmp(char *pic_path,int x,int y,int w,int h); //相册 void photo(); //音乐 void music(); //视频 void video(); void guan(); //蓝牙控制照片 void photo1(); //蓝牙控制音频 void music1(); //蓝牙控制视频 void video1(); //全局变量 int ts_fd; int lcd_fd; unsigned *FB; int pos_x,pos_y; int fd_fifo;
//获取坐标 void get_xy(int *x,int *y) { //读取坐标信息,存储到信息结构体 struct input_event ts_buf; int x_read = 0; int y_read = 0; int pre = -1; while (1) { //产生阻塞,等待用户触摸屏幕 read(ts_fd,&ts_buf,sizeof(ts_buf)); if(ts_buf.type == EV_ABS && ts_buf.code == ABS_X) { *x = ts_buf.value; x_read = 1; } else if(ts_buf.type == EV_ABS && ts_buf.code == ABS_Y) { *y = ts_buf.value; y_read = 1; } else if(ts_buf.type == EV_KEY && ts_buf.code == BTN_TOUCH) { pre = ts_buf.value; } if (x_read == 1 && y_read == 1 && pre == 0 ) { *x = *x*800/1024; *y = *y*480/600; break; } } }
//显示某张大小的图片某个位置 int show_bmp(char *pic_path,int x,int y,int w,int h) { //映射新的显存 unsigned *new_FB = FB + (x+y*800); //打开BMP图片文件 int bmp_fd = open(pic_path,O_RDWR); //读取BMP中RGB数据 //先将BMP图片的文件头14个字节和信息头40个字节读取到bmp_info unsigned char bmp_info[54]={0}; read(bmp_fd,bmp_info,sizeof(bmp_info)); unsigned char bmp_buf[3*w*h]; read(bmp_fd,bmp_buf,sizeof(bmp_buf)); unsigned int lcd_buf[w*h]; for(int i = 0; i<w*h; i++) { lcd_buf[i] = bmp_buf[i*3] << 0 | bmp_buf[i*3+1] << 8 | bmp_buf[i*3+2] << 16 | 0x00 << 24; } //图片上下颠倒 int n,m; unsigned int lcd_buf1[w*h]; for (m = 0; m < h; m++) { for(n = 0; n < w; n++) { lcd_buf1[n+m*w] = lcd_buf[n+(h-1-m)*w]; } } //将aRGB写入到LCD屏中 for (m = 0; m < h; m++) { for(n = 0; n < w; n++) { *(new_FB+n+m*800) = lcd_buf1[n+m*w]; } } close(bmp_fd); } 5. 完成Photo1()函数,实现蓝牙控制电子相册功能,实现相册展示,关键代码如下: //蓝牙控制相册 void photo1(){ // 1.访问串口3 int fd = open("/dev/ttySAC3", O_RDWR | O_NOCTTY); // 2.配置串口3参数 init_tty(fd); char buf[10] = {0}; int ret; int current_photo = 1; // 记录当前显示的照片编号,初始值为1。 char bmp_path[20]; //字符串数组,用于存储照片的路径 int switch_x = 700; // 切换按钮的右边x 坐标 int switch_y = 200; // 切换按钮的右边 y 坐标 int switch_width = 100; // 切换按钮的宽度 int switch_height = 50; // 切换按钮的高度 int left_switch_x = 0; // 左边切换按钮的左边 x 坐标 int left_switch_y = 200; // 左边切换按钮的右边 y 坐标 int left_switch_width = 100; // 左边切换按钮的宽度 int left_switch_height = 50; // 左边切换按钮的高度 while (1) { //使用当前照片编号动态构建照片路径,并将结果存储在bmp_path字符串中 sprintf(bmp_path, "./%d.bmp", current_photo); //调用一个名为show_bmp的函数,显示指定路径的照片在屏幕上。 show_bmp(bmp_path, 0, 0, 800, 480); // 读数据 ret = read(fd, buf, 10); if (ret == -1) { perror("read error"); return -1; } // 对比 if (0 == strcmp(buf, "on")) { printf("下一张图片\n"); current_photo++; if (current_photo > 3) { current_photo = 1; } } else if (0 == strcmp(buf, "off")) { printf("退出相册\n"); show_bmp("./img1.bmp", 0, 0, 800, 480); break; }else if (0 == strcmp(buf, "back")) { current_photo--; if (current_photo < 1) { current_photo = 3; } } write(fd, buf, strlen(buf)); // 清空buf bzero(buf, 10); } }
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/mman.h> #include <linux/input.h> #include <stdlib.h> #include <string.h> #include <termios.h> //打开触摸屏设备文件 void open_ts(); //关闭触摸屏设备文件 void close_ts(); //获取坐标 void get_xy(int *x,int *y); //打开LCD设备文件 void open_lcd(); //关闭LCD设备文件 void close_lcd(); //显示某张大小的图片某个位置 int show_bmp(char *pic_path,int x,int y,int w,int h); //相册 void photo(); //音乐 void music(); //视频 void video(); void guan(); //蓝牙控制照片 void photo1(); //蓝牙控制音频 void music1(); //蓝牙控制视频 void video1(); //全局变量 int ts_fd; int lcd_fd; unsigned *FB; int pos_x,pos_y; int fd_fifo;
//音频 void music(){ int current_track = 0; int switch_x = 700; int switch_y = 200; int switch_width = 100; int switch_height = 50; int left_switch_x = 0; int left_switch_y = 200; int left_switch_width = 100; int left_switch_height = 50; const char* mu[] = { "jay.mp3", "yy.mp3", }; char command[50]; sprintf(command, "madplay %s &", mu[current_track]); while(1){ get_xy(&pos_x, &pos_y); system("madplay jay.mp3 &"); if (pos_x > 700 && pos_x < 800 && pos_y > 0 && pos_y < 50) { printf("退出音乐\n"); show_bmp("./img1.bmp", 0, 0, 800, 480); break; } if (pos_x > switch_x && pos_x < switch_x + switch_width && pos_y > switch_y && pos_y < switch_y + switch_height) { printf("下一首\n"); current_track++; if (current_track > 2) { current_track = 0; } } if (pos_x > left_switch_x && pos_x < left_switch_x + left_switch_width && pos_y > left_switch_y && pos_y < left_switch_y + left_switch_height) { printf("上一首\n"); current_track--; if (current_track < 0) { current_track = 2; } } if (pos_x > 95 && pos_x < 245 && pos_y > 255 && pos_y < 405) { printf("暂停\n"); system("killall -19 madplay"); } if (pos_x > 500 && pos_x < 600 && pos_y > 255 && pos_y < 405) { printf("继续\n"); system("killall -18 madplay"); } if (pos_x > 700 && pos_x < 800 && pos_y > 400 && pos_y < 480) { printf("终止\n"); system("killall -9 madplay"); } } }
//蓝牙控制音乐 void music1(){ // 1.访问串口3 int fd = open("/dev/ttySAC3", O_RDWR | O_NOCTTY); // 2.配置串口3参数 init_tty(fd); char buf[10] = {0}; int ret; show_bmp("./music.bmp", 0, 0, 800, 480); int current_track = 0; // 当前播放的音频文件索引 int switch_x = 700; // 切换按钮的右边x 坐标 int switch_y = 200; // 切换按钮的右边 y 坐标 int switch_width = 100; // 切换按钮的宽度 int switch_height = 50; // 切换按钮的高度 int left_switch_x = 0; // 左边切换按钮的左边 x 坐标 int left_switch_y = 200; // 左边切换按钮的右边 y 坐标 int left_switch_width = 100; // 左边切换按钮的宽度 int left_switch_height = 50; // 左边切换按钮的高度 // 音频文件列表 const char* mu[] = { "jay.mp3", "yy.mp3" }; // 拼接当前音频文件的路径 char command[50]; sprintf(command, "madplay %s &", mu[current_track]); system("madplay jay.mp3 &"); // 后台播放 while (1) { // 读数据 ret = read(fd, buf, 10); if (ret == -1) { perror("read error"); return -1; } // 对比 if (0 == strcmp(buf, "1")) { printf("退出音乐\n"); show_bmp("./img1.bmp", 0, 0, 800, 480); break; } else if (0 == strcmp(buf, "2")) { printf("下一首\n"); current_track++; if (current_track > 2) { current_track = 0; // 切换到第一首音频文件 } }else if (0 == strcmp(buf, "3")) { printf("上一首\n"); current_track--; if (current_track < 0) { current_track = 2; // 切换到最后一首音频文件 } }else if (0 == strcmp(buf, "4")) { printf("上一首\n"); current_track--; if (current_track < 0) { current_track = 2; // 切换到最后一首音频文件 } }else if (0 == strcmp(buf, "5")) { printf("暂停\n"); system("killall -19 madplay"); // 暂停音乐播放 }else if (0 == strcmp(buf, "6")) { printf("继续\n"); system("killall -18 madplay"); // 继续音乐播放 }else if (0 == strcmp(buf, "7")) { printf("终止\n"); system("killall -9 madplay"); // 继续音乐播放 } write(fd, buf, strlen(buf)); // 清空buf bzero(buf, 10); } }
int init_tty(int fd) { struct termios old_flags, new_flags; // 清空new_flags bzero(&new_flags, sizeof(new_flags)); // 1. 获取旧的属性 tcgetattr(fd, &old_flags); // 2. 设置原始模式 cfmakeraw(&new_flags); // 3. 激活本地连接CLOCAL与接收使能CREAD的选项 new_flags.c_cflag |= CLOCAL | CREAD; // 4. 设置波特率 cfsetispeed(&new_flags, B9600); cfsetospeed(&new_flags, B9600); // 5. 设置数据位位8位 // 清空原有的数据位 new_flags.c_cflag &= ~CSIZE; new_flags.c_cflag |= CS8; // 6. 设置奇偶校验位 new_flags.c_cflag &= ~PARENB; // 7. 设置一位停止位 new_flags.c_cflag &= ~CSTOPB; // 8. 设置等待时间,最少接收字符个数 new_flags.c_cc[VTIME] = 0; new_flags.c_cc[VMIN] = 1; // 9. 清空串口缓冲区 tcflush(fd, TCIFLUSH); // 10. 设置串口的属性到文件中 tcsetattr(fd, TCSANOW, &new_flags); return 0; } //蓝牙控制视频 void video1(){ system("mplayer -slave -quiet -input file=/fifo -geometry 0:0 -zoom -x 800 -y 400 1.mp4 &"); sleep(1); // 1.访问串口3 int fd = open("/dev/ttySAC3", O_RDWR | O_NOCTTY); // 2.配置串口3参数 init_tty(fd); char buf[10] = {0}; int ret; system("mplayer -slave -quiet -input file=/fifo -geometry 0:0 -zoom -x 800 -y 400 1.mp4 &"); sleep(1); while (1) { // 读数据 ret = read(fd, buf, 10); if (ret == -1) { perror("read error"); return -1; } // 对比 if (0 == strcmp(buf, "1")) { printf("暂停 继续!\n"); Send_Cmd("pause\n"); } else if (0 == strcmp(buf, "2")) { printf("音量+\n"); Send_Cmd("volume +10\n"); } else if (0 == strcmp(buf, "3")) { printf("音量-\n"); Send_Cmd("volume -10\n"); } else if (0 == strcmp(buf, "4")) { printf("快进!\n"); Send_Cmd("seek +10\n"); } else if (0 == strcmp(buf, "5")) { printf("快退!\n"); Send_Cmd("seek -10\n"); } else if (0 == strcmp(buf, "6")) { system("killall -9 mplayer"); printf("退出视频\n"); show_bmp("./img1.bmp",0,0,800,480); break; } write(fd, buf, strlen(buf)); // 清空buf bzero(buf, 10); } } //视频 void video() { system("mplayer -slave -quiet -input file=/fifo -geometry 0:0 -zoom -x 800 -y 400 1.mp4 &"); sleep(1); //show_bmp("13.bmp",0,0,800,480); while(1) { get_xy(&pos_x, &pos_y); //判断坐标位置 if(pos_x>241 && pos_x<440 && pos_y>400 && pos_y<480) { printf("暂停 继续!\n"); Send_Cmd("pause\n"); } if(pos_x>0 && pos_x<120 && pos_y>400 && pos_y<480) { printf("音量+\n"); Send_Cmd("volume +10\n"); } if(pos_x>121 && pos_x<240 && pos_y>400 && pos_y<480) { printf("音量-\n"); Send_Cmd("volume -10\n"); } if(pos_x>441 && pos_x<560 && pos_y>400 && pos_y<480) { printf("快进!\n"); Send_Cmd("seek +10\n"); } if(pos_x>561 && pos_x<680 && pos_y>400 && pos_y<480) { printf("快退!\n"); Send_Cmd("seek -10\n"); } if(pos_x>681 && pos_x<799 && pos_y>400 && pos_y<480) { system("killall -9 mplayer"); break; } } } void guan(){ if(access("/fifo",F_OK))// 默认管道文件创建在根目录 F_OK:判断是否存在 { //如果没有创建 mkfifo("/fifo",777); //创建管道文件函数 } fd_fifo = open("/fifo",O_RDWR); if(fd_fifo == -1) { printf("创建管道文件失败!\n"); return -1; } } int Send_Cmd(char *cmd)//写入管道文件 { write(fd_fifo,cmd,strlen(cmd)); return 0; }





Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。