zigbee无线传感网实训---实现LCD开发板播放音乐并切换歌曲(The Seventh day)

承接第六天:zigbee无线传感网实训---完成开发板WiFi(The Sixth day)

一、制作mp3解码库
================================
    1、制作zlib库
        拷贝zlib-1.2.8.tar.gz文件到家目录        

cd ~

        解压      

tar xzvf zlib-1.2.8.tar.gz    //“/home/gec/”

        配置zlib          

 mkdir /home/gec/audio
 export CC=arm-none-linux-gnueabi-gcc
 export AR=arm-none-linux-gnueabi-ar
 export RANLIB=arm-none-linux-gnueabi-ranlib
 cd /home/gec/zlib-1.2.8
 ./configure --prefix=/home/gec/audio

        编译     

  make

        安装        

 make install

----------------------------------
    2、制作libid3tag

        解压:            

tar xzvf libid3tag-0.15.1b.tar.gz -C ~

        配置:           

 ./configure --prefix=/home/gec/audio \
            --host=arm-none-linux-gnueabi 
            出错1:
                configure: error: zlib.h was not found
                *** You must first install zlib (libz) before you can build this package.
                *** If zlib is already installed, you may need to use the CPPFLAGS
                *** environment variable to specify its installed location, e.g. -I<dir>.
            解决:
                指明头文件路径:
                    ./configure --prefix=/home/gec/audio \
                    --host=arm-none-linux-gnueabi \
                    CPPFLAGS=-I/home/gec/audio/include
            出错2:
                configure: error: libz was not found
                *** You must first install zlib (libz) before you can build this package.
                *** If zlib is already installed, you may need to use the LDFLAGS
                *** environment variable to specify its installed location, e.g. -L<dir>.
            解决2:
                指明库文件路径:
                    ./configure --prefix=/home/gec/audio \
                    --host=arm-none-linux-gnueabi \
                    CPPFLAGS=-I/home/gec/audio/include \
                    LDFLAGS=-L/home/gec/audio/lib

       编译 

make

        安装            

make install

-----------------------------------------------------------
    3、制作libmad       
        解压:         

tar xzvf libmad-0.15.1b.tar.gz -C ~

cd /home/gec/libmad-0.15.1b

        配置:
          

 ./configure --prefix=/home/gec/audio \
            --host=arm-none-linux-gnueabi
        编译:
            make
            出错:
                arm-none-linux-gnueabi-gcc.br_real: error: unrecognized command line option '-fforce-mem'
                Makefile:383: recipe for target 'version.lo' failed
                make[2]: *** [version.lo] Error 1
                make[2]: Leaving directory '/home/gec/libmad-0.15.1b'
                Makefile:424: recipe for target 'all-recursive' failed
                make[1]: *** [all-recursive] Error 1
                make[1]: Leaving directory '/home/gec/libmad-0.15.1b'
                Makefile:249: recipe for target 'all' failed
                make: *** [all] Error 2
            解决:
                更改Makefile文件在129行删除'-fforce-mem'
                    vi Makefile 按下i进入插入模式删除'-fforce-mem'
            再make

         安装:         

  make install

-----------------------------------------------------------------------
    4、制作madplay      
        解压:           

 tar xzvf madplay-0.15.2b.tar.gz -C ~


 cd /home/gec/madplay-0.15.2b

        配置:
          

 ./configure --prefix=/home/gec/audio \
            --host=arm-none-linux-gnueabi \
            CPPFLAGS=-I/home/gec/audio/include \
            LDFLAGS=-L/home/gec/audio/lib

        编译:           

 make

        安装:          

 make install

---------------------------------------------------------------------
    5、移植madplay到开发板
        拷贝madpla到共享文件夹      

 cp /home/gec/audio/bin/madplay /mnt/hgfs/code

      下载到开发板 

tftp -g 192.168.1.147 -r madpla
chmod 777 madpla
./madpla

   ---------------------------------------------------------------------
    6、运行madplay      

  ./madpla ***.mp3 //歌曲需下载到开发板

        
        若出错提示缺少“***.so”文件
            解决:
                将/home/gec/audio里面的lib压缩打包下载到开发板后解压然后设置换件变量指明库文件路径即可
                具体方法可参照jpeg解码库的移植
