use VS1033(MP3 decoder) and UL024TF(TFTLCD)

Dependencies:   TFTLCDwithFastIO FastIO SDFileSystem TouchPanel VS1033 mbed

Revision:
5:a493361aead2
Parent:
4:28900cff8f48
Child:
6:74271fce2368
--- a/main.cpp	Thu Jul 16 12:43:08 2015 +0000
+++ b/main.cpp	Thu Jul 16 18:18:00 2015 +0000
@@ -1,5 +1,6 @@
 #include "ili9328.h"
-#include <stdio.h>
+#include "stdio.h"
+#include "string.h"
 #include "SDFileSystem.h"
 #include "VS1053.h"
 #include "image.h"
@@ -15,11 +16,11 @@
 SDFileSystem sd(/*MOSI*/ P0_9, /*MISO*/ P0_8, /*SCK*/ P1_29, /*CS*/ P0_2, /*Mountpoint*/ "sd");
 VS1053       mp3(/*MOSI*/ P1_22 , /*MISO*/ P1_21, /*SCK*/ P1_20, /*CS*/ P1_23,
                  /*BSYNC*/ P2_17, /*DREQ*/ P2_16, /*RST*/ P2_18, /*SPI freq.*/ 10000000);
+Ticker Int100ms;
 
 Serial pc(P0_19,P0_18);
 #define SD_READ_BLOCK_SIZE (1024)
 
-
 #define BOTTON_PREV_XPOS    (15)
 #define BOTTON_PREV_YPOS    (170)
 #define BOTTON_PLAY_XPOS    (91)
@@ -99,6 +100,15 @@
      return result;
 }
 
