use VS1033(MP3 decoder) and UL024TF(TFTLCD)

Dependencies:   FastIO SDFileSystem TFTLCDwithFastIO TouchPanel VS1033 mbed

Fork of 11U68_MP3Player with TFTLCD by en 129

main.cpp

Committer:
nameless129
Date:
2015-07-16
Revision:
4:28900cff8f48
Parent:
3:67e97c846cec
Child:
5:a493361aead2

File content as of revision 4:28900cff8f48:

#include "ili9328.h"
#include <stdio.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
BusOut dataBus( P1_28,P2_3,P1_18,P1_24,P1_19,P1_26,P1_27,P1_25 ); // 16 pins
// 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, &dataBus, NC, P1_9 ); // control pins and data bus

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

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)
#define BOTTON_PLAY_YPOS    (170)
#define BOTTON_STOP_XPOS    (168)
#define BOTTON_STOP_YPOS    (170)
#define BOTTON_NEXT_XPOS    (245)
#define BOTTON_NEXT_YPOS    (170)
#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 && xpos <= (BOTTON_PREV_XPOS+60) ) &&
         (ypos >= BOTTON_PREV_YPOS && ypos <= (BOTTON_PREV_YPOS+60)) )
     {
         result = 1;
     }
     return result;
}

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

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

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

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

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


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;
    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;
                                          //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_volup     = { RGB16, 60, 60, &bmp_button_volup };
    const bitmap_t img_button_voldown   = { RGB16, 60, 60, &bmp_button_voldown };

    pc.baud(115200);
    printf("Power ON\r\n");

    //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( &TerminusFont );

/*  for Caliblation
    lcd.DrawCircle(40,40,10,COLOR_WHITE);
    lcd.DrawCircle(280,200,10,COLOR_WHITE);
*/
                    //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 );

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

    while(1)
    {
//////////////////////////////////////////////////////////////
/*  TouchPanel GetStatas Section                            */
//////////////////////////////////////////////////////////////

        XReadPos = getTouchPanelPosX();
        YReadPos = getTouchPanelPosY();

        if( checkTouchBotton_prev(XReadPos,YReadPos) )
        {
            if(touchPanel_Touched == 0)
            {
                mp3Player_State = MP3_STATE_PREVREQ;
                touchPanel_Touched = 1;
            }
        }
        else if( checkTouchBotton_play(XReadPos,YReadPos) )
        {
            if(touchPanel_Touched == 0)
            {
                if( (mp3Player_State != MP3_STATE_PLAYING) && (mp3Player_State != MP3_STATE_PLAYREQ) )
                {
                    mp3Player_State = MP3_STATE_PLAYREQ;
                }
                touchPanel_Touched = 1;
            }
        }
        else if( checkTouchBotton_stop(XReadPos,YReadPos) )
        {
            if(touchPanel_Touched == 0)
            {
                if( (mp3Player_State != MP3_STATE_STOPPING) && (mp3Player_State != MP3_STATE_STOPREQ) )
                {
                    mp3Player_State = MP3_STATE_STOPREQ;
                }
                touchPanel_Touched = 1;
            }
        }
        else if( checkTouchBotton_next(XReadPos,YReadPos) )
        {
            if(touchPanel_Touched == 0)
            {
                mp3Player_State = MP3_STATE_NEXTREQ;
                touchPanel_Touched = 1;
            }
        }
        else if( checkTouchBotton_volup(XReadPos,YReadPos) )
        {
            if(touchPanel_Touched == 0)
            {
                if(mp3Player_Volume >= 6527)
                {
                    mp3Player_Volume-=6527;
                    f_mp3Playwe_VolmeUpdate = 1;
                }
                touchPanel_Touched = 1;
            }

        }
        else if( checkTouchBotton_voldown(XReadPos,YReadPos) )
        {
            if(touchPanel_Touched == 0)
            {
                if(mp3Player_Volume <= 0xE57F )
                {
                    mp3Player_Volume+=6527;
                    f_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)
        {
            fp = fopen("/sd/1.mp3", "rb");
            SDFileOpenFailCnt = 0;
            while(!fp)
            {
                SDFileOpenFailCnt+=1;
                if(SDFileOpenFailCnt >= 3)
                {
                    printf("Fail SD init\r\n");
                    printf("System Stop.\r\n");
                    while(1);
                }
                printf("Fail file open n=%d\r\n",SDFileOpenFailCnt);
                sd.disk_initialize();
                fp = fopen("/sd/1.mp3", "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;
            mp3Player_State = MP3_STATE_PLAYING;
        }
        if( mp3Player_State == MP3_STATE_PLAYING )
        {
            printf("totalSendSize:%d\r\n",mp3_totalSizeSent);

            if(mp3_totalSizeSent>=mp3_fileSize) //play song end
            {
                mp3Player_State = MP3_STATE_STOPREQ;
            }
            else                                //transmit from SDCard to VS1033
            {
                mp3_ReadFileSize = fread(buf, sizeof(uint8_t), SD_READ_BLOCK_SIZE, fp);
                mp3_totalSizeSent += mp3.sendDataBlock(buf, mp3_ReadFileSize);
                f_mp3Playwe_Playing = 1;
            }
        }
        if( mp3Player_State == MP3_STATE_STOPREQ )
        {
            uint16_t returnCode=0;
            uint16_t stopFailCnt = 0;
            do
            {
                returnCode = mp3.stop();
                printf("STOP\r\nSTOP CODE:%d\r\n",returnCode);

                if(returnCode != 0)
                {
                    stopFailCnt++;
                }
                if(stopFailCnt >= 10)
                {
                    printf("ERROR! impossible of stop\r\nSystem STOP\r\n");
                    while(1);
                }
            }while(returnCode != 0);
            fclose(fp);
            fp = NULL;
            f_mp3Playwe_Playing = 0;
            mp3Player_State = MP3_STATE_STOPPING;            
        }
        if( mp3Player_State == MP3_STATE_STOPPING )
        {
        }

        if( mp3Player_State == MP3_STATE_VOLCHANGEREQ )
        {
        }
    }    
}