===========================================================================
二、给系统发送命令
    #include <stdlib.h>

       int system(const char *command);
       
       command:你要发送的命令
       
       挂载额外内存
       cat /proc/partitions
            [root@GEC6818 /]#cat /proc/partitions 
            major minor  #blocks  name

             179        0    7634944 mmcblk0
             179        1      65536 mmcblk0p1
             179        2     772096 mmcblk0p2
             179        3     438272 mmcblk0p3
             179        4          1 mmcblk0p4
             179        5       8192 mmcblk0p5
             179        6      22528 mmcblk0p6
             179        7    6324224 mmcblk0p7    ----->没有使用
             179       16       4096 mmcblk0boot1
             179        8       4096 mmcblk0boot0       格式化未使用的分区mmcblk0p7
            mkfs.vfat /dev/mmcblk0p7
        使用mount命令挂载该分区
            cd /
            mkdir ***
            mount -t vfat /dev/mmcblk0p7  /***
       
---------------------------------------------------------------------
实现LCD开发板播放音乐并切换歌曲
music_touch.h

#ifndef __TOUCH_H_
#define __TOUCH_H_

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#include <linux/input.h>

struct get_abs_xy
{
    int x;
    int y;
    int ts;
};

int init_touch(void);
void get_xy(struct get_abs_xy *p);


#endif

music_touch.c

#include "music_touch.h"

int init_touch(void)
{
    //打开触摸屏
    int ts = open("/dev/input/event0", O_RDONLY);
    if(ts == -1)
    {
        perror("打开触摸屏失败:");
        exit(0);
    }
    return ts;
}

void get_xy(struct get_abs_xy *p)
{
    
    struct input_event myevent;
    bzero(&myevent, sizeof(myevent));
    bool x_flag = false;
    bool y_flag = true;

    while(1)
    {
        read(p->ts, &myevent, sizeof(myevent));
        
        if(myevent.type == EV_ABS)
        {
            if(myevent.code==ABS_X && x_flag==false && y_flag == true)
            {
                p->x = myevent.value;
                x_flag = true;
                y_flag = false;
            }

            if(myevent.code==ABS_Y && x_flag == true && y_flag == false)
            {
                p->y = myevent.value;
                x_flag = false;
                y_flag = true;
            }
            printf("X: %d Y:%d\n", p->x, p->y);
            if(myevent.code==ABS_PRESSURE && myevent.value==0)
                break;
        }
        if(myevent.type==EV_KEY && myevent.code==BTN_TOUCH && myevent.value==0)
            break;
        
    }

    
    return ;    
}

play_mp3.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include "music_touch.h"

int main()
{
    char buf[80];
    int vol = -30;
    struct get_abs_xy *touch = calloc(1, sizeof(struct get_abs_xy));
    touch->ts = init_touch();
    char *music_pathname[] = {"/aaa/SeeYouAgain.mp3",
                            "/aaa/KLL.mp3",
                            "/aaa/WHYS.mp3"};

    int s=0;
    
    int lcd = open("/dev/fb0", O_RDWR);

    if(lcd == -1)
    {
        perror("打开LCD失败");    
        exit(0);
    }

    // 2, 映射一块恰当大小的内存
    char *p = mmap(NULL, // 不指定映射内存的位置,让系统自动选取
               800*480*4, // 映射内存的大小
               PROT_READ | PROT_WRITE, // 以读、写模式操作映射内存
               MAP_SHARED, // 其他线程、进程可以共享访问这块内存
               lcd, // LCD文件描述符
               0    // 映射内存与LCD的偏移量,0代表完全重合
               );
    int bmp =  open("beijing1.bmp", O_RDONLY);
    if(bmp == -1)
    {
        perror("打开图片失败!");
        close(lcd);
        munmap(p, 800*480*4);
        exit(0);    
    }
    char bmp_buf[800*480*3];
    
    lseek(bmp, 54, SEEK_SET);
    int ret = read(bmp, bmp_buf, sizeof(bmp_buf));
    if(ret <= 0)
    {
        perror("读取bmp数据失败!");
        close(lcd);
        close(bmp);
        munmap(p, 800*480*4);
        exit(0);
    }
    int lcd_buf[800*480];
    // 4, 铺满整个映射内存
    for(int i=0; i<800*480; i++)
    {
        lcd_buf[i]=bmp_buf[i*3] | bmp_buf[i*3+1]<<8 | bmp_buf[i*3+2]<<16;
    }

    for(int y=0; y<480; y++)
    {
        for(int x=0; x<800; x++)
        {
            memcpy(p+4*x+800*4*y, &lcd_buf[x+(479-y)*800], 4);
        }
    }

    // 5, 释放资源
    munmap(p, 800*480*4);
    close(lcd);
    
    while(1)
    {
        
        get_xy(touch);
        printf("X: %d Y:%d\n", touch->x, touch->y);
        if(touch->x>266 && touch->x<400 && touch->y>320 && touch->y<480)
        {
            touch->x=0;
            touch->y=0;
            system("killall -KILL madplay");
            printf("已暂停\n");
        }
        if(touch->x>400 && touch->x<533 && touch->y>320 && touch->y<480)
        {
            touch->x=0;
            touch->y=0;
            bzero(buf, sizeof(buf));
            sprintf(buf, "/aaa/madplay %s -a %d &", music_pathname[s%3], vol);
            printf("/aaa/madplay %s -a %d\n", music_pathname[s%3], vol);
            system(buf);
        }
        if(touch->x>0 && touch->x<133 && touch->y>320 && touch->y<480)
        {
            touch->x=0;
            touch->y=0;
            bzero(buf, sizeof(buf));
            system("killall -KILL madplay");
            if((s-1<0))
            {
                s=3;
            }
            sprintf(buf, "/aaa/madplay %s -a %d &", music_pathname[(s-1)%3], vol);
            printf("/aaa/madplay %s -a %d\n",music_pathname[(s-1)%3], vol);
            system(buf);
            printf("上一首\n");
            s--;
        }
        //下一首
        if(touch->x>666 && touch->x<800 && touch->y>320 && touch->y<480)
        {
            touch->x=0;
            touch->y=0;
            bzero(buf, sizeof(buf));
            system("killall -KILL madplay");
            sprintf(buf, "/aaa/madplay %s -a %d &", music_pathname[(s+1)%3], vol);
            printf("/aaa/madplay %s -a %d\n",music_pathname[(s+1)%3], vol);
            system(buf);
            printf("下一首\n");
            s++;
        }
        
        //加声音
        if(touch->x>533 && touch->x<666 && touch->y>320 && touch->y<480)
        {
            touch->x=0;
            touch->y=0;
            vol +=5;
            printf("当前音量为: %d\n",vol);
        }
        //减声音
        if(touch->x>133 && touch->x<266 && touch->y>320 && touch->y<480)
        {
            touch->x=0;
            touch->y=0;
            vol -=5;
            printf("当前音量为: %d\n",vol);
        }
    }
    
} 

编译:

虚拟机:

arm-none-linux-gnueabi-gcc *.c -o play_mp3

lcd开发板

tftp -g 192.168.1.147 -r play_mp3
chmod 777 play_mp3
./play_mp3

结果:

猜你喜欢

转载自blog.csdn.net/myclass1312/article/details/82661669