+uint8_t     g_mp3Player_State = 0;
+uint16_t    g_mp3Player_Volume = 0;
+bool        gf_mp3Playwe_VolmeUpdate = 0;
+bool        gf_100ms = 0;
+
+void Int100msFunc()
+{
+    gf_100ms = 1;
+}
 
 int main()
 {
@@ -107,14 +117,11 @@
     size_t      mp3_ReadFileSize = 0;
     size_t      mp3_totalSizeSent=0;
     size_t      mp3_fileSize=0;
-    uint8_t     mp3Player_State = 0;
-    uint16_t    mp3Player_Volume = 0;
-    bool        f_mp3Playwe_VolmeUpdate = 0;
     bool        f_mp3Playwe_Playing = 0;
     uint8_t     buf[SD_READ_BLOCK_SIZE];
     char        str[100];
     int32_t     XReadPos = 0,YReadPos = 0;
-    bool        touchPanel_Touched = 0;
+    static bool touchPanel_Touched = 0;
                                           //Format,Xsize,Ysize,PixelData
     const bitmap_t img_button_play      = { RGB16, 60, 60, &bmp_button_play };
     const bitmap_t img_button_stop      = { RGB16, 60, 60, &bmp_button_stop };
@@ -122,9 +129,11 @@
     const bitmap_t img_button_prev      = { RGB16, 60, 60, &bmp_button_prev };
     const bitmap_t img_button_volup     = { RGB16, 60, 60, &bmp_button_volup };
     const bitmap_t img_button_voldown   = { RGB16, 60, 60, &bmp_button_voldown };
+    
+    pc.baud(921600);
+    printf("Power ON\r\n");
 
-    pc.baud(115200);
-    printf("Power ON\r\n");
+    Int100ms.attach_us(&Int100msFunc, 100000);
 
     //SD Init.(本来ならいらないけど、SDカードによっちゃもう1回リセットしないとダメポ
     sd.disk_initialize();
@@ -146,12 +155,20 @@
 
     lcd.SetBackground(COLOR_BLUE);
     lcd.FillScreen(-1);
-    lcd.SetFont( &TerminusFont );
+    lcd.SetFont( &TerminusBigFont );
 
-/*  for Caliblation
-    lcd.DrawCircle(40,40,10,COLOR_WHITE);
-    lcd.DrawCircle(280,200,10,COLOR_WHITE);
-*/
+  //for Caliblation
+    while(0)
+    {
+        int32_t     XReadPos = 0,YReadPos = 0;
+        lcd.DrawCircle(40,40,10,COLOR_WHITE);
+        lcd.DrawCircle(280,200,10,COLOR_WHITE);
+        XReadPos = getTouchPanelPosX();
+        YReadPos = getTouchPanelPosY();
+        printf("X:%d Y:%d\r\n",XReadPos,YReadPos);
+        wait_ms(500);
+    }
+
                     //Xpos, Ypos, ImageData(type:bitmap_t), Scale
     lcd.DrawBitmap( BOTTON_PREV_XPOS,    BOTTON_PREV_YPOS,    (const bitmap_t*)&img_button_prev, 1 );
     lcd.DrawBitmap( BOTTON_PLAY_XPOS,    BOTTON_PLAY_YPOS,    (const bitmap_t*)&img_button_play, 1 );
@@ -160,7 +177,67 @@
     lcd.DrawBitmap( BOTTON_VOLUP_XPOS,   BOTTON_VOLUP_YPOS,   (const bitmap_t*)&img_button_volup, 1 );
     lcd.DrawBitmap( BOTTON_VOLDOWN_XPOS, BOTTON_VOLDOWN_YPOS, (const bitmap_t*)&img_button_voldown, 1 );
 
-//mp3Player_Stateに対して↓
+////////////////////////////////////////////////////////////
+/*  SDCard GetFileList Section                            */
+////////////////////////////////////////////////////////////
+#define SD_MAX_FILENAME_LENGTH  (256)   //MAX 256
+#define SD_MAX_FILE_COUNT       (20)
+
+    char SDFileList[SD_MAX_FILE_COUNT][SD_MAX_FILENAME_LENGTH]={0};
+    char SDFileList_short[SD_MAX_FILE_COUNT][SD_MAX_FILENAME_LENGTH]={0};
+    char SDFileList_cnt = 0;
+    char mp3PlayingFileName[SD_MAX_FILENAME_LENGTH]={0};
+    uint8_t mp3PlayingFileNo = 0;
+    int i = 0;
+    DIR *d;
+    struct dirent *p;
+
+    d = opendir("/sd");
+    if ( d != NULL )
+    {
+        while ( (p = readdir(d)) != NULL )
+        {
+            char *ret;
+            ret = strstr(p->d_name,"System");   //System Vol..フォルダは除外
+            if(ret == 0)
+            {
+                sprintf((char *)(SDFileList_short[i]+0),"%s",(const char *)p->d_name);
+                sprintf((char *)(SDFileList[i]+0),"/sd/%s",(const char *)p->d_name);
+                i+=1;
+            }
+        }
+        SDFileList_cnt = i;
+    }
+    closedir(d);
+
+    for(i=0;i<SDFileList_cnt;i++)
+    {
+        sprintf(str,"%d:%s",i,SDFileList_short[i]);
+        lcd.Print( str, LEFT, i*28 ); // align text to center horizontally and use starndard colors
+        lcd.Print( "*", LEFT, 0 ); // align text to center horizontally and use starndard colors
+    }
+
+    mp3PlayingFileNo = 0;
+    sprintf(mp3PlayingFileName,"%s",SDFileList[mp3PlayingFileNo]);
+
+    printf("file count=%d\r\n",SDFileList_cnt);
+    printf("FirstPlayFile:%s\r\n",mp3PlayingFileName);
+    g_mp3Player_State = 0;
+    f_mp3Playwe_Playing = 0;
+    while(1)
+    {
+
+//////////////////////////////////////////////////////////////
+/*  TouchPanel GetStatas Section                            */
+//////////////////////////////////////////////////////////////
+    if(gf_100ms == 1)
+    {
+        XReadPos = getTouchPanelPosX();
+        YReadPos = getTouchPanelPosY();
+        gf_100ms = 0;
+    }
+
+//g_mp3Player_Stateに対して↓
 #define MP3_STATE_STOPPING      (0)
 #define MP3_STATE_STOPREQ       (1)
 #define MP3_STATE_PLAYREQ       (2)
@@ -168,21 +245,15 @@
 #define MP3_STATE_NEXTREQ       (4)
 #define MP3_STATE_PREVREQ       (5)
 #define MP3_STATE_VOLCHANGEREQ  (6)
-
-    while(1)
-    {
-//////////////////////////////////////////////////////////////
-/*  TouchPanel GetStatas Section                            */
-//////////////////////////////////////////////////////////////
-
-        XReadPos = getTouchPanelPosX();
-        YReadPos = getTouchPanelPosY();
+#define MP3_STATE_REPLAY        (7)
+#define MP3_STATE_FILECHANGE    (8)
 
         if( checkTouchBotton_prev(XReadPos,YReadPos) )
         {
             if(touchPanel_Touched == 0)
             {
-                mp3Player_State = MP3_STATE_PREVREQ;
+                printf("prev touch\r\n");
+                g_mp3Player_State = MP3_STATE_PREVREQ;
                 touchPanel_Touched = 1;
             }
         }
@@ -190,9 +261,10 @@
         {
             if(touchPanel_Touched == 0)
             {
-                if( (mp3Player_State != MP3_STATE_PLAYING) && (mp3Player_State != MP3_STATE_PLAYREQ) )
+                printf("play touch\r\n");
+                if( (g_mp3Player_State != MP3_STATE_PLAYING) && (g_mp3Player_State != MP3_STATE_PLAYREQ) )
                 {
-                    mp3Player_State = MP3_STATE_PLAYREQ;
+                    g_mp3Player_State = MP3_STATE_PLAYREQ;
                 }
                 touchPanel_Touched = 1;
             }
@@ -201,9 +273,10 @@
         {
             if(touchPanel_Touched == 0)
             {
-                if( (mp3Player_State != MP3_STATE_STOPPING) && (mp3Player_State != MP3_STATE_STOPREQ) )
+                printf("stop touch\r\n");
+                if( (g_mp3Player_State != MP3_STATE_STOPPING) && (g_mp3Player_State != MP3_STATE_STOPREQ) )
                 {
-                    mp3Player_State = MP3_STATE_STOPREQ;
+                    g_mp3Player_State = MP3_STATE_STOPREQ;
                 }
                 touchPanel_Touched = 1;
             }
@@ -212,7 +285,8 @@
         {
             if(touchPanel_Touched == 0)
             {
-                mp3Player_State = MP3_STATE_NEXTREQ;
+                printf("next touch\r\n");
+                g_mp3Player_State = MP3_STATE_NEXTREQ;
                 touchPanel_Touched = 1;
             }
         }
@@ -220,38 +294,41 @@
         {
             if(touchPanel_Touched == 0)
             {
-                if(mp3Player_Volume >= 6527)
+                printf("volup touch\r\n");
+                if(g_mp3Player_Volume >= 6527)
                 {
-                    mp3Player_Volume-=6527;
-                    f_mp3Playwe_VolmeUpdate = 1;
+                    g_mp3Player_Volume-=6527;
+                    gf_mp3Playwe_VolmeUpdate = 1;
                 }
                 touchPanel_Touched = 1;
             }
-
+    
         }
         else if( checkTouchBotton_voldown(XReadPos,YReadPos) )
         {
             if(touchPanel_Touched == 0)
             {
-                if(mp3Player_Volume <= 0xE57F )
+                printf("voldown touch\r\n");
+                if(g_mp3Player_Volume <= 0xE57F )
                 {
-                    mp3Player_Volume+=6527;
-                    f_mp3Playwe_VolmeUpdate = 1;
+                    g_mp3Player_Volume+=6527;
+                    gf_mp3Playwe_VolmeUpdate = 1;
                 }
                 touchPanel_Touched = 1;
             }
-            printf("VolDownTouched\r\n");
         }
         else //any botton not touched
         {
             touchPanel_Touched = 0;
         }
+
 //////////////////////////////////////////////////////////////
 /*  MP3 Player Control Section                              */
 //////////////////////////////////////////////////////////////
-        if(mp3Player_State == MP3_STATE_PLAYREQ)
+        if(g_mp3Player_State == MP3_STATE_PLAYREQ)
         {
-            fp = fopen("/sd/1.mp3", "rb");
+            printf("FileOpen:%s\r\n",mp3PlayingFileName);
+            fp = fopen(mp3PlayingFileName, "rb");
             SDFileOpenFailCnt = 0;
             while(!fp)
             {
@@ -264,7 +341,7 @@
                 }
                 printf("Fail file open n=%d\r\n",SDFileOpenFailCnt);
                 sd.disk_initialize();
-                fp = fopen("/sd/1.mp3", "rb");
+                fp = fopen(mp3PlayingFileName, "rb");
                 wait(1);
             }
 
@@ -277,15 +354,15 @@
             rewind(fp);
             clearerr(fp);     
             mp3_totalSizeSent = 0;
-            mp3Player_State = MP3_STATE_PLAYING;
+            g_mp3Player_State = MP3_STATE_PLAYING;
         }
-        if( mp3Player_State == MP3_STATE_PLAYING )
+        if( g_mp3Player_State == MP3_STATE_PLAYING )
         {
-            printf("totalSendSize:%d\r\n",mp3_totalSizeSent);
+//            printf("totalSendSize:%d\r\n",mp3_totalSizeSent);
 
             if(mp3_totalSizeSent>=mp3_fileSize) //play song end
             {
-                mp3Player_State = MP3_STATE_STOPREQ;
+                g_mp3Player_State = MP3_STATE_STOPREQ;
             }
             else                                //transmit from SDCard to VS1033
             {
@@ -294,7 +371,7 @@
                 f_mp3Playwe_Playing = 1;
             }
         }
-        if( mp3Player_State == MP3_STATE_STOPREQ )
+        if( (g_mp3Player_State == MP3_STATE_STOPREQ) || (g_mp3Player_State == MP3_STATE_REPLAY) )
         {
             uint16_t returnCode=0;
             uint16_t stopFailCnt = 0;
@@ -307,7 +384,7 @@
                 {
                     stopFailCnt++;
                 }
-                if(stopFailCnt >= 10)
+                if(stopFailCnt >= 20)
                 {
                     printf("ERROR! impossible of stop\r\nSystem STOP\r\n");
                     while(1);
@@ -316,14 +393,65 @@
             fclose(fp);
             fp = NULL;
             f_mp3Playwe_Playing = 0;
-            mp3Player_State = MP3_STATE_STOPPING;            
+            if(g_mp3Player_State == MP3_STATE_STOPREQ)
+            {
+                g_mp3Player_State = MP3_STATE_STOPPING;
+            }
+            if(g_mp3Player_State == MP3_STATE_REPLAY)
+            {
+                g_mp3Player_State = MP3_STATE_PLAYREQ;
+            }
         }
-        if( mp3Player_State == MP3_STATE_STOPPING )
+
+        if( g_mp3Player_State == MP3_STATE_STOPPING )
+        {
+            f_mp3Playwe_Playing = 0;
+        }
+
+        if( g_mp3Player_State == MP3_STATE_NEXTREQ )
         {
+            if( mp3PlayingFileNo >= (SDFileList_cnt -1) )
+            {
+                mp3PlayingFileNo = 0;
+            }
+            else
+            {
+                mp3PlayingFileNo+=1;
+            }
+            g_mp3Player_State = MP3_STATE_FILECHANGE;
         }
 
-        if( mp3Player_State == MP3_STATE_VOLCHANGEREQ )
+        if( g_mp3Player_State == MP3_STATE_PREVREQ )
+        {
+            if( mp3PlayingFileNo <= 0 )
+            {
+                mp3PlayingFileNo = (SDFileList_cnt - 1);
+            }
+            else
+            {
+                mp3PlayingFileNo-=1;
+            }
+            g_mp3Player_State = MP3_STATE_FILECHANGE;
+        }
+        if( g_mp3Player_State == MP3_STATE_FILECHANGE )
         {
+            printf("next:%d,%s\r\n",mp3PlayingFileNo,SDFileList[mp3PlayingFileNo]);
+            sprintf(mp3PlayingFileName,"%s",SDFileList[mp3PlayingFileNo]);
+            if(f_mp3Playwe_Playing == 1)
+            {
+                g_mp3Player_State = MP3_STATE_REPLAY;
+            }
+            else
+            {
+                g_mp3Player_State = MP3_STATE_STOPPING;
+            }
+        }
+
+        if( gf_mp3Playwe_VolmeUpdate == 1 )
+        {
+            mp3.VolControl(g_mp3Player_Volume);
+            gf_mp3Playwe_VolmeUpdate = 0;
+            printf("Vol:%d\r\n",g_mp3Player_Volume);
         }
     }    
 }