use VS1033(MP3 decoder) and UL024TF(TFTLCD)

Dependencies:   TFTLCDwithFastIO FastIO SDFileSystem TouchPanel VS1033 mbed

main.cpp

Committer:
nameless129
Date:
2015-07-30
Revision:
8:198b69e78c39
Parent:
7:fc65d965a569
Child:
9:96b053c0865e

File content as of revision 8:198b69e78c39:

#pragma O3

#include "FastIO.h"
#include "ili9328.h"
#include "stdio.h"
#include "string.h"
#include "SDFileSystem.h"
#include "VS1053.h"
#include "image.h"
#include "TouchPanel.h"
#include "mbed.h"

// prepare the data bus for writing commands and pixel data
// create the lcd instance
//(PinName CS, PinName RESET, PinName RS, PinName WR, BusOut *DATA_PORT, PinName BL=NC, PinName RD=NC, backlight_t blType=Constant, float defaultBackLightLevel=1.0)
ILI9328_LCD lcd( P0_12, P0_11, P0_13, P0_14, NULL, NC, P1_9 ); // control pins and data bus

SDFileSystem sd(/*MOSI*/ P0_9, /*MISO*/ P0_8, /*SCK*/ P1_29, /*CS*/ P0_2, /*Mountpoint*/ "sd", NC, SDFileSystem::SWITCH_NONE, 10000000);
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.*/ 5000000);
Ticker Int100ms;

Serial pc(P0_19,P0_18);
#define SD_READ_BLOCK_SIZE (512)

#define BOTTON_PREV_XPOS    (15)
#define BOTTON_PREV_YPOS    (180)
#define BOTTON_PLAY_XPOS    (91)
#define BOTTON_PLAY_YPOS    (180)
#define BOTTON_STOP_XPOS    (168)
#define BOTTON_STOP_YPOS    (180)
#define BOTTON_NEXT_XPOS    (245)
#define BOTTON_NEXT_YPOS    (180)
#define BOTTON_VOLUP_XPOS   (245)
#define BOTTON_VOLUP_YPOS   (10)
#define BOTTON_VOLDOWN_XPOS (245)
#define BOTTON_VOLDOWN_YPOS (80)

bool checkTouchBotton_prev(int32_t xpos,int32_t ypos)
{
    bool result = 0;
    if( ( (xpos >= BOTTON_PREV_XPOS+5) && (xpos <= BOTTON_PREV_XPOS+55) ) &&
        ( (ypos >= BOTTON_PREV_YPOS+5) && (ypos <= BOTTON_PREV_YPOS+55) ) )
     {
         result = 1;
     }
     return result;
}

bool checkTouchBotton_play(int32_t xpos,int32_t ypos)
{
    bool result = 0;
    if( ( (xpos >= BOTTON_PLAY_XPOS+5) && (xpos <= BOTTON_PLAY_XPOS+55) ) &&
        ( (ypos >= BOTTON_PLAY_YPOS+5) && (ypos <= BOTTON_PLAY_YPOS+55) ) )
     {
         result = 1;
     }
     return result;
}

bool checkTouchBotton_stop(int32_t xpos,int32_t ypos)
{
    bool result = 0;
    if( ( (xpos >= BOTTON_STOP_XPOS+5) && (xpos <= BOTTON_STOP_XPOS+55) ) &&
        ( (ypos >= BOTTON_STOP_XPOS+5) && (ypos <= BOTTON_STOP_XPOS+55) ) )
     {
         result = 1;
     }
     return result;
}

bool checkTouchBotton_next(int32_t xpos,int32_t ypos)
{
    bool result = 0;
    if( ( (xpos >= BOTTON_NEXT_XPOS+5) && (xpos <= BOTTON_NEXT_XPOS+55) ) &&
        ( (ypos >= BOTTON_NEXT_YPOS+5) && (ypos <= BOTTON_NEXT_YPOS+55) ) )
     {
         result = 1;
     }
     return result;
}

