嵌入式音乐相册

1.操作环境
Ubuntu 18.04
WMware
嵌入式开发板

2.功能描述:
1)相册:点击”相册“按钮,循环展示相册里的图片,每个图片之间间隔5s,播放图片的过程中伴随着音乐的播放,图片展示完毕自动返回主菜单;
2)音乐:点击”音乐“按钮,展示分菜单,上面有”从头播放“、”结束播放“、”暂停“、”播放“、”上一首“、”下一首“,”返回上一级菜单“几个按钮,按下的对应按钮做出相关反应。

3.实物展示
在这里插入图片描述
在这里插入图片描述

4.代码
head.h

#ifndef _HEAD_H_  
#define _HEAD_H_  

//头文件

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
#include <strings.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <linux/fb.h>
#include <stdbool.h>
#include <stdio.h>
//全局变量
#define BMP_SIZE 800*480*3 //图片像素大小
#define LCD_SIZE 800*480*4 //LCD像素大小

#define TS_PATH   "/dev/input/event0"
#define LCD_WIDTH  			800
#define LCD_HEIGHT 			480
#define FB_SIZE				(LCD_WIDTH * LCD_HEIGHT * 4)
#define Music_count      	sizeof(musics)/sizeof(musics[0])


int x;
int y;
int wy;

static int Cur_Idx=0;
static int bIsPlaying=0;

//结构体
enum
{
   ACTION_START,
   ACTION_STOP,
   ACTION_PAUSE,
   ACTION_PLAY,
   ACTION_NEXT,
   ACTION_PREV,
   ACTION_EXIT,
};


//函数声明 
int Get_Action();
void Music_Start(const char* Music);
void Music_Pause();
void Music_Play();
void Music_Stop();
void Action_handler();
int show_bmp();
int touch();
int color();
void Music_criclePlay();


#endif   

main.c

#include "head.h"

int main(){
   //color();
	while(1){
		wy=1;
		show_bmp("./menu0.bmp");
		touch();
		if(x>89 &&x<373  &&y>31  &&y<205){//相册
			Music_criclePlay();
			show_bmp("./1.bmp");
			sleep(5);
			show_bmp("./2.bmp");
			sleep(5);
			show_bmp("./3.bmp");
			sleep(5);
		}
		if(x>430 &&x<715  &&y>268  &&y<442  ){//mp3
			show_bmp("./menu.bmp");
			while(1)
			{
				Action_handler();
				if(wy==0){
					break;
				}
			}
		}
		/*if(x> &&x<  &&y>  &&y<  ){//结束
			show_bmp("./5.bmp");
			sleep(1);
			break;
		}
		*/
    }
	return 0;
}

touch.c

#include "head.h"


int touch(){
	struct input_event ts;//定义输入事件结构体变量
	//打开触摸屏
	int fd=open("/dev/input/event0",O_RDONLY);
		if(fd==-1){
			perror("open");
			return -1;
		}
		//获取用户坐标(读)
		while(1){
		int ret=read(fd,&ts,sizeof(ts));
		if(ret==-1){
			perror("read");
			return -2;
		}
		if(ts.type==EV_ABS&&ts.code==ABS_X){
			x=ts.value*800/1024;
		}
		if(ts.type==EV_ABS&&ts.code==ABS_Y){
			y=ts.value*480/600;
		}
		if(ts.type==EV_KEY&&ts.code==BTN_TOUCH&&ts.value==0){
			break;
		}
	}
	close(fd);
	return 0;
				
}

show_bmp.c

#include "head.h"

