FTP Streaming Music Player with WIZwiki-W7500

Dependencies:   FTPClient SDFileSystem TextLCD WIZnetInterface mbed

Files at this revision

API Documentation at this revision

Comitter:
justinkim
Date:
Tue Sep 22 23:14:59 2015 +0000
Commit message:
Music Player

Changed in this revision

FTPClient.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
VS1002.cpp Show annotated file Show diff for this revision Revisions of this file
VS1002.h Show annotated file Show diff for this revision Revisions of this file
WIZnetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FTPClient.lib	Tue Sep 22 23:14:59 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/WIZnet/code/FTPClient/#fe95043a506e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Tue Sep 22 23:14:59 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Tue Sep 22 23:14:59 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/benyun/code/TextLCD/#7dd9751172e1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VS1002.cpp	Tue Sep 22 23:14:59 2015 +0000
@@ -0,0 +1,237 @@
+#include "VS1002.h"
+#include "mbed.h"
+
+Serial pc(USBTX, USBRX);
+TextLCD lcd(D8, D9, D0, D1, D2, D15);
+
+/* ==================================================================
+ * Constructor
+ * =================================================================*/
+
+VS1002::VS1002(PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name, PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, PinName dreq, PinName dcs)
+    : _DREQ(dreq), _RST(rst), _spi(mosi, miso, sck), _CS(cs), _DCS(dcs), _sd(mmosi, mmiso, ssck, ccs, name) {
+    
+    }    
+
+/*===================================================================
+ * Functions
+ *==================================================================*/
+ 
+void VS1002::cs_low(void)
+{
+    _CS = 0;                                
+}
+void VS1002::cs_high(void)
+{
+    _CS = 1;                                
+}
+void VS1002::dcs_low(void)
+{
+    _DCS = 0;
+}
+void VS1002::dcs_high(void)
+{
+    _DCS = 1;
+}
+void VS1002::sci_en(void)                    //SCI enable
+{
+    cs_high();
+    dcs_high();
+    cs_low();
+}
+void VS1002::sci_dis(void)                    //SCI disable
+{
+    cs_high();
+}
+void VS1002::sdi_en(void)                    //SDI enable
+{
+    dcs_high();
+    cs_high();
+    dcs_low();
+}
+void VS1002::sdi_dis(void)                    //SDI disable
+{
+    dcs_high();
+}
+void VS1002::reset(void)                    //hardware reset
+{
+    wait(0.01);
+    _RST = 0;
+    wait(0.01);
+    _RST = 1;
+    wait(0.10);
+}
+void VS1002::power_down(void)                //hardware and software reset
+{
+    cs_low();
+    reset();
+    sci_write(0x00, SM_PDOWN);
+    wait(0.01);
+    reset();
+}
+void VS1002::sci_initialise(void)
+{
+    _RST = 1;                                //no reset
+    _spi.format(8,0);                        //spi 8bit interface, steady state low
+    _spi.frequency(1000000);                //rising edge data record, freq. 1Mhz
+    
+    cs_low();
+    for(int i=0; i<4; i++)
+    {
+    _spi.write(0xFF);                        //clock the chip a bit
+    }
+    cs_high();
+    dcs_high();
+    wait_us(5);
+}
+void VS1002::sdi_initialise(void)
+{
+    _spi.format(8,0);
+    _spi.frequency(7000000);                //set to 7MHz
+    
+    cs_high();
+    dcs_high();
+}
+void VS1002::sci_write(unsigned char address, unsigned short int data)
+{
+    sci_en();                         //enables SCI/disables SDI
+    
+    while(!_DREQ);                          //wait unitl data request is high
+    _spi.write(0x02);                      //SCI write
+    _spi.write(address);                   //register address
+    _spi.write((data >> 8) & 0xFF);          //write out first half of data word
+    _spi.write(data & 0xFF);              //write out second half of data word
+    
+    sci_dis();                            //enables SDI/disables SCI
+    wait_us(5);
+}
+void VS1002::sdi_write(unsigned char datum)
+{
+    sdi_en();
+    
+    while(!_DREQ);
+    _spi.write(datum);
+    
+    sci_dis();
+}
+unsigned short int VS1002::read(unsigned short int address)
+{
+    cs_low();                                //enables SCI/disables SDI
+    
+    while(!_DREQ);                            //wait unitl data request is high
+    _spi.write(0x03);                        //SCI write
+    _spi.write(address);                    //register address
+    unsigned short int received = _spi.write(0x00);    //write out dummy byte
+    received <<= 8;
+    received += _spi.write(0x00);            //write out dummy byte
+    
+    cs_high();                                //enables SDI/disables SCI
+    
+    return received;                        //return received word
+}
+void VS1002::sine_test_activate(unsigned char wave)
+{
+    cs_high();                                //enables SDI/disables SCI
+    
+    while(!_DREQ);                            //wait unitl data request is high
+    _spi.write(0x53);                        //SDI write
+    _spi.write(0xEF);                        //SDI write
+    _spi.write(0x6E);                        //SDI write
+    _spi.write(wave);                        //SDI write
+    _spi.write(0x00);                        //filler byte
+    _spi.write(0x00);                        //filler byte
+    _spi.write(0x00);                        //filler byte
+    _spi.write(0x00);                        //filler byte
+
+    cs_low();                                //enables SCI/disables SDI
+}
+void VS1002::sine_test_deactivate(void)
+{
+    cs_high();
+    
+    while(!_DREQ);
+    _spi.write(0x45);                        //SDI write
+    _spi.write(0x78);                        //SDI write
+    _spi.write(0x69);                        //SDI write
+    _spi.write(0x74);                        //SDI write
+    _spi.write(0x00);                        //filler byte
+    _spi.write(0x00);                        //filler byte
+    _spi.write(0x00);                        //filler byte
+    _spi.write(0x00);                        //filler byte
+}
+
+
+void VS1002::volume(signed int left, signed int right)
+{
+    while(_DREQ == 0);
+       
+    unsigned short int _left = -left;       //convert the decibel values into a format
+    unsigned short int _right = -right;     //readable by the chip cf. datasheet p.32 subsection 8.6.11
+    _left *= 2;
+    _right *= 2;
+    unsigned short int attenuation = ((256 * _left) + _right);
+    cs_low();
+    sci_write(0x0B, attenuation);               //writeout these values
+    cs_high();
+}
+
+void VS1002::play_song(int song_number)
+{
+    /*====== Song Select ======*/
+    char str[16];        //folder where the songs are located
+    sprintf(str,"/sdc/%d",song_number);   //appending song number to path of the file
+    strcat(str,".mp3");                 //appending .mp3 to file name
+    FILE *song;
+    unsigned char array[512];           //array for reading data from file
+    bool play_new=false;                // Variable to see if new_song has be assigned or not
+    song = fopen(str, "r");    // Open the music file in read mode
+    /* Printing to LCD the present status */
+     lcd.cls();
+  if(pause) 
+    lcd.printf("     Paused ");
+  if(mute) 
+   lcd.printf("Muted");
+  if(!mute && !pause)
+   lcd.printf("     Playing..."); 
+
+  lcd.printf("\n %d %s",new_song_number,song_name[new_song_number-1]);
+   
+    if(!song) 
+    {
+        lcd.printf("\n \n Error!!");
+        new_song_number+=1;               // Goto Next song on completion of one song
+        if(new_song_number==10)
+            new_song_number=1;
+    }
+      while(!feof(song))
+    {
+            if(!pause)
+           {
+          
+           fread(&array, 1, 512, song);           
+           for(int i=0; i<512; i++)
+           {
+                sdi_write(array[i]);
+           }
+           volume(volume_set,volume_set);
+           }
+          if(new_song_number!=song_number)
+           {
+            play_new=true;
+             break;
+            }
+             
+             
+          }
+    
+           fclose(song);                              //close the file
+
+      if(!play_new)
+      {
+     new_song_number+=1;               // Goto Next song on completion of one song
+     if(new_song_number==10)
+      new_song_number=1; 
+     play_new=false;                   
+     }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VS1002.h	Tue Sep 22 23:14:59 2015 +0000
@@ -0,0 +1,75 @@
+#ifndef VS1002_H
+#define VS1002_H
+
+#include "mbed.h"
+#include "SDFileSystem.h"
+#include "string"
+#include "string.h"
+#include "TextLCD.h"
+
+
+//SCI_MODE register bits as of p.26 of the datasheet
+#define SM_DIFF            0x0001
+#define SM_SETTOZERO    0x0002
+#define SM_RESET        0x0004
+#define SM_OUTOFWAV        0x0008
+#define SM_PDOWN        0x0010
+#define SM_TESTS        0x0020
+#define    SM_STREAM        0x0040
+#define SM_PLUSV        0x0080
+#define    SM_DACT            0x0100
+#define    SM_SDIORD        0x0200
+#define    SM_SDISHARE        0x0400
+#define    SM_SDINEW        0x0800
+#define    SM_ADPCM        0x1000
+#define    SM_ADPCM_HP        0x2000
+
+extern int new_song_number;
+extern int volume_set;
+extern bool pause;
+extern bool mute;
+extern char * song_name[9];
+
+
+class VS1002  {
+
+public:
+
+    VS1002(PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name, PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, PinName dreq, PinName dcs);
+     
+    void cs_low(void); 
+    void cs_high(void);
+    void dcs_low(void);
+    void dcs_high(void);
+    void sci_en(void);
+    void sci_dis(void);
+    void sdi_en(void);
+    void sdi_dis(void);
+    
+    void sci_initialise(void);
+    void sdi_initialise(void);
+    void reset(void);
+    void power_down(void);
+ 
+    void sci_write(unsigned char, unsigned short int);
+    void sdi_write(unsigned char);
+    unsigned short int read(unsigned short int);
+    void sine_test_activate(unsigned char);
+    void volume(signed int,signed int);
+    void sine_test_deactivate(void);
+    void play_song(int);
+    
+    int num_of_files;
+    
+    DigitalIn _DREQ;
+    DigitalOut _RST;
+    
+protected:
+
+    SPI _spi;
+    DigitalOut _CS;
+    DigitalOut _DCS;
+    SDFileSystem _sd;
+
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnetInterface.lib	Tue Sep 22 23:14:59 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#3b64df29662f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Sep 22 23:14:59 2015 +0000
@@ -0,0 +1,264 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "SDFileSystem.h"
+#include <stdio.h>
+#include <string.h>
+
+#include "FTPClient.h"
+ #include "VS1002.h" 
+ #include "TextLCD.h"
+
+#define MAC     "\x00\x08\xDC\x11\x34\x78"
+#define IP      "192.168.77.191"
+#define MASK    "255.255.255.0"
+#define GATEWAY "192.168.77.1"
+
+#define FTP_SERVER_IP "192.168.77.209"
+
+#define _MAX_FNAME_LEN_   127
+#define _FTP_UPDATE_TIME_  20
+
+
+Serial uart(USBTX, USBRX);
+
+//SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
+//SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
+//SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F
+//SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 
+
+EthernetInterface eth;
+
+DigitalOut led1(LED1); //server listning status
+DigitalOut led2(LED2); //socket connecting status
+
+InterruptIn K_VU(D3); // Create the interrupt receiver object on pin 26
+InterruptIn K_NT(D4); // Create the interrupt receiver object on pin 26
+InterruptIn K_PS(D5); // Create the interrupt receiver object on pin 26
+InterruptIn K_BK(D6); // Create the interrupt receiver object on pin 26
+InterruptIn K_VD(D7); // Create the interrupt receiver object on pin 26
+VS1002 mp3(PB_3, PB_2, PB_1, PB_0,"sdc",D11, D12 ,D13, PC_12, PC_15, PC_14, PC_13);  //Setup Audio decoder. Name is VS1002 even though VS1053 is used.
+TextLCD lcd1(D8, D9, D0, D1, D2, D15); //setup lcd
+
+FTPClient myFTP("/sdc");  // mountname in MySeeedStudioTFTv2
+
+Ticker ledTick;
+
+/* Global Variables to store Status*/
+int new_song_number=1;  //Variable to store the Song Number
+int volume_set=-20;     //Variable to store the Volume
+int previous_volume;    //Variable to store the volume when muted
+bool pause=false;       //Variable to store the status of Pause button 
+bool mute=false;        //Variable to store the status of mute button
+
+int check=0;    //Capacitative touch generates interrupt on both press and release. This variable tracks this and updates only on press.
+char *song_name[9]={"Good Day","Leong","Sponsor","I'm So Sexy","My Life","Oh My god","Wonderful bar","Whale Hunting","Love"}; //Array of song names entered manually
+
+uint32_t ftp_time_1s = 0;
+
+char myfilelist[MAX_SS] = {0,};
+
+void ledTickfunc()
+{
+    led1 = !led1;
+    if(ftp_time_1s)
+    {
+        if(ftp_time_1s++ > _FTP_UPDATE_TIME_) ftp_time_1s = 0;
+    }
+}
+
+void Next_Song() 
+{
+    new_song_number+=1;  // Next Song
+    if(new_song_number==10)
+        new_song_number=1;
+        
+    lcd1.cls();
+    if(pause) 
+        lcd1.printf("     Paused ");
+    else
+        lcd1.printf("     Playing..."); 
+    
+    lcd1.printf("\n %d %s",new_song_number,song_name[new_song_number-1]);
+}
+
+void Preveious_Song() 
+{
+    new_song_number-=1;  // Preveious Song
+    if(new_song_number==0)
+        new_song_number=9;
+        
+    lcd1.cls();
+    if(pause) 
+        lcd1.printf("     Paused ");
+    else
+        lcd1.printf("     Playing..."); 
+    
+    lcd1.printf("\n %d %s",new_song_number,song_name[new_song_number-1]);
+}
+
+void Pause_Song() 
+{
+    pause=!pause; // Pause/Play button
+    
+    lcd1.cls();
+    if(pause) 
+        lcd1.printf("     Paused ");
+    else
+        lcd1.printf("     Playing..."); 
+    
+    lcd1.printf("\n %d %s",new_song_number,song_name[new_song_number-1]);
+}
+
+void Volume_Up() 
+{
+    volume_set+=3; // Volume Up
+    if(volume_set>=0)
+        volume_set=0;
+}
+
+void Volume_Down() 
+{
+    volume_set-=3;  //Volume Down
+    if(volume_set<-55)
+        volume_set=-55;
+}
+/*
+void Volume_Mute() 
+{
+    mute=!mute;  //Mute/Unmute
+    
+    if(mute)
+    {
+        previous_volume=volume_set; // Attenuation of -55 db is small enough to not hear anything
+        volume_set=-55;
+    }
+    else
+    {
+        volume_set=previous_volume;
+    }
+}*/
+
+int main (void)
+{
+    *(volatile uint32_t *)(0x41001014) = 0x0060100;
+    *(volatile uint32_t *)(0x41003000) = 0x10;
+    *(volatile uint32_t *)(0x41003004) = 0x10;
+    *(volatile uint32_t *)(0x41003008) = 0x10;
+    *(volatile uint32_t *)(0x41003080) = 0x10;
+    *(volatile uint32_t *)(0x41003098) = 0x10;
+    
+    char* tok = NULL;
+    char* lasts = NULL;
+    char filename[_MAX_FNAME_LEN_];
+    FILE* fp;
+    
+//    Serial Interface eth;
+    uart.baud(115200);
+    uart.printf("Initializing\r\n");
+
+//    EthernetInterface eth;
+    uart.printf("Initializing Ethernet\r\n");
+
+    //eth.init(); //Use DHCP
+    eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  //IP,mask,Gateway
+    uart.printf("Connecting\r\n");
+    eth.connect();
+    uart.printf("IP Address is %s\r\n", eth.getIPAddress());
+
+//    Check File System
+    uart.printf("Checking File System\r\n");
+    DIR *d = opendir("/sdc/");
+    if (d != NULL) {
+        uart.printf("SD Card Present\r\n");
+        closedir(d);
+    } else {
+        uart.printf("SD Card Root Directory Not Found\r\n");
+    }
+
+    ledTick.attach(&ledTickfunc,1);
+
+    while(1)
+    {
+        if(ftp_time_1s == 0)
+        {
+            //Configure the display driver
+            ftp_time_1s = 1;
+            
+            printf(" UPDATING MP3\r\n");
+            printf("==================\n\r\n");
+            myFTP.open("192.168.77.209", 21, "user", "pass");
+        
+            if(myFTP.open("192.168.77.209", 21, "user", "pass"))
+            {
+                printf("Connect Success to FTPServer\r\n");
+                printf("Connected to FTP Server\r\n");
+
+                myFTP.ls(myfilelist);
+            
+                if(*myfilelist !=0)
+                {
+                    tok = myfilelist;
+                    while(tok)
+                    {
+                        tok = strtok_r(tok,"\r\n",&lasts);
+                        if(tok != NULL)
+                        {
+                            printf("tok=%s\r\n",tok);
+                            if(strstr(tok,"mp3"))
+                            {
+                                sprintf(filename,"/sdc/%s",tok);
+                                fp = fopen(filename, "r");
+                                printf("fp=%d\r\n",fp);
+                                if(fp==NULL)
+                                {
+                                    myFTP.getfile(tok);
+                                    printf("Get File : %s\r\n",tok);
+                                    printf("New file : %s\r\n",tok);
+                                }
+                                else fclose(fp);
+                            }
+                            tok = lasts;
+                        }
+                    }
+                }
+                else printf(" Empty FTP Server\r\n");
+                printf("\n UPDATE DONE\n\r\n");
+                myFTP.quit();
+            }
+            else
+            {
+                printf(" Can't connect to FTP Server\r\n");
+                printf(" UPDATE FAIL\r\n");
+            }
+            printf("==================\n");
+        }
+        
+        /*============================================================ 
+         * MP3 Initialising 
+         *==========================================================*/
+
+        mp3._RST = 1; 
+        mp3.cs_high();                                  //chip disabled 
+        mp3.sci_initialise();                           //initialise MBED
+        mp3.sci_write(0x00,(SM_SDINEW+SM_STREAM+SM_DIFF)); 
+        mp3.sci_write(0x03, 0x9800); 
+        mp3.sdi_initialise();
+
+        /* UI Button setup */
+        K_VU.fall(&Volume_Up);
+        K_VU.mode(PullUp);
+        K_VD.fall(&Volume_Down);
+        K_VD.mode(PullUp);
+        K_NT.fall(&Next_Song);
+        K_NT.mode(PullUp);
+        K_BK.fall(&Preveious_Song);
+        K_BK.mode(PullUp);
+        K_PS.fall(&Pause_Song);
+        K_PS.mode(PullUp);
+        
+        while(1)
+        {
+            mp3.play_song(new_song_number);
+        }
+    }    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Sep 22 23:14:59 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/bad568076d81
\ No newline at end of file