bool checkTouchBotton_volup(int32_t xpos,int32_t ypos)
{
    bool result = 0;
    if( ( (xpos >= BOTTON_VOLUP_XPOS+5) && (xpos <= BOTTON_VOLUP_XPOS+55) ) &&
        ( (ypos >= BOTTON_VOLUP_YPOS+5) && (ypos <= BOTTON_VOLUP_YPOS+55) ) )
     {
         result = 1;
     }
     return result;
}

bool checkTouchBotton_voldown(int32_t xpos,int32_t ypos)
{
    bool result = 0;
    if( ( (xpos >= BOTTON_VOLDOWN_XPOS+5) && (xpos <= BOTTON_VOLDOWN_XPOS+55) ) &&
        ( (ypos >= BOTTON_VOLDOWN_YPOS+5) && (ypos <= BOTTON_VOLDOWN_YPOS+55) ) )
     {
         result = 1;
     }
     return result;
}

#define TOUCHPANEL_BOTTON_TOUCH_STOP    (0)
#define TOUCHPANEL_BOTTON_TOUCH_PLAY    (1)
#define TOUCHPANEL_BOTTON_TOUCH_NEXT    (2)
#define TOUCHPANEL_BOTTON_TOUCH_PREV    (3)
#define TOUCHPANEL_BOTTON_TOUCH_VOLUP   (4)
#define TOUCHPANEL_BOTTON_TOUCH_VOLDOWN (5)
#define TOUCHPANEL_BOTTON_NOT_TOUCH     (-1)
#define TOUCHPANEL_OTHER_TOUCH          (-2)

int8_t detectTouchBotton(int32_t xpos,int32_t ypos)
{
    int8_t         result = 0;

    if( checkTouchBotton_stop(xpos,ypos) )
    {
        result = TOUCHPANEL_BOTTON_TOUCH_STOP;
    }
    else if( checkTouchBotton_play(xpos,ypos) )
    {
        result = TOUCHPANEL_BOTTON_TOUCH_PLAY;
    }
    else if( checkTouchBotton_prev(xpos,ypos) )
    {
        result = TOUCHPANEL_BOTTON_TOUCH_PREV;
    }
    else if( checkTouchBotton_next(xpos,ypos) )
    {
        result = TOUCHPANEL_BOTTON_TOUCH_NEXT;
    }
    else if( checkTouchBotton_volup(xpos,ypos) )
    {
        result = TOUCHPANEL_BOTTON_TOUCH_VOLUP;
    }
    else if( checkTouchBotton_voldown(xpos,ypos) )
    {
        result = TOUCHPANEL_BOTTON_TOUCH_VOLDOWN;
    }
    else if( (xpos == -1) || (ypos == -1) )
    {
        result = TOUCHPANEL_BOTTON_NOT_TOUCH;
    }
    else
    {
        result = TOUCHPANEL_OTHER_TOUCH;
    }
    
    return result;    
}

uint8_t     g_mp3Player_State = 0;
uint16_t    g_mp3Player_Volume = 0;
bool        gf_mp3Playwe_VolmeUpdate = 0;
bool        gf_TimerInt = 0;

void Int100msFunc()
{
    gf_TimerInt = 1;
}

int main()
{
    static FILE *fp = NULL;
    uint8_t     SDFileOpenFailCnt = 0;
    size_t      mp3_ReadFileSize = 0;
    size_t      mp3_totalSizeSent=0;
    size_t      mp3_fileSize=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;
    uint32_t    totalPlay = 0;
    int         dot1_FileSize = 0;
    int         dot_XPos = 0,old_dot_XPos = 0;
    int         lcd_FileViewPage = 0,old_lcd_FileViewPage = 0;
    int         lcd_FileViewListEnd = 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 };
    const bitmap_t img_button_next      = { RGB16, 60, 60, &bmp_button_next };
    const bitmap_t img_button_prev      = { RGB16, 60, 60, &bmp_button_prev };
    const bitmap_t img_button_pause     = { RGB16, 60, 60, &bmp_button_pause };