int show_bmp(const char *pathname){

	char bmp_buf[BMP_SIZE];//定义一个临时的储存空间
	int lcd_buf[800*480];
	bzero(bmp_buf,BMP_SIZE);//清空数组
	bzero(lcd_buf,sizeof(lcd_buf));
	//1、打开LCD
	int lcd_fd = open("/dev/fb0",O_RDWR);
	if(lcd_fd == -1)
	{
		perror("open lcd");
		return -1;
	}
	//2、打开图片
	int bmp_fd = open(pathname,O_RDONLY);
	if(bmp_fd == -1)
	{
		perror("open bmp");
		return -1;
	}
	//3、读像素(RGB)
	int ret = read(bmp_fd,bmp_buf,BMP_SIZE);
	if(ret == -1)
	{
		perror("read");
		return -2;
	}
	close(bmp_fd);
	//4、将 RGB-----> ARGB
	int i;
	for(i=0; i<800*480; i++)
	{
		lcd_buf[i] = bmp_buf[i*3]<<0 | bmp_buf[i*3+1]<<8 | bmp_buf[i*3+2]<<16;
	}
	
	//5、使用内存映射
	int *fd_map = NULL;
	
	fd_map = mmap(NULL,
				800*480*4,
				PROT_READ | PROT_WRITE,
				MAP_SHARED,
				lcd_fd,
				0);			
	if(fd_map == NULL)
	{
		perror("mmap");
		return -3;
	}	
	//6、翻转图片
	int x,y;
	for(y=0; y<480; y++)
	{
		for(x=0; x<800; x++)
		{
			*(fd_map+(479-y)*800+x) = lcd_buf[800*y+x];
		}
	}
	//7、解除映射,关闭LCD
	munmap(fd_map,800*480*4);

	return 0;
}

music_play.c

#include "head.h"






const char* musics[] =
{
	"./1.mp3",
	"./2.mp3",
	"./3.mp3",
	"./4.mp3",
};




int Get_Action()
{
			touch();
            if((x >=17 && x <=195)
				&&(y >=307 && y <=361))
			{
                //close(tsfd);
				return ACTION_START;
			}
		//暂停
     		if((x >=605 && x <=795)
				&&(y >=399 && y <=460))
			{
				//close(tsfd);
                return ACTION_PAUSE;
			}
		//结束播放
			if((x >=17 && x <=195)
				&&(y >=399 && y <=460))
			{
				//close(tsfd);
                return ACTION_STOP;
			}
		//播放
			if((x >=340 && x <=466)
				&&(y >=312 && y <=430))
			{
				//close(tsfd);
                return ACTION_PLAY;
			}
		//下一首
			if((x >=495 && x <=575)
				&&(y >=312 && y <=430))
			{
				//close(tsfd);
                return ACTION_NEXT;
			}
			if((x >=231 && x <=312)
				&&(y >=306 && y <=464))
			{
				//close(tsfd);
                return ACTION_PREV;
			}
		//返回
			if((x >=605 && x <=795)
				&&(y >=307 && y <=361))
			{
				//close(tsfd);
                return ACTION_EXIT;
			}
		}
	
void Music_Start(const char* Music)
{
   if(bIsPlaying)
	   return ;
   bIsPlaying = 1;
   char cmd[100] = {0};
   sprintf(cmd,"madplay %s & \n",Music);
   system(cmd);
}
void Music_criclePlay(){
	system("madplay 4.mp3 &");
}
void Music_Pause()
{
	system("killall -STOP madplay");
}
void Music_Play()
{
	system("killall -CONT madplay");
}
void Music_Stop()
{
	if(bIsPlaying)
	{
	   bIsPlaying = 0;
	   system("killall -KILL madplay");
	}
}

void Action_handler()
{
    int action = Get_Action();
	switch(action)
	{
		case ACTION_START:
            Music_Start(musics[0]);
			break;
		case ACTION_STOP:
            Music_Stop();
			break;
		case ACTION_PAUSE:
            Music_Pause();
			break;
		case ACTION_PLAY:
            Music_Play();
			break;
        case ACTION_NEXT:            
			Cur_Idx++;
		    Cur_Idx %= Music_count;
			Music_Stop();
			Music_Start(musics[Cur_Idx]);
			break;
		case ACTION_PREV:
            Cur_Idx--;
		    Cur_Idx %= Music_count;
			//(Cur_Idx -1 + Music_count) 
            Music_Stop();
            Music_Start(musics[Cur_Idx]);
			break;
		case ACTION_EXIT:
		    Music_Stop();
			//exit(0);
			wy=0;
			break;
	}
}

color.c

#include "head.h"

int color(){
//打开lcd液晶显示屏
int fd=open("/dev/fb0",O_RDWR);
if(fd==-1){
perror("open");
return -1;
}
//定义颜色
char A=0;
char R=255;
char G=240;
char B=245;
//合成颜色
char buf[800*480*4];
for(int i=0;i<800*480*4;i+=4){
buf[0+i]=B;
buf[1+i]=G;
buf[2+i]=R;
buf[3+i]=A;
}
int ret=write(fd,buf,sizeof(buf));
if(ret==-1){
perror("write");
return -1;
}
//关闭lcd
close(fd);
return 0;
}
  • 7
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值