OSDN Git Service

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