OSDN Git Service

...
[kozos-expbrd/kozos_expbrd.git] / firm / sample / simple_mp3_player / os / task_fileio.c
1
2 #include "pff.h"
3 #include "lib.h"
4 #include "task_fileio.h"
5 #include "task_audio.h"
6 #include "task_menu.h"
7
8 static FATFS fatfs;
9 static DIR dir;
10 static FILINFO filinfo;
11
12 static int readfunc(void *buf, int siz)
13 {
14     WORD cnt;
15     pf_read(buf, siz, &cnt);
16     return cnt;
17 }
18
19 static int play(const char *filename)
20 {
21     FRESULT r = pf_open(filename);
22     if (r != FR_OK) {
23         return 1;
24     }
25     static int vol_prev = 0, vol_curr = 0;
26     int divcnt = 0;
27     int control = 0;
28     while (audio_play(readfunc)) {
29         switch ((divcnt++) % 8) {
30             case 0:
31                 /*
32                  * \e$BAaAw$j$d4,$-La$7\e(B
33                  */
34                 menu_get_audio_control(&control);
35                 if (control != 0) {
36                     switch (control) {
37                         case AUDIO_CONTROL_FWD:
38                             if (fatfs.fptr + 8192 < fatfs.fsize) {
39                                 pf_lseek(fatfs.fptr + 8192);
40                             }
41                             audio_cancel();
42                             break;
43                         case AUDIO_CONTROL_REW:
44                             if (16384 < fatfs.fptr) {
45                                 pf_lseek(fatfs.fptr - 16384);
46                             }
47                             audio_cancel();
48                             break;
49                     }
50                 }
51                 break;
52             case 1:
53                 /*
54                  * \e$B%U%!%$%k>pJs$NDLCN\e(B
55                  */
56                 menu_file_info(
57                         filinfo.fname, fatfs.fptr * 100 / fatfs.fsize);
58                 break;
59             case 2:
60                 /*
61                  * \e$B%*!<%G%#%*>pJs$N<hF@\e(B
62                  */
63                 menu_get_audio_info(&vol_curr);
64                 if (vol_curr != vol_prev) {
65                     audio_volume(vol_curr, vol_curr);
66                 }
67                 vol_prev = vol_curr;
68                 break;
69         }
70     }
71     return 0;
72 }
73
74 static int is_music_file(const char *filename)
75 {
76     int len = strlen(filename);
77     if (len < 4) {
78         return 0;
79     }
80     if (strcmp(filename + len - 4, ".MP3") == 0) {
81         return 1;
82     }
83     if (strcmp(filename + len - 4, ".WAV") == 0) {
84         return 1;
85     }
86     return 0;
87 }
88
89 int task_fileio(int argc, char *argv[])
90 {
91   while (1) {
92       menu_file_info("Mounting...", 0);
93       if (pf_mount(&fatfs)) {
94           continue;
95       }
96       while (1) {
97           menu_file_info("Directory", 0);
98           if (pf_opendir(&dir, "")) {
99               break;
100           }
101           while (!pf_readdir(&dir, &filinfo) && filinfo.fname[0]) {
102               menu_file_info(filinfo.fname, 0);
103               if (!(filinfo.fattrib & (AM_DIR | AM_HID))) {
104                   if (is_music_file(filinfo.fname)) {
105                       if (play(filinfo.fname)) {
106                           break;
107                       }
108                   }
109               }
110           }
111       }
112   }
113
114   return 0;
115 }
116