//    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");

    Int100ms.attach_us(&Int100msFunc, 100000);

    //SD Init.(本来ならいらないけど、SDカードによっちゃもう1回リセットしないとダメポ
    sd.disk_initialize();

    //LCD Init.
    lcd.Initialize(LANDSCAPE,RGB16);

/* for sine test mode */
//    mp3.sine_test_activate(SineWave_10k);
//    while(1);

    //MP3 decoder Init.
    mp3.hardwareReset();
    mp3.sci_init();
    mp3.sdi_init();
    wait(0.1);

    printf("init CMPL.\r\n"); 

    lcd.SetBackground(COLOR_BLUE);
    lcd.FillScreen(-1);
    lcd.SetFont( &TerminusBigFont );

  //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 );
    lcd.DrawBitmap( BOTTON_STOP_XPOS,    BOTTON_STOP_YPOS,    (const bitmap_t*)&img_button_stop, 1 );
    lcd.DrawBitmap( BOTTON_NEXT_XPOS,    BOTTON_STOP_YPOS,    (const bitmap_t*)&img_button_next, 1 );

//    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 );

////////////////////////////////////////////////////////////
/*  SDCard GetFileList Section                            */
////////////////////////////////////////////////////////////
#define SD_MAX_FILENAME_LENGTH  (256)   //MAX 256
#define SD_MAX_FILE_COUNT       (50)

    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);
                printf("FileFound:%d,%s\r\n",i,SDFileList[i]);
                i+=1;
            }
        }
        SDFileList_cnt = i;
    }
    closedir(d);

