一、功能與要求
實現(xiàn)功能:本系統(tǒng)需要使用粵嵌的GEC-6818開發(fā)板設計一款娛樂影音系統(tǒng),其中包括圖片顯示(相冊)、音樂播放、視頻播放,游戲四個部分,在每個部分內部,具有操控各個部分的功能觸摸按鍵。本系統(tǒng)還應具有藍牙遠程操控功能。
具體要求:對使用者具有良好的可視交互體驗,可自由切換各個模塊。在圖片模塊里面,能夠點擊左右兩側來切換上一張和下一張圖片,退出相冊顯示主屏幕;在音樂模塊里面,能夠點擊屏幕進行播放/暫停/退出;在視頻模塊中,點擊屏幕進行播放/暫停/快進/快退/音量加/音量減/退出。所有模塊還應具有退出至菜單的功能。這些交互除了需要通過觸摸屏幕完成,也還應通過藍牙進行遠程操作。
演示視頻
【基于粵嵌gec6818開發(fā)板嵌入式開發(fā)電子相冊,音樂播放,視頻播放,2048游戲】
文章來源:http://www.zghlxwxcb.cn/news/detail-540490.html
(二)軟件設計
電子相冊功能程序如下:
- 準備圖片素材:1.bmp,2.bmp,3.bmp,img1.bmp。
- 頭文件、設置變量和定義函數(shù),關鍵代碼如下:
#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;
- 獲取坐標,讀取坐標信息,存儲到信息結構體,產(chǎn)生阻塞,等待用戶觸摸屏幕,關鍵代碼如下:
//獲取坐標
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)
{
//產(chǎn)生阻塞,等待用戶觸摸屏幕
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;
}
}
}
- 完成show_bmp函數(shù),實現(xiàn)顯示某張大小的圖片某個位置,關鍵代碼如下:
//顯示某張大小的圖片某個位置
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數(shù)據(jù)
//先將BMP圖片的文件頭14個字節(jié)和信息頭40個字節(jié)讀取到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()函數(shù),實現(xiàn)藍牙控制電子相冊功能,實現(xiàn)相冊展示,關鍵代碼如下:
//藍牙控制相冊
void photo1(){
// 1.訪問串口3
int fd = open("/dev/ttySAC3", O_RDWR | O_NOCTTY);
// 2.配置串口3參數(shù)
init_tty(fd);
char buf[10] = {0};
int ret;
int current_photo = 1; // 記錄當前顯示的照片編號,初始值為1。
char bmp_path[20]; //字符串數(shù)組,用于存儲照片的路徑
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)
{
//使用當前照片編號動態(tài)構建照片路徑,并將結果存儲在bmp_path字符串中
sprintf(bmp_path, "./%d.bmp", current_photo);
//調用一個名為show_bmp的函數(shù),顯示指定路徑的照片在屏幕上。
show_bmp(bmp_path, 0, 0, 800, 480);
// 讀數(shù)據(jù)
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);
}
}
音頻播放功能代碼如下:
- 準備音樂素材:jay.mp3,yy.mp3
- 頭文字,設置變量、定義函數(shù)。
#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("繼續(xù)\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參數(shù)
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)
{
// 讀數(shù)據(jù)
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("繼續(xù)\n");
system("killall -18 madplay"); // 繼續(xù)音樂播放
}else if (0 == strcmp(buf, "7"))
{
printf("終止\n");
system("killall -9 madplay"); // 繼續(xù)音樂播放
}
write(fd, buf, strlen(buf));
// 清空buf
bzero(buf, 10);
}
}
5.視頻部分核心代碼如下:
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. 設置數(shù)據(jù)位位8位
// 清空原有的數(shù)據(jù)位
new_flags.c_cflag &= ~CSIZE;
new_flags.c_cflag |= CS8;
// 6. 設置奇偶校驗位
new_flags.c_cflag &= ~PARENB;
// 7. 設置一位停止位
new_flags.c_cflag &= ~CSTOPB;
// 8. 設置等待時間,最少接收字符個數(shù)
new_flags.c_cc[VTIME] = 0;
new_flags.c_cc[VMIN] = 1;
// 9. 清空串口緩沖區(qū)
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參數(shù)
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)
{
// 讀數(shù)據(jù)
ret = read(fd, buf, 10);
if (ret == -1)
{
perror("read error");
return -1;
}
// 對比
if (0 == strcmp(buf, "1"))
{
printf("暫停 繼續(xù)!\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("暫停 繼續(xù)!\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))// 默認管道文件創(chuàng)建在根目錄 F_OK:判斷是否存在
{
//如果沒有創(chuàng)建
mkfifo("/fifo",777); //創(chuàng)建管道文件函數(shù)
}
fd_fifo = open("/fifo",O_RDWR);
if(fd_fifo == -1)
{
printf("創(chuàng)建管道文件失??!\n");
return -1;
}
}
int Send_Cmd(char *cmd)//寫入管道文件
{
write(fd_fifo,cmd,strlen(cmd));
return 0;
}
三 實物展示
文章來源地址http://www.zghlxwxcb.cn/news/detail-540490.html
需要源碼和答辯PPT的私信我
到了這里,關于基于粵嵌gec6818開發(fā)板嵌入式開發(fā)電子相冊,音樂播放,視頻播放,2048游戲的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!