你好,游客 登录 注册 搜索
背景:
阅读新闻

mini2440 MP3播放器

[日期:2012-08-23] 来源:Linux社区  作者:zy799894671 [字体:大 中 小]
应用程序:

mymp3.h:

  1. typedef struct node  
  2. {  
  3.     char data[100];  
  4.     struct node *left;  
  5.     struct node *right;  
  6. }NODE;  
  7.   
  8. enum modules{mod1,mod2};  
  9. enum mp3buttons{pre,start,next,pau,up,down};  
  10.   
  11. NODE *mycreatedoublelist(void);  
  12. extern void mp3_display(NODE *,int,int *,int *,int);  
  13. extern int module(void);  

mp3.c:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <unistd.h>  
  5. #include <sys/ioctl.h>  
  6. #include <sys/types.h>  
  7. #include <sys/stat.h>  
  8. #include <fcntl.h>  
  9. #include <sys/select.h>  
  10. #include <sys/time.h>  
  11. #include <errno.h>  
  12. #include <sys/types.h>  
  13. #include <sys/wait.h>  
  14. #include <linux/soundcard.h>  
  15. #include <sys/ipc.h>  
  16. #include <sys/shm.h>  
  17.  
  18. #include "mymp3.h"   
  19.   
  20. int main(void)  
  21. {  
  22.     int buttons_fd,mix_fd;  
  23.     int iLeft = 0;  
  24.     int iRight = 0;  
  25.     int iLevel;  
  26.     int volume = 75;  
  27.     iLeft = iRight = volume;  
  28.     iLevel = (iRight << 8) + iLeft;  
  29.     int mod;  
  30.     int ret;  
  31.     int sta = 0;  
  32.     int isstart = 0;  
  33.     int rand;  
  34.     int flg = 0;  
  35.     int num = 0;  
  36.     int stop = 0;  
  37.     char play[60];  
  38.     NODE *shmptr = NULL;  
  39.     int shmid;  
  40.     NODE *head = mycreatedoublelist();  
  41.     NODE *p = head;  
  42.     mod = module();  
  43.     mp3_display(head,10,&volume,&flg,mod);  
  44.     char buttons[6] = {'0', '0', '0', '0', '0', '0'};  
  45.     buttons_fd = open("/dev/my_buttons",0);  
  46.     if (buttons_fd < 0)   
  47.     {  
  48.         perror("open device buttons\n");  
  49.         exit(1);  
  50.     }  
  51.     mix_fd = open("/dev/mixer",O_WRONLY);  
  52.     if(mix_fd < 0)  
  53.     {  
  54.         perror("open device mixer\n");  
  55.     exit(1);  
  56.     }  
  57.     ioctl(mix_fd,MIXER_WRITE(SOUND_MIXER_VOLUME),&iLevel);  
  58.     shmid = shmget(IPC_PRIVATE,sizeof(NODE),0777);  
  59.     shmptr = (NODE *)shmat(shmid,0,0);  
  60.     memcpy(shmptr,p,sizeof(NODE));  
  61.     for (;;)   
  62.     {  
  63.         char current_buttons[6];  
  64.     int count_of_changed_key;  
  65.     int i;  
  66.     int changeflg = 0;  
  67.     pid_t pid,playpid;  
  68.     if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons)   
  69.     {  
  70.         perror("read buttons:");  
  71.         exit(1);  
  72.     }  
  73.     for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++)   
  74.     {  
  75.         if (buttons[i] != current_buttons[i])   
  76.         {  
  77.             buttons[i] = current_buttons[i];  
  78.         if(buttons[i] == '1')  
  79.         {  
  80.             ;  
  81.         }  
  82.         else  
  83.         {  
  84.             switch(i)  
  85.             {  
  86.                 case pau:  
  87.                 {  
  88.                 if(isstart != 0)  
  89.                 {  
  90.                     if(stop == 0)  
  91.                     {  
  92.                     system("killall -STOP madplay &");  
  93.                         kill(pid,19);  
  94.                     stop = 1;  
  95.                     }  
  96.                     else  
  97.                     {  
  98.                         printf("haved stopped!\n");  
  99.                     sleep(1);  
  100.                     }  
  101.                                 mp3_display(shmptr,pau,&volume,&flg,mod);  
  102.                 }  
  103.                 else  
  104.                 {  
  105.                     printf("Please input start frist!\n");  
  106.                     sleep(1);  
  107.                 }  
  108.                     break;  
  109.                 }  
  110.                 case pre:  
  111.                 {  
  112.                 if(num != 0)  
  113.                 {  
  114.                 kill(pid,9);  
  115.                 system("killall -STOP madplay &");  
  116.                 system("killall -9 madplay &");  
  117.                     num = 0;  
  118.                 }  
  119.                 if(mod == 1)  
  120.                 {  
  121.                         p = shmptr;  
  122.                     p = p->left;  
  123.                     memset(shmptr,0,sizeof(NODE));  
  124.                     memcpy(shmptr,p,sizeof(NODE));  
  125.                 }  
  126.                 else  
  127.                 {  
  128.                     rand = random() % 14 + 1;  
  129.                         while(rand--)  
  130.                         {  
  131.                         p = shmptr;  
  132.                     p = p->left;  
  133.                             memset(shmptr,0,sizeof(NODE));  
  134.                         memcpy(shmptr,p,sizeof(NODE));  
  135.                 }  
  136.                 }  
  137.                     changeflg = 1;  
  138.                 }  
  139.                 case next:  
  140.                 {  
  141.                     if(changeflg != 1)  
  142.                 {  
  143.                     if(num != 0)  
  144.                 {  
  145.                     kill(pid,9);  
  146.                     system("killall -STOP madplay &");  
  147.                     system("killall -9 madplay &");  
  148.                         num = 0;  
  149.                 }  
  150.                 if(mod == 1)  
  151.                 {  
  152.                     p = shmptr;  
  153.                         p = p->right;  
  154.                     memset(shmptr,0,sizeof(NODE));  
  155.                     memcpy(shmptr,p,sizeof(NODE));  
  156.                 }  
  157.                 else  
  158.                 {  
  159.                     rand = random() % 14 + 1;  
  160.                         while(rand--)  
  161.                     {  
  162.                         p = shmptr;  
  163.                         p = p->right;  
  164.                             memset(shmptr,0,sizeof(NODE));  
  165.                         memcpy(shmptr,p,sizeof(NODE));  
  166.                     }  
  167.                 }  
  168.                 }  
  169.                 }  
  170.                 case start:  
  171.                 {  
  172.                 if(stop == 1)  
  173.                 {  
  174.                 system("killall -CONT madplay &");  
  175.                     kill(pid,18);  
  176.                         stop = 0;  
  177.                                 mp3_display(shmptr,start,&volume,&flg,mod);  
  178.                 }  
  179.                 else  
  180.                 {  
  181.                     if(num != 0)  
  182.                     {  
  183.                     system("killall -STOP madplay &");  
  184.                     system("killall -9 madplay &");  
  185.                     kill(pid,9);  
  186.                     num = 0;  
  187.                     }  
  188.                     pid = fork();  
  189.                     if(pid < 0)  
  190.                     {  
  191.                         perror("fork error\n");  
  192.                     }  
  193.                     else if(pid == 0)  
  194.                     {  
  195.                     loopv:  
  196.                     playpid = fork();  
  197.                     if(playpid < 0)  
  198.                     {  
  199.                         perror("fork2 error\n");  
  200.                     }  
  201.                     else if(playpid == 0)  
  202.                     {  
  203.                         execl("/music/madplay","./madplay",shmptr->data,NULL);  
  204.                     }  
  205.                     wait(&sta);  
  206.                     if(WIFEXITED(sta) != 0)  
  207.                         {  
  208.                     p = shmptr;  
  209.                     p = p->right;  
  210.                         memset(shmptr,0,sizeof(NODE));  
  211.                         memcpy(shmptr,p,sizeof(NODE));  
  212.                                         mp3_display(shmptr,start,&volume,&flg,mod);  
  213.                         goto loopv;  
  214.                     }  
  215.                     else  
  216.                     {  
  217.                                         mp3_display(shmptr,start,&volume,&flg,mod);  
  218.                             goto loopv;  
  219.                     }  
  220.                 }  
  221.                 else  
  222.                 {  
  223.                             changeflg = 0;  
  224.                         num = 1;  
  225.                       
  226.                                     mp3_display(shmptr,start,&volume,&flg,mod);  
  227.                 }  
  228.                 }  
  229.                 isstart = 1;  
  230.                 break;  
  231.                 }  
  232.                 case up:  
  233.                 {  
  234.                             mp3_display(shmptr,up,&volume,&flg,mod);  
  235.                 iLeft = iRight = volume;  
  236.                 iLevel = (iRight << 8) + iLeft;  
  237.                 ioctl(mix_fd,MIXER_WRITE(SOUND_MIXER_VOLUME),&iLevel);  
  238.                     break;  
  239.                 }  
  240.                 case down:  
  241.                 {  
  242.                             mp3_display(shmptr,down,&volume,&flg,mod);  
  243.                 iLeft = iRight = volume;  
  244.                 iLevel = (iRight << 8) + iLeft;  
  245.                 ioctl(mix_fd,MIXER_WRITE(SOUND_MIXER_VOLUME),&iLevel);  
  246.                     break;  
  247.                 }  
  248.                 default:  
  249.                 {  
  250.                     break;  
  251.                 }  
  252.             }  
  253.                 count_of_changed_key++;  
  254.         }  
  255.         }  
  256.         }  
  257.         if (count_of_changed_key)   
  258.         {  
  259.             printf("\n");  
  260.         }  
  261.     }  
  262.     shmctl(shmid,IPC_RMID,NULL);  
  263.     close(mix_fd);  
  264.     close(buttons_fd);  
  265.     return 0;  
  266. }  
linux
相关资讯       mini2440 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款