//g_mp3Player_Stateに対して↓
#define MP3_STATE_STOPPING      (0)
#define MP3_STATE_STOPREQ       (1)
#define MP3_STATE_PLAYREQ       (2)
#define MP3_STATE_PLAYING       (3)
#define MP3_STATE_NEXTREQ       (4)
#define MP3_STATE_PREVREQ       (5)
#define MP3_STATE_VOLCHANGEREQ  (6)
#define MP3_STATE_REPLAY        (7)
#define MP3_STATE_FILECHANGE    (8)
#define MP3_STATE_PAUSEREQ      (9)
#define MP3_STATE_PAUSEING      (10)
#define MP3_STATE_RESUMEREQ     (11)
    
    printf("file count=%d\r\n",SDFileList_cnt);
    mp3PlayingFileNo = 0;
    g_mp3Player_State = MP3_STATE_FILECHANGE;
    f_mp3Playwe_Playing = 0;

    while(1)
    {

//////////////////////////////////////////////////////////////
/*  TouchPanel GetStatas Section                            */
//////////////////////////////////////////////////////////////
        if(gf_TimerInt == 1)
        {
            XReadPos = getTouchPanelPosX();
            YReadPos = getTouchPanelPosY();
//            printf("X:%d Y:%d\r\n",XReadPos,YReadPos);
            switch( detectTouchBotton(XReadPos,YReadPos) )
            {
                case TOUCHPANEL_BOTTON_TOUCH_PREV:
                    if(touchPanel_Touched == 0)
                    {
                        printf("prev touch\r\n");
                        g_mp3Player_State = MP3_STATE_PREVREQ;
                        touchPanel_Touched = 1;
                    }
                    break;
                case TOUCHPANEL_BOTTON_TOUCH_PLAY:
                    if(touchPanel_Touched == 0)
                    {
                        printf("play touch\r\n");
                        if( (g_mp3Player_State == MP3_STATE_STOPPING) )
                        {
                            g_mp3Player_State = MP3_STATE_PLAYREQ;
                            printf("MP3_STATE_PLAYREQ\r\n");
                        }
                        if( g_mp3Player_State == MP3_STATE_PLAYING )
                        {
                            g_mp3Player_State = MP3_STATE_PAUSEREQ;
                            printf("MP3_STATE_PAUSEREQ\r\n");
                        }
                        if( g_mp3Player_State == MP3_STATE_PAUSEING )
                        {
                            g_mp3Player_State = MP3_STATE_RESUMEREQ;
                            printf("MP3_STATE_RESUMEREQ\r\n");
                        }

                        touchPanel_Touched = 1;
                    }
                    break;
                case TOUCHPANEL_BOTTON_TOUCH_STOP:
                    if(touchPanel_Touched == 0)
                    {
                        printf("stop touch\r\n");
                        if( (g_mp3Player_State != MP3_STATE_STOPPING) && (g_mp3Player_State != MP3_STATE_STOPREQ) )
                        {
                            g_mp3Player_State = MP3_STATE_STOPREQ;
                        }
                        touchPanel_Touched = 1;
                    }
                    break;
                case TOUCHPANEL_BOTTON_TOUCH_NEXT:
                    if(touchPanel_Touched == 0)
                    {
                        printf("next touch\r\n");
                        g_mp3Player_State = MP3_STATE_NEXTREQ;
                        touchPanel_Touched = 1;
                    }
                    break;
                case TOUCHPANEL_OTHER_TOUCH:
                    printf("otherTouch\r\n");
                    printf("X:%d Y:%d\r\n",XReadPos,YReadPos);
                    break;
                case TOUCHPANEL_BOTTON_NOT_TOUCH:
//                    printf("notTouch\r\n");
                    touchPanel_Touched = 0;
                    break;
                default :
                    break;
            }
        }

//////////////////////////////////////////////////////////////
/*  MP3 Player Control Section                              */
//////////////////////////////////////////////////////////////
        if(g_mp3Player_State == MP3_STATE_PLAYREQ)
        {
            printf("FileOpen:%s\r\n",mp3PlayingFileName);
            fp = fopen(mp3PlayingFileName, "rb");
            SDFileOpenFailCnt = 0;
            while(!fp)
            {
                SDFileOpenFailCnt+=1;
                if(SDFileOpenFailCnt >= 3)
                {
                    printf("Fail SD init\r\n");
                    printf("System Stop.\r\n");
                    NVIC_SystemReset();
                }
                printf("Fail file open n=%d\r\n",SDFileOpenFailCnt);
                sd.disk_initialize();
                fp = fopen(mp3PlayingFileName, "rb");
                wait(1);
            }

            //Get file size
            fseek( fp, 0, SEEK_END );
            mp3_fileSize = ftell( fp );
            printf("FileOpen. size=%dbit\r\n",mp3_fileSize);

            //move file pointer to top.
            rewind(fp);
            clearerr(fp);
            mp3_totalSizeSent = 0;
            lcd.Print("Playing   ",15,145);

            lcd.DrawRect(135,155,310,165,COLOR_WHITE);
            lcd.FillRect(136,156,309,164,COLOR_BLUE);
            dot1_FileSize = mp3_fileSize/175;           

            g_mp3Player_State = MP3_STATE_PLAYING;
            lcd.DrawBitmap( BOTTON_PLAY_XPOS,    BOTTON_PLAY_YPOS,    (const bitmap_t*)&img_button_pause, 1 );

            totalPlay++;
            printf("PlayCount=%d\r\n",totalPlay);
        }
        if( g_mp3Player_State == MP3_STATE_PLAYING )
        {

            if(mp3_totalSizeSent>=mp3_fileSize) //play song end
            {
                f_mp3Playwe_Playing = 1;
                g_mp3Player_State = MP3_STATE_NEXTREQ;  //next song
            }
            else                                //transmit from SDCard to VS1033
            {
                if( mp3.checkDREQ() == 1 )
                {                   
                    mp3_ReadFileSize = fread(buf, sizeof(uint8_t), SD_READ_BLOCK_SIZE, fp);
                    mp3_totalSizeSent += mp3.sendDataBlock(buf, mp3_ReadFileSize);
                    dot_XPos = mp3_totalSizeSent / dot1_FileSize;
                    if(old_dot_XPos != dot_XPos)
                    {
                        lcd.DrawLine(135+dot_XPos, 156, 135+dot_XPos, 164); //x1:135 y1:155 x2:310 y3:165
                    }
                    old_dot_XPos = dot_XPos;
//                    printf("SendedSize:%d LinePos:%d\r\n",mp3_totalSizeSent,dot_XPos);
//                    printf(" HDAT0:0x%x HDAT1:0x%x\r\n",mp3.readReg(mp3.SCI_HDAT0),mp3.readReg(mp3.SCI_HDAT1));

                }

                f_mp3Playwe_Playing = 1;
            }
        }

        if( g_mp3Player_State == MP3_STATE_PAUSEREQ )
        {
            lcd.DrawBitmap( BOTTON_PLAY_XPOS,    BOTTON_PLAY_YPOS,    (const bitmap_t*)&img_button_play, 1 );
            g_mp3Player_State = MP3_STATE_PAUSEING;
        }
        if( g_mp3Player_State == MP3_STATE_PAUSEING )
        {
        }
        if( g_mp3Player_State == MP3_STATE_RESUMEREQ )
        {
            lcd.DrawBitmap( BOTTON_PLAY_XPOS,    BOTTON_PLAY_YPOS,    (const bitmap_t*)&img_button_pause, 1 );
            g_mp3Player_State = MP3_STATE_PLAYING;
        }
        if( (g_mp3Player_State == MP3_STATE_STOPREQ) || (g_mp3Player_State == MP3_STATE_REPLAY) )
        {
            uint16_t returnCode=0;
            uint16_t stopFailCnt = 0;
            do
            {
                returnCode = mp3.stop();
                printf("STOP\r\nSTOP CODE:%d\r\n",returnCode);
                printf(" HDAT0:0x%x HDAT1:0x%x\r\n",mp3.readReg(mp3.SCI_HDAT0),mp3.readReg(mp3.SCI_HDAT1));

                if(returnCode != 0)
                {
                    stopFailCnt++;
                }
                if(stopFailCnt >= 20)
                {
                    printf("ERROR! impossible of stop\r\nVS1033 Reset\r\n");
                    mp3.hardwareReset();
                    mp3.sci_init();
                    mp3.sdi_init();
                    wait(1);
                    stopFailCnt = 0;
                    returnCode = 0;
                }
            }while(returnCode != 0);

            fclose(fp);
            fp = NULL;
            f_mp3Playwe_Playing = 0;
            lcd.Print("STOP      ",15,145);

            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( 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( 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]);

            lcd_FileViewPage = mp3PlayingFileNo/5;
            if(old_lcd_FileViewPage != lcd_FileViewPage)
            {
                lcd.FillRect(0,0,320,140,COLOR_BLUE);
            }
            old_lcd_FileViewPage = lcd_FileViewPage;

            if(SDFileList_cnt >= 5)
            {
                if( (SDFileList_cnt-(5*lcd_FileViewPage)) >= 5 )
                {
                    lcd_FileViewListEnd = (5*lcd_FileViewPage+5);

                }
                else
                {
                    lcd_FileViewListEnd = (5*lcd_FileViewPage+ ((SDFileList_cnt-(5*lcd_FileViewPage))%5) );
                }
                printf("FilePage:%d max:%d\r\n",lcd_FileViewPage, lcd_FileViewListEnd);

                for(i=(5*lcd_FileViewPage); i<lcd_FileViewListEnd; i++)
                {
                    sprintf(str,"%d:%s",i,SDFileList_short[i]);
                    lcd.Print( str, LEFT, (i%5)*28 ); // align text to center horizontally and use starndard colors
                }
            }
            else
            {
                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
                }
            }
            if(mp3PlayingFileNo >= 10)
            {
                lcd.Print( "**", LEFT, 28*(mp3PlayingFileNo%5) ); // align text to center horizontally and use starndard colors
            }
            else
            {
                lcd.Print( "*", LEFT, 28*(mp3PlayingFileNo%5) ); // align text to center horizontally and use starndard colors
            }
            
            sprintf(mp3PlayingFileName,"%s",SDFileList[mp3PlayingFileNo]);
            if(f_mp3Playwe_Playing == 1)
            {
                g_mp3Player_State = MP3_STATE_REPLAY;
            }
            else
            {
                g_mp3Player_State = MP3_STATE_STOPPING;
            }
        }
